Commit Graph

896 Commits

Author SHA1 Message Date
Kavon Farvardin 21b2ebff12 Sema: infer inverses in opaque return types
Opaque return types are special type declarations that have it
own nested generic signature. Thus, given this:
```
  protocol P<A> { associatedtype A: ~Copyable }
  func f<T: ~Copyable>() -> some P<T> {}
```
The generic signature for f is <T where T Escapable>, and
for the opaque return type, its nested signature ends up as
```
  <X where X: P, X.A == T>
```
With SE-503, we will now also expand a default for the suppressed
primary associated type, so the signature after expansion becomes
```
  <X where X: P, X.A == T, X.A: Copyable>
```
It would be smarter to effectively have this rule
```
  X.A == T, T: ~Copyable
  ----------------------
     X.A: ~Copyable
```
where we infer the inverse on X.A to cancel-out the
expanded default X.A: Copyable. We already do this for
two in-scope type parameters, and it would be better if
we did it if one side was out-of-scope, but that would
be source-breaking to do in general.

In the case of opaque return types, the fact that
it has a nested generic signature seems more an
artifact of the implementation. There also is little
risk of source break, as the only kinds of same-type
requirements that can appear are from parameterized
protocol type.

The experimental suppressed associated types prior to
SE-503 wouldn't be broken by this change, as they do
not infer defaults that need suppression, and we only
filter-out requirements from defaults expansion, rather
than explicitly-written ones.

rdar://175500824
2026-05-04 14:49:34 -07:00
Kavon Farvardin 5b63b50cd6 NFC: avoid repeated representativeGPs lookups 2026-04-29 16:56:01 -07:00
Kavon Farvardin 6651cb6389 NFC: refactor the applyInverses bool
There's a need for more control over how default requirements
for conformance to Copyable/Escapable are expanded, and
subsequently how inverses are applied or inferred to cancel-out
those defaults.

The pattern of `/*applyInverses*/BOOL` is insufficient, so this
is a refactoring to grow that into a proper type that carries
an option that can be used in some future scenario about inferring
inverses for opaque return types.
2026-04-29 16:56:00 -07:00
Kavon Farvardin 610a9f98ed SuppAssocTypes: enable SE-503 by default 2026-04-22 22:11:41 -07:00
Kavon Farvardin e5b2ead0d7 Sema: remove SE427NoInferenceOnExtension
The feature was added only to avoid a reverse-condfail
in the initial bring-up of noncopyable generics.

Nearly 2 years have passed since the last time I tried
to remove this old technical debt [1] and had to revert
it due to outdated build bots [2]. Hoping that won't be
a problem this time.

[1] 5b2f2cbfcf
[2] https://github.com/swiftlang/swift/pull/75267

resolves rdar://131560183
2026-03-11 09:57:47 -07:00
Anthony Latsis 31cafb0a27 Switch DiagnosticEngine::warnUntilLanguageMode and co. to LanguageMode 2026-02-10 16:06:56 +00:00
Kavon Farvardin 6dda8b98d9 Reparenting: require statement of inheritance
We were walking the extensions of protocols in
requirement lowering and in getInheritedProtocols
to also count the @reparented entries in all
extensions. I had already seen some request
evaluator cycles triggered by this with objc
protocols, and it seems unnessecary as it's
actually a benefit to force people to write the
inheritance on the protocol itself.
2026-02-06 13:23:22 -08:00
Kavon Farvardin 49462296c0 Reparenting: fix crash in TypeAliasRequirementsRequest
There isn't always an inheritance clause, despite a protocol
inheriting from some other protocol, with reparenting.
2026-02-04 14:59:30 -08:00
Kavon Farvardin 006a0a494a Reparenting: require an extension
This change forces you to write ``@reparented` relationships
on an extension of a protocol, rather than on the protocol
itself.

The ProtocolConformance needs to be associated with some
GenericContext and IRGen expects it to be an ExtensionDecl.
That environment defines under what conditions the conformance
exists. We also need to define witnesses for the new parent's
requirements in an extension anyway, so it's a natural fit.

The previous workaround for this was kind of awful, as it'd
require searching all the protocol's extensions and "guess"
which extension they want to represent the conformance. While
we could try to synthesize an extension, there's two
challenges there:

1. Due to SuppressedAssociatedTypes, it's not so simple to
synthesize an unconstrained ExtensionDecl.
2. We currently rely on same-type requirements to pin the
associated types to particular witnesses of those requirements
in the extension. So it's not purely unconstrained! For example,

```
extension Seq: @reparented BorrowSeq where Iter == MyIter {}
```

The constraints that are disallowed (but not yet diagnosed)
are conditional conformance requirements, as the default
conformance for a reparenting cannot depend on those.

Thus, it's better that programmers to specify the extension.
2026-02-03 16:40:21 -08:00
Kavon Farvardin 35a0a93703 Merge pull request #86080 from kavon/suppressed-assoc-types-with-defaults-3
SuppressedAssociatedTypesWithDefaults: make scoping pay attention to root
2025-12-16 20:59:09 -08:00
Kavon Farvardin fbd51c06df Sema: apply scoping SuppAssocType scoping rule roots
The generic parameter at the root of a DependentMemberType is
the main thing we care about in terms of the scoping rule.

We have a check for ineffective / invalid inverses in a later
phase, so if the suppression on the type member was wrong then
we should still raise an error about it.
2025-12-16 15:20:56 -08:00
Anthony Latsis 153dd02cd8 Merge pull request #85833 from swiftlang/jepa-main
[NFC] "SwiftVersion" → "LanguageMode" in `DiagnosticEngine::warnUntilSwiftVersion`, etc.
2025-12-05 09:34:30 +00:00
Anthony Latsis 88220a33c3 [NFC] "SwiftVersion" → "LanguageMode" in DiagnosticEngine::warnUntilSwiftVersion, etc. 2025-12-04 15:11:07 +00:00
Kavon Farvardin c9d2f522c0 Sema: introduce ProtocolInversesRequest
We need special handling for protocols whose requirement
signature exists but is in a serialized state, as we
cannot run the StructuralRequirementsRequest on such
a protocol as there's no work to be done, effectively.
2025-12-02 21:09:54 -08:00
Kavon Farvardin 6f95203dfd Sema: introduce SuppressedAssociatedTypesWithDefaults
This is similar to SuppressedAssociatedTypes, but infers
default requirements when primary associated types of
protocols are suppressed. This defaulting for the primary
associated types happens in extensions of the protocol,
along with generic parameters, whenever a source-written
requirement states a conformance requirement for the protocol.

Thus, the current scheme for this defaulting is a simplistic,
driven by source-written requirements, rather than facts
that are inferred while building generic signatures.

Defaults are not expanded for infinitely many associated types.

rdar://135168163
2025-12-02 18:00:03 -08:00
Kavon Farvardin 761502fabe NFC: explain the missingInverses 2025-12-02 18:00:02 -08:00
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
Kathy Gray d68a7a7ef0 Requirement Machine: Fix an issue with pack expansion + tuple type crashes
Support pack expansion types in term rewriting, maintaining shape invariants and not
throwing assertions unnecessarily.

Additional tests added for an inifinite case and a concrete case.
2025-10-31 17:53:59 +00:00
Kathy Gray fcb7d76e74 RequirementMachine: Adding shape abstractions
Adding abstractions to check terms for shape symbol and remove the shape
symbol from the end of the sequence of symbols, rather than manually
manipulating the end() sequence externally.
2025-10-31 17:29:45 +00:00
Hamish Knight a83ea3c8bb Merge pull request #84745 from hamishknight/fishmonger
[Evaluator] Enforce consistent results for cyclic requests
2025-10-09 17:08:04 +01:00
Hamish Knight f7e459a9b5 [AST] Factor out GenericSignature::forInvalid
Factor out the common logic from `getPlaceholderGenericSignature`.
2025-10-08 21:16:02 +01: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 244d2afea3 RequirementMachine: New way of propagating failure when building rewrite system for protocol
If we failed to construct a rewrite system for a protocol, either because
the Knuth-Bendix algorithm failed or because of a request cycle while
resolving requirements, we would end up in a situation where the resulting
rewrite system didn't include all conformance requirements and associated
types, so name lookup would find declarations whose interface types are
not valid type parameters.

Fix this by propagating failure better and just doing nothing in
getReducedTypeParameter().

Fixes rdar://147277543.
2025-10-04 09:17:46 -04:00
swift-ci 64aaf36dc5 Merge remote-tracking branch 'origin/main' into rebranch 2025-09-11 17:58:46 -07: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
Anthony Latsis c1d794364b Adjust code after changes to llvm::TrailingObjects API
See:
- https://github.com/llvm/llvm-project/pull/138970
- https://github.com/llvm/llvm-project/pull/144930
2025-07-19 01:48:18 +01:00
swift-ci 53d799f9b8 Merge remote-tracking branch 'origin/main' into rebranch 2025-07-15 21:35:21 -07:00
Slava Pestov ee440f3c91 AST: Remove MakeAbstractConformanceForGenericType
While the intent behind this functor was noble, it has grown in complexity
considerably over the years, and it seems to be nothing but a source of
crashes in practice. I don't want to deal with it anymore, so I've decided
to just subsume all usages with LookUpConformanceInModule instead.
2025-07-15 16:34:11 -04:00
swift-ci a626a79577 Merge remote-tracking branch 'origin/main' into rebranch 2025-06-18 20:55:46 -07:00
Slava Pestov 5987bbf966 Merge pull request #82321 from slavapestov/rqm-fixes
RequirementMachine: Add more limits to catch runaway computation, and fix a bug
2025-06-18 23:00:38 -04:00
Slava Pestov fee97ea96a RequirementMachine: Fix the most embarassing bug of all time
The implementation of Knuth-Bendix completion has had a subtle
bookkeeping bug since I first wrote the code in 2021.

It is possible for two rules to overlap in more than one position,
but the ResolvedOverlaps set was a set of pairs (i, j), where
i and j are the index of the two rules. So overlaps other than
the first were not considered. Fix this by changing ResolvedOverlaps
to a set of triples (i, j, k), where k is the position in the
left-hand side of the first rule.

The end result is that we would incorrectly accept the protocol M3
shown in the test case. I'm pretty sure the monoid that M3 encodes
does not have a complete presentation over any alphabet, so of
course it should not be accepted here.
2025-06-17 17:51:26 -04:00
Slava Pestov 7f8175b3da RequirementMachine: Add two more completion termination checks for concrete type requirements
The concrete nesting limit, which defaults to 30, catches
things like A == G<A>. However, with something like
A == (A, A), you end up with an exponential problem size
before you hit the limit.

Add two new limits.

The first is the total size of the concrete type, counting
all leaves, which defaults to 4000. It can be set with the
-requirement-machine-max-concrete-size= frontend flag.

The second avoids an assertion in addTypeDifference() which
can be hit if a certain counter overflows before any other
limit is breached. This also defaults to 4000 and can be set
with the -requirement-machine-max-type-differences= frontend flag.
2025-06-17 17:51:25 -04:00
swift-ci 7cdd0a9eb1 Merge remote-tracking branch 'origin/main' into rebranch 2025-06-17 11:59:39 -07:00
Slava Pestov 4fa2e979fa RequirementMachine: Don't crash if we cannot desugar a same-shape requirement 2025-06-17 09:52:02 -04:00
swift-ci b2f2ae4169 Merge remote-tracking branch 'origin/main' into rebranch 2025-06-10 23:19:26 -07:00
Slava Pestov 3b6ee7cf0b RequirementMachine: Add an assert 2025-06-10 16:49:57 -04:00
swift-ci fe523c4b0b Merge remote-tracking branch 'origin/main' into rebranch 2025-05-20 02:15:11 -07:00
Hamish Knight edca7c85ad Adopt ABORT throughout the compiler
Convert a bunch of places where we're dumping to stderr and calling
`abort` over to using `ABORT` such that the message gets printed to
the pretty stack trace. This ensures it gets picked up by
CrashReporter.
2025-05-19 20:55:01 +01:00
Hamish Knight b8fc71c684 [AST] Fix an accidental use of llvm::errs 2025-05-19 20:55:01 +01:00
Anthony Latsis fe575c66a9 Manually merge remote-tracking branch 'origin/main' into rebranch
Conflicts:
lib/AST/RequirementMachine/RequirementLowering.cpp
2025-05-15 13:48:37 +01:00
Joe Groff 52d7781758 Merge pull request #81424 from jckarter/same-type-constraint-stop-copyable
Sema: Allow `T == NonCopyableOrEscapable` same-type constraints without a redundant `T: ~Copyable`.
2025-05-14 08:09:05 -07: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 0c536923c9 Manually merge branch 'main' into rebranch 2025-05-07 14:05:43 +01:00
Slava Pestov df02862c2a Sema: Disallow SE-0361 with variadic generic types for now
`extension G<Int>` introduces a same-type requirement, and
this isn't supported for variadic generic types yet.

Make sure we pass a valid source location here to diagnose
instead of dropping the error.

- Fixes https://github.com/apple/swift/issues/70432
- Fixes rdar://119613080
2025-05-05 16:04:17 -04:00
swift-ci a546b80a0d Merge remote-tracking branch 'origin/main' into rebranch 2025-04-29 23:53:41 -07:00
Slava Pestov 6b4710ed22 Merge pull request #81142 from slavapestov/more-type-subst-cleanup
Clean up duplicated opened existential archetype handling in SIL and more
2025-04-30 02:42:57 -04:00
swift-ci 576e371bab Merge remote-tracking branch 'origin/main' into rebranch 2025-04-29 23:13:47 -07:00
Slava Pestov 651e0af4d1 RequirementMachine: Compare weight before length in Term/MutableTerm::compare() 2025-04-29 13:55:30 -04:00
Slava Pestov cf1572c65b AST: Add ASTContext::TheSelfType for convenience 2025-04-28 11:49:50 -04:00
Anthony Latsis 7b9318fa84 Refactor uses of obsoleted llvm::DenseMap::{FindAndConstruct,getOrInsertDefault}
Per 390b82dd4c485ec64cf8a6c52fb73e391792262e (llvm-project).
2025-04-24 01:52:46 +01:00