Commit Graph

103252 Commits

Author SHA1 Message Date
Pavel Yaskevich
36261876b6 Merge pull request #82459 from xedin/rdar-154010220-6.2
[6.2][CSSimplify] Prevent `missing call` fix from recording fixes while ma…
2025-06-24 20:51:17 -07:00
Slava Pestov
e78d7349d7 Merge pull request #82447 from slavapestov/fix-rdar152509409-6.2
[6.2] AST: Tweak ConformanceLookupTable::compareConformances() some more
2025-06-24 21:19:54 -04:00
Andrew Trick
78e8b5af50 Disable surprising lifetime inference of implicit initializers
Non-escapable struct definitions often have inicidental integer fields that are
unrelated to lifetime. Without an explicit initializer, the compiler would infer
these fields to be borrowed by the implicit intializer.

    struct CountedSpan: ~Escapable {
      let span: Span<Int>
      let i: Int

      /* infer: @lifetime(copy span, borrow i) init(...) */
    }

This was done because
- we always want to infer lifetimes of synthesized code if possible
- inferring a borrow dependence is always conservative

But this was the wrong decision because it inevitabely results in lifetime
diagnostic errors elsewhere in the code that can't be tracked down at the use
site:

    let span = CountedSpan(span: span, i: 3) // ERROR: span depends on the lifetime of this value

Instead, force the author of the data type to specify whether the type actually
depends on trivial fields or not. Such as:

    struct CountedSpan: ~Escapable {
      let span: Span<Int>
      let i: Int

      @lifetime(copy span) init(...) { ... }
    }

This fix enables stricter diagnostics, so we need it in 6.2.

Fixes rdar://152130977 ([nonescapable] confusing diagnostic message when a
synthesized initializer generates dependence on an Int parameter)

(cherry picked from commit 8789a686fed869e3cd7bc4e748a443e71df464e1)
2025-06-24 18:09:09 -07:00
Pavel Yaskevich
767dbc0d78 [CSSimplify] Narrow down tuple wrapping for pack expansion matching
Follow-up for https://github.com/swiftlang/swift/pull/82326.

The optional injection is only viable is the wrapped type is
not yet resolved, otherwise it's safe to wrap the optional.

(cherry picked from commit 4804f2131b)
2025-06-24 17:27:28 -07:00
Pavel Yaskevich
574537fc8a [Concurrency] Global actor isolated conformances are only allowed to override nonisolated.
Follow-up for https://github.com/swiftlang/swift/pull/79893.

More than one global actor isolated conformance at any level
creates a clash and conforming type should be inferred as `nonisolated`.

Resolves: rdar://154202375
(cherry picked from commit 2e1fe444a6)
2025-06-24 17:23:03 -07:00
Andrew Trick
d3002ed0b2 [nonescapable] remove '@_lifetime' requirement on implicit accessors
This avoids diagnostic errors on synthesized accessors, which are impossible for developers to understand.

Fixes rdar://153793344 (Lifetime-dependent value returned by generated accessor '_read')

(cherry picked from commit 855b3e4446)
2025-06-24 15:06:30 -07:00
Egor Zhdan
a686f323e5 Merge pull request #82420 from swiftlang/egorzhdan/6.2-namespace-printer-null-crash
🍒[cxx-interop] Fix printing of namespaces declared in bridging headers
2025-06-24 22:35:11 +01:00
Andrew Trick
2052d2c61e Fix MoveOnlyObjectCheckerPImpl::check() for mark_dependence.
Handle the presence of mark_dependence instructions after a begin_apply.

Fixes a compiler crash:
"copy of noncopyable typed value. This is a compiler bug. ..."

(cherry picked from commit 7a29d9d8b6)
2025-06-24 12:54:39 -07:00
Andrew Trick
4cd7f450a3 Fix MoveOnlyObjectCheckerPImpl::check() changed flag
Extract the special pattern matching logic that is otherwise unrelated to the
check() function. This makes it obvious that the implementation was failing to
set the 'changed' flag whenever needed.

(cherry picked from commit c41715ce8c)
2025-06-24 12:54:39 -07:00
Andrew Trick
1fa15f23fe Fix SILCombine of MarkDependenceInst.
Do not eliminate a mark_dependence on a begin_apply scope even though the token
has a trivial type.

Ideally, token would have a non-trivial Builtin type to avoid special cases.

(only relevant on 6.2)
2025-06-24 12:54:39 -07:00
Meghana Gupta
1d1f9b063f Merge pull request #82431 from meg-gupta/lifetimeenumcp
[6.2] Add lifetime dependencies on function types representing ~Escapable enum elements with ~Escapable payloads
2025-06-24 11:06:27 -07:00
Pavel Yaskevich
39a7138a0c [CSSimplify] Prevent missing call fix from recording fixes while matching types
We need to be very careful while matching types to test whether a
fix is applicable or not to avoid adding extraneous fixes and failing
the path early. This is a temporary workaround, the real fix would
be to let `matchTypes` to propagate `TMF_ApplyingFixes` down.

Resolves: rdar://154010220
Resolves: https://github.com/swiftlang/swift/issues/82397
(cherry picked from commit 7ecb1fd1db)
2025-06-24 09:49:02 -07:00
Allan Shortlidge
2371e1fec1 Merge pull request #82435 from tshortli/reorganize-availability-6.2
[6.2] SILGen: Reorganize some availability related code
2025-06-24 09:20:45 -07:00
Pavel Yaskevich
67449c91e4 Merge pull request #82432 from xedin/nonisolated_nonsending-fixes-6.2
[6.2][Concurrency] A few `nonisolated(nonsending)` fixes
2025-06-24 09:12:45 -07:00
Slava Pestov
e5af49a282 AST: Tweak ConformanceLookupTable::compareConformances() some more
If two conformances imply a conformance to the same marker
protocol, don't diagnose redundancy if they differ by
unavailability. Instead, allow the more available conformance
to win.

This allows declaring a type that conforms to a protocol
that inherits from SendableMetatype, followed by an
unavailable Sendable conformance on the same type.

Fixes rdar://152509409.
2025-06-23 22:57:05 -04:00
Doug Gregor
c251897278 Allow '@unsafe' on import declarations to silence '@preconcurrency' warning
'@preconcurrency' imports open up memory safety holes with respect to
Sendable, which are diagnosed under strict memory safety + strict
concurrency checking. Allow one to write '@unsafe' on those imports to
silence the diagnostic about it.
2025-06-23 19:12:24 -07:00
Dario Rexin
96d6eb87ee Merge pull request #82352 from drexin/wip-153681688-6.2
[6.2][IRGen] Fix placeholder logic for emission of conditionally inverted protocols
2025-06-23 18:22:26 -07:00
Allan Shortlidge
059688fa02 SILGen: Reorganize code related to availability.
NFC.
2025-06-23 15:57:17 -07:00
Allan Shortlidge
b1b1e00865 SILGen: Rename SILGenBackDeploy.cpp to SILGenAvailability.cpp.
NFC.
2025-06-23 15:57:16 -07:00
Pavel Yaskevich
61c480f852 [Concurrency] References to nonisolated(nonsending) properties don't leave caller's isolation
Make sure that referencing `nonisolated(nonsending)` properties,
especially through a witness is as not treated as leaving the
isolation domain of the caller.

Resolves: rdar://153922620
(cherry picked from commit 35a41ab9cf)
2025-06-23 13:58:18 -07:00
Pavel Yaskevich
c68a17b8c4 [Concurrency] Don't infer nonisolated(nonsending) on synchronous witnesses
If the requirement is `nonisolated(nonsending)` but witness is
synchronous, prevent actor isolation inference from requirements
because this isolation only applies to asynchronous declarations
at the moment.

Resolves: rdar://153680826
(cherry picked from commit a964282275)
2025-06-23 13:58:07 -07:00
Meghana Gupta
41881a85af Avoid circular reference errors by adding an early bailout for imported enums 2025-06-23 13:50:33 -07:00
Meghana Gupta
9604081c46 Serialize/deserialize lifetime dependencies on enum elements 2025-06-23 13:50:13 -07:00
Meghana Gupta
3e359e4774 Support lifetime dependence inference on enum elements 2025-06-23 13:48:57 -07:00
Meghana Gupta
8e5a047ae8 [NFC] Prepare LifetimeDependenceInfo to hold EnumElementDecl* 2025-06-23 13:48:39 -07:00
Slava Pestov
f9a6432ffd Sema: Expand isolated conformance inference to consider conformances to inherited protocols
Otherwise, if PDerived inherits from P and P's requirements are
witnessed by @MainActor-isolated members, we fail to infer
@MainActor isolation on the conformance to PDerived.

- Fixes https://github.com/swiftlang/swift/issues/82222.
- Fixes rdar://153219831.
2025-06-23 15:19:55 -04:00
Egor Zhdan
782f26e726 [cxx-interop] Fix printing of namespaces declared in bridging headers
If a C++ namespace has redeclarations in a bridging header, printing AST for the namespace would crash the compiler. This is because such a redeclaration would not have an owning Clang module, and the AST printer did not account for that.

This change fixes the crash.

rdar://151715540
(cherry picked from commit cc9c51deea)
2025-06-23 18:56:19 +01:00
Egor Zhdan
bf20dab48c Merge pull request #82185 from swiftlang/susmonteiro/6.2-class-metadata-private-fields
🍒 [cxx-interop] Support for printing C++ foreign references
2025-06-23 17:52:00 +01:00
Gábor Horváth
2f7ea38223 Merge pull request #82305 from swiftlang/gaborh/shared-references-are-safe-on-6.2
[6.2][cxx-interop] Shared references are considered safe
2025-06-23 18:48:10 +02:00
Gábor Horváth
70f1a0f9aa Merge pull request #82175 from swiftlang/gaborh/using-shadow-decl-mangling-on-6.2
[6.2][cxx-interop] Fix crash in ASTMangler triggered by UsingShadowDecls
2025-06-23 18:46:14 +02:00
Michael Gottesman
8f3c0aed52 Merge pull request #82388 from gottesmm/release/6.2-153082633
[6.2][silgen] Make async_Main compatible with calling nonisolated(nonsending) functions.
2025-06-23 09:40:08 -07:00
nate-chandler
99f3547e55 Merge pull request #82402 from jamieQ/6-2-copy-prop-ossa-dead-ends-build-time-fix
[6.2][SILOptimizer]: slow OSSA lifetime canonicalization mitigation
2025-06-23 01:02:47 -07:00
Andrew Trick
156f68fb61 Merge pull request #82381 from atrick/62-nonescapable-accessor-on-trivial
[6.2] Add Feature: NonescapableAccessorOnTrivial
2025-06-22 18:15:41 -07:00
Jamie
11525cabf8 [SILOptimizer]: slow OSSA lifetime canonicalization mitigation
OSSA lifetime canonicalization can take a very long time in certain
cases in which there are large basic blocks. to mitigate this, add logic
to skip walking the liveness boundary for extending liveness to dead
ends when there aren't any dead ends in the function.

Updates `DeadEndBlocks` with a new `isEmpty` method and cache to
determine if there are any dead-end blocks in a given function.

(cherry picked from commit 1f3f830fc7)
2025-06-22 18:08:06 -05:00
Michael Gottesman
25c2359cca Merge pull request #82379 from gottesmm/release/6.2-152522631
[6.2][sema] Work around a double curry thunk actor isolation inference bug that is a knock on effect of ced96aa5cd.
2025-06-20 20:41:59 -07:00
Andrew Trick
4fa7e136e9 Add Feature: NonescapableAccessorOnTrivial
To guard the new UnsafeMutablePointer.mutableSpan APIs.

This allows older compilers to ignore the new APIs. Otherwise, the type checker
will crash on the synthesized _read accessor for a non-Escapable type:

    error: cannot infer lifetime dependence on the '_read' accessor because 'self'
    is BitwiseCopyable, specify '@lifetime(borrow self)'

I don't know why the _read is synthesized in these cases, but apparently it's
always been that way.

Fixes: rdar://153773093 ([nonescapable] add a compiler feature to guard
~Escapable accessors when self is trivial)

(cherry picked from commit cc357f4f32)
2025-06-20 16:01:41 -07:00
Michael Gottesman
06d91d4a3f [silgen] Make async_Main compatible with calling nonisolated(nonsending) functions.
The problem is that async_Main was setting an executor as its main executor
instead of an actor. This patch fixes the issue by just grabbing the main actor
instead.

rdar://153082633
(cherry picked from commit 862ef621c7)
2025-06-20 15:54:36 -07:00
Michael Gottesman
845e1bcd14 [sema] Work around a double curry thunk actor isolation inference bug that is a knock on effect of ced96aa5cd.
Specifically, there is currently a bug in TypeCheckConcurrency.cpp where we do
not visit autoclosures. This causes us to never set the autoclosure's
ActorIsolation field like all other closures. For a long time we were able to
get away with this just by relying on the isolation of the decl context of the
autoclosure... but with the introduction of nonisolated(nonsending), we found
cases where the generated single curry autoclosure would necessarily be
different than its decl context (e.x.: a synchronous outer curry thunk that is
nonisolated that returns an inner curry thunk that is
nonisolated(nonsending)). This problem caused us to hit asserts later in the
compiler since the inner closure was actually nonisolated(nonsending), but we
were thinking that it should have been concurrent.

To work around this problem, I changed the type checker in
ced96aa5cd to explicitly set the isolation of
single curry thunk autoclosures when it generates them. The reason why we did
this is that it made it so that we did not have to have a potential large source
break in 6.2 by changing TypeCheckConcurrency.cpp to visit all autoclosures it
has not been visiting.

This caused a follow on issue where since we were now inferring the inner
autoclosure to have the correct isolation, in cases where we were creating a
double curry thunk for an access to a global actor isolated field of a
non-Sendable non-global actor isolated nominal type, we would have the outer
curry thunk have unspecified isolation instead of main actor isolation. An
example of this is the following:

```swift
class A {
  var block:  @MainActor () -> Void = {}
}

class B {
  let a = A()

  func d() {
    a.block = c // Error! Passing task isolated 'self' to @MainActor closure.
  }

  @MainActor
  func c() {}
}
```

This was unintentional. To work around this, this commit changes the type
checker to explicitly set the double curry thunk isolation to the correct value
when the type checker generates the double curry thunk in the same manner as it
does for single curry thunks and validates that if we do set the value to
something explicitly that it has the same value as the single curry thunk.

rdar://152522631
(cherry picked from commit c28490b527)
2025-06-20 11:05:24 -07:00
Pavel Yaskevich
cb9925d556 [CSSimplify] VariadicGenerics: Don't attempt to wrap optional into one-element tuple
If we have a tuple with unresolved pack expansion on one side
and an optional type on the other, prevent `matchTypes` from
wrapping optional into a one-element tuple because the matching
should be handled as part of the optional injection.

Resolves: rdar://152940244
(cherry picked from commit 6cfbafd968)
2025-06-20 00:15:07 -07:00
Hamish Knight
8f66e610df Merge pull request #82340 from hamishknight/record-opened-6.2 2025-06-19 08:19:06 +01:00
Dario Rexin
dc71b7e4c4 Merge pull request #82241 from drexin/wip-149882902-6.2
[6.2][IRGen] Emit null check before swift_conformsToProtocol for nullable metatypes
2025-06-18 15:56:50 -07:00
Dario Rexin
ae8c455170 [IRGen] Fix placeholder logic for emission of conditionally inverted protocols
rdar://153681688

Instead fo counting the actual conformances, the logic took the size of the bit field, i.e. used the highest set bit, so when a type had a conditional conformance only on ~Escapable, but not on ~Copyable, it would still add 2 placeholders, but only fill one.
2025-06-18 14:47:40 -07:00
Joe Groff
009ce4b82a Add an "addressable for dependencies" flag to value witness flags.
This may be useful for type layout of borrow fields in the future, should we
decide that addressable-for-dependencies borrows should always be represented
by a pointer. rdar://153650278
2025-06-18 12:47:36 -07:00
Hamish Knight
612f7b213c Merge pull request #82319 from hamishknight/keyed-6.2
[6.2] [Sema] Tighten up function call check in `resolveKeyPathExpr`
2025-06-18 18:24:30 +01:00
Pavel Yaskevich
3d1c592456 Merge pull request #82324 from xedin/SE-0449-fixes-6.2
[6.2][Concurrency] SE-0449: Fix a few issues related to `nonisolated` type declarations
2025-06-18 09:14:07 -07:00
Hamish Knight
4d654b271f [CS] Distinguish locators for generic args in addSpecializationConstraint
Make sure we give each argument a different locator to ensure we
can correctly record any opened types.

rdar://153674889
2025-06-18 16:21:45 +01:00
Joe Groff
c224505ac5 Merge pull request #82296 from jckarter/transitive-addressability-fields-and-addressors-6.2
[6.2] SILGen: Handle struct fields and addressors as addressable storage.
2025-06-18 07:20:12 -07:00
Hamish Knight
0409102619 Merge pull request #82304 from hamishknight/wrapping-paper-6.2 2025-06-18 08:51:09 +01:00
Pavel Yaskevich
06d1224aec [Concurrency] SE-0449: nonisolated on a type should prevent isolation inference from protocol requirements
If a `nonisolated` type conforms to a global-isolated protocol
the witnesses to the protocol requirements should infer the
isolation from the protocol but instead be `nonisolated`.

Resolves: rdar://145519840
(cherry picked from commit e50acbf3d5)
2025-06-17 15:29:18 -07:00
Pavel Yaskevich
7d0e2b51fd [Concurrency] SE-0449: Implied conformances to nonisolated protocols make witnesses nonisolated
Even if the requirement is stated on an isolated protocol if the
conformance is implied by a nonisolated one all of the requirements
and witnesses should be nonisolated.

(cherry picked from commit 06be7bda39)
2025-06-17 15:29:18 -07:00