Skip to content

Commit 66556d5

Browse files
committed
[AST] Add @_lifetime type attribute
1 parent 1066c82 commit 66556d5

File tree

8 files changed

+82
-0
lines changed

8 files changed

+82
-0
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2496,6 +2496,13 @@ BridgedDifferentiableTypeAttr BridgedDifferentiableTypeAttr_createParsed(
24962496
swift::SourceLoc nameLoc, swift::SourceRange parensRange,
24972497
BridgedDifferentiabilityKind cKind, swift::SourceLoc kindLoc);
24982498

2499+
SWIFT_NAME("BridgedLifetimeTypeAttr.createParsed(_:atLoc:nameLoc:"
2500+
"parensRange:entry:)")
2501+
BridgedLifetimeTypeAttr BridgedLifetimeTypeAttr_createParsed(
2502+
BridgedASTContext cContext, swift::SourceLoc atLoc,
2503+
swift::SourceLoc nameLoc, swift::SourceRange parensRange,
2504+
BridgedLifetimeEntry entry);
2505+
24992506
SWIFT_NAME("BridgedIsolatedTypeAttr.createParsed(_:atLoc:nameLoc:parensRange:"
25002507
"isolationKind:isolationKindLoc:)")
25012508
BridgedIsolatedTypeAttr BridgedIsolatedTypeAttr_createParsed(

include/swift/AST/Attr.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4349,6 +4349,19 @@ class DifferentiableTypeAttr
43494349
void printImpl(ASTPrinter &printer, const PrintOptions &options) const;
43504350
};
43514351

4352+
class LifetimeTypeAttr : public SimpleTypeAttrWithArgs<TypeAttrKind::Lifetime> {
4353+
LifetimeEntry *entry;
4354+
4355+
public:
4356+
LifetimeTypeAttr(SourceLoc atLoc, SourceLoc kwLoc, SourceRange parens,
4357+
LifetimeEntry *entry)
4358+
: SimpleTypeAttr(atLoc, kwLoc, parens), entry(entry) {}
4359+
4360+
LifetimeEntry *getLifetimeEntry() const { return entry; }
4361+
4362+
void printImpl(ASTPrinter &printer, const PrintOptions &options) const;
4363+
};
4364+
43524365
class OpaqueReturnTypeOfTypeAttr
43534366
: public SimpleTypeAttrWithArgs<TypeAttrKind::OpaqueReturnTypeOf> {
43544367
Located<StringRef> MangledName;

include/swift/AST/TypeAttr.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ SIMPLE_TYPE_ATTR(retroactive, Retroactive)
6161
SIMPLE_TYPE_ATTR(unchecked, Unchecked)
6262
SIMPLE_TYPE_ATTR(preconcurrency, Preconcurrency)
6363
SIMPLE_TYPE_ATTR(unsafe, Unsafe)
64+
TYPE_ATTR(_lifetime, Lifetime)
6465
SIMPLE_TYPE_ATTR(_local, Local)
6566
SIMPLE_TYPE_ATTR(_noMetadata, NoMetadata)
6667
TYPE_ATTR(_opaqueReturnTypeOf, OpaqueReturnTypeOf)

lib/AST/Attr.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,16 @@ void DifferentiableTypeAttr::printImpl(ASTPrinter &printer,
238238
printer.printStructurePost(PrintStructureKind::BuiltinAttribute);
239239
}
240240

241+
void LifetimeTypeAttr::printImpl(ASTPrinter &printer,
242+
const PrintOptions &options) const {
243+
printer.callPrintStructurePre(PrintStructureKind::BuiltinAttribute);
244+
printer.printAttrName("@_lifetime");
245+
printer << "(";
246+
printer << entry->getString();
247+
printer << ")";
248+
printer.printStructurePost(PrintStructureKind::BuiltinAttribute);
249+
}
250+
241251
void ConventionTypeAttr::printImpl(ASTPrinter &printer,
242252
const PrintOptions &options) const {
243253
printer.callPrintStructurePre(PrintStructureKind::BuiltinAttribute);

lib/AST/Bridging/TypeAttributeBridging.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ BridgedDifferentiableTypeAttr BridgedDifferentiableTypeAttr_createParsed(
6969
atLoc, nameLoc, parensRange, {unbridged(cKind), kindLoc});
7070
}
7171

72+
BridgedLifetimeTypeAttr BridgedLifetimeTypeAttr_createParsed(
73+
BridgedASTContext cContext, swift::SourceLoc atLoc,
74+
swift::SourceLoc nameLoc, swift::SourceRange parensRange,
75+
BridgedLifetimeEntry entry) {
76+
return new (cContext.unbridged())
77+
LifetimeTypeAttr(atLoc, nameLoc, parensRange, entry.unbridged());
78+
}
79+
7280
BridgedIsolatedTypeAttr BridgedIsolatedTypeAttr_createParsed(
7381
BridgedASTContext cContext, SourceLoc atLoc, SourceLoc nameLoc,
7482
SourceRange parensRange, BridgedIsolatedTypeAttrIsolationKind cIsolation,

lib/ASTGen/Sources/ASTGen/TypeAttrs.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ extension ASTGenVisitor {
8484
case .Differentiable:
8585
return (self.generateDifferentiableTypeAttr(attribute: node)?.asTypeAttribute)
8686
.map(BridgedTypeOrCustomAttr.typeAttr(_:))
87+
case .Lifetime:
88+
return (self.generateLifetimeTypeAttr(attribute: node)?.asTypeAttribute)
89+
.map(BridgedTypeOrCustomAttr.typeAttr(_:))
8790
case .OpaqueReturnTypeOf:
8891
return (self.generateOpaqueReturnTypeOfTypeAttr(attribute: node)?.asTypeAttribute)
8992
.map(BridgedTypeOrCustomAttr.typeAttr(_:))
@@ -246,6 +249,20 @@ extension ASTGenVisitor {
246249
kindLoc: differentiabilityLoc
247250
)
248251
}
252+
253+
func generateLifetimeTypeAttr(attribute node: AttributeSyntax) -> BridgedLifetimeTypeAttr? {
254+
guard let entry = self.generateLifetimeEntry(attribute: node) else {
255+
return nil
256+
}
257+
258+
return .createParsed(
259+
self.ctx,
260+
atLoc: self.generateSourceLoc(node.atSign),
261+
nameLoc: self.generateSourceLoc(node.attributeName),
262+
parensRange: self.generateAttrParensRange(attribute: node),
263+
entry: entry
264+
)
265+
}
249266

250267
func generateIsolatedTypeAttr(attribute node: AttributeSyntax) -> BridgedIsolatedTypeAttr? {
251268
let isolationKindLoc = self.generateSourceLoc(node.arguments)

lib/Parse/ParseDecl.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "swift/Parse/ParseDeclName.h"
4040
#include "swift/Parse/ParseSILSupport.h"
4141
#include "swift/Parse/Parser.h"
42+
#include "swift/Parse/ParserResult.h"
4243
#include "swift/Strings.h"
4344
#include "swift/Subsystems.h"
4445
#include "llvm/ADT/PointerUnion.h"
@@ -4978,6 +4979,22 @@ ParserStatus Parser::parseTypeAttribute(TypeOrCustomAttr &result,
49784979
return makeParserSuccess();
49794980
}
49804981

4982+
case TypeAttrKind::Lifetime: {
4983+
const auto entryResult = parseLifetimeEntry(AtLoc);
4984+
if (entryResult.isNull()) {
4985+
return makeParserError();
4986+
}
4987+
4988+
auto *entry = entryResult.get();
4989+
4990+
if (!justChecking) {
4991+
result = new (Context) LifetimeTypeAttr(
4992+
AtLoc, attrLoc, SourceRange(entry->getStartLoc(), entry->getEndLoc()),
4993+
entryResult.get());
4994+
}
4995+
return makeParserSuccess();
4996+
}
4997+
49814998
case TypeAttrKind::Convention: {
49824999
ConventionTypeAttr *convention = nullptr;
49835000
if (parseConventionAttributeInternal(AtLoc, attrLoc, convention,

test/expr/closure/lifetimes.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %target-typecheck-verify-swift -enable-experimental-feature Lifetimes
2+
3+
struct NE: ~Escapable {
4+
5+
}
6+
7+
func bar1(_ closure: @_lifetime(copy ne) (_ ne: NE) -> NE, _ ne: NE) {
8+
_ = closure(ne)
9+
}

0 commit comments

Comments
 (0)