Commit Graph

93 Commits

Author SHA1 Message Date
Anton Korobeynikov
0b345097e0 Deconstruct Escapable check for coroutines with normal results 2025-11-13 12:34:16 -08:00
Henrik G. Olsson
33a059f689 Merge pull request #84136 from hnrklssn/print-lifetime-arg-backticks
[ASTPrinter] Escape @_lifetime arguments when needed

Printing a LifetimeDescriptor would never wrap it in backticks (even if originally wrapped in backticks). This would result in the output not being able to be parsed

rdar://159992995
2025-09-08 15:29:16 -07:00
Henrik G. Olsson
d7d839e9f3 [ASTPrinter] Escape @_lifetime arguments when needed
rdar://159992995
2025-09-05 23:03:46 -07:00
Henrik G. Olsson
fe14d5d636 [ASTPrinter] Move LifetimeDescriptor::getString and add test (NFC)
This refactor and test case are preparations for the implementation in
the next commit.
2025-09-05 23:03:26 -07:00
Andrew Trick
37acd5781e Infer @_lifetime for mutating methods without experimental feature
Infer @_lifetime(self: copy self) for mutating methods without requiring the
experimental Lifetimes feature to be disabled. Treatment of mutating 'self' is
now consistent with other 'inout' parameters.

Example:

    extension MutableSpan {
      mutating func mutatingMethod() {...}
    }
2025-09-03 22:42:13 -07:00
Andrew Trick
ada46a0cf0 Lifetime inference: restructure to support multiple dependencies.
Restructure the inference logic to allow the same function declaration to
independently annotate or infer different lifetime targets.

For example:

   @_lifetime(borrow a)
   /* DEFAULT: @_lifetime(b: copy b)
   func f(a: A, b: inout B) -> NE

The fact that we prevented this was somewhat accidental and surprising.

This restructuring simplifies the code but also gives us much more control over
the inference logic.

Fixes: rdar://159288750 ("A function cannot have a ~Escapable 'inout' parameter
in addition to other ~Escapable parameters" is not actionable)
2025-09-03 22:42:12 -07:00
Andrew Trick
52b990a008 Lifetime inference: fix a crash on implicit _modify for subscripts.
Fixes rdar://155976839 (Compiler crash when _modify is not specified for
~Escapable type)
2025-08-12 22:02:17 -07:00
Andrew Trick
60ce851d21 Lifetime inference: give subscripts an implicit dependency on 'self'
Allow a subscript getter to return a non-Escapable type without an explicit
'@'lifetime annotation.

    subscript(_ index: Int) -> Span {
      get {} // OK
    }
2025-08-12 22:02:17 -07:00
Meghana Gupta
222ee7389a Diagnose @_lifetime on targets that are Escapable
Lifetime dependencies can be attached to ~Escapable types only
2025-07-22 06:00:38 -07:00
Meghana Gupta
8387b7db14 Update callers of getLoweredOwnership() and add a test 2025-07-16 11:34:14 -07:00
Meghana Gupta
c2836af597 Fix getLoweredOwnership() for setters of ~Escapable types 2025-07-16 10:47:02 -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
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
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
Andrew Trick
87f2510a27 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)
2025-06-25 16:34:43 -07: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
Meghana Gupta
0eb32f6943 Fixes after merge conflict 2025-06-23 16:52:18 -07:00
Meghana Gupta
2a2deea77b Avoid circular reference errors by adding an early bailout for imported enums 2025-06-23 13:42:55 -07:00
Meghana Gupta
db24b6f758 Support lifetime dependence inference on enum elements 2025-06-23 13:42:53 -07:00
Meghana Gupta
ccda38b513 [NFC] Prepare LifetimeDependenceInfo to hold EnumElementDecl* 2025-06-23 13:42:48 -07:00
Andrew Trick
855b3e4446 [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')
2025-06-22 23:27:09 -07:00
Andrew Trick
125a0862a9 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)
2025-06-16 10:41:06 -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
Andrew Trick
9dd88e277e [NFC] LifetimeDependence: fix indentation
To avoid diff/merge confusion.
2025-06-11 10:37:40 -07:00
Meghana Gupta
dcf072f9d0 Introduce a new suppressible experimental feature to guard @_lifetime 2025-06-07 12:49:07 -07:00
Meghana Gupta
0dfa1fc312 Update spelling for representing lifetime dependencies to @_lifetime 2025-06-07 12:49:07 -07:00
Meghana Gupta
cbe253326b Fix diagnostic messages for some cases of invalid lifetime dependencies 2025-06-01 07:55:07 -07:00
Andrew Trick
f66c04056f 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)
2025-05-21 00:25:00 -07:00
Andrew Trick
4436e942ae [NFC] fix a comment typo in LifetimeDependence 2025-05-05 21:01:34 -07:00
Andrew Trick
88bbc18730 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
2025-05-03 00:03:34 -07:00
Andrew Trick
36551e370d LifetimeDependence type checking: avoid an assertion on error.
When the type checker encounters incomplete type error, bail out early to avoid
an assertion.
2025-04-29 15:36:48 -07:00
Andrew Trick
e9ac803e94 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.
2025-04-27 22:34:16 -07:00
Andrew Trick
a7c9b6eccc Fix LifetimeDependence attribute checking
Handle parameters with no name: foo(_:T)
2025-04-25 17:58:23 -07:00
Nate Chandler
041851e799 [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:03:54 -07:00
Hamish Knight
6ceb9c1bdd Merge pull request #80634 from hamishknight/lifetime-variance
[Sema] Ignore types with type variables in `filterEscapableLifetimeDependencies`
2025-04-09 01:40:42 +01:00
Andrew Trick
6235b28a95 Merge pull request #80621 from atrick/infer-lazy-inout
Add a "lazy" lifetime inference for mutating interface methods
2025-04-08 11:16:06 -07:00
Hamish Knight
9114aad02c [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 14:14:42 +01:00
Andrew Trick
a86fe4fa30 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:
...)'
2025-04-07 18:06:42 -07:00
Meghana Gupta
eab8afb904 Silence diagnostic when @lifetime(borrow) is used on inout parameters in swiftinterface files only 2025-04-07 16:55:36 -07:00
Meghana Gupta
ef1e94577f Revert "Merge pull request #80540 from swiftlang/revert-80452-lifetimeinout"
This reverts commit 6eaa07a880, reversing
changes made to e75ee3f4cf.
2025-04-04 09:50:13 -07:00
Artem Chikin
39e1791b67 Revert "Add support for inout lifetime dependence" 2025-04-04 09:00:09 -07:00
Meghana Gupta
88ce18eb8b [NFC] Reorganize getDependenceKindFromDescriptor 2025-04-03 17:32:10 -07:00
Meghana Gupta
0d3d89ae22 [NFC] Update diagnostic msg 2025-04-03 17:22:13 -07:00
Meghana Gupta
d87a2b2ada Rename ParsedLifetimeDependenceKind::Scope -> ParsedLifetimeDependenceKind::Borrow 2025-04-03 17:22:13 -07:00
Meghana Gupta
cfacd25df4 Add support for @lifetime(&arg) 2025-04-03 17:22:06 -07:00
Meghana Gupta
245915d795 [NFC] Replace calls of inferLifetimeDependenceKindFromType and isCompatibleOwnership to inferLifetimeDependenceKind 2025-04-02 17:19:32 -07: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