Commit Graph

1364 Commits

Author SHA1 Message Date
Slava Pestov e33a43aa3e Merge pull request #89167 from slavapestov/clean-up-variadic-parameter-requirement-check
Sema: Fix crash when declaring an old-style variadic parameter with nonescaping type
2026-05-18 22:19:08 -04:00
Slava Pestov f05d1cab32 Sema: Fix crash when declaring an old-style variadic parameter with nonescaping type
This is not supported for the same reason you can't have a noncopyable
variadic parameter, but the code was hard-coded to check for copyability
only.

Generalize this to apply the generic signature for Array via the
common code path, and add a custom note to explain why those requirements
are checked in the first place.

Cleaning this up also fixed some fuzzer crashes.

Reported on the forums:
https://forums.swift.org/t/variadic-escapable-arguments-crash-the-compiler/86727/2
2026-05-18 10:49:23 -04:00
Hamish Knight 841b1318a2 [test] Add additional test for inverse with class protocol composition 2026-05-15 19:48:31 +01:00
Allan Shortlidge 18e4fd76b5 Merge pull request #88886 from tshortli/improve-use-any-apple-os-diags
Sema: Improve `UseAnyAppleOSAvailability` diagnostics
2026-05-08 14:41:40 -07:00
Ben Cohen 8cb5dfc117 AST: don't propagate null Type from invalid associated conformance (#88873)
When a pattern conformance in the pack is invalid (e.g. its underlying
conformance failed because of a prior diagnostic),
`getAssociatedConformance` returned an invalid `ProtocolConformanceRef`
whose `getType()` is null. The null type was pushed into `packElements`
and tripped an assertion in `PackType::get`. Substitute `ErrorType` for
the null so substitution can complete and the existing diagnostics
surface to the user.

Fixes https://github.com/swiftlang/swift/issues/88434.
2026-05-06 22:04:22 -07:00
Ben Cohen cc9804cd5a Sema: fix crash diagnosing inverse on class-bound-protocol composition (#88861)
`any Foo & ~Copyable` where `Foo: AnyObject` triggered a null Type in
the `inverse_with_class_constraint` diagnostic: the layout requires a
class via protocol refinement, but `getSuperclass()` is null with no
explicit superclass term. Select the "AnyObject" branch when there's no
concrete superclass.

resolves rdar://161839720 (Type checker crash on illegal existential
type)
2026-05-06 15:53:45 -07:00
Allan Shortlidge cea706a03f AST: Improve InFlightDiagnostic::fixItRemove() heuristics.
If the fix-it would remove a range that is followed by a newline and the
remaining text on the line is empty or all whitespace then remove the entire
line. This produces better results when a fix-it removes an attribute that is
written on a line by itself.
2026-05-06 11:03:30 -07:00
Kavon Farvardin e92398b17e Test: SE-503 opaque returns with -debug-generic-signatures
Additional test coverage of expected signatures.

There's currently no syntax to suppress the `t_1_0: Copyable`
default requirement in the opaque return type. But we at least
now support suppression for the associated type.
2026-05-04 14:49:34 -07:00
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 610a9f98ed SuppAssocTypes: enable SE-503 by default 2026-04-22 22:11:41 -07:00
Kavon Farvardin c996b68a83 Test: test inherited suppression restriction 2026-04-03 17:50:39 -07:00
Kavon Farvardin 6626096dc5 Test: extra test for implied conformances 2026-04-03 17:50:39 -07:00
Kavon Farvardin 1f2b51dbe6 Test: more test coverage for single-level expansion behavior
The `Assoc.Assoc: ~Copyable` cancels out the assoc type default
inferred for it from the `Assoc: Recur` requirement in the protocol's
signature. This seems to cause that level and any deeper levels to no
longer infer a default.

We have coverage for this in the signatures tests but not here.
2026-04-03 17:50:39 -07:00
Kavon Farvardin fe50668d0e SuppAssocTypes: add deprecation warning of legacy version
We need to start moving people over to using the version of
suppressed associated types that has the officially accepted
behavior according to SE-503, and that is via
`SuppressedAssociatedTypesWithDefaults`.

To allow for time to migrate to the new version, I'm introducing
the deprecation message as a warning for now.

This set of changes also moves most of the test suite over to using
the officially accepted version.

rdar://172231663
2026-03-10 17:08:10 -07:00
Nate Cook 856b205170 De-underscore the BorrowingSequence protocols (#87483)
This replaces #87114, aligning the `BorrowingSequence` protocols and
related types to the Swift Evolution proposal, and without included
reparenting of `Sequence`.
2026-03-04 07:13:20 -06:00
Kavon Farvardin 6a915549eb Reparenting: allow implied conditional conformances
The only way to avoid a source break when introducing a
reparenting is to allow implied conditional conformance
to a @reparentable protocol, as prior code would not have
explicitly stated it.
2026-02-06 16:20:56 -08: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 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
Slava Pestov 623f36b472 Fix test/Generics/Inputs/conditional_conformances_invisible_witness_other.swift
- Fixes rdar://168942655
2026-01-27 00:08:19 +00:00
Rintaro Ishizaki f19171b6bb [Test] Temporaliry disable a failing test case
rdar://168942655
2026-01-26 15:02:26 -08:00
Sam Pyankov c3ee2bce8e Merge pull request #86749 from sepy97/revert_public-decl-internal-protocol
Revert "Diagnose public protocol witnessed by an internal protocol ex…
2026-01-26 10:16:34 -08:00
Slava Pestov ee0f6dd681 Sema: Narrow down the skipProtocolSelfConstraint hack
We actually have to check this constraint, otherwise we accept invalid
code where we're referencing a protocol requirement with a concrete
base type which conditionally conforms to the protocol, but the
conditional requirements are unsatisfied.

However, we still need to skip it for AnyObject lookup, Distributed
Actors, and some weird edge case in conformance checking for isolated
conformances.

We should clean this up further later, but for now this closes a soundness
hole.

Fixes rdar://168129757.
2026-01-25 21:11:59 +00:00
Sam Pyankov 70835508b0 Revert "Diagnose public protocol witnessed by an internal protocol extension"
This reverts commit 6280a7001f.
rdar://168627163
2026-01-23 10:28:19 -08:00
Xi Ge 4a6badd6ee Merge pull request #85846 from sepy97/public-decl-internal-protocol
Diagnose public protocol requirements witnessed by internal protocol extensions
2025-12-17 09:39:32 -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
Kavon Farvardin 64abbed84e Sema: add coverage for existential suppression 2025-12-15 16:46:20 -08:00
Sam Pyankov 6280a7001f Diagnose public protocol witnessed by an internal protocol extension
Private member witnessing a public constraint should be deprecated.
Previously existing workaround is checked and compiler emits a warning
when strict access check is not passed. Test files were fixed to expect
the corresponding warning.

rdar://74904373
2025-12-15 16:23:06 -08:00
Slava Pestov 98aee232f6 Sema: Fix 'generic parameter mentioned in signature' check edge case with subscripts
If a generic parameter is same-type-constrained to an outer type
parameter, it doesn't have to appear in the subscript's signature.

- Fixes https://github.com/swiftlang/swift/issues/50917.
2025-12-15 15:27:20 -05:00
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