Commit Graph

164 Commits

Author SHA1 Message Date
Anthony Latsis
bda6edb85c AST: Rename GenericContext::isGeneric to hasGenericParamList
`isGeneric` is a misleading name because this method checks for the
existence of a `GenericParamList`, which is not implied by genericity.
2025-11-11 15:55:16 +00:00
Anthony Latsis
58fa8bf762 RequirementMachine: Diagnose unsupported value generic parameter definitions properly
The flow was such that we recorded subtype constraints regardless of the
subject type's nature. Extract value generics handling out of the
devious `else if` chain, and never record any subtype constraints if the
subject type is a non-type parameter.

While we're here, generalize the diagnostic message for user-written
subtype constraints on value generic parameters and emit it
consistently, not just if the right-hand side contains a protocol type.
2025-10-08 02:13:03 +01:00
Slava Pestov
0507b02024 RequirementMachine: Fix crash-on-invalid with concrete type requirements involving packs
We would crash in some cases, or produce a slightly misleading
diagnostic about same-element requirements, which are related but
not quite the same.

In the fullness of time, we should figure out this corner of the
language. Until then, add a new diagnostic since this is really
about same-type requirements between concrete types and packs.

Fixes rdar://159790557.
2025-09-10 20:20:31 -04:00
Slava Pestov
4fa2e979fa RequirementMachine: Don't crash if we cannot desugar a same-shape requirement 2025-06-17 09:52:02 -04:00
Joe Groff
e4a6faa3f2 Sema: Allow T == NonCopyableOrEscapable same-type constraints without a redundant T: ~Copyable.
Enhance the logic in `applyInverses` to also take into account same-type constraints spelled in
the generic signature, so that same-type-constraining a type parameter to a type that is itself
not `Copyable` or `Escapable` suppresses the default application of those constraints on the
type parameter. Fixes rdar://147757973.
2025-05-13 11:31:41 -07:00
Anthony Latsis
a0930dec31 AST: Cut down on DescriptiveDeclKind usage in DiagnosticsSema.def 2025-04-05 12:31:19 +01:00
Slava Pestov
91503710bc AST: Remove origType parameter from ProtocolConformanceRef::getTypeWitness() 2025-04-03 17:35:32 -04:00
Doug Gregor
ffca6968ed [Isolated conformances] Diagnose conflict with isolated conformances and Sendable 2025-02-28 13:08:35 -08:00
Slava Pestov
afbd81a599 AST/Sema: Use ProtocolConformanceRef::getTypeWitness() 2024-09-19 14:18:33 -04:00
Slava Pestov
da0130d297 AST: Replace calls to substBaseType() with getAssociatedType() 2024-09-19 14:18:32 -04:00
Alejandro Alonso
6f83e3c28f Fix type parsing when preceeded by '-' 2024-09-11 10:28:44 -07:00
Alejandro Alonso
7cd424812f Move some value checking to desugaring 2024-09-04 15:13:46 -07:00
Alejandro Alonso
f4f60f4344 Remove Value requirement Add GenericTypeParamKind 2024-09-04 15:13:43 -07:00
Alejandro Alonso
7c85261a77 Add runtime support 2024-09-04 15:13:27 -07:00
Alejandro Alonso
3e9c4633ca Add some new diagnostics and tests 2024-09-04 15:13:27 -07:00
Alejandro Alonso
75c2cbf593 Implement value generics
Some requirement machine work

Rename requirement to Value

Rename more things to Value

Fix integer checking for requirement

some docs and parser changes

Minor fixes
2024-09-04 15:13:25 -07:00
Holly Borla
dbda2738a6 [Features] Gate same-element requirements behind an experimental feature flag. 2024-07-15 10:19:32 -07:00
Holly Borla
2ea4586580 [Requirement Machine] Implement same-element requirements. 2024-07-15 10:19:32 -07:00
Slava Pestov
2adad14f7b RequirementMachine: Don't call getContextSubstitutionMap() on an existential type 2024-07-10 13:28:26 -04:00
Slava Pestov
fae01d9776 AST: Remove ModuleDecl parameter from more places 2024-07-06 12:05:46 -04:00
Slava Pestov
f34e57c016 RequirementMachine: Use ProtocolDecl::getAllInheritedProtocols() 2024-06-25 15:20:42 -04:00
Slava Pestov
273c4b2b1a RequirementMachine: Convert to new assertions 2024-06-22 08:53:22 -04:00
Tim Kientzle
1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -07:00
Kavon Farvardin
779184f922 NCGenerics: fix reverse condfail
I gated inverses on associated types behind a feature flag, but a
slightly older stdlib being rebuilt from source will now fail because it
 used that functionality for `_Pointer` without the feature.

So, just permit the feature if we're working with a module built from
an interface file, to avoid this reverse condfail
.
2024-04-18 15:01:24 -07:00
Slava Pestov
454e5cff67 Merge pull request #72825 from slavapestov/two-minor-fixes-and-tests
RequirementMachine: Fix two minor crash on invalid bugs
2024-04-03 22:42:24 -04:00
Slava Pestov
61dbaf892d RequirementMachine: Fix crash with protocol typealias having unbound generic type
Make sure we don't try to introduce an implicit same-type requirement
in this case.

Fixes rdar://problem/63731199.
2024-04-03 17:47:39 -04:00
Kavon Farvardin
30983530b6 NCGenerics: introduce SuppressedAssociatedTypes
The model for associated types hasn't been fully worked-out for
noncopyable generics, but there is some support already that is being
used by the stdlib for an internal-only (and rather cursed) protocol
`_Pointer` to support `UnsafePointer`, etc.

This patch gates the existing experimental support for associated types
behind a feature flag. This flag doesn't emit feature-guards in
interfaces, since support for it is tied closely to NoncopyableGenerics
and has been there from its early days.
2024-04-02 16:53:36 -07:00
Kavon Farvardin
149c052ec5 use new noncopyable types infrastructure
The infrastructure underpinning the new feature NoncopyableGenerics is
mature enough to be used.
2024-03-14 23:10:44 -07:00
Slava Pestov
76670472dc AST: Remove InverseRequirement::enumerateDefaultedParams() 2024-02-24 07:25:59 -05:00
Kavon Farvardin
63ed8ec4a6 NCGenerics: handle legacy definition of Sendable
This should be a temporary measure while bootstrapping the feature.
2024-02-15 11:07:49 -08:00
Slava Pestov
4f1d8e3746 Sema: Clean up InferredGenericSignatureRequest
- It's wasteful to cache because each invocation is unique
- Inference sources only need to be Types and not TypeLocs
- We can pass in an explicit SourceLoc for diagnostics

(cherry picked from commit 4e39dac206dd8e0818ca509f5f09f93425a48c62)
2024-02-09 16:02:32 -08:00
Slava Pestov
25355b2b57 Sema: Don't re-sugar the type in StructuralTypeRequest::evaluate()
(cherry picked from commit 1db69ab2485d5d61ecdceaf6f10720378b149dda)
2024-02-09 16:02:15 -08:00
Kavon Farvardin
c8bf35ab96 NCGenerics: precedence of inverses & reqirements
With the removal of `fromDefault` in StructuralRequirement,
`applyInverses` no longer could distinguish which reqirements came from
`expandDefaults` and which were explicitly written in source. Thus, for
a generic parameter like `<T> where T: Copyable, T: ~Copyable`, the
inverse `~Copyable` was eliminating the explicitly-written requirement,
causing `T` to be noncopyable.

We want to emit an error in such cases, so this swaps things around so
we only ever applyInverses on a requirements list that is from
expandDefaults.
2024-02-08 13:43:49 -08:00
Hamish Knight
05615fa0e4 NFC: Rename TypeWalker's SkipChildren to SkipNode
For consistency with ASTWalker.
2024-02-05 15:27:25 +00:00
Kavon Farvardin
a816489024 RequirementLowering: remove assert
Finding a structural requirement that is not involving an invertible
protocol is not suspicious at all.
2024-02-04 19:57:29 -08:00
Slava Pestov
8db0af48bd Merge pull request #71352 from slavapestov/remove-redundant-requirements
Remove -warn-redundant-requirements flag
2024-02-03 08:28:02 -05:00
Kavon Farvardin
3908c8182d NCGenerics: sometimes synth. Copyable/Escapable
When the Swift module is not available, we'll synthesize the
Copyable/Escapable decls into the Builtin module.

In the future, it might be nice to just do this always, and define
typealiases for those types in the stdlib to refer to the ones in the
builtin module.
2024-02-02 18:47:03 -08:00
Slava Pestov
d2f136254e AST: Remove fromDefault and inferred from StructuralRequirement 2024-02-02 14:57:20 -05:00
Slava Pestov
dcca5ced0f RequirementMachine: Remove -warn-redundant-requirements flag 2024-02-02 14:57:19 -05:00
Kavon Farvardin
a9b2725520 [NFC] fast-path to skip a requirement scan 2024-01-10 19:37:21 -08:00
Kavon Farvardin
63b3e7624d [NCGenerics] fold InverseType into PCT
We already need to track the inverses separate from the members in a
ProtocolCompositionType, since inverses aren't real types. Thus, the
only purpose being served by InverseType is to be eliminated by
RequirementLowering when it appears in a conformance requirement.

Instead, we introduce separate type InverseRequirement just to keep
track of which inverses we encounter to facilitate cancelling-out
defaults and ensuring that the inverses are respected after running
the RequirementMachine.
2023-12-07 22:14:23 -08:00
Pavel Yaskevich
5bc6ec6184 Merge pull request #69890 from xedin/rdar-99758612
[ClangImporter] Add support for `swift_attr(“@Sendable”)` on ObjC protocols
2023-11-27 13:17:31 -08:00
Pavel Yaskevich
ab4f8d0dc0 [RequirementMachine] Inherited requirements collection should account for synthesized protocols
ClangImporter adds `SynthesizedProtocolAttr` to model inheritance
from `Sendable` on protocols imported from Objective-C.
2023-11-16 10:58:34 -08:00
Slava Pestov
08f8781798 RequirementMachine: Don't drop errors on the floor with @objc protocols 2023-11-14 15:46:43 -05:00
Slava Pestov
39ec9903e8 RequirementMachine: Untangle requirement desugaring from requirement inference
Refactor the code to match what's written up in generics.tex.

It's easier to understand what's going on if requirement inference
first introduces a bunch of requirements that might be trivial,
and then all user-written and inferred requirements are desugared
at the end in a separate pass.
2023-11-14 15:46:33 -05:00
Kavon Farvardin
455fcdc5e0 [Sema] add redundant ~Copyable requirement warning
Originally I tried to implement a fancy redundant-inverse error
diagnostic that identifies the "previous" requirement already seen. I
ended up removing this error diagnostic because it was tricky to
implement well and we weren't diagnosing other redundant requirements.

Turns out we do have a mode of the compiler to diagnose redundant
requirements, but as warnings, using `-warn-redundant-requirements`.
This warning is much simpler in that it just points to one requirement
that is redundant.
2023-11-06 15:38:10 -08:00
Kavon Farvardin
4334279233 [nfc] clean and jot down some TODOs 2023-10-28 16:12:55 -07:00
Kavon Farvardin
2e66b69953 [Sema] prevent inverse-constraints on outer params
Since there is no propagation of inverse constraints in the requirement
machine, we need to fully desugar these requirements at the point of
defining a generic parameter. That desugaring involves determining which
default conformance requirements need to be applied to a generic
parameter, accounting for inverses.

But, nested generic contexts in scope of those expanded generic
parameters can still write constraints on that outer parameter. For
example, this method's where clause can have its own constraints on `T`:

```
struct S<T> {
 func f() where T: ~Copyable {}
}
```

But, the generic signature of `S` already has a `T: Copyable` that was
expanded. The method `f` will always see a `T` that conforms to
`Copyable`, so it's impossible for `f` to claim that it applies for
`T`'s that lack Copyable.

Put another way, it's not valid for this method `f`, whose generic
signature is based on its parent's `S`, to weaken or remove requirements
 from parent's signature. Only positive requirements can be
 added to them.
2023-10-27 15:01:10 -07:00
Kavon Farvardin
bb341e7928 [NoncopyableGenerics] fix isNoncopyable for packs
We're not yet going to allow noncopyable types into packs, so this
change prevents the use of `~Copyable` on an `each T` generic parameter.
 It also fixes how we query for whether a `repeat X` parameter is
 copyable.
2023-10-27 15:01:10 -07:00
Kavon Farvardin
a92181827a [Sema] handle inverses everywhere
Previously, inverses were only accounted-for in inheritance clauses.

This batch of changes handles inverses appearing in other places, like:

- Protocol compositions
- `some ~Copyable`
- where clauses

with proper attribution of default requirements in their absence.
2023-10-27 15:01:10 -07:00