Commit Graph

28619 Commits

Author SHA1 Message Date
Doug Gregor
29f9c75fd6 Merge pull request #83001 from DougGregor/recursion-check-before-invalid
[Associated type inference] Do recursive validation check before "invalid" check
2025-07-11 15:26:18 -07:00
Pavel Yaskevich
d8a12e9b75 Merge pull request #82925 from xedin/remove-concurrency-attr-clash-downgrade
[Concurrency] Don't downgrade explicit isolation attribute clashes
2025-07-11 10:52:06 -07:00
Slava Pestov
03de964154 AST: Change RequirementEnvironment::getRequirementToWitnessThunkSubs() to use contextual types
In the provided test case, the generic signature of the
protocol requirement is

    <Self, T where Self == T.A, T: P2>

The conformance requirement `Self: P1` is derived from `T: P2`
and `Self.A: P1` in P1. So given a substitution map
`{ Self := X, T := T }` that replaces T with a concrete type X
and T with a type parameter T, there are two ways to recover a
substituted conformance for `Self: P1`:

- We can apply the substitution map to Self to get X, and look
  up conformance of X to P1, to get a concrete conformance.

- We can evaluate the conformance path `(T: P2)(Self.A: P1)`,
  to get an abstract conformance.

Both answers are correct, but SILGenModule::emitProtocolWitness()
was assuming it would always get a concrete conformance back.

This was the case until e3c8f423bc,
but then we started returning an abstract conformance. SILGen
would then mangle the protocol witness thunk in a way that was
not sufficiently unique, and as a result, we could miscompile
a program where two witness tables both hit this same scenario.

By using contextual types in the getRequirementToWitnessThunkSubs()
substitution map, we ensure that evaluating the conformance path
against the substitution map produces the same result as performing
the global lookup.

Also, to prevent this from happening again, add a check to SILGen
to guard against emitting two protocol witness thunks with the
same mangled name.

Unfortunately, this is done intentionally as part of some
backward deployment logic for coroutine accessors. There is a
hack to allow duplicate thunks with the same name in this case,
but this should be revisited some day.

Fixes rdar://problem/155624135..
2025-07-11 13:31:35 -04:00
Pavel Yaskevich
020596918d Merge pull request #82961 from gottesmm/pr-b3685ba26b885701787f7b6087dc7d9d873aa500
[rbi] Teach SendNonSendable how to more aggressively suppress sending errors around obfuscated Sendable functions
2025-07-11 10:15:21 -07:00
Doug Gregor
72bda97f86 [Associated type inference] Do recursive validation check before "invalid" check
The "invalid" check can kick off recursive checking, which is why it
should happen second.
2025-07-11 10:09:16 -07:00
Allan Shortlidge
302fa00a31 Merge pull request #82962 from tshortli/layout-prespecialization-feature 2025-07-10 21:28:36 -07:00
Slava Pestov
9a8f3baa06 Sema: Add room for a PreparedOverload to BindOverload constraint 2025-07-10 19:53:19 -04:00
Slava Pestov
83f95aeaed Sema: Allocate PreparedOverload in solver arena 2025-07-10 19:53:19 -04:00
Slava Pestov
7916ed982f Sema: Move DeclReferenceType to PreparedOverload.h 2025-07-10 19:53:19 -04:00
Slava Pestov
f7a552385c Sema: PreparedOverload => PreparedOverloadBuilder 2025-07-10 19:53:18 -04:00
Slava Pestov
8527e88423 Sema: Extract prepareOverload() from resolveOverload() 2025-07-10 19:53:15 -04:00
Slava Pestov
8f0eaff8df Sema: Plumb PreparedOverload into getTypeOfReferenceWithSpecialTypeCheckingSemantics() 2025-07-10 19:52:40 -04:00
Slava Pestov
02cc5a2a27 Sema: Fix handling of AddedConstraint in PreparedOverload::discharge() 2025-07-10 19:52:40 -04:00
Doug Gregor
84e202eb02 Merge pull request #82954 from DougGregor/strict-memory-safety-codable-synthesis
[Strict memory safety] Eliminate spurious warnings with synthesized Codable
2025-07-10 14:07:27 -07:00
Allan Shortlidge
c6dad96492 Make LayoutPrespecialization a baseline feature instead of experimental.
Since LayoutPrespecialization has been enabled by default in all compiler
invocations for quite some time, it doesn't make sense for it to be treated as
experimental feature. Make it a baseline feature and remove all the
checks for it from the compiler.
2025-07-10 11:25:28 -07:00
Michael Gottesman
ea4d0440f4 Revert "[Concurrency] Prevent use of nonisolated(nonsending) and @concurrent on @_inheritActorContext parameters"
This reverts commit e032f33cfb.
2025-07-10 11:15:00 -07:00
Doug Gregor
1142fa3bdd [Strict memory safety] Eliminate spurious warnings with synthesized Codable
When synthesizing code for Codable conformances involving unsafe types,
make sure to wrap the resulting expressions in "unsafe" when strict memory safety is enabled.

Tweak the warning-emission logic to suppress warnings about spurious
"unsafe" expressions when the compiler generated the "unsafe" itself,
so we don't spam the developer with warnings they can't fix. Also make
the checking for other suppression considerations safe when there are
no source locations, eliminating a potential assertion.

Fixes rdar://153665692.
2025-07-10 09:03:04 -07:00
Slava Pestov
f0c783aa5d Merge pull request #82936 from slavapestov/builtin-fixed-array-cleanup
BuiltinFixedArray cleanup
2025-07-10 03:21:28 -04:00
Doug Gregor
3542b1df0f Merge pull request #82938 from DougGregor/unsafe-for-in-fixes 2025-07-09 23:16:52 -07:00
Slava Pestov
0ab6d53fff Sema: Implement matchTypes() for BuiltinFixedArrayType 2025-07-09 22:16:13 -04:00
Slava Pestov
2f90d50a78 Sema: Simplify value generic check in ConstraintSystem::getTypeOfMemberReference() 2025-07-09 22:15:25 -04:00
Doug Gregor
35628cb503 [Strict memory safety] Fix "unsafe" checking for the for..in loop
The `$generator` variable we create for the async for..in loop is
`nonisolated(unsafe)`, so ensure that we generate an `unsafe`
expression when we use it. This uncovered some inconsistencies in how
we do `unsafe` checking for for..in loops, so fix those.

Fixes rdar://154775389.
2025-07-09 16:19:14 -07:00
Slava Pestov
751edee921 Sema: Rework PreparedOverload to store a list of changes 2025-07-09 15:00:10 -04:00
Slava Pestov
2a0b11a22e Sema: Collect property wrappers in PreparedOverload 2025-07-09 15:00:03 -04:00
Slava Pestov
be6cf92982 Sema: Plumb PreparedOverload through to replaceInferableTypesWithTypeVars() 2025-07-09 15:00:03 -04:00
Slava Pestov
ab72407108 Sema: Collect opened pack expansion types in PreparedOverload 2025-07-09 15:00:02 -04:00
Slava Pestov
c5741e31ed Sema: Stash the existential archetype in PreparedOverload 2025-07-09 15:00:02 -04:00
Slava Pestov
426c7a3fdf Sema: Collect constraints in PreparedOverload 2025-07-09 15:00:02 -04:00
Slava Pestov
543793564e Sema: Record opened type variables in PreparedOverload 2025-07-09 15:00:02 -04:00
Slava Pestov
b9256307b5 Sema: Stub out PreparedOverload 2025-07-09 15:00:02 -04:00
Pavel Yaskevich
a9373c0f3f [Concurrency] Don't downgrade explicit isolation attribute clashes
The compiler shouldn't accept mismatch in explicit isolation attributes
because it could lead to incorrect isolation selection.
2025-07-09 10:59:13 -07:00
Alexis Laferrière
9e953d2f96 Merge pull request #82891 from xymus/cdecl-reserved-names
Sema: Report the use of reserved names by `@cdecl` functions
2025-07-09 09:22:03 -07:00
Slava Pestov
af7d54fb16 Merge pull request #82876 from slavapestov/unused-var-jul-8
Sema: Remove unused variable
2025-07-08 21:18:43 -04:00
Pavel Yaskevich
57ef411ce9 Merge pull request #82869 from xedin/logic-infix-operators-perf
[CSOptimizer] Prefer logical infix operator if it has smaller overload set
2025-07-08 16:49:45 -07:00
Alexis Laferrière
a23c658c9d Sema: Report the use of reserved @cdecl names 2025-07-08 13:00:06 -07:00
John McCall
0f919f1d68 Merge pull request #82867 from rjmccall/dont-capture-defer-isolation
Don't force a capture of an isolated parameter in defer bodies.
2025-07-08 11:58:18 -04:00
Slava Pestov
7f35779ce8 Sema: Remove unused variable 2025-07-08 09:48:08 -04:00
Pavel Yaskevich
1f8e487e2e [CSOptimizer] Prefer logical infix operator if it has smaller overload set
Infix logical operators are usually not overloaded and don't
form disjunctions, but when they do, let's prefer them over
other operators when they have fewer choices because it helps
to split operator chains.
2025-07-08 00:10:47 -07:00
Pavel Yaskevich
785e7b1571 Merge pull request #82858 from gottesmm/rdar154969621
[concurrency] Perform some fixes so that transfernonsendable_closureliterals_isolationinference.swift now passes.
2025-07-08 00:04:14 -07:00
Pavel Yaskevich
dab6f3d12e Merge pull request #82807 from xedin/se-0487-implementation-adjustments
[AST/Sema] SE-0487: Adjust implementation based on the LSG feedback
2025-07-08 00:03:36 -07:00
Pavel Yaskevich
1c546e93fa Merge pull request #82857 from xedin/inheritActorContext-and-nonisolated-nonsending
[Concurrency] Prevent use of `nonisolated(nonsending)` and `@concurrent`
2025-07-08 00:03:23 -07:00
Doug Gregor
cee2aeadbb Merge pull request #82846 from DougGregor/isolated-conformance-to-sendablemetatype-proto-error
[SE-0470] Promote isolated-conformance-to-sendable-metatype protocol to an error
2025-07-07 21:06:47 -07:00
John McCall
e30728c5a6 Don't force a capture of an isolated parameter in defer bodies.
SILGen already has an exception for this from -enable-actor-data-race-checks,
so there's no need for it, and it causes problems in actor inits.

Fixes rdar://155239032
2025-07-07 23:29:04 -04:00
Slava Pestov
e67be5c683 Merge pull request #82690 from slavapestov/fix-rdar116938972
Sema: Improve the 'protocol method can't impose new requirements on Self' check
2025-07-07 23:25:58 -04:00
Konrad `ktoso` Malawski
c6f223f57a Merge pull request #82793 from ktoso/wip-correct-isolated-handling-with-caller-isolation 2025-07-08 12:01:54 +09:00
Michael Gottesman
648bb8fe30 [concurrency] Perform some fixes so that transfernonsendable_closureliterals_isolationinference.swift now passes.
The reason why this failed is that concurrently to @xedin landing
79af04ccc4, I enabled
NonisolatedNonsendingByDefault on a bunch of other tests. That change broke the
test and so we needed to fix it.

This commit fixes a few issues that were exposed:

1. We do not propagate nonisolated(nonsending) into a closure if its inferred
context isolation is global actor isolated or if the closure captures an
isolated parameter. We previously just always inferred
nonisolated(nonsending). Unfortunately since we do not yet have capture
information in CSApply, this required us to put the isolation change into
TypeCheckConcurrency.cpp and basically have function conversions of the form:

```
(function_conversion_expr type="nonisolated(nonsending) () async -> Void"
   (closure_expr type="() async -> ()" isolated_to_caller_isolation))
```

Notice how we have a function conversion to nonisolated(nonsending) from a
closure expr that has an isolation that is isolated_to_caller.

2. With this in hand, we found that this pattern caused us to first thunk a
nonisolated(nonsending) function to an @concurrent function and then thunk that
back to nonisolated(nonsending), causing the final function to always be
concurrent. I put into SILGen a peephole that recognizes this pattern and emits
the correct code.

3. With that in hand, we found that we were emitting nonisolated(nonsending)
parameters for inheritActorContext functions. This was then fixed by @xedin in

With all this in hand, closure literal isolation and all of the other RBI tests
with nonisolated(nonsending) enabled pass.

rdar://154969621
2025-07-07 17:21:41 -07:00
Pavel Yaskevich
e032f33cfb [Concurrency] Prevent use of nonisolated(nonsending) and @concurrent on @_inheritActorContext parameters
`@_inheritActorContext` is a form of isolation which precludes
direct use of inference of `nonisolated(nonsending)` and `@concurrent`
just like other isolation attributes/modifiers would i.e. `isolated`
or `@isolated(any)`.
2025-07-07 17:21:41 -07:00
Pavel Yaskevich
e42ac61d9f [Concurrency] Prevent use of nonisolated(nonsending) and @concurrent on @_inheritActorContext parameters
`@_inheritActorContext` is a form of isolation which precludes
direct use of inference of `nonisolated(nonsending)` and `@concurrent`
just like other isolation attributes/modifiers would i.e. `isolated`
or `@isolated(any)`.
2025-07-07 17:13:28 -07:00
Doug Gregor
56e38b33b6 [SE-0470] Promote isolated-conformance-to-sendable-metatype protocol to error
Forming an isolated conformance to a SendableMetatype-inherting
protocol opens up a soundness hole any time the conformance is used.
Reword the recently-introduced diagnostic for this case and promote it
to an error (except when it's preconcurrency).

Fixes rdar://154808002.
2025-07-07 11:35:19 -07:00
Slava Pestov
4dc0db31f9 Sema: Improve the 'protocol method can't impose new requirements on Self' check
This check had two problems. First, it would assert upon encountering
a layout requirement, due to an unimplemented code path.

A more fundamental issue is that the logic wasn't fully sound, because
it would miss certain cases, for example:

    protocol P {
      associatedtype A

      func run<B: Equatable>(_: B) where B == Self.A
    }

Here, the reduced type of `Self.A` is `B`, and at first glance, the
requirement `B: Equatable` appears to be fine. However, this
is actually a new requirement on `Self`, and the protocol be rejected.

Now that we can change the reduction order by assigning weights to
generic parameters, this check can be implemented in a better way,
by building a new generic signature first, where all generic
parameters introduced by the protocol method, like 'B' above, are
assigned a non-zero weight.

With this reduction order, any type that is equivalent to
a member type of `Self` will have a reduced type rooted in `Self`,
at which point the previous syntactic check becomes sound.

Since this may cause us to reject code we accepted previously,
the type checker now performs the check once: first on the original
signature, which may miss certain cases, and then again on the new
signature built with the weighted reduction order.

If the first check fails, we diagnose an error. If the second
check fails, we only diagnose a warning.

However, this warning will become an error soon, and it really
can cause compiler crashes and miscompiles to have a malformed
protocol like this.

Fixes rdar://116938972.
2025-07-07 12:23:58 -04:00