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,

0 commit comments

Comments
 (0)