Commit Graph

21504 Commits

Author SHA1 Message Date
Gábor Horváth
ef63ec99e9 [6.2][cxx-interop] Fix crash when using bridging headers in reverse interop
Explanation: The original code had the assumption we only import
modules. However, there is a flag to import an umbrella header in which
case the clang nodes have no owning module. This PR prevents a null
dereference in that case.
Issues: rdar://157489426
Original PRs: #83540
Risk: Low, added a check to avoid null dereference.
Testing: Added a compiler test.
Reviewers: @egorzhdan
2025-08-07 15:58:31 +01:00
Doug Gregor
5abbf2e4c9 [SE-0466] Under main actor default isolation, explicit nonisolated is not special
Given an explicitly-nonisolated type such as

    nonisolated struct S { }

all extensions of S were also being treated as nonisolated. This meant
that being implicitly nonisolated (i.e., when you're using nonisolated
default isolation) was different from explicitly-writing nonisolated,
which is unfortunate and confusing. Align the rules, such that an
extension of S will get default isolation:

    extension S {
      func f() { } // @MainActor if we're in main actor default isolation
    }
2025-07-25 12:21:35 -07:00
Doug Gregor
2db51c9159 Merge pull request #82923 from DougGregor/sendable-prohibits-mainactor-by-default-6.2
[6.2] [SE-0466] Enable SendableProhibitsMainActorInference by default
2025-07-23 20:04:56 -07:00
Gabor Horvath
f48da45438 [cxx-interop] Configure requires ObjC from frontend option
We sometimes don't have the information in the modulemaps whether a
module requires ObjC or not. This info is useful for reverse interop.
This PR introduces a frontend flag to have a comma separated list of
modules that we should import as if they had "requires ObjC" in their
modulemaps.
2025-07-18 10:35:51 +01:00
Gábor Horváth
ab123929f6 [6.2][cxx-interop] Types exposed from ObjC modules should be behind a macro
Explanation: We the generated reverse interop headers to be valid C++,
so every declaration coming from an Obj-C module should be behind an
ifdef. Unfortunately, we do not always have this information but we do
know that our frameworks contain Obj-C code. So this PR makes sure every
entity coming from our frameworks are behind ifdef.
Issues: rdar://152836730
Original PRs: #83002
Risk: Low, the change is narrow and straightforward.
Testing: Added a compiler test.
Reviewers: @egorzhdan
2025-07-16 11:19:55 +01:00
Doug Gregor
457d44c624 [SE-0466] Enable SendableProhibitsMainActorInference by default
This implements the SE-0466 amendment under discussion for all code
with default main-actor isolation.

Finalizes rdar://151029300.
2025-07-09 10:49:03 -07:00
Gábor Horváth
b51b58db30 [6.2][cxx-interop] Fix unqualified name lookup failure
Explanation: C++ interop synthesizes certain forwarding functions in an
_ObjC module. This confuses MemberImportVisibility. This patch adds
logic to work this around by keeping a mapping between the synthesized
and the original function and looks up where the synthesized functions
belong to based on the original functions' parent module.
Scope: C++ forward interop when MemberImportVisibility is enabled.
Issues: rdar://154887575
Original PRs: #82840
Risk: Low, a narrow change makes getModuleContextForNameLookupForCxxDecl more
precise, and it is only used with MemberImportVisibility.
Testing: Added a compiler test.
Reviewers: @egorzhdan, @tshortli, @hnrklssn
2025-07-09 17:59:11 +01:00
Doug Gregor
3755a6b7e1 [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 13:17:12 -07:00
Artem Chikin
8cec61e50d Merge pull request #82779 from artemcm/62_DepScanImportAccessControl
[6.2 🍒][Dependency Scanning] Keep track of each imported module's access control
2025-07-07 10:53:00 -07:00
Meghana Gupta
2e7556c144 Fix a lifetime dependence diagnostic
`LifetimeDescriptor::getName()` can crash if the descriptor had a `self`.
Replace with `LifetimeDescriptor::getString()`

(cherry picked from commit 6d0a6d2760)
2025-07-04 11:44:35 -07:00
Artem Chikin
1c9b864738 [Dependency Scanning] Keep track of each imported module's access control
Adds an access control field for each imported module identified. When multiple imports of the same module are found, this keeps track of the most "open" access specifier.
2025-07-03 11:12:46 -07:00
Hamish Knight
ef8e576d23 Merge pull request #82741 from hamishknight/err-val-6.2
[6.2] [AST] Use ErrorType for invalid value generic parameter
2025-07-03 14:32:44 +01:00
Hamish Knight
2e0b9d7f8a Merge pull request #82743 from hamishknight/diag-it-6.2 2025-07-03 09:24:25 +01:00
Egor Zhdan
40c4f3cd9d Merge pull request #82723 from swiftlang/egorzhdan/6.2-cfoptions-block-param
🍒[cxx-interop] Fix generated header for Swift closures using `CF_OPTIONS` types
2025-07-02 23:54:40 +01:00
Hamish Knight
e0ed130019 Constify DiagnosticState::determineBehavior
Split out the state mutation into a new `updateFor`
function that we call for diagnostic emission, allowing
`DiagnosticTransaction::hasErrors` to query the behavior without
mutating any state.
2025-07-02 21:29:32 +01:00
Hamish Knight
4c6e06fed3 [AST] Use ErrorType for invalid value generic parameter
If we fail to resolve the value type for a value generic parameter,
previously we would have returned a null Type, causing crashes
downstream. Instead, return an ErrorType, leaving a null Type for
cases where the generic parameter isn't a value generic at all.

rdar://154856417
2025-07-02 21:19:45 +01:00
Hamish Knight
13118899cd NFC: Constify GenericTypeParamDeclGetValueTypeRequest's parameter 2025-07-02 21:19:45 +01:00
Anthony Latsis
6ddd1c95f3 Merge pull request #82664 from swiftlang/jepa2-6.2
[6.2] Sema: Fix the insertion location for conformances attributes
2025-07-02 16:51:55 +01:00
Egor Zhdan
0be39efad4 [cxx-interop] Fix generated header for Swift closures using CF_OPTIONS types
If a Swift class has a field, which has a closure type, which takes an instance of a `CF_OPTIONS`/`NS_OPTIONS` type as a parameter, the reverse interop logic would generate an invalid Objective-C++ header for such type.

This was discovered with UIKit's `UIControlState` type, which is declared with `NS_OPTIONS` in Objective-C, then renamed to `UIControl.State` with API Notes, and then re-exported to Objective-C++ via the generated header.

rdar://129622886

(cherry picked from commit e95c9ecffc)
2025-07-02 14:52:15 +01:00
John McCall
eff0845360 Merge pull request #82613 from rjmccall/local-function-isolation-fixes-6.2
[6.2] Local function isolation fixes
2025-07-01 13:34:16 -04:00
QuietMisdreavus
a75f94eef4 [6.2] use RespectOriginallyDefinedIn when mangling extension contexts (#82657)
- **Explanation**: USR mangling can include an extension context infix
(`AAE`) when an extended type uses `@_originallyDefinedIn` on platforms
other than the active one. This adds a check for the
`RespectOriginallyDefinedIn` flag when checking extension decls against
their extended type.
- **Scope**: Changes USR mangling in these situations so that USRs are
the same for the same code regardless of platform.
- **Issues**: rdar://152598492
- **Original PRs**: https://github.com/swiftlang/swift/pull/82348
- **Risk**: Low. The change is limited to situations where the name
mangler is already disrespecting the alternate module name, and only
additionally turns on that flag for any USR mangling.
- **Testing**: Automated tests
- **Reviewers**: @edymtt @augusto2112
2025-07-01 00:30:15 -07:00
Anthony Latsis
1f28a04d29 Sema: Fix the insertion location for conformances attributes 2025-07-01 02:58:48 +01:00
John McCall
06dec05473 Fix a bunch of bugs with the isolation of local funcs. Since we
use local funcs to implement `defer`, this also fixes several
bugs with that feature, such as it breaking in nonisolated
functions when a default isolation is in effect in the source file.

Change how we compute isolation of local funcs. The rule here is
supposed to be that non-`@Sendable` local funcs are isolated the
same as their enclosing context. Unlike closure expressions, this
is unconditional: in instance-isolated functions, the isolation
does not depend on whether `self` is captured. But the computation
was wrong: it didn't translate global actor isolation between
contexts, it didn't turn parameter isolation into capture isolation,
and it fell through for several other kinds of parent isolation,
causing the compiler to try to apply default isolation instead.
I've extracted the logic from the closure expression path into a
common function and used it for both paths.

The capture computation logic was forcing a capture of the
enclosing isolation in local funcs, but only for async functions.
Presumably this was conditional because async functions need the
isolation for actor hops, but sync functions don't really need it.
However, this was causing crashes with `-enable-actor-data-race-checks`.
(I didn't investigate whether it also failed with the similar
assertion we do with preconcurrency.) For now, I've switched this
to capture the isolated instance unconditionally. If we need to
be more conservative by either only capturing when data-race checks
are enabled or disabling the checks when the isolation isn't captured,
we can look into that.

Fix a bug in capture isolation checking. We were ignoring captures
of nonisolated declarations in order to implement the rule that
permits `nonisolated(unsafe)` variables to be captured in
non-sendable closures. This check needs to only apply to variables!
The isolation of a local func has nothing to do with its sendability
as a capture.

That fix exposed a problem where we were being unnecessarily
restrictive with generic local func declarations because we didn't
consider them to have sendable type. This was true even if the
genericity was purely from being declared in a generic context,
but it doesn't matter, they ought to be sendable regardless.

Finally, fix a handful of bugs where global actor types were not
remapped properly in SILGen.
2025-06-30 01:48:48 -04:00
John McCall
00463df058 [NFC] Encapsulate the parameter index of an ActorIsolation 2025-06-30 01:48:37 -04:00
Doug Gregor
2f838823ee Merge pull request #82590 from DougGregor/no-main-actor-when-conforming-to-sendable-6.2
[6.2] [SE-0466 experimental amendment] Don't infer @mainactor on types conforming to Sendable
2025-06-28 05:57:50 -07:00
Doug Gregor
b5cce1f694 [SE-0466] Don't infer @MainActor on types conforming to Sendable
When the default isolation is main-actor, don't infer @MainActor
for a type that conforms to a protocol P in its primary definition when
P inherits from Sendable. Such types should remain non-isolated
because they're highly unlikely to be able to implement the P
conformance (which cannot be isolated).

Put this feature behind a new experimental flag,
SendableProhibitsMainActorInference.

Implements rdar://151029300
2025-06-27 15:14:26 -07:00
Slava Pestov
eabc8efb41 AST: Better cope with UnboundGenericType in TypeBase::getSuperclass()
Returning the unsubstituted superclass type is not correct,
because it may contain type parameters. Let's form a new
UnboundGenericType instead.

- Fixes https://github.com/swiftlang/swift/issues/82160.
- Fixes rdar://152989888.
2025-06-27 18:11:40 -04:00
Argyrios Kyrtzidis
0127926255 Merge pull request #82513 from sina-mahdavi/sina-mahdavi/cherry-pick-fix-llvm-prefix-mapping-calls
[6.2🍒] fix calls to llvm prefix mapping functions to use space-separated option format
2025-06-27 15:08:32 -07:00
Andrew Trick
92fd571d07 Merge pull request #82511 from atrick/62-inout-syntax
[6.2] Fix diagnostics for missing or invalid @_lifetime annotations on inout params
2025-06-26 23:33:16 -07:00
Andrew Trick
0b0dbd9ee0 Diagnostic note for invalid @_lifetime annotations on inout params
Users commonly try to write a lifetime dependency on an 'inout' parameters as:

    @_lifetime(a: &a)
    func f_inout_useless(a: inout MutableRawSpan) {}

This is useless. Guide them toward what they really wanted:

    @_lifetime(a: copy a)

Fixes rdar://151618856 (@lifetime(..) gives inconsistent error messages)

(cherry picked from commit 87f2510a27)
2025-06-26 12:58:40 -07:00
Andrew Trick
fcbcc8c1e4 Fix misleading Lifetime diagnostics for inout parameters
Correctly diagnose this as:
"invalid use of inout dependence on the same inout parameter

    @_lifetime(a: &a)
    func f_inout_useless(a: inout MutableRawSpan) {}

Correctly diagnose this as:
"lifetime-dependent parameter must be 'inout'":

    @_lifetime(a: borrow a)
    func f_inout_useless(a: borrowing MutableRawSpan) {}

(cherry picked from commit 05fa82b7a7)
2025-06-26 12:58:40 -07:00
Andrew Trick
e409752e00 Lifetime diagnostics: clarify @_lifetime usage for inout parameters
This comes up often when passing a MutableSpan as an 'inout' argument.  The
vague diagnostic was causing developers to attempt incorrect @_lifetime
annotations. Be clear about why the annotation is needed and which annotation
should be used.

(cherry picked from commit df0b81c88d)
2025-06-26 12:58:39 -07:00
Andrew Trick
52654095c1 Fix LifetimeDependence diagnostic formatting
Remove incorrectly nested single quotes from the suggested fix.

(cherry picked from commit 465d6a82e7)
2025-06-26 12:58:39 -07:00
Slava Pestov
7ed5d2bfc2 ASTDemangler: Round-trip @isolated @sil_implicit_leading_param parameter attributes
We sometimes mangle SILFunctionTypes when generating debug info
for reabstraction thunks, and these can have various exotic
parameter and result attributes. Two recent additions were
never plumbed through the mangler, causing assertion failures
when emitting debug info.

Fixes rdar://153730847.
2025-06-26 15:00:44 -04:00
Hamish Knight
8e0749eea2 Merge pull request #82498 from hamishknight/tic-tac-toe-6.2 2025-06-26 09:07:26 +01:00
Sina Mahdavi
fabbbc87c8 [6.2🍒] fix calls to llvm prefix mapping functions to use space-separated option format 2025-06-25 16:48:50 -07:00
Hamish Knight
0c8468e0ee [SourceKit] Print backticks if needed in printDisplayName
Ensure we print raw identifier names with backticks for e.g the
document structure request.

rdar://152524780
2025-06-25 19:52:09 +01:00
Andrew Trick
54651d1b16 Merge pull request #82473 from atrick/62-explicit-init
[6.2] Disable surprising lifetime inference of implicit initializers
2025-06-25 11:32:40 -07:00
Andrew Trick
0839675e6d Merge pull request #82407 from atrick/62-fix-accessor-infer
[6.2] [nonescapable] remove '@_lifetime' requirement on implicit accessors
2025-06-25 00:57:45 -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
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
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
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
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
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
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