Skip to content

Update spelling for representing lifetime dependencies to @_lifetime #82067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 10, 2025

Conversation

meg-gupta
Copy link
Contributor

@meg-gupta meg-gupta commented Jun 6, 2025

LifetimeDependence is a supported experimental feature for which we have to maintain source compatibility. During language evolution if we decide to update its semantics or syntax under @lifetime attribute, it can introduce significant confusion and maintenance problems to both early adopters and compiler developers.

To mitigate this, this PR introduces an underscore, effectively making the annotation @_lifetime, to provide a clear distinction and avoid potential conflicts during future development. Standard library will continue to use @lifetime in the near term.

@_lifetime is implemented as a suppressible language feature and @lifetime is printed in the suppressed path since the oldest compilers we have to support with a new sdk have @lifetime support. This will ensure when the standard library migrates to @_lifetime we will continue to support older compiler.

rdar://151901744

Copy link
Contributor

@atrick atrick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Thanks! Great that this will make it in to 6.2!

@meg-gupta meg-gupta force-pushed the lifetimeunderscored branch from a773ce3 to 854ff84 Compare June 7, 2025 04:45
@meg-gupta meg-gupta requested a review from a team as a code owner June 7, 2025 04:45
@meg-gupta meg-gupta force-pushed the lifetimeunderscored branch 5 times, most recently from 3d565da to ff11d4c Compare June 7, 2025 10:31
@meg-gupta
Copy link
Contributor Author

@swift-ci test

@meg-gupta meg-gupta force-pushed the lifetimeunderscored branch from ff11d4c to 74e4c2e Compare June 7, 2025 19:49
@meg-gupta
Copy link
Contributor Author

@swift-ci test

@meg-gupta
Copy link
Contributor Author

@swift-ci test macOS platform

Copy link
Contributor

@Xazax-hun Xazax-hun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C++ interop specific parts mostly look good to me. I have one question about backward compatibility in the SwiftifyImport macro. The rest of the questions are independent of this PR. I'd like @egorzhdan to take a look at the modules changes as he is more familiar with that part.

if (!attr->isUnderscored()) {
// Allow @lifetime only in the stdlib, cxx and backward compatibility
// modules under -enable-experimental-feature LifetimeDependence
if (!Ctx.MainModule->isStdlibModule() && !Ctx.MainModule->isCxxModule() &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@egorzhdan Could you double check if this makes sense to you? (Including the recently added isCxxModule.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this check is meant to cover @lifetime uses in CxxSpan.swift/ CxxVector.swift etc that may end up in the swiftinterface files.

void AttributeChecker::visitLifetimeAttr(LifetimeAttr *attr) {}
void AttributeChecker::visitLifetimeAttr(LifetimeAttr *attr) {
if (!attr->isUnderscored()) {
// Allow @lifetime only in the stdlib, cxx and backward compatibility
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a stable release with @lifetime annotated code in stdlib/cxx modules? Or is this a temporary workaround for early adopters?

Copy link
Contributor Author

@meg-gupta meg-gupta Jun 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want early adopters to use @_lifetime instead of @lifetime. stdlib will continue using @lifetime in the short term.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

release/6.2 will be the first stable release with lifetime dependencies as a supported experimental feature.


// RUN: %target-swift-ide-test -print-module -module-to-print=CountedByLifetimeboundClang -plugin-path %swift-plugin-dir -I %S/Inputs -source-filename=x -enable-experimental-feature SafeInteropWrappers -Xcc -Wno-nullability-completeness | %FileCheck %s

// swift-ide-test doesn't currently typecheck the macro expansions, so run the compiler as well
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/CountedByLifetimebound.swiftmodule -I %S/Inputs -enable-experimental-feature SafeInteropWrappers -enable-experimental-feature LifetimeDependence -strict-memory-safety -warnings-as-errors -Xcc -Werror -Xcc -Wno-nullability-completeness %s
// RUN: %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/CountedByLifetimebound.swiftmodule -I %S/Inputs -enable-experimental-feature SafeInteropWrappers -enable-experimental-feature Lifetimes -strict-memory-safety -warnings-as-errors -Xcc -Werror -Xcc -Wno-nullability-completeness %s
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hnrklssn Do we actually need Lifetimes/LifetimeDependence feature enabled for this to work? I think we decided that this should work regardless, so if that is the case I think we should remove this from tests. If not, we should decide if this is intentional or want to treat it as a bug.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The macro expansions themselves are fine without it, but I think at one point it was needed for the functions we use to call the overloads to be able to use inout MutableSpan. That may have been relaxed since then, but I'm not 100% on that

@@ -1445,7 +1445,7 @@ func containsLifetimeAttr(_ attrs: AttributeListSyntax, for paramName: TokenSynt
guard let attr = elem.as(AttributeSyntax.self) else {
continue
}
if attr.attributeName != "lifetime" {
if attr.attributeName != "_lifetime" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see traces of still supporting @lifetime in some restricted scenarios in this PR. This makes me wonder if this is also a codepath that should at least temporarily support both @lifetime and @_lifetime.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This codepath does not seem to be exercised in the stdlib/cxx modules - but only in case we are importing C++ as Swift in which we want @_lifetime instead of @lifetime.

cc @hnrklssn

@meg-gupta
Copy link
Contributor Author

@swift-ci test macOS platform

@meg-gupta meg-gupta merged commit 7d45453 into swiftlang:main Jun 10, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants