Skip to content

Commit 7d45453

Browse files
authored
Merge pull request #82067 from meg-gupta/lifetimeunderscored
Update spelling for representing lifetime dependencies to @_lifetime
2 parents 26d589e + 74e4c2e commit 7d45453

File tree

104 files changed

+1298
-1013
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+1298
-1013
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,10 +1165,11 @@ BridgedLifetimeEntry BridgedLifetimeEntry_createParsed(
11651165
BridgedASTContext cContext, BridgedSourceRange cRange,
11661166
BridgedArrayRef cSources, BridgedLifetimeDescriptor cTarget);
11671167

1168-
SWIFT_NAME("BridgedLifetimeAttr.createParsed(_:atLoc:range:entry:)")
1168+
SWIFT_NAME(
1169+
"BridgedLifetimeAttr.createParsed(_:atLoc:range:entry:isUnderscored:)")
11691170
BridgedLifetimeAttr BridgedLifetimeAttr_createParsed(
11701171
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
1171-
BridgedSourceRange cRange, BridgedLifetimeEntry cEntry);
1172+
BridgedSourceRange cRange, BridgedLifetimeEntry cEntry, bool isUnderscored);
11721173

11731174
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedMacroSyntax {
11741175
BridgedMacroSyntaxFreestanding,

include/swift/AST/Attr.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@ class DeclAttribute : public AttributeBase {
239239

240240
NumFeatures : 31
241241
);
242+
243+
SWIFT_INLINE_BITFIELD(LifetimeAttr, DeclAttribute, 1,
244+
isUnderscored : 1
245+
);
242246
} Bits;
243247
// clang-format on
244248

@@ -3351,26 +3355,33 @@ class LifetimeAttr final : public DeclAttribute {
33513355
LifetimeEntry *entry;
33523356

33533357
LifetimeAttr(SourceLoc atLoc, SourceRange baseRange, bool implicit,
3354-
LifetimeEntry *entry)
3358+
LifetimeEntry *entry, bool isUnderscored)
33553359
: DeclAttribute(DeclAttrKind::Lifetime, atLoc, baseRange, implicit),
3356-
entry(entry) {}
3360+
entry(entry) {
3361+
Bits.LifetimeAttr.isUnderscored = isUnderscored;
3362+
}
33573363

33583364
public:
33593365
static LifetimeAttr *create(ASTContext &context, SourceLoc atLoc,
33603366
SourceRange baseRange, bool implicit,
3361-
LifetimeEntry *entry);
3367+
LifetimeEntry *entry, bool isUnderscored);
33623368

33633369
LifetimeEntry *getLifetimeEntry() const { return entry; }
33643370

3371+
bool isUnderscored() const { return bool(Bits.LifetimeAttr.isUnderscored); }
3372+
33653373
static bool classof(const DeclAttribute *DA) {
33663374
return DA->getKind() == DeclAttrKind::Lifetime;
33673375
}
33683376

33693377
/// Create a copy of this attribute.
33703378
LifetimeAttr *clone(ASTContext &ctx) const {
3371-
return new (ctx) LifetimeAttr(AtLoc, Range, isImplicit(), entry);
3379+
return new (ctx)
3380+
LifetimeAttr(AtLoc, Range, isImplicit(), entry, isUnderscored());
33723381
}
33733382

3383+
std::string getString() const;
3384+
33743385
bool isEquivalent(const LifetimeAttr *other, Decl *attachedTo) const;
33753386
};
33763387

include/swift/AST/DeclAttr.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,7 @@ DECL_ATTR(lifetime, Lifetime,
842842
OnAccessor | OnConstructor | OnFunc | OnSubscript,
843843
LongAttribute | ABIBreakingToAdd | ABIStableToRemove | APIBreakingToAdd | APIStableToRemove | AllowMultipleAttributes | EquivalentInABIAttr,
844844
161)
845+
DECL_ATTR_ALIAS(_lifetime, Lifetime)
845846

846847
SIMPLE_DECL_ATTR(_addressableSelf, AddressableSelf,
847848
OnAccessor | OnConstructor | OnFunc | OnSubscript,

include/swift/AST/DiagnosticsSema.def

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8265,6 +8265,9 @@ ERROR(pack_iteration_where_clause_not_supported, none,
82658265
// MARK: Lifetime Dependence Syntax
82668266
//------------------------------------------------------------------------------
82678267

8268+
WARNING(use_lifetime_underscored, PointsToFirstBadToken,
8269+
"Unsupported use of @lifetime, use @_lifetime to specify lifetime dependencies", ())
8270+
82688271
ERROR(lifetime_dependence_invalid_param_name, none,
82698272
"invalid parameter name specified %0", (Identifier))
82708273
ERROR(lifetime_dependence_invalid_param_index, none,
@@ -8299,7 +8302,7 @@ ERROR(lifetime_dependence_immortal_alone, none,
82998302
"cannot specify any other dependence source along with immortal", ())
83008303
ERROR(lifetime_dependence_invalid_inherit_escapable_type, none,
83018304
"cannot copy the lifetime of an Escapable type, use "
8302-
"'@lifetime(%1%0)' instead",
8305+
"'@_lifetime(%1%0)' instead",
83038306
(StringRef, StringRef))
83048307
ERROR(lifetime_dependence_cannot_use_parsed_borrow_consuming, none,
83058308
"invalid use of %0 dependence with %1 ownership",
@@ -8330,11 +8333,11 @@ ERROR(lifetime_dependence_feature_required_inout, none,
83308333
(StringRef, Identifier))
83318334

83328335
ERROR(lifetime_dependence_cannot_infer_return, none,
8333-
"%0 with a ~Escapable result requires '@lifetime(...)'", (StringRef))
8336+
"%0 with a ~Escapable result requires '@_lifetime(...)'", (StringRef))
83348337
ERROR(lifetime_dependence_cannot_infer_mutating, none,
8335-
"%0 with a ~Escapable 'self' requires '@lifetime(self: ...)'", (StringRef))
8338+
"%0 with a ~Escapable 'self' requires '@_lifetime(self: ...)'", (StringRef))
83368339
ERROR(lifetime_dependence_cannot_infer_inout, none,
8337-
"%0 with a ~Escapable 'inout' parameter requires '@lifetime(%1: ...)'",
8340+
"%0 with a ~Escapable 'inout' parameter requires '@_lifetime(%1: ...)'",
83388341
(StringRef, Identifier))
83398342

83408343
//------------------------------------------------------------------------------
@@ -8345,15 +8348,15 @@ ERROR(lifetime_dependence_cannot_infer_return_no_param, none,
83458348
"%0 with a ~Escapable result needs a parameter to depend on",
83468349
(StringRef))
83478350
NOTE(lifetime_dependence_cannot_infer_return_immortal, none,
8348-
"'@lifetime(immortal)' can be used to indicate that values produced by "
8351+
"'@_lifetime(immortal)' can be used to indicate that values produced by "
83498352
"this initializer have no lifetime dependencies", ())
83508353
ERROR(lifetime_dependence_cannot_infer_bitwisecopyable, none,
83518354
"cannot infer lifetime dependence on %0 because '%1' is BitwiseCopyable, "
8352-
"specify '@lifetime(borrow self)'",
8355+
"specify '@_lifetime(borrow self)'",
83538356
(StringRef, StringRef))
83548357
ERROR(lifetime_dependence_cannot_infer_kind, none,
83558358
"cannot infer the lifetime dependence scope on %0 with a ~Escapable "
8356-
"parameter, specify '@lifetime(borrow %1)' or '@lifetime(copy %1)'",
8359+
"parameter, specify '@_lifetime(borrow %1)' or '@_lifetime(copy %1)'",
83578360
(StringRef, StringRef))
83588361
ERROR(lifetime_dependence_cannot_infer_scope_ownership, none,
83598362
"cannot borrow the lifetime of '%0', which has consuming ownership on %1",

include/swift/AST/Module.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,9 @@ class ModuleDecl
11581158
/// \returns true if this module is the "swift" standard library module.
11591159
bool isStdlibModule() const;
11601160

1161+
/// \returns true if this module is the "Cxx" module.
1162+
bool isCxxModule() const;
1163+
11611164
/// \returns true if this module is the "_Concurrency" standard library module.
11621165
bool isConcurrencyModule() const;
11631166

include/swift/AST/PrintOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,9 @@ struct PrintOptions {
395395
/// as public
396396
bool SuppressIsolatedDeinit = false;
397397

398+
/// Suppress @_lifetime attribute and emit @lifetime instead.
399+
bool SuppressLifetimes = false;
400+
398401
/// Whether to print the \c{/*not inherited*/} comment on factory initializers.
399402
bool PrintFactoryInitializerComment = true;
400403

include/swift/Basic/Features.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,9 @@ EXPERIMENTAL_FEATURE(ModuleSelector, false)
525525
/// in a file scope.
526526
EXPERIMENTAL_FEATURE(DefaultIsolationPerFile, false)
527527

528+
/// Enable @_lifetime attribute
529+
SUPPRESSIBLE_EXPERIMENTAL_FEATURE(Lifetimes, true)
530+
528531
#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
529532
#undef EXPERIMENTAL_FEATURE
530533
#undef UPCOMING_FEATURE

include/swift/Parse/Parser.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,8 +1127,8 @@ class Parser {
11271127
);
11281128

11291129
/// Parse the @lifetime attribute.
1130-
ParserResult<LifetimeAttr> parseLifetimeAttribute(SourceLoc AtLoc,
1131-
SourceLoc Loc);
1130+
ParserResult<LifetimeAttr>
1131+
parseLifetimeAttribute(StringRef attrName, SourceLoc atLoc, SourceLoc loc);
11321132

11331133
/// Common utility to parse swift @lifetime decl attribute and SIL @lifetime
11341134
/// type modifier.
@@ -1158,7 +1158,8 @@ class Parser {
11581158

11591159
bool isParameterSpecifier() {
11601160
if (Tok.is(tok::kw_inout)) return true;
1161-
if (Context.LangOpts.hasFeature(Feature::LifetimeDependence) &&
1161+
if ((Context.LangOpts.hasFeature(Feature::LifetimeDependence) ||
1162+
Context.LangOpts.hasFeature(Feature::Lifetimes)) &&
11621163
isSILLifetimeDependenceToken())
11631164
return true;
11641165
if (!canHaveParameterSpecifierContextualKeyword()) return false;

lib/AST/ASTPrinter.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3261,6 +3261,13 @@ suppressingFeatureIsolatedDeinit(PrintOptions &options,
32613261
action();
32623262
}
32633263

3264+
static void
3265+
suppressingFeatureLifetimes(PrintOptions &options,
3266+
llvm::function_ref<void()> action) {
3267+
llvm::SaveAndRestore<bool> scope(options.SuppressLifetimes, true);
3268+
action();
3269+
}
3270+
32643271
namespace {
32653272
struct ExcludeAttrRAII {
32663273
std::vector<AnyAttrKind> &ExcludeAttrList;

lib/AST/Attr.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,7 +1690,11 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
16901690

16911691
case DeclAttrKind::Lifetime: {
16921692
auto *attr = cast<LifetimeAttr>(this);
1693-
Printer << attr->getLifetimeEntry()->getString();
1693+
if (!attr->isUnderscored() || Options.SuppressLifetimes) {
1694+
Printer << "@lifetime" << attr->getLifetimeEntry()->getString();
1695+
} else {
1696+
Printer << "@_lifetime" << attr->getLifetimeEntry()->getString();
1697+
}
16941698
break;
16951699
}
16961700

@@ -1962,7 +1966,7 @@ StringRef DeclAttribute::getAttrName() const {
19621966
return "_allowFeatureSuppression";
19631967
}
19641968
case DeclAttrKind::Lifetime:
1965-
return "lifetime";
1969+
return cast<LifetimeAttr>(this)->isUnderscored() ? "_lifetime" : "lifetime";
19661970
}
19671971
llvm_unreachable("bad DeclAttrKind");
19681972
}
@@ -3224,8 +3228,15 @@ isEquivalent(const AllowFeatureSuppressionAttr *other, Decl *attachedTo) const {
32243228

32253229
LifetimeAttr *LifetimeAttr::create(ASTContext &context, SourceLoc atLoc,
32263230
SourceRange baseRange, bool implicit,
3227-
LifetimeEntry *entry) {
3228-
return new (context) LifetimeAttr(atLoc, baseRange, implicit, entry);
3231+
LifetimeEntry *entry, bool isUnderscored) {
3232+
return new (context)
3233+
LifetimeAttr(atLoc, baseRange, implicit, entry, isUnderscored);
3234+
}
3235+
3236+
std::string LifetimeAttr::getString() const {
3237+
return (isUnderscored() ? std::string("@_lifetime")
3238+
: std::string("@lifetime")) +
3239+
getLifetimeEntry()->getString();
32293240
}
32303241

32313242
bool LifetimeAttr::isEquivalent(const LifetimeAttr *other,

lib/AST/Bridging/DeclAttributeBridging.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,10 +527,11 @@ BridgedLifetimeEntry BridgedLifetimeEntry_createParsed(
527527

528528
BridgedLifetimeAttr BridgedLifetimeAttr_createParsed(
529529
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
530-
BridgedSourceRange cRange, BridgedLifetimeEntry cEntry) {
530+
BridgedSourceRange cRange, BridgedLifetimeEntry cEntry,
531+
bool isUnderscored) {
531532
return LifetimeAttr::create(cContext.unbridged(), cAtLoc.unbridged(),
532533
cRange.unbridged(), /*implicit=*/false,
533-
cEntry.unbridged());
534+
cEntry.unbridged(), isUnderscored);
534535
}
535536

536537
BridgedMacroRole BridgedMacroRole_fromString(BridgedStringRef str) {

lib/AST/FeatureSet.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,36 @@ static bool usesFeatureSendingArgsAndResults(Decl *decl) {
258258
return false;
259259
}
260260

261+
static bool findUnderscoredLifetimeAttr(Decl *decl) {
262+
auto hasUnderscoredLifetimeAttr = [](Decl *decl) {
263+
if (!decl->getAttrs().hasAttribute<LifetimeAttr>()) {
264+
return false;
265+
}
266+
// Since we ban mixing @lifetime and @_lifetime on the same decl, checking
267+
// any one LifetimeAttr on the decl is sufficient.
268+
// FIXME: Implement the ban.
269+
return decl->getAttrs().getAttribute<LifetimeAttr>()->isUnderscored();
270+
};
271+
272+
switch (decl->getKind()) {
273+
case DeclKind::Var: {
274+
auto *var = cast<VarDecl>(decl);
275+
return llvm::any_of(var->getAllAccessors(), hasUnderscoredLifetimeAttr);
276+
}
277+
default:
278+
return hasUnderscoredLifetimeAttr(decl);
279+
}
280+
}
281+
261282
static bool usesFeatureLifetimeDependence(Decl *decl) {
262283
if (decl->getAttrs().hasAttribute<LifetimeAttr>()) {
284+
if (findUnderscoredLifetimeAttr(decl)) {
285+
// Experimental feature Lifetimes will guard the decl.
286+
return false;
287+
}
263288
return true;
264289
}
290+
265291
if (auto *afd = dyn_cast<AbstractFunctionDecl>(decl)) {
266292
return afd->getInterfaceType()
267293
->getAs<AnyFunctionType>()
@@ -273,6 +299,10 @@ static bool usesFeatureLifetimeDependence(Decl *decl) {
273299
return false;
274300
}
275301

302+
static bool usesFeatureLifetimes(Decl *decl) {
303+
return findUnderscoredLifetimeAttr(decl);
304+
}
305+
276306
static bool usesFeatureInoutLifetimeDependence(Decl *decl) {
277307
auto hasInoutLifetimeDependence = [](Decl *decl) {
278308
for (auto attr : decl->getAttrs().getAttributes<LifetimeAttr>()) {

lib/AST/LifetimeDependence.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ LifetimeEntry::create(const ASTContext &ctx, SourceLoc startLoc,
3838
}
3939

4040
std::string LifetimeEntry::getString() const {
41-
std::string result = "@lifetime(";
41+
std::string result = "(";
4242
if (targetDescriptor.has_value()) {
4343
result += targetDescriptor->getString();
4444
result += ": ";
@@ -300,14 +300,15 @@ class LifetimeDependenceChecker {
300300
assert(lifetimeDependencies.empty());
301301

302302
// Handle Builtins first because, even though Builtins require
303-
// LifetimeDependence, we don't force Feature::LifetimeDependence
303+
// LifetimeDependence, we don't force the experimental feature
304304
// to be enabled when importing the Builtin module.
305305
if (afd->isImplicit() && afd->getModuleContext()->isBuiltinModule()) {
306306
inferBuiltin();
307307
return currentDependencies();
308308
}
309309

310310
if (!ctx.LangOpts.hasFeature(Feature::LifetimeDependence)
311+
&& !ctx.LangOpts.hasFeature(Feature::Lifetimes)
311312
&& !ctx.SourceMgr.isImportMacroGeneratedLoc(returnLoc)) {
312313

313314
// Infer inout dependencies without requiring a feature flag. On

lib/AST/Module.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,6 +2017,10 @@ bool ModuleDecl::isStdlibModule() const {
20172017
return !getParent() && getName() == getASTContext().StdlibModuleName;
20182018
}
20192019

2020+
bool ModuleDecl::isCxxModule() const {
2021+
return !getParent() && getName() == getASTContext().Id_Cxx;
2022+
}
2023+
20202024
bool ModuleDecl::isConcurrencyModule() const {
20212025
return !getParent() && getName() == getASTContext().Id_Concurrency;
20222026
}

lib/ASTGen/Sources/ASTGen/DeclAttrs.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,19 +1169,17 @@ extension ASTGenVisitor {
11691169
/// @lifetime(self)
11701170
/// ```
11711171
func generateLifetimeAttr(attribute node: AttributeSyntax) -> BridgedLifetimeAttr? {
1172-
guard self.ctx.langOptsHasFeature(.LifetimeDependence) else {
1173-
// TODO: Diagnose
1174-
fatalError("@lifetime attribute requires 'LifetimeDependence' feature")
1175-
}
11761172
guard let entry = self.generateLifetimeEntry(attribute: node) else {
11771173
// TODO: Diagnose?
11781174
return nil
11791175
}
1176+
11801177
return .createParsed(
11811178
self.ctx,
11821179
atLoc: self.generateSourceLoc(node.atSign),
11831180
range: self.generateAttrSourceRange(node),
1184-
entry: entry
1181+
entry: entry,
1182+
isUnderscored: node.attributeName.as(IdentifierTypeSyntax.self)?.name.text == "_lifetime"
11851183
)
11861184
}
11871185

lib/Macros/Sources/SwiftMacros/SwiftifyImportMacro.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,7 @@ func getReturnLifetimeAttribute(
13871387
.attribute(
13881388
AttributeSyntax(
13891389
atSign: .atSignToken(),
1390-
attributeName: IdentifierTypeSyntax(name: "lifetime"),
1390+
attributeName: IdentifierTypeSyntax(name: "_lifetime"),
13911391
leftParen: .leftParenToken(),
13921392
arguments: .argumentList(LabeledExprListSyntax(args)),
13931393
rightParen: .rightParenToken()))
@@ -1445,7 +1445,7 @@ func containsLifetimeAttr(_ attrs: AttributeListSyntax, for paramName: TokenSynt
14451445
guard let attr = elem.as(AttributeSyntax.self) else {
14461446
continue
14471447
}
1448-
if attr.attributeName != "lifetime" {
1448+
if attr.attributeName != "_lifetime" {
14491449
continue
14501450
}
14511451
guard let args = attr.arguments?.as(LabeledExprListSyntax.self) else {
@@ -1479,7 +1479,7 @@ func paramLifetimeAttributes(
14791479
.attribute(
14801480
AttributeSyntax(
14811481
atSign: .atSignToken(),
1482-
attributeName: IdentifierTypeSyntax(name: "lifetime"),
1482+
attributeName: IdentifierTypeSyntax(name: "_lifetime"),
14831483
leftParen: .leftParenToken(),
14841484
arguments: .argumentList(LabeledExprListSyntax([LabeledExprSyntax(expression: expr)])),
14851485
rightParen: .rightParenToken())))

0 commit comments

Comments
 (0)