Commit Graph

78 Commits

Author SHA1 Message Date
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
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
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
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
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
Andrew Trick
b15d308053 LifetimeDependence type check: infer trivial _read accessor
This fixes a small oversight in the type checker's LifetimeDependence
inference. Allow inference on _read accessors even when 'self' is a trivial
type. This is needed because the compiler synthesizes a _read accessor even when
the user defines a getter (this is probably a mistake, but it's easire to just
fix inference at this point). There is no workaround because it defining both a
getter and '_read' is illegal!

    extension UnsafeMutableRawBufferPointer {
      var mutableBytes: MutableRawSpan {
        @_lifetime(borrow self)
        get {
          unsafe MutableRawSpan(_unsafeBytes: self)
        }
      }
    }

Fixes rdar://153346478 (Can't compile the
UnsafeMutableRawBufferPointer.mutableBytes property)

(cherry picked from commit 125a0862a9)
2025-06-16 10:50:57 -07:00
Andrew Trick
efa276a62c 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)

(cherry picked from commit a38925493b)
2025-06-12 16:03:55 -07:00
Andrew Trick
f0e9d98055 [NFC] LifetimeDependence: fix indentation
To avoid diff/merge confusion.

(cherry picked from commit 9dd88e277e)
2025-06-12 16:03:55 -07:00
Meghana Gupta
58b714b968 Introduce a new suppressible experimental feature to guard @_lifetime 2025-06-11 08:03:40 -07:00
Meghana Gupta
a9b831788d Update spelling for representing lifetime dependencies to @_lifetime 2025-06-11 08:03:39 -07:00
Meghana Gupta
ea35d41f2a [6.2] Fix diagnostic messages for some cases of invalid lifetime dependencies
The diagnostic messages were misleading and suggested an invalid alternative.

rdar://151810577
2025-06-02 16:04:13 -07:00
Andrew Trick
1bd73dafb6 Allow passing MutableSpan 'inout' without an experimental feature.
This adds a new lifetime inference rule, loosening the requirement for @lifetime
annotations even when the experimental LifetimeDependence mode is
enabled. Additionally, it enables this new inference rule even when the
experimental mode is disabled. All other inference rules continue to require the
experimental feature. The rule is:

If a function or method has a single inout non-Escapable parameter other than
'self' and has no other non-Escapable parameters including 'self', then infer a
single @lifetime(copy) dependency on the inout parameter from its own incoming
value.

This supports the common case in which the user of a non-Escapable type,
such as MutableSpan, wants to modify the span's contents without modifying
the span value itself. It should be possible to use MutableSpan this way
without requiring any knowledge of lifetime annotations. The tradeoff is
that it makes authoring non-Escapable types less safe. For example, a
MutableSpan method could update the underlying unsafe pointer and forget to
declare a dependence on the incoming pointer.

Disallowing other non-Escapable parameters rules out the easy mistake of
programmers attempting to trivially reassign the inout parameter. There's
is no way to rule out the possibility that they derive another
non-Escapable value from an Escapable parameteter. So users can still write
the following:

    func reassign(s: inout MutableSpan<Int>, a: [Int]) {
      s = a.mutableSpan
    }

The 'reassign' declaration will type check, but it's implementation will
diagnose a lifetime error on 's'.

Fixes rdar://150557314 ([nonescapable] Declaration of inout MutableSpan
parameter requires LifetimeDependence experimental feature)

(cherry picked from commit dbcba013434aeaa042e9f5cc9ec0829d762b74e0)
2025-05-21 00:14:31 -07:00
Andrew Trick
cd343dfd26 [NFC] fix a comment typo in LifetimeDependence
(cherry picked from commit ce74ca3b5332d6bf839fe2412385bf947be0d106)
2025-05-05 10:22:20 -07:00
Andrew Trick
57e9585afa Fix LifetimeDependence type inference for setters.
A setter on a non-Escapable type may have a dependency on both it's incoming
'self' and 'newValue'. If the 'newValue' dependency does not match the getter's
dependency, then lifetime diagnostics will not accept the generated '_modify'
accessor:

error: lifetime-dependent value returned by generated accessor '_modify'

To fix this, make sure that we don't (conservatively) infer a borrow
dependency on 'newValue'.

Fixes rdar://150444400

(cherry picked from commit 88bbc18730)
2025-05-05 10:22:20 -07:00
Andrew Trick
5719d4c726 LifetimeDependence type checking: avoid an assertion on error.
When the type checker encounters incomplete type error, bail out early to avoid
an assertion.

(cherry picked from commit 36551e370d)
2025-04-30 00:09:39 -07:00
Andrew Trick
baa02e0529 LifetimeDependence: fix a type checker crash on implicit init
When the type checker diagnoses an error on an implicit initializer,
return immediately before handling its parameter to avoid an assert.

(cherry picked from commit e9ac803e94)
2025-04-27 22:38:34 -07:00
Andrew Trick
37a25e848a Fix LifetimeDependence attribute checking
Handle parameters with no name: foo(_:T)

(cherry picked from commit a7c9b6eccc)
2025-04-25 19:21:15 -07:00
Nate Chandler
b4b1dfe03b [CoroutineAccessors] Infer same lifetime deps.
When the feature is enabled, a read2 accessor is generated when a
mutating getter is declared.

rdar://149385088
2025-04-16 16:04:59 -07:00
Meghana Gupta
7a28b5f136 Silence diagnostic when @lifetime(borrow) is used on inout parameters in swiftinterface files only 2025-04-09 10:19:12 -07:00
Meghana Gupta
8e21190ae3 [NFC] Reorganize getDependenceKindFromDescriptor 2025-04-09 10:19:11 -07:00
Meghana Gupta
374ceb6a80 [NFC] Update diagnostic msg 2025-04-09 10:18:11 -07:00
Meghana Gupta
96aeea9f2b Rename ParsedLifetimeDependenceKind::Scope -> ParsedLifetimeDependenceKind::Borrow 2025-04-09 10:18:11 -07:00
Meghana Gupta
91ad1451ce Add support for @lifetime(&arg) 2025-04-09 10:18:11 -07:00
Meghana Gupta
aa3edb4323 [NFC] Replace calls of inferLifetimeDependenceKindFromType and isCompatibleOwnership to inferLifetimeDependenceKind 2025-04-09 10:18:08 -07:00
Andrew Trick
e88265de93 Merge pull request #80646 from atrick/62-infer-mutating
[6.2] Add a "lazy" lifetime inference for mutating interface methods
2025-04-09 03:46:24 -07:00
Andrew Trick
d86553a675 Add a "lazy" lifetime inference for mutating interface methods
When type checking a .swiftinterface file, Assume that a mutating methods does
not depend on its parameters.  This is unsafe but needed because some
MutableSpan APIs snuck into the standard library interface without specifying
dependencies.

Fixes rdar://148697444 error: a mutating method with a ~Escapable 'self' requires '@lifetime(self:
...)'

(cherry picked from commit a86fe4fa30)
2025-04-08 11:40:27 -07:00
Hamish Knight
87f9dcb6f8 [Sema] Ignore types with type variables in filterEscapableLifetimeDependencies
If the type still has type variables, avoid trying to check if it's
escapable.

rdar://148749815
2025-04-08 17:25:11 +01:00
Joe Groff
6b605f41cb Type substitution eliminates dependencies with Escapable targets.
When a generic function has potentially Escapable outputs, those outputs
declare lifetime dependencies, which have no effect when substitution
leads to those types becoming `Escapable` in a concrete context.
This means that type substitution should canonically eliminate lifetime
dependencies targeting Escapable parameters or returns, and that
type checking should allow a function value with potentially-Escapable
lifetime dependencies to bind to a function type without those dependencies
when the target of the dependencies is Escapable.

Fixes rdar://147533059.
2025-04-02 08:54:45 -07:00
Andrew Trick
a431a7bc5f LifetimeDependence: conditionallyAddressableParamIndices
Preserve conditionallyAddressableParamIndices independent of any
addressableParamIndices. The conditional dependencies are subject to change
based on type substitution.
2025-03-23 16:59:11 -07:00
Andrew Trick
4513e6b303 Restore lazy inference for mutating accessors
Inference used to accept explicit setters on non-Escapable 'self'
Restore this behavior under:
-enable-experimental-lifetime-dependence-inference
2025-03-19 22:22:21 -07:00
Andrew Trick
6b7d3006cb Fix lifetime inference with older .swiftinterface files.
This was fix was accidentally not include in the previous commit,
which breaks older .swiftinterface files without it:

commit 75ba7a845c
Merge: befc15e6df d41c4d4cc9
Author: Andrew Trick <atrick@apple.com>
Date:   Wed Mar 19 18:22:35 2025

    Merge pull request #80064 from atrick/lifetime-inference

    LifetimeDependence: implement strict type checking
2025-03-19 22:22:21 -07:00
Andrew Trick
5d2c829b6a LifetimeDependence: implement strict type checking
Rework the type checker to support completely checking lifetime dependence
requirements. Don't let anything through without the feature being enabled and
complete annotation or inference.

First, prevent lifetime dependencies from sneaking into source that does not
enable LifetimeDependence. This is essential for controlling the scope of the
feature.

Fixing this is disruptive because, ever since `~Escapable` was introduced we
have been declaring empty non-Escapable types without enabling
LifetimeDependence. Such as:

      struct Implicit_Init_Nonescapable : ~Escapable {}

Fixes: rdar://145979187 ([nonescapable] diagnose implicit non-Escapable
initializers as an error unless LifetimeDependence is enabled)

Various forms of unsupported 'inout' dependencies are now also caught by the
type checker.

Second, disable lifetime dependency inferrence except in unambiguous cases and
some implicitly generated cases.

Fixes: rdar://131176898 ([nonescapable] missing diagnostic for incorrectly inferred inherited dependence)

This is important to avoid source compatibility problems as inference rules
change. They will change as the proposal goes through review.

This fixes various latent missing dependency bugs.

Disable experimental lifetime dependence inference. Unambiguous lifetime
dependency candidates will still be inferred by default, without any frontend
options. Ambiguous candidates will, however, no longer be inferred unless
-Xfrontend -enable_experimental_lifetime_dependence_inference is enabled.

This all has to be done without breaking existing .swiftinterface files. So
backward compatibility logic is maintained.

Examples of inference rules that are no longer enabled by default:

1. do not infer a dependency on non-Escapable 'self' for methods with more than
zero parameters:

    extension NE: ~Escapable {
      /*@lifetime(self)*/ // ERROR: 'self' not inferred
      func method<Arg>(arg: Arg) -> NE { ... }
    }

2. Never infer a 'copy' dependency kind for explicit functions

    extension NE: ~Escapable {
      @lifetime(self) // ERROR: 'copy' not inferred
      func method() -> NE { ... }

      @lifetime(self) // ERROR: 'copy' not inferred
      var property : NE { /*@lifetime(self: newValue)*/ set { ... } }
    }
2025-03-19 11:59:04 -07:00
Joe Groff
c949e6295f SIL: Lower fields that are conditionally addressable because of a dependency.
Parameters of generic type need to be treated as potentially
addressable-for-dependencies, but we don't want callers using the generic
function with concrete types that are known not to be addressable-for-
dependencies to be overconstrained. In SILFunctionType lowering, lower
these dependencies distinctly as conditionally addressable, meaning that
the dependency on an argument depends on whether the concrete type of
that argument is (potentially) addressable-for-dependencies or not.
2025-03-15 16:07:03 -07:00
Gabor Horvath
22e2276c4f [cxx-interop] Do not require the LifetimeDependence feature in _SwiftifyImport
We use experimental features to let people know that the construct is
subject to change and users should not rely on this unless they are
willing to rewrite the uses of this feature later. However, in compiler
generated code everything should be fair game, we will update the
compiler when these features change. This is a requirement to be able to
turn safe wrapper generation on by default.
2025-03-11 17:38:53 +00:00
Rintaro Ishizaki
b729746629 [AST] Make LifetimeDescriptor hold Identifier instead of StringRef
No reason to keep the name as a StringRef
2025-02-17 08:59:18 -08:00
Joe Groff
c65475628f SIL: Lower lifetime dependencies when lowering function types.
Map the lifetime dependencies described in terms of the formal AST-level parameters
to the correct parameter(s) in the lowered SIL function type. There can be 0, 1,
or many SIL parameters per formal parameter because of tuple exploding. Also,
record which dependencies are on addressable parameters (meaning that the dependency
includes not only the value of the parameter, but its specific memory location).
2025-02-14 09:47:53 -08:00
Andrew Trick
89ea8f6efa [NFC] Fix @lifetime SIL parsing: handle AST output
Disable inference diagnostics because the AST output makes implicit initializers
explicit.

Enable parsing the @lifetime declaration syntax to handle explicit annotations
on declarations.
2024-12-14 22:46:54 -08:00
Andrew Trick
e4e07603b1 Merge pull request #77695 from atrick/handle_markdep
[NFC] mark_dependence handling for coroutines and accessors.
2024-11-19 10:22:25 -08:00
Andrew Trick
a0b21d5e5b LifetimeDependence: broaden assert: allow trivial borrows.
During SIL parsing, allow `@``lifetime(borrow <argNum>)` on trivial arguments.
2024-11-18 22:02:44 -08:00
Meghana Gupta
ae28d667ff Introduce LifetimeDependence experimental feature 2024-11-18 18:08:32 -08:00
Meghana Gupta
daba1cb7bb Delete lifetime dependence inference on mutating self
We don't need this anymore since we have @lifetime(target: sources) syntax
2024-11-15 06:45:15 -08:00
Meghana Gupta
a7b55b2c0c Remove NonescapableTypes feature guard while querying lifetime dependence 2024-11-11 14:34:17 -08:00
Nate Chandler
f1f0ccdeff [NFC] Improved predicate names. 2024-11-06 20:52:21 -08:00
Meghana Gupta
b443ff76e4 [NFC] Use llvm::TrailingObjects to represent variable list of lifetime sources 2024-10-08 15:11:26 -07:00