Commit Graph

60 Commits

Author SHA1 Message Date
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
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
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
Meghana Gupta
db24b6f758 Support lifetime dependence inference on enum elements 2025-06-23 13:42:53 -07:00
John Hui
f9e71d6770 [NFC] Silence unused variable warning in assert()
Clang complains that paramIndicesLength is unused. This block is already
guarded by CONDITIONAL_ASSERT_enabled(), so promoting assert() to
ASSERT() should not affect the behavior of assertions-disabled builds.
2025-06-05 17:01:05 -07:00
Meghana Gupta
36f3574f9c Fix deserialization of lifetime dependencies on ast function types
While deserializing AST function types FunctionType and GenericFunctionType which include
lifetime dependencies and an implicit self parameter, we don't correctly populate
ASTExtInfoBuilder.lifetimeDependencies.  We end up reading one dependency less due to
incorrect index calculation.

Unlike SILFunctionType, AST function types FunctionType and GenericFunctionType
do not include implicit self in their param list. They represent methods with
implicit self as like: `(Self) -> (Args...) -> Result` and don't have any information
to indicate they may have implicit self. Since we use number of parameters while
deserializing lifetime dependencies, we go wrong for such function types.

Serialize the length of parameter indices, so that lifetime dependencies can be
deserialized to that length.

rdar://151768216
2025-06-02 17:29:05 -07:00
Meghana Gupta
5f0d37ca30 Remove disabled asserts in LifetimeDependence 2025-05-07 12:10:18 -07:00
Karoy Lorentey
b7bb2d5793 [AST] Temporarily turn off another assert
`scopeLifetimeParamIndices` sometimes ends up non-null but empty, which causes crashes in module deserialization, blocking cross-module work on nonescapable types.

rdar://147765187
2025-04-25 16:37:35 -07:00
Andrew Trick
501abb0975 Add SILFunctionType::isAddressable & ApplySite::isAddressable. 2025-04-07 14:42:10 -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
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
Joe Groff
562d7dc832 Merge pull request #80438 from jckarter/substitute-away-escapable-lifetime-deps
Type substitution eliminates dependencies with Escapable targets.
2025-04-02 16:56:54 -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
Karoy Lorentey
4cd035182a [AST] Temporarily turn off minor assert to avoid source compat regression 2025-03-24 12:16:21 -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
edaf04b6f7 Print '@'lifetime(copy x) in the .swiftinterface 2025-03-19 11:59:04 -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
Andrew Trick
bc6200e592 SwiftCompilerSources: bridge '@'_addressable dependencies 2025-03-03 11:56:37 -08: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
Meghana Gupta
b443ff76e4 [NFC] Use llvm::TrailingObjects to represent variable list of lifetime sources 2024-10-08 15:11:26 -07:00
Meghana Gupta
8e8b21e25e Fix AST printing of @lifetime 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
Gabor Horvath
16e012bb5e [cxx-interop] Teach importer to interpret lifetimebound annotations
The lifetimebound annotations are now imported as lifetime dependencies.
This works for basic cases but there are still some parts missing:
* Support lifeitmebound annotations on constructors
* A way to represent immortal/static lifetimes on the C++ side
2024-09-18 10:51:05 +01:00
Meghana Gupta
7af6c11e7e Store parsed @lifetime attribute on the function type 2024-09-09 22:02:44 -07:00
Meghana Gupta
2b011b0a3a [NFC] Rename/reorg LifetimeDependence utils 2024-09-09 22:02:44 -07:00
Meghana Gupta
e61d87c01c Add support for parsing @lifetime attribute to specify lifetime dependencies on declarations 2024-09-09 22:02:42 -07: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
076d70acbb Update getLifetimeDependenceInfo to getLifetimeDependencies in CSSimplify 2024-07-10 14:48:34 -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
Meghana Gupta
c14559173d Add dependsOn(immortal) 2024-06-11 11:18:10 -07:00
Meghana Gupta
31b7a4e437 Move LifetimeDependence.cpp to AST 2024-04-16 12:05:23 -07:00
Meghana Gupta
afd2052e0b NFC: Reorganize LifetimeDependence utils 2024-04-08 22:33:32 -07:00
Meghana Gupta
9c57458163 Fix index numbering in lifetime dependence 2024-04-08 22:33:28 -07:00
Meghana Gupta
e60e43cac1 Serialize inferred lifetime dependence info as well 2024-04-08 15:15:47 -07:00
Meghana Gupta
b5ca933002 Update lifetime dependence syntax and inference as per changes in the pitch
Pitch - https://github.com/apple/swift-evolution/pull/2305

Changes highlights:

dependsOn(paramName) and dependsOn(scoped argName) syntax

dependsOn(paramName) -> copy lifetime dependence for all parameters/self except
                         when we have Escapable parameters/self, we assign scope
                         lifetime dependence.

Allow lifetime dependence on parameters without ownership modifier.

Always infer copy lifetime dependence except when we have
Escapable parameters/self, we infer scope lifetime dependence.

Allow lifetime dependence inference on parameters without ownership modifier.
2024-03-23 18:19:47 -07:00
Meghana Gupta
5ad0a8b064 Parse explicit lifetime dependence specifiers in SIL 2024-03-19 13:34:50 -07:00
Meghana Gupta
eb84095c0d Serialize lifetime dependence info on function types as well 2024-03-05 16:20:52 -08:00
Ben Barham
ef8825bfe6 Migrate llvm::Optional to std::optional
LLVM has removed llvm::Optional, move over to std::optional. Also
clang-format to fix up all the renamed #includes.
2024-02-21 11:20:06 -08:00
Meghana Gupta
0fd6ef3309 Add support for lifetime dependence mangling 2024-02-14 13:16:31 -08:00
Meghana Gupta
56d2027644 Add LifetimeDependence::get from param indices 2024-02-14 11:49:11 -08:00
Andrew Trick
8b24382494 SwiftCompilerSources: cleanup and extend ArgumentConventions
Provide APIs needed by lifetime dependence diagnostics, namely LifetimeDependenceConvention.

Reorganize the APIs so it's easy to find related functionality which
API is responsible for which functionality.

Remove the originalFunctionConvention complexity. It is no longer
needed for lifetime dependence inference, and generally should be
avoided in SIL.

Add some placeholder FIXMEs because this not a good PR in which to
change existing functionality.
2024-02-12 09:57:14 -08:00
Andrew Trick
28b60e051d Bridge SILFunctionType LifetimeDependenceInfo 2024-02-12 09:57:14 -08:00