Skip to content

Commit 1b34cbe

Browse files
authored
Merge pull request #82146 from meg-gupta/cpunderscore
[6.2] Update spelling for representing lifetime dependencies to @_lifetime
2 parents 9bf7538 + 8d3f386 commit 1b34cbe

File tree

102 files changed

+1287
-1002
lines changed

Some content is hidden

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

102 files changed

+1287
-1002
lines changed

include/swift/AST/ASTBridging.h

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

1147-
SWIFT_NAME("BridgedLifetimeAttr.createParsed(_:atLoc:range:entry:)")
1147+
SWIFT_NAME(
1148+
"BridgedLifetimeAttr.createParsed(_:atLoc:range:entry:isUnderscored:)")
11481149
BridgedLifetimeAttr BridgedLifetimeAttr_createParsed(
11491150
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
1150-
BridgedSourceRange cRange, BridgedLifetimeEntry cEntry);
1151+
BridgedSourceRange cRange, BridgedLifetimeEntry cEntry, bool isUnderscored);
11511152

11521153
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedMacroSyntax {
11531154
BridgedMacroSyntaxFreestanding,

include/swift/AST/Attr.h

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

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

@@ -3219,26 +3223,33 @@ class LifetimeAttr final : public DeclAttribute {
32193223
LifetimeEntry *entry;
32203224

32213225
LifetimeAttr(SourceLoc atLoc, SourceRange baseRange, bool implicit,
3222-
LifetimeEntry *entry)
3226+
LifetimeEntry *entry, bool isUnderscored)
32233227
: DeclAttribute(DeclAttrKind::Lifetime, atLoc, baseRange, implicit),
3224-
entry(entry) {}
3228+
entry(entry) {
3229+
Bits.LifetimeAttr.isUnderscored = isUnderscored;
3230+
}
32253231

32263232
public:
32273233
static LifetimeAttr *create(ASTContext &context, SourceLoc atLoc,
32283234
SourceRange baseRange, bool implicit,
3229-
LifetimeEntry *entry);
3235+
LifetimeEntry *entry, bool isUnderscored);
32303236

32313237
LifetimeEntry *getLifetimeEntry() const { return entry; }
32323238

3239+
bool isUnderscored() const { return bool(Bits.LifetimeAttr.isUnderscored); }
3240+
32333241
static bool classof(const DeclAttribute *DA) {
32343242
return DA->getKind() == DeclAttrKind::Lifetime;
32353243
}
32363244

32373245
/// Create a copy of this attribute.
32383246
LifetimeAttr *clone(ASTContext &ctx) const {
3239-
return new (ctx) LifetimeAttr(AtLoc, Range, isImplicit(), entry);
3247+
return new (ctx)
3248+
LifetimeAttr(AtLoc, Range, isImplicit(), entry, isUnderscored());
32403249
}
32413250

3251+
std::string getString() const;
3252+
32423253
bool isEquivalent(const LifetimeAttr *other, Decl *attachedTo) const;
32433254
};
32443255

include/swift/AST/DeclAttr.def

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

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

include/swift/AST/DiagnosticsSema.def

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8195,6 +8195,9 @@ ERROR(pack_iteration_where_clause_not_supported, none,
81958195
// MARK: Lifetime Dependence Syntax
81968196
//------------------------------------------------------------------------------
81978197

8198+
WARNING(use_lifetime_underscored, PointsToFirstBadToken,
8199+
"Unsupported use of @lifetime, use @_lifetime to specify lifetime dependencies", ())
8200+
81988201
ERROR(lifetime_dependence_invalid_param_name, none,
81998202
"invalid parameter name specified %0", (Identifier))
82008203
ERROR(lifetime_dependence_invalid_param_index, none,
@@ -8229,7 +8232,7 @@ ERROR(lifetime_dependence_immortal_alone, none,
82298232
"cannot specify any other dependence source along with immortal", ())
82308233
ERROR(lifetime_dependence_invalid_inherit_escapable_type, none,
82318234
"cannot copy the lifetime of an Escapable type, use "
8232-
"'@lifetime(%1%0)' instead",
8235+
"'@_lifetime(%1%0)' instead",
82338236
(StringRef, StringRef))
82348237
ERROR(lifetime_dependence_cannot_use_parsed_borrow_consuming, none,
82358238
"invalid use of %0 dependence with %1 ownership",
@@ -8260,11 +8263,11 @@ ERROR(lifetime_dependence_feature_required_inout, none,
82608263
(StringRef, Identifier))
82618264

82628265
ERROR(lifetime_dependence_cannot_infer_return, none,
8263-
"%0 with a ~Escapable result requires '@lifetime(...)'", (StringRef))
8266+
"%0 with a ~Escapable result requires '@_lifetime(...)'", (StringRef))
82648267
ERROR(lifetime_dependence_cannot_infer_mutating, none,
8265-
"%0 with a ~Escapable 'self' requires '@lifetime(self: ...)'", (StringRef))
8268+
"%0 with a ~Escapable 'self' requires '@_lifetime(self: ...)'", (StringRef))
82668269
ERROR(lifetime_dependence_cannot_infer_inout, none,
8267-
"%0 with a ~Escapable 'inout' parameter requires '@lifetime(%1: ...)'",
8270+
"%0 with a ~Escapable 'inout' parameter requires '@_lifetime(%1: ...)'",
82688271
(StringRef, Identifier))
82698272

82708273
//------------------------------------------------------------------------------
@@ -8275,15 +8278,15 @@ ERROR(lifetime_dependence_cannot_infer_return_no_param, none,
82758278
"%0 with a ~Escapable result needs a parameter to depend on",
82768279
(StringRef))
82778280
NOTE(lifetime_dependence_cannot_infer_return_immortal, none,
8278-
"'@lifetime(immortal)' can be used to indicate that values produced by "
8281+
"'@_lifetime(immortal)' can be used to indicate that values produced by "
82798282
"this initializer have no lifetime dependencies", ())
82808283
ERROR(lifetime_dependence_cannot_infer_bitwisecopyable, none,
82818284
"cannot infer lifetime dependence on %0 because '%1' is BitwiseCopyable, "
8282-
"specify '@lifetime(borrow self)'",
8285+
"specify '@_lifetime(borrow self)'",
82838286
(StringRef, StringRef))
82848287
ERROR(lifetime_dependence_cannot_infer_kind, none,
82858288
"cannot infer the lifetime dependence scope on %0 with a ~Escapable "
8286-
"parameter, specify '@lifetime(borrow %1)' or '@lifetime(copy %1)'",
8289+
"parameter, specify '@_lifetime(borrow %1)' or '@_lifetime(copy %1)'",
82878290
(StringRef, StringRef))
82888291
ERROR(lifetime_dependence_cannot_infer_scope_ownership, none,
82898292
"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
@@ -1166,6 +1166,9 @@ class ModuleDecl
11661166
/// \returns true if this module is the "swift" standard library module.
11671167
bool isStdlibModule() const;
11681168

1169+
/// \returns true if this module is the "Cxx" module.
1170+
bool isCxxModule() const;
1171+
11691172
/// \returns true if this module is the "_Concurrency" standard library module.
11701173
bool isConcurrencyModule() const;
11711174

include/swift/AST/PrintOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,9 @@ struct PrintOptions {
355355
/// as public
356356
bool SuppressIsolatedDeinit = false;
357357

358+
/// Suppress @_lifetime attribute and emit @lifetime instead.
359+
bool SuppressLifetimes = false;
360+
358361
/// Whether to print the \c{/*not inherited*/} comment on factory initializers.
359362
bool PrintFactoryInitializerComment = true;
360363

include/swift/Basic/Features.def

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

529+
/// Enable @_lifetime attribute
530+
SUPPRESSIBLE_EXPERIMENTAL_FEATURE(Lifetimes, true)
531+
529532
#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
530533
#undef EXPERIMENTAL_FEATURE
531534
#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
@@ -3258,6 +3258,13 @@ suppressingFeatureIsolatedDeinit(PrintOptions &options,
32583258
action();
32593259
}
32603260

3261+
static void
3262+
suppressingFeatureLifetimes(PrintOptions &options,
3263+
llvm::function_ref<void()> action) {
3264+
llvm::SaveAndRestore<bool> scope(options.SuppressLifetimes, true);
3265+
action();
3266+
}
3267+
32613268
namespace {
32623269
struct ExcludeAttrRAII {
32633270
std::vector<AnyAttrKind> &ExcludeAttrList;

lib/AST/Attr.cpp

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

16841684
case DeclAttrKind::Lifetime: {
16851685
auto *attr = cast<LifetimeAttr>(this);
1686-
Printer << attr->getLifetimeEntry()->getString();
1686+
if (!attr->isUnderscored() || Options.SuppressLifetimes) {
1687+
Printer << "@lifetime" << attr->getLifetimeEntry()->getString();
1688+
} else {
1689+
Printer << "@_lifetime" << attr->getLifetimeEntry()->getString();
1690+
}
16871691
break;
16881692
}
16891693

@@ -1951,7 +1955,7 @@ StringRef DeclAttribute::getAttrName() const {
19511955
return "_allowFeatureSuppression";
19521956
}
19531957
case DeclAttrKind::Lifetime:
1954-
return "lifetime";
1958+
return cast<LifetimeAttr>(this)->isUnderscored() ? "_lifetime" : "lifetime";
19551959
}
19561960
llvm_unreachable("bad DeclAttrKind");
19571961
}
@@ -3144,8 +3148,15 @@ isEquivalent(const AllowFeatureSuppressionAttr *other, Decl *attachedTo) const {
31443148

31453149
LifetimeAttr *LifetimeAttr::create(ASTContext &context, SourceLoc atLoc,
31463150
SourceRange baseRange, bool implicit,
3147-
LifetimeEntry *entry) {
3148-
return new (context) LifetimeAttr(atLoc, baseRange, implicit, entry);
3151+
LifetimeEntry *entry, bool isUnderscored) {
3152+
return new (context)
3153+
LifetimeAttr(atLoc, baseRange, implicit, entry, isUnderscored);
3154+
}
3155+
3156+
std::string LifetimeAttr::getString() const {
3157+
return (isUnderscored() ? std::string("@_lifetime")
3158+
: std::string("@lifetime")) +
3159+
getLifetimeEntry()->getString();
31493160
}
31503161

31513162
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
@@ -257,10 +257,36 @@ static bool usesFeatureSendingArgsAndResults(Decl *decl) {
257257
return false;
258258
}
259259

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

301+
static bool usesFeatureLifetimes(Decl *decl) {
302+
return findUnderscoredLifetimeAttr(decl);
303+
}
304+
275305
static bool usesFeatureInoutLifetimeDependence(Decl *decl) {
276306
auto hasInoutLifetimeDependence = [](Decl *decl) {
277307
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
@@ -1165,19 +1165,17 @@ extension ASTGenVisitor {
11651165
/// @lifetime(self)
11661166
/// ```
11671167
func generateLifetimeAttr(attribute node: AttributeSyntax) -> BridgedLifetimeAttr? {
1168-
guard self.ctx.langOptsHasFeature(.LifetimeDependence) else {
1169-
// TODO: Diagnose
1170-
fatalError("@lifetime attribute requires 'LifetimeDependence' feature")
1171-
}
11721168
guard let entry = self.generateLifetimeEntry(attribute: node) else {
11731169
// TODO: Diagnose?
11741170
return nil
11751171
}
1172+
11761173
return .createParsed(
11771174
self.ctx,
11781175
atLoc: self.generateSourceLoc(node.atSign),
11791176
range: self.generateAttrSourceRange(node),
1180-
entry: entry
1177+
entry: entry,
1178+
isUnderscored: node.attributeName.as(IdentifierTypeSyntax.self)?.name.text == "_lifetime"
11811179
)
11821180
}
11831181

lib/Macros/Sources/SwiftMacros/SwiftifyImportMacro.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,7 +1383,7 @@ func getReturnLifetimeAttribute(
13831383
.attribute(
13841384
AttributeSyntax(
13851385
atSign: .atSignToken(),
1386-
attributeName: IdentifierTypeSyntax(name: "lifetime"),
1386+
attributeName: IdentifierTypeSyntax(name: "_lifetime"),
13871387
leftParen: .leftParenToken(),
13881388
arguments: .argumentList(LabeledExprListSyntax(args)),
13891389
rightParen: .rightParenToken()))
@@ -1441,7 +1441,7 @@ func containsLifetimeAttr(_ attrs: AttributeListSyntax, for paramName: TokenSynt
14411441
guard let attr = elem.as(AttributeSyntax.self) else {
14421442
continue
14431443
}
1444-
if attr.attributeName != "lifetime" {
1444+
if attr.attributeName != "_lifetime" {
14451445
continue
14461446
}
14471447
guard let args = attr.arguments?.as(LabeledExprListSyntax.self) else {
@@ -1475,7 +1475,7 @@ func paramLifetimeAttributes(
14751475
.attribute(
14761476
AttributeSyntax(
14771477
atSign: .atSignToken(),
1478-
attributeName: IdentifierTypeSyntax(name: "lifetime"),
1478+
attributeName: IdentifierTypeSyntax(name: "_lifetime"),
14791479
leftParen: .leftParenToken(),
14801480
arguments: .argumentList(LabeledExprListSyntax([LabeledExprSyntax(expression: expr)])),
14811481
rightParen: .rightParenToken())))

0 commit comments

Comments
 (0)