Commit Graph

92 Commits

Author SHA1 Message Date
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
Meghana Gupta
f591680c2a Update SIL printing of @lifetime
Lifetime dependencies in SIL tests continue to be represented as a type modifier on the target.

As before, they are represented as a LifetimeDependentTypeRepr in the AST.
2024-10-08 15:10:59 -07:00
Meghana Gupta
4ac90a3e11 Diagnose multiple @lifetime attributes with same target 2024-10-08 15:10:59 -07:00
Meghana Gupta
31c1dc7eb5 Add support for specifying target in @lifetime
@lifetime(target: source1, source2...) where target can be any
parameter or 'self'. We cannot have @lifetime attributes with duplicate targets.

Also, update the internal data structures. Previously LifetimeEntry stored
pairwise (target, source) dependencies. Now, LifetimeEntry will store an optional
target descriptor and an array of source descriptors.
2024-10-08 15:10:48 -07:00
Meghana Gupta
011b5bf10c Delete dependsOn support
Lifetime dependencies will now be represented with @lifetime attribute in the language.

dependsOn is a type modifier and was represented as a LifetimeDependentTypeRepr in the AST.

I am deleting dependsOn syntax parsing support and retaining LifetimeDependentTypeRepr support.

We may want to represent lifetime dependencies in a function type with a type attribute in the future.
If we use a decl attribute instead, then support for LifetimeDependentTypeRepr can be deleted.
2024-10-07 00:24:35 -07:00
Meghana Gupta
148155710f Merge pull request #76624 from meg-gupta/lifetimedepscoped
Add support for borrow lifetime dependence
2024-10-02 13:58:14 -07:00
Nate Chandler
ecba5db6e5 [NFC] AST: Added AccessorKind predicates.
And replaced direct comparisons with calls to predicates.  In
preparation for introducing parallel non-underscored accessors.
2024-09-25 21:06:36 -07:00
Meghana Gupta
f8f043f585 Change @lifetime to always mean inherit lifetime dependence 2024-09-20 22:05:23 -07:00
Meghana Gupta
34782b750e Parse scoped lifetime dependence kind within the @lifetime attribute 2024-09-20 21:50:51 -07:00
Meghana Gupta
7af6c11e7e Store parsed @lifetime attribute on the function type 2024-09-09 22:02:44 -07:00
Meghana Gupta
b610036e6a [NFC] Refactor out common utilities to be used by lifetime attribute 2024-09-09 22:02:44 -07:00
Meghana Gupta
2b011b0a3a [NFC] Rename/reorg LifetimeDependence utils 2024-09-09 22:02:44 -07:00
Alejandro Alonso
a434126554 Disallow referencing a value generic outside of certain contexts 2024-09-04 15:13:45 -07:00
Slava Pestov
375363a473 AST: Move global conformance lookup entry points to ConformanceLookup.h 2024-08-08 23:35:58 -04:00
Andrew Trick
50ee84a0b4 Infer LifetimeDependenceInfo on a mutating method
We need this at least until we have 'dependsOn(self)' syntax.

When 'self' is nonescapable and the result is 'void', assume that 'self' depends
on a single nonescapable argument.
2024-07-30 15:57:57 -07:00
Andrew Trick
11640461ef Lifetime dependence: add inferrence for setters.
A nonescapable computed property should always depend on 'newValue'. We simply
infer that now. There's no way to explicitly spell the dependence.
2024-07-30 15:57:57 -07:00
Meghana Gupta
154989463b Add support for lifetime dependence in parameter position 2024-07-10 14:20:03 -07:00
Meghana Gupta
8137aed238 Rename LifetimeDependentReturnTypeRepr -> LifetimeDependentTypeRepr 2024-07-10 14:20:03 -07:00
Slava Pestov
3fcda140bb AST: ModuleDecl::checkConformance() is a static method 2024-07-06 12:05:46 -04:00
Tim Kientzle
1098054291 Merge branch 'main' into tbkka-assertions2 2024-06-18 17:52:00 -07:00
Akira Hatanaka
d92f181ace Create two versions (for caller and callee) of the functions that answer questions about parameter convention (#74124)
Create two versions of the following functions:

isConsumedParameter
isGuaranteedParameter
SILParameterInfo::isConsumed
SILParameterInfo::isGuaranteed
SILArgumentConvention::isOwnedConvention
SILArgumentConvention::isGuaranteedConvention

These changes will be needed when we add a new convention for
non-trivial C++ types as the functions will return different answers
depending on whether they are called for the caller or the callee. This
commit doesn't change any functionality.
2024-06-18 09:06:09 -07:00
Meghana Gupta
c14559173d Add dependsOn(immortal) 2024-06-11 11:18:10 -07:00
Tim Kientzle
1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -07:00
Andrew Trick
b5b0c75ccd Remove diagnostic: lifetime_dependence_on_bitwise_copyable
Allow lifetime depenendence on types that are BitwiseCopyable & Escapable.

This is unsafe in the sense that the compiler will not diagnose any use of the
dependent value outside of the lexcial scope of the source value. But, in
practice, dependence on an UnsafePointer is often needed. In that case, the
programmer should have already taken responsibility for ensuring the lifetime of the
pointer over all dependent uses. Typically, an unsafe pointer is valid for the
duration of a closure. Lifetime dependence prevents the dependent value from
being returned by the closure, so common usage is safe by default.

Typical example:

func decode(_ bufferRef: Span<Int>) { /*...*/ }

extension UnsafeBufferPointer {
  // The client must ensure the lifetime of the buffer across the invocation of `body`.
  // The client must ensure that no code modifies the buffer during the invocation of `body`.
  func withUnsafeSpan<Result>(_ body: (Span<Element>) throws -> Result) rethrows -> Result {
    // Construct Span using its internal, unsafe API.
    try body(Span(unsafePointer: baseAddress!, count: count))
  }
}

func decodeArrayAsUBP(array: [Int]) {
  array.withUnsafeBufferPointer { buffer in
    buffer.withUnsafeSpan {
      decode($0)
    }
  }
}

In the future, we may add SILGen support for tracking the lexical scope of
BitwiseCopyable values. That would allow them to have the same dependence
behavior as other source values.
2024-05-22 17:10:56 -07:00
Nate Chandler
ed5c7ef7ae [BitwiseCopyable] Promote to feature.
SE-0426 was accepted.
2024-04-24 15:52:20 -07:00
Meghana Gupta
b835c14964 LifetimeDependence inference, dont use BitwiseCopyable type as candidate 2024-04-16 13:19:45 -07:00
Meghana Gupta
a05c34c72f Fix typos in diagnostics 2024-04-16 13:19:40 -07:00
Meghana Gupta
eae192f1fb NFC: clang-format LifetimeDependence.cpp 2024-04-16 12:05:27 -07:00
Meghana Gupta
31b7a4e437 Move LifetimeDependence.cpp to AST 2024-04-16 12:05:23 -07:00