Commit Graph

4105 Commits

Author SHA1 Message Date
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
Andrew Trick
05fa82b7a7 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) {}
2025-06-25 16:34:43 -07:00
Andrew Trick
df0b81c88d 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.
2025-06-25 16:34:42 -07:00
Andrew Trick
465d6a82e7 Fix LifetimeDependence diagnostic formatting
Remove incorrectly nested single quotes from the suggested fix.
2025-06-25 15:24:12 -07:00
Anthony Latsis
797548a085 AST: Fix out of sync diagnostic
`declared_protocol_conformance_here` depends on the raw values of
`ConformanceEntryKind`
2025-06-25 09:46:07 +01:00
Doug Gregor
0e3da0e499 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:06:14 -07:00
Slava Pestov
5987bbf966 Merge pull request #82321 from slavapestov/rqm-fixes
RequirementMachine: Add more limits to catch runaway computation, and fix a bug
2025-06-18 23:00:38 -04:00
Slava Pestov
7f8175b3da RequirementMachine: Add two more completion termination checks for concrete type requirements
The concrete nesting limit, which defaults to 30, catches
things like A == G<A>. However, with something like
A == (A, A), you end up with an exponential problem size
before you hit the limit.

Add two new limits.

The first is the total size of the concrete type, counting
all leaves, which defaults to 4000. It can be set with the
-requirement-machine-max-concrete-size= frontend flag.

The second avoids an assertion in addTypeDifference() which
can be hit if a certain counter overflows before any other
limit is breached. This also defaults to 4000 and can be set
with the -requirement-machine-max-type-differences= frontend flag.
2025-06-17 17:51:25 -04:00
Slava Pestov
b79f2817f9 Sema: Fix MissingCallFailure::diagnoseAsError() crash with special base names 2025-06-17 09:52:04 -04:00
Slava Pestov
908c9368ed Parse: Only accept certain literals as enum case raw values
Just checking for LiteralExpr is too broad, because Sema doesn't
know what to do with RegexLiteralExpr for example.
2025-06-17 09:19:00 -04:00
Cyndy Ishida
64168edbf5 [Modules] Allow implicit conversions when loading interfaces with invalid os versions (#82228)
Initially, the compiler rejected building dependencies that contained OS
versions in an invalid range. However, this happens to be quite
disruptive, so instead allow it and request that these versions be
implicitly bumped based on what `llvm::Triple::getCanonicalVersionForOS`
computes.

resolves: rdar://153205856
2025-06-16 12:46:32 -07:00
Andrew Trick
d90903a823 Merge pull request #82189 from atrick/lifedep-trivial-inout
Disallow @_lifetime(borrow) for trivial 'inout' arguments
2025-06-12 10:30:54 -07:00
Artem Chikin
a35c112a70 Merge pull request #81919 from artemcm/DiagnoseMissingModulesSeenInSerializedSearchPaths
[Dependency Scanning] On failure to locate a module, attempt to diagnose if binary dependencies contain search paths with this module.
2025-06-11 18:00:01 -07:00
Andrew Trick
a38925493b Disallow @_lifetime(borrow) for trivial 'inout' arguments
@_lifetime(borrow holder) // ERROR
    func test(holder: inout Holder) -> NE

Fixes rdar://153040843 ([nonescapable] disallow @_lifetime(borrow)
for trivial 'inout' arguments)
2025-06-11 11:33:35 -07:00
Allan Shortlidge
6e3a2d22c8 ModuleInterface: Canonicalize OS version numbers in loaded swiftinterfaces. 2025-06-10 22:11:05 -07:00
Allan Shortlidge
e16c638fc3 AST: Warn for non-existent platform versions in @available attributes. 2025-06-10 22:11:05 -07:00
Meghana Gupta
7d454533a0 Merge pull request #82067 from meg-gupta/lifetimeunderscored
Update spelling for representing lifetime dependencies to @_lifetime
2025-06-09 19:01:31 -07:00
Allan Shortlidge
26d589e099 Merge pull request #82075 from tshortli/diagnose-unrecognized-availability-domains-as-errors
AST: Diagnose unrecognized platforms as errors with `CustomAvailability` enabled
2025-06-09 18:22:30 -07:00
Allan Shortlidge
c0589753d4 Merge pull request #82079 from tshortli/implementation-only-deprecation-diagnostic-group
AST: Add a warning group for `@_implementationOnly` deprecation diagnostics
2025-06-09 17:43:30 -07:00
Pavel Yaskevich
a5d92bc346 Merge pull request #82053 from xedin/rdar-150689994
[Diagnostics] Correctly diagnose situations when immutable value is passed to `inout` parameter
2025-06-09 00:12:13 -07:00
Meghana Gupta
74e4c2e597 Downgrade use of @lifetime outside stdlib as a warning 2025-06-07 12:49:08 -07:00
Meghana Gupta
0dfa1fc312 Update spelling for representing lifetime dependencies to @_lifetime 2025-06-07 12:49:07 -07:00
Allan Shortlidge
8ceba34f62 AST: Add a warning group for @_implementationOnly deprecation diagnostics.
This allows developers to control the level of these diagnostics.

Resolves rdar://152735425.
2025-06-06 19:12:32 -07:00
Allan Shortlidge
85444fd1e5 AST: Diagnose unrecognized platforms as errors with CustomAvailability enabled.
When the CustomAvailability experimental feature is enabled, make it an error
to specify an unrecognized availability domain name. Also, add these
diagnostics to a diagnostic group so that developers can control their behavior
when they are warnings.

Resolves rdar://152741624.
2025-06-06 17:30:01 -07:00
Pavel Yaskevich
6bbc101a98 [Diagnostics] Correctly diagnose situations when immutable value is passed to inout parameter
Currently the note is going to point to the "callee" but that is
incorrect when the failure is related to an argument of a call.

Detect this situation in `RValueTreatedAsLValueFailure::diagnoseAsNote`
and produce a correct note.

Resolves: rdar://150689994
2025-06-06 14:36:19 -07:00
Pavel Yaskevich
aaf153d7a1 [Concurrency] Fix SendableMetatype conformance failures to behave like Sendable ones
No warnings with minimal checking, warnings with `strict-concurrency=complete` and
if declaration is `@preconcurrency` until next major swift version.

Resolves: rdar://151911135
Resolves: https://github.com/swiftlang/swift/issues/81739
2025-06-06 08:48:29 -07:00
Pavel Yaskevich
3495c61840 [Diagnostics] Use contains instead of has when non-Sendable type appears inside of a member type 2025-06-05 11:14:55 -07:00
Artem Chikin
d4abba10a5 Add a new diagnostic group and documentation for the module-not-found failure which specifies that a missing module dependency can be found on a search path serialized in another Swift binary module dependency. 2025-06-04 16:57:01 -07:00
Artem Chikin
aedc2fadb3 [Dependency Scanning] Serialized Swift binary module serialized search paths 2025-06-04 16:55:45 -07:00
Artem Chikin
90f2fba2ae [Dependency Scanning] On failure to locate a module, attempt to diagnose if binary dependencies contain search paths with this module.
Unlike with implicitly-built modules (prior to Swift 6 mode), explicitly-built modules require that all search paths be specified explicitly and no longer inherit search paths serialized into discovered Swift binary modules. This behavior was never intentional and is considered a bug. This change adds a diagnostic note to a scan failure: for each binary Swift module dependency, the scanner will attempt to execute a dependency scanning query for each serialized search path inside that module. If such diagnostic query returns a result, a diagnostic will be emitted to inform the user that the dependency may be found in the search path configuration of another Swift binary module dependency, specifying which search path contains the "missing" module, and stating that such search paths are not automatically inherited by the current compilation.
2025-06-04 16:55:37 -07:00
Doug Gregor
704cb81d93 Merge pull request #81963 from DougGregor/non-sendable-metatype-diag-group
Add a diagnostic group for the diagnostic about non-Sendable metatypes.
2025-06-04 09:41:51 -07:00
Doug Gregor
9f0dda5417 Add a diagnostic group for the diagnostic about non-Sendable metatypes.
This is a new restriction that folks are sure to run into, so provide
it with some actionable documentation. Fixes rdar://152450956.
2025-06-03 16:45:28 -07:00
Slava Pestov
cd5ecbee16 Sema: Implement missing part of SE-0346
The proposal states that this should work, but this was never
implemented:

    protocol P<A> {
      associatedtype A
    }

    struct S: P<Int> {}

- Fixes https://github.com/swiftlang/swift/issues/62906.
- Fixes rdar://91842338.
2025-06-03 17:28:19 -04:00
Meghana Gupta
ba678b5518 Merge pull request #81893 from meg-gupta/lifetimediagnostics
Fix diagnostic messages for some cases of invalid lifetime dependencies
2025-06-02 16:01:00 -07:00
Pavel Yaskevich
e1e9f04398 Merge pull request #81863 from xedin/using-for-default-isolation-in-file-context
[AST/Sema] SE-0478:  Implement `using` declaration under an experimental flag
2025-06-02 09:56:29 -07:00
Allan Shortlidge
d5ef256b40 Merge pull request #81751 from tshortli/migratable-member-import-visibility 2025-06-01 08:49:01 -07:00
Meghana Gupta
cbe253326b Fix diagnostic messages for some cases of invalid lifetime dependencies 2025-06-01 07:55:07 -07:00
Pavel Yaskevich
36b77116bd [Concurrency] Add a request to retrieve default isolation of a file
The default isolation is computed based on `using` declaration
found in the file, if any.
2025-05-31 10:49:50 -07:00
Slava Pestov
f6f0d75263 Merge pull request #81843 from slavapestov/hamish-fuzzer-fixes
Hamish fuzzer fixes
2025-05-31 12:28:56 -04:00
Allan Shortlidge
aca604660f AST/Sema: Make MemberImportVisibility a migratable feature.
The migration to `MemberImportVisibility` can be performed mechanically by
adding missing import declarations, so offer automatic migration for the
feature.

Resolves rdar://151931597.
2025-05-30 15:34:08 -07:00
Artem Chikin
8ec341eac4 [Dependency Scanning] Bridge diagnostics emitted during bridging header scanning
Instead of simply dumping them to stderr.

Resolves rdar://151993075
2025-05-30 13:50:37 -07:00
Slava Pestov
0e1561f05a Sema: Change a couple of Identifier usages to DeclName instead 2025-05-30 14:43:48 -04:00
Alexis Laferrière
7d6447be20 Merge pull request #81519 from xymus/cdecl-diags
Sema: Update more diagnostics for `@cdecl` and representability in the C language
2025-05-29 13:37:29 -07:00
Arnold Schwaighofer
7ac551636b Merge pull request #81714 from aschwaighofer/se0460
SE-0460: Introduce @specialized attribute
2025-05-28 12:03:48 -07:00
Daniil Kovalev
1e403ecf5c [AutoDiff] Support custom derivatives for @_alwaysEmitIntoClient functions (#78908)
Consider an `@_alwaysEmitIntoClient` function and a custom derivative
defined
for it. Previously, such a combination resulted different errors under
different
circumstances.

Sometimes, there were linker errors due to missing derivative function
symbol -
these occurred when we tried to find the derivative in a module, while
it
should have been emitted into client's code (and it did not happen).

Sometimes, there were SIL verification failures like this:

```
SIL verification failed: internal/private function cannot be serialized or serializable: !F->isAnySerialized() || embedded
```

Linkage and serialization options for the derivative were not handled
properly,
and, instead of PublicNonABI linkage, we had Private one which is
unsupported
for serialization - but we need to serialize `@_alwaysEmitIntoClient`
functions
so the client's code is able to see them.

This patch resolves the issue and adds proper handling of custom
derivatives
of `@_alwaysEmitIntoClient` functions. Note that either both the
function and
its custom derivative or none of them should have
`@_alwaysEmitIntoClient`
attribute, mismatch in this attribute is not supported.

The following cases are handled (assume that in each case client's code
uses
the derivative).

1. Both the function and its derivative are defined in a single file in
   one module.

2. Both the function and its derivative are defined in different files
which
   are compiled to a single module.

3. The function is defined in one module, its derivative is defined in
another
   module.

4. The function and the derivative are defined as members of a protocol
extension in two separate modules - one for the function and one for the
   derivative. A struct conforming the protocol is defined in the third
   module.

5. The function and the derivative are defined as members of a struct
extension in two separate modules - one for the function and one for the
   derivative.

The changes allow to define derivatives for methods of `SIMD`.

Fixes #54445
<!--
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!
-->
2025-05-25 09:47:15 -04:00
Arnold Schwaighofer
13ff5abdb8 Introduce @specialized attribute
Implements SE-0460 -- the non-underscored version of @specialized.

It allows to specify "internal" (not abi affecting) specializations.

rdar://150033316
2025-05-23 13:12:47 -07:00
Pavel Yaskevich
43c3e4a539 Merge pull request #81719 from xedin/rdar-151720646
[TypeChecker] Improve diagnostics for access to actor-isolated values…
2025-05-23 09:58:16 -07:00
Doug Gregor
43f6feaf19 Merge pull request #81738 from DougGregor/no-unchecked
Remove the note that suggests using `@unchecked`
2025-05-23 13:46:22 +01:00
Doug Gregor
a922e8e356 Remove the note that suggests using @unchecked
`@unchecked Sendble` is dangerous, and almost always the wrong thing to
use. Don't have the compiler suggest it.
2025-05-23 09:12:51 +01:00