Commit Graph

22662 Commits

Author SHA1 Message Date
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
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
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
Artem Chikin
8961d8da9a [Dependency Scanning] Diagnose an error when only finding incompatible Swift binary modules
When querying a Swift module, the scanner now also keeps track of all discovered candidate binary modules which are not compatible with current compilation.

- If a Swift dependency is successfully resolved to a compatible binary module or a textual interface, a warning is emitted for every incompatible binary Swift module discovered along the way.
- If a Swift dependency is not resolved, but incompatible module candidates were found, an error is emitted - while it is likely that the scan would fail downstream, it is also possible that an underlying Clang module dependency (with the same name) is successfuly resolved and the Swift lookup failure is ignored, which is still going to lead to failures most of the time if the client code assumes the presence of the Swift overlay module in this scenario.

This change refactors common error reporting by the scanner into a 'ModuleDependencyIssueReporter' class, which also keeps track of all diagnosed failed lookups to avoid repeating diagnostics.
2025-07-07 11:11:34 -07:00
Artem Chikin
b889976dc9 Merge pull request #82695 from artemcm/DepScanCycleShadow
[Dependency Scanning] Emit a note if a dependency cycle is between the source target and another Swift module with the same name
2025-07-07 10:52:14 -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
Allan Shortlidge
6e08b8980f SILGen: Refactor emission of availability checks.
Generalize SILGen for `if #available` checks by delegating the determination of
the query function and its arguments to `AvailabilityQuery`. Update SILGen for
`@backDeployed` thunks to use the same infrastructure.

NFC.
2025-07-06 19:44:53 -07:00
Allan Shortlidge
a4f59f9481 AST: Return @backDeployed attribute along with version from Decl query.
Also, introduce a convenience on `BackDeployedAttr` for getting its associated
`AvailabilityDomain`.

NFC.
2025-07-06 19:44:53 -07:00
Doug Gregor
2e27ff8734 Merge pull request #82814 from DougGregor/region-inference-dynamic-isolated-conformances
[SE-0470] Extend region analysis to account for isolated conformances
2025-07-06 09:24:17 -07:00
Meghana Gupta
61fc2e052a Merge pull request #82787 from meg-gupta/lifetimediag
Fix a lifetime dependence diagnostic
2025-07-04 21:11:45 -07:00
Pavel Yaskevich
0444d297b6 [AST/Sema] SE-0487: Remove @preEnumExtensibility attribute 2025-07-04 11:06:44 -07:00
Pavel Yaskevich
43eec8fede [AST/Sema] SE-0487: Expand @nonexhaustive attribute to support warn argument
The spelling `@nonexhaustive(warn)` replaces `@preEnumExtensibility`
attriubte.
2025-07-04 10:20:25 -07:00
Doug Gregor
deec3e1cd5 [SE-0470] Warn about isolated conformance to SendableMetatype-inheriting protocol
An isolated conformance to a SendableMetatype-inheriting protocol
cannot actually be used in generic code, because the SendableMetatype
requirement itself prevents it. Warn about this case so folks aren't
surprised at runtime.

This is a part of issue #82550 / rdar://154437489.
2025-07-03 22:27:23 -07:00
Pavel Yaskevich
fe1ae75711 [AST/Sema] SE-0487: Rename @extensible into @nonexhaustive
This is an accepted spelling for the attribute. This commit
also renames the feature flag from `ExtensibleAttribute` to
`NonexhaustiveAttribute` to match the spelling of the attribute.
2025-07-03 17:50:05 -07:00
Meghana Gupta
6d0a6d2760 Fix a lifetime dependence diagnostic
`LifetimeDescriptor::getName()` can crash if the descriptor had a `self`.
Replace with `LifetimeDescriptor::getString()`
2025-07-03 15:20:23 -07:00
Michael Gottesman
677b897ff6 Merge pull request #82555 from gottesmm/pr-b4de2f5d58d852cd3e9e606101e1b0ff42ce6092
[nonisolated-nonsending] Make the AST not consider nonisolated(nonsending) to be an actor isolation crossing point.
2025-07-03 05:22:57 -07:00
Hamish Knight
31408fe5cb Merge pull request #82724 from hamishknight/diag-it 2025-07-03 09:23:44 +01:00
Hamish Knight
e2a8ace0ca Merge pull request #82721 from hamishknight/err-val 2025-07-03 09:23:24 +01:00
Michael Gottesman
010fa39f31 [rbi] Use interned StringRefs for diagnostics instead of SmallString<64>.
This makes the code easier to write and also prevents any lifetime issues from a
diagnostic outliving the SmallString due to diagnostic transactions.
2025-07-02 16:50:24 -07:00
Allan Shortlidge
ede7bdb5d7 Merge pull request #82671 from tshortli/zippered-miscompile-if-unavailable
SILGen: Fix `if #unavailable` mis-compile for zippered libraries
2025-07-02 16:05:51 -07:00
Michael Gottesman
4ce4fc4f95 [rbi] Wrap use ActorIsolation::printForDiagnostics with our own SILIsolationInfo::printActorIsolationForDiagnostics.
I am doing this so that I can change how we emit the diagnostics just for
SendNonSendable depending on if NonisolatedNonsendingByDefault is enabled
without touching the rest of the compiler.

This does not actually change any of the actual output though.
2025-07-02 12:13:50 -07:00
Allan Shortlidge
59d74fa1c7 Sema: Diagnose opaque types that depend on availability of a custom domain.
Serialization and IRGen don't yet support opaque return types that would depend
on querying availability of a custom domain so we need to reject this code to
avoid mis-compiling it.
2025-07-02 11:23:42 -07:00
Allan Shortlidge
4a76c04cf5 SILGen: Fix if #unavailable mis-compile for zippered libraries.
Inverted availability queries were mis-compiled for zippered libraries because
the code that emits calls to `isOSVersionAtLeastOrVariantVersionAtLeast()` was
not updated when the `if #unavailable` syntax was introduced (at that time
support for zippered libraries had not yet been upstreamed). The result of
these calls is now inverted when appropriate.

To make it easier to manage the growing complexity of supporting availability
queries, Sema now models the relevant information about an availability query
with the new `AvailabilityQuery` type. It encapsulates the domain for the
query, the result if it is known at compile time, and the version tuple
arguments to pass to a runtime invocation if applicable.

Resolves rdar://147929876.
2025-07-02 11:23:42 -07:00
Arnold Schwaighofer
0442c7c417 Merge pull request #82646 from aschwaighofer/wip_opt_remarks_wmo
Support LLVM optimization remarks in WMO mode
2025-07-02 10:40:35 -07:00
Hamish Knight
a18f72c29a 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 17:46:10 +01:00
Hamish Knight
1bd32a1a6b NFC: Constify GenericTypeParamDeclGetValueTypeRequest's parameter 2025-07-02 14:46:55 +01:00
Pavel Yaskevich
a522448e90 [Concurency] Allow declarations with @isolated(any) parameters be marked as @concurrent/nonisolated(nonsending)
It's shouldn't be possible to use these attributes directly on
the function type that is `@isolated(any)` as per SE-0461 proposal
but it shouldn't preclude declarations that have parameters with
`@isolated(any)` from using them.

Resolves: rdar://154754939
2025-07-01 15:07:23 -07:00
Artem Chikin
0d3def55df [Dependency Scanning] Emit a note if a dependency cycle is between the source target and another Swift module with the same name
The note will point the user to where the "other" module with the same name is located and mention whether it is an SDK module. This is nice to have in various circumstances where developers attempt to define a module with the same name as a Swift module that already exists on their search paths, for example in the SDK.
2025-07-01 13:38:45 -07:00
Arnold Schwaighofer
b30bd40b83 Add code to create llvm::RemarkStreamer objects for all the LLVMModules in WMO mode
rdar://154403078
2025-07-01 07:19:33 -07:00
Pavel Yaskevich
bec4ebd126 Merge pull request #82574 from xedin/solver-perf-behind-a-flag
[ConstraintSystem] Implement disjunction favoring algorithm behind a flag
2025-06-30 16:27:01 -07:00
Augusto Noronha
0c20222aea Merge pull request #81700 from augusto2112/revert-debug-info-witness-table
Revert "Merge pull request #79171 from augusto2112/debug-info-witness…
2025-06-30 15:28:15 -07:00
Arnold Schwaighofer
7ddf75c791 Merge pull request #82107 from tobias-stadler/remarks-fix-llvm-setup
[IRGen] Setup LLVMRemarkStreamer using existing RemarkStreamer
2025-06-30 13:50:31 -07:00
QuietMisdreavus
1948907eb3 use RespectOriginallyDefinedIn when mangling extension contexts (#82348)
Resolves rdar://152598492

Consider the following Swift, adapted from a real-world framework:

```swift
@available(macOS 10.8, *)
@_originallyDefinedIn(module: "another", macOS 11.0)
public struct SimpleStruct {}

@available(macOS 12.0, iOS 13.0, *)
public extension SimpleStruct {
    struct InnerStruct {}
}
```

In this scenario, `SimpleStruct` was originally in a module called
`another`, but was migrated to this module around the time of macOS
11.0. Since then, the module was ported to iOS and gained a nested type
`SimpleStruct.InnerStruct`. When mangling USRs for this nested type, the
result differs depending on whether we're targeting macOS or iOS.
They're mostly the same, but the macOS build yields a USR with an `AAE`
infix, designating that the `InnerStruct` was defined in an extension
from a module with the name of the base module. On iOS, this infix does
not exist.

The reason this is happening is because of the implementation of
`getAlternateModuleName` checking the availability spec in the
`@_originallyDefinedIn` attribute against the currently active target.
If the target matches the spec, then the alternate module name is
reported, otherwise the real module name is. Since the iOS build reports
the real module name, the mangling code doesn't bother including the
extension-context infix, instead just opting to include the parent
type's name and moving on.

This PR routes around this issue by passing the
`RespectOriginallyDefinedIn` variable to the
`ExtensionDecl::isInSameDefiningModule` method, and using that to skip
the alternate module name entirely. It also sets
`RespectOriginallyDefinedIn` to `false` in more places when mangling
USRs, but i'm not 100% confident that it was all necessary. The goal was
to make USRs more consistent across platforms, regardless of the
surrounding context.
2025-06-30 12:57:12 -06:00
Alexis Laferrière
529cc3ccb6 Merge pull request #82039 from xymus/cdecl-enum
PrintAsClang: Introduce `@cdecl` enums
2025-06-30 09:56:28 -07:00
John McCall
3439e0caab 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-29 01:23:04 -04:00
Pavel Yaskevich
c1e0e6622d [TypeChecker] Implement a per-module block list for disjunction optimizer
Allow enabling performance hacks via a block list action - `ShouldUseTypeCheckerPerfHacks`
2025-06-27 23:43:12 -07:00
Pavel Yaskevich
ac24a8e251 [CSOptimizer] Introduce _OptionalNilComparisonType as candidate for nil while ranking == and != operators
`==` and `!=` operators have special overloads that allow matching
`nil` literal on either side even if wrapped type on the other side
doesn't conform to `Equatable`.
2025-06-27 23:43:10 -07:00
John McCall
2eee30dfbe [NFC] Encapsulate the parameter index of an ActorIsolation 2025-06-27 19:48:12 -04:00
Steven Wu
a298c7532e [Caching] Put caching diagnostics into its own DiagnosticGroups (#82507)
Add explanation and documentation for swift caching diagnostics.

rdar://154335823

<!--
If this pull request is targeting a release branch, please fill out the
following form:

https://github.com/swiftlang/.github/blob/main/PULL_REQUEST_TEMPLATE/release.md?plain=1

Otherwise, replace this comment with a description of your changes and
rationale. Provide links to external references/discussions if
appropriate.
If this pull request resolves any GitHub issues, link them like so:

  Resolves <link to issue>, resolves <link to another issue>.

For more information about linking a pull request to an issue, see:

https://docs.github.com/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue
-->

<!--
Before merging this pull request, you must run the Swift continuous
integration tests.
For information about triggering CI builds via @swift-ci, see:

https://github.com/apple/swift/blob/main/docs/ContinuousIntegration.md#swift-ci

Thank you for your contribution to Swift!
-->

---------

Co-authored-by: Allan Shortlidge <tshortli@gmail.com>
2025-06-27 15:15:12 -07:00
Alexis Laferrière
81a0f98783 Merge pull request #82194 from xymus/cdecl-parser
Parser: Accept `@cdecl` with an optional identifier for a custom C name
2025-06-27 15:06:11 -07:00
Steven Wu
439bd972e8 Merge pull request #82556 from cachemeifyoucan/eng/PR-issue-82490
[Caching] Move per-query CAS state out of the SwiftDependencyScanningService
2025-06-27 09:49:32 -07:00
Andrew Trick
5a866009f9 Merge pull request #82472 from atrick/explicit-init
Disable surprising lifetime inference of implicit initializers
2025-06-27 04:19:05 -07:00
Steven Wu
2a18922c4b [Caching] Move per-query CAS state out of the SwiftDependencyScanningService
Move per-query state out of ScanningService. There is still a check to
make sure the CASOptions are matching between queries because of the
requirement on clang scanner. Otherwise, the scanning service should
contain no per-query information anymore.

Resolves: https://github.com/swiftlang/swift/issues/82490
2025-06-26 17:03:27 -07:00
Anthony Latsis
1f89bb6a94 Merge pull request #82452 from swiftlang/jepa2
Sema: Fix the insertion location for conformances attributes
2025-06-26 21:38:41 +01:00
Andrew Trick
7abe2222f9 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)
2025-06-26 12:47:01 -07:00
Anthony Latsis
de6870ed35 Merge pull request #82478 from swiftlang/jepa3
AST: Fix out of sync diagnostic
2025-06-26 18:33:22 +01:00
Pavel Yaskevich
32a0d80b60 Merge pull request #82085 from xedin/rdar-149811049
[ClangImporter] SE-0463: Implement `@Sendable` inference exception for global actor isolated functions
2025-06-26 09:26:28 -07:00