Commit Graph

1335 Commits

Author SHA1 Message Date
Kavon Farvardin
a42d520656 Merge pull request #85850 from kavon/suppressed-assoc-types-with-defaults-2
Test: additional test coverage for SuppressedAssociatedTypesWithDefaults
2025-12-05 22:03:37 -08:00
Kavon Farvardin
c3861247c4 Test: additional test coverage for SuppressedAssociatedTypesWithDefaults 2025-12-05 17:05:21 -08:00
Kavon Farvardin
12cb3f9a9a Merge pull request #85704 from kavon/suppressed-assoc-types-with-defaults
Sema: introduce SuppressedAssociatedTypesWithDefaults
2025-12-03 23:07:40 -08:00
Kavon Farvardin
ab1d83a31b Sema: fix cycle involving protocol extensions
We've had a load-bearing optimization that avoids rebuilding
the generic signature of the protocol extension to avoid a
cycle involving a typealias in the extension that's mentioned
as a requirement in the protocol, for example:

```
public protocol P3 {
  associatedtype A
}

public protocol P4: P3 where A == B {}

extension P4 {
  public typealias B = String
}
```
What was happening is that we're inside a
StructuralRequirementsRequest, so we're performing TypeResolution
in the Structural stage. When we encounter this typealias that's
within a protocol extension, we'd end up accidentally requesting
the interface type of that alias through
`TypeChecker::substMemberTypeWithBase`
(via `aliasDecl->getUnderlyingType()`).

What we should do instead is call `aliasDecl->getStructuralType()`
and skip the substitution during that stage.

There happened to already be code doing this, but it was only
kicking in if the DeclContext `isa<ProtocolDecl>`, which excludes
extensions of a protocol. I see no reason why extensions of a
protocol should be excluded, so I assume it was unintentional.

Thus, the fix boils down to using `DeclContext::getSelfProtocolDecl`
which does include extensions of protocols. With this fix, the
optimization is no longer load-bearing on the example above.

See the history of this hack in f747121080
or rdar://problem/129540617
2025-12-03 08:03:19 -08:00
Ben Cohen
fbb420f38d Allow Equatable: ~Copyable (#85746)
Under review
[here](https://forums.swift.org/t/se-0499-support-copyable-escapable-in-simple-standard-library-protocols/83297).
Multi-part version of #85079.
2025-12-03 05:45:38 -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
Ryan Mansfield
e729672fcb [Test] Mark UNSUPPORTED Generics/rdar160804717.swift on watchOS 2025-11-24 18:36:25 -05:00
Kavon Farvardin
9071d1e0a7 Test: split out some tests using SuppressedAssociatedTypes
There's about to be a second version that I'd like to
test in parallel with the old version.
2025-11-20 14:29:04 -08: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
Slava Pestov
7648833d24 Merge pull request #85094 from slavapestov/fix-rdar160804717
Fix spurious "infinite conformance substitution error" with parameter packs
2025-10-23 20:57:03 -04:00
Slava Pestov
b8d0ba9732 AST: Fix substOpaqueTypesWithUnderlyingTypes() to set SubstFlags::PreservePackExpansionLevel
The "iterate until fixed point" would cause an infinite loop where we
wrap type parameter packs in a PackElementType and increase the level
forever otherwise.

This was a regression from commit 103428fe80.

Fixes rdar://160804717.
2025-10-23 15:36:53 -04:00
Slava Pestov
0ff6614c70 Add regression test for fixed crasher 2025-10-23 12:57:32 -04:00
Hamish Knight
71841bdbd5 [Sema] Avoid a couple of downstream diags with invalid same-type reqs
Avoid conformance failures and member lookup errors for generic
signatures with invalid same-type requirements.
2025-10-08 21:16:02 +01:00
Hamish Knight
11d299c212 Merge pull request #84698 from hamishknight/carousel
[Evaluator] Avoid emitting duplicate "through reference here" notes
2025-10-07 20:14:52 +01:00
Pavel Yaskevich
b0a2bd6578 Merge pull request #84715 from xedin/rdar-159401910
[Diagnostics] Fix a few issues with existential type mismatches
2025-10-07 09:39:36 -07:00
Henrik G. Olsson
d3214de950 Merge pull request #84685 from hnrklssn/verify-all
This adds the -verify-ignore-unrelated flag. When -verify is used without -verify-ignore-unrelated, diagnostics emitted in buffers other than the main file and those passed with -verify-additional-file (except diagnostics emitted at <unknown>:0) will now result in an error. They were previously ignored. The old behaviour is still available as opt-in using -verify-ignore-unrelated, but by being strict by default it should make it harder to accidentally miss diagnostics.

To avoid unnecessary performance overhead, -verify-additional-file is still required to parse the expected-* directives in files other than the main file.
2025-10-06 18:01:47 -07:00
Pavel Yaskevich
af909078e5 [CSSimplify] Look through InstanceType while attempting to diagnose conformance failures
Matching existential types could introduce `InstanceType` element
at the end of the locator path, it's okay to look through them to
diagnose the underlying issue.
2025-10-05 23:57:09 -07:00
Henrik G. Olsson
ce8f967c82 add -verify-ignore-unrelated to another test 2025-10-05 15:33:41 -07:00
Hamish Knight
13023de4c4 [Evaluator] Avoid emitting duplicate "through reference here" notes
Filter out any duplicate notes to help cut down on the noise for
request cycle diagnostics. Some of the note locations here still aren't
great, but this at least stops us from repeating them for each
intermediate request.
2025-10-05 20:48:08 +01:00
Slava Pestov
811a201bc1 Merge pull request #84675 from slavapestov/rqm-protocol-failure-bookkeeping
RequirementMachine: New way of propagating failure when building rewrite system for protocol
2025-10-05 14:10:00 -04:00
Henrik G. Olsson
cbc0ec3b88 Add -verify-ignore-unrelated where necessary (NFC)
These are tests that fail in the next commit without this flag. This
does not add -verify-ignore-unrelated to all tests with -verify, only
the ones that would fail without it. This is NFC since this flag is
currently a no-op.
2025-10-04 14:19:52 -07: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
Hamish Knight
669a2ce9b0 [CS] Sink placeholder handling logic into Solution::simplifyType
Move the logic from `FailureDiagnostic::resolveType` into
`Solution::simplifyType` to allow completion to use it too. While
here, also handle cases where the placeholder is from a different
member of the equivalence class to the generic parameter.
2025-10-04 12:56:52 +01:00
Slava Pestov
8ad16fbc80 Merge pull request #84451 from slavapestov/test-84419
Sema: Add test case for https://github.com/swiftlang/swift/issues/84419
2025-09-23 10:50:30 -04:00
Slava Pestov
22234ef22f Sema: Add test case for https://github.com/swiftlang/swift/issues/84419
This was probably fixed by https://github.com/swiftlang/swift/pull/83070.
2025-09-22 19:24:44 -04:00
Hamish Knight
0a55516cf7 Use PrintOptions::forDebugging in a few more places
Use for a few `dump` methods, generic signature debugging, and enable
by default for swift-ide-test.
2025-09-21 23:19:06 +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
Pavel Yaskevich
beb05fb3d0 [Tests] NFC: Add a test-case for regression when TypeSubstituter::transformSubstitutionMap() is enabled 2025-08-22 14:00:31 -07:00
Slava Pestov
e26034b7ac Sema: New opaque return type circularity check that doesn't trigger lazy type checking of bodies
Commit b70f8a82b1 introduced a usage of
ReplaceOpaqueTypesWithUnderlyingTypes in Sema. Previously this was only
called from SILGen.

The problem was that ReplaceOpaqueTypesWithUnderlyingTypes would call
getUniqueUnderlyingTypeSubstitutions(), which triggers a request to
type check the body of the referenced function.

While this didn't result in unnecessary type checking work, because
UniqueUnderlyingTypeSubstitutionsRequest::evaluate() would skip bodies
in secondary files, it did change declaration checking order.

The specific issue we saw was a bad interaction with associated type
inference and unqualified lookup in a WMO build, and a complete test
case is hard to reduce here.

However, no behavior change is intended with this change, modulo bugs
elsewhere related to declaration checking order, so I feel OK not adding
a test case.

I'll hopefully address the unqualified lookup issue exposed in the
radar soon; it has a reproducer independent of opaque return types.

Fixes rdar://157329046.
2025-08-14 17:01:47 -04:00
Slava Pestov
b70f8a82b1 Sema: Improve the infinite opaque return type check
Now look through other opaque return types that appear in the
underlying type. This catches various forms of recursion that
otherwise would cause a SILGen or SILOptimizer crash.

- Fixes rdar://82992151.
2025-07-18 20:03:03 -04:00
Slava Pestov
f8cf708753 RequirementMachine: Increase default type differences limit to 13000
Fixes rdar://154684522.
2025-07-06 11:22:47 -04: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
Slava Pestov
eec924b505 Sema: Fix crash in diagnoseIfSynthesisUnsupportedForDecl() with tuple extension 2025-06-17 09:52:03 -04:00
Slava Pestov
5cdc9a6570 AST: More robust TypeBase::getSuperclassForDecl()
This can return ErrorType if the AST is invalid.

A handful of callers handle the ErrorType result, but most don't,
blindly assuming the result is always a nominal type. This resulted
in a crash in at least one test case.

Lift the burden from callers by always returning a nominal type here.
2025-06-10 16:49:57 -04:00
Meghana Gupta
44e05fa858 [NFC] Update tests and diagnostics 2025-06-07 12:49:01 -07:00
Slava Pestov
cd5ecbee16 Sema: Implement missing part of SE-0346
The proposal states that this should work, but this was never
implemented:

    protocol P<A> {
      associatedtype A
    }

    struct S: P<Int> {}

- Fixes https://github.com/swiftlang/swift/issues/62906.
- Fixes rdar://91842338.
2025-06-03 17:28:19 -04:00
Slava Pestov
d8e418a0f9 AST: Fix crash when type parameter is substituted with an existential
getContextSubstitutionMap() didn't handle the case where getAnyNominal()
returns a ProtocolDecl. This should not take the "fast path", which is
only suitable for concrete nominals.

This manifested as a crash-on-invalid -- the user probably meant to write
"T.Value: Collection" rather than "T.Value == Collection".

Fixes rdar://151479861.
2025-06-02 19:17:21 -04: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
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
Tony Allevato
a3993f972a Merge pull request #80498 from allevato/json-usr-fixes
[AST] More JSON AST dump improvements.
2025-04-23 15:35:17 -04:00
Anthony Latsis
2cd90bdd69 AST: Quote attributes more consistently in DiagnosticsSema.def 2025-04-22 18:23:36 +01:00
Anthony Latsis
2d899d0e73 AST: Cut down on DescriptiveDeclKind usage in DiagnosticsCommon.def 2025-04-05 12:31:20 +01:00
Tony Allevato
fa1f82967f Also show the new conformance details in the default dump. 2025-04-03 12:37:15 -04:00
Slava Pestov
43d9d7f3dc Add regression test for fixed crasher 2025-03-27 15:36:21 -04:00
Slava Pestov
d77c6f413d RequirementMachine: Skip protocol type aliases that contain unbound dependent member types
In the below, 'Self.A.A' is not a type parameter; rather, since
'Self.A' is concretely known to be 'S', we resolve it as 'S.A',
which performs a name lookup and finds the concrete type alias 'A':

    public struct S {
      public typealias A = Int
    }

    public protocol P {
      typealias A = S
    }

    public struct G<T> {}

    public protocol Q: P {
      typealias B = G<Self.A.A>
    }

This is fine, but such a type alias should not participate in
the rewrite system. Let's exclude them like any other invalid
requirement.

The type alias itself is not an error; however, it is an error
to use it from a 'where' clause requirement. This is not
diagnosed yet, though.

Fixes rdar://136686001.
2025-03-26 10:55:23 -04:00
Andrew Trick
64a48d08e1 Update tests for strict @lifetime type checking 2025-03-19 11:59:04 -07:00
coffmark
cc6f1a1548 Sema: Add missing space to Copyable conformance suppression fix-it 2025-02-25 08:48:46 +09:00