Skip to content

Commit 6ef59c0

Browse files
committed
Added @escaping attribute parsing
1 parent 6e95043 commit 6ef59c0

19 files changed

+84
-14
lines changed

include/swift/AST/Attr.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ TYPE_ATTR(autoclosure)
3535
TYPE_ATTR(convention)
3636
TYPE_ATTR(noreturn)
3737
TYPE_ATTR(noescape)
38+
TYPE_ATTR(escaping)
3839

3940
// SIL-specific attributes
4041
TYPE_ATTR(block_storage)
@@ -263,6 +264,8 @@ SIMPLE_DECL_ATTR(discardableResult, DiscardableResult,
263264

264265
SIMPLE_DECL_ATTR(GKInspectable, GKInspectable, OnVar, 66)
265266

267+
SIMPLE_DECL_ATTR(escaping, Escaping, OnParam, 67)
268+
266269
#undef TYPE_ATTR
267270
#undef DECL_ATTR_ALIAS
268271
#undef SIMPLE_DECL_ATTR

include/swift/AST/DiagnosticsParse.def

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1247,7 +1247,6 @@ ERROR(attr_swift3_migration_expected_rparen,none,
12471247
ERROR(attr_bad_swift_name,none,
12481248
"ill-formed Swift name '%0'", (StringRef))
12491249

1250-
12511250
// unowned
12521251
ERROR(attr_unowned_invalid_specifier,none,
12531252
"expected 'safe' or 'unsafe'", ())
@@ -1260,6 +1259,10 @@ WARNING(attr_warn_unused_result_removed,none,
12601259
ERROR(attr_warn_unused_result_expected_rparen,none,
12611260
"expected ')' after 'warn_unused_result' attribute", ())
12621261

1262+
// escaping
1263+
ERROR(attr_escaping_conflicts_noescape,none,
1264+
"@escaping conflicts with @noescape", ())
1265+
12631266
//------------------------------------------------------------------------------
12641267
// Generics parsing diagnostics
12651268
//------------------------------------------------------------------------------

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1788,7 +1788,8 @@ ERROR(noescape_implied_by_autoclosure,none,
17881788
"redundantly specified", ())
17891789
ERROR(noescape_conflicts_escaping_autoclosure,none,
17901790
"@noescape conflicts with @autoclosure(escaping)", ())
1791-
1791+
ERROR(escaping_function_type,none,
1792+
"@escaping may only be applied to parameters of function type", ())
17921793

17931794
// NSManaged attribute
17941795
ERROR(attr_NSManaged_not_instance_member,none,

include/swift/AST/Types.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2159,11 +2159,12 @@ class AnyFunctionType : public TypeBase {
21592159
// |representation|isAutoClosure|noReturn|noEscape|throws|
21602160
// | 0 .. 3 | 4 | 5 | 6 | 7 |
21612161
//
2162-
enum : uint16_t { RepresentationMask = 0x00F };
2163-
enum : uint16_t { AutoClosureMask = 0x010 };
2164-
enum : uint16_t { NoReturnMask = 0x020 };
2165-
enum : uint16_t { NoEscapeMask = 0x040 };
2166-
enum : uint16_t { ThrowsMask = 0x080 };
2162+
enum : uint16_t { RepresentationMask = 0x00F };
2163+
enum : uint16_t { AutoClosureMask = 0x010 };
2164+
enum : uint16_t { NoReturnMask = 0x020 };
2165+
enum : uint16_t { NoEscapeMask = 0x040 };
2166+
enum : uint16_t { ThrowsMask = 0x080 };
2167+
enum : uint16_t { ExplicitlyEscapingMask = 0x100 };
21672168

21682169
uint16_t Bits;
21692170

@@ -2186,15 +2187,18 @@ class AnyFunctionType : public TypeBase {
21862187

21872188
// Constructor with no defaults.
21882189
ExtInfo(Representation Rep, bool IsNoReturn,
2189-
bool IsAutoClosure, bool IsNoEscape, bool Throws)
2190+
bool IsAutoClosure, bool IsNoEscape, bool IsExplicitlyEscaping,
2191+
bool Throws)
21902192
: ExtInfo(Rep, IsNoReturn, Throws) {
21912193
Bits |= (IsAutoClosure ? AutoClosureMask : 0);
21922194
Bits |= (IsNoEscape ? NoEscapeMask : 0);
2195+
Bits |= (IsExplicitlyEscaping ? ExplicitlyEscapingMask : 0);
21932196
}
21942197

21952198
bool isNoReturn() const { return Bits & NoReturnMask; }
21962199
bool isAutoClosure() const { return Bits & AutoClosureMask; }
21972200
bool isNoEscape() const { return Bits & NoEscapeMask; }
2201+
bool isExplicitlyEscaping() const { return Bits & ExplicitlyEscapingMask; }
21982202
bool throws() const { return Bits & ThrowsMask; }
21992203
Representation getRepresentation() const {
22002204
unsigned rawRep = Bits & RepresentationMask;
@@ -2327,6 +2331,12 @@ class AnyFunctionType : public TypeBase {
23272331
bool isNoEscape() const {
23282332
return getExtInfo().isNoEscape();
23292333
}
2334+
2335+
/// \brief True if the parameter declaration it is attached to has explicitly
2336+
/// been marked with the @escaping attribute. This is a temporary measure.
2337+
bool isExplicitlyEscaping() const {
2338+
return getExtInfo().isExplicitlyEscaping();
2339+
}
23302340

23312341
bool throws() const {
23322342
return getExtInfo().throws();

include/swift/Serialization/ModuleFormat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,7 @@ namespace decls_block {
590590
BCFixed<1>, // auto-closure?
591591
BCFixed<1>, // noreturn?
592592
BCFixed<1>, // noescape?
593+
BCFixed<1>, // explicitlyEscaping?
593594
BCFixed<1> // throws?
594595
>;
595596

lib/AST/ASTDumper.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2899,6 +2899,7 @@ namespace {
28992899
printFlag(T->isNoReturn(), "noreturn");
29002900
printFlag(T->isAutoClosure(), "autoclosure");
29012901
printFlag(T->isNoEscape(), "noescape");
2902+
printFlag(T->isExplicitlyEscaping(), "escaping");
29022903
printFlag(T->throws(), "throws");
29032904

29042905
printRec("input", T->getInput());

lib/AST/ASTPrinter.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3655,9 +3655,12 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
36553655
Printer << "@autoclosure ";
36563656
else
36573657
Printer << "@autoclosure(escaping) ";
3658-
} else if (info.isNoEscape())
3658+
} else if (info.isNoEscape()) {
36593659
// autoclosure implies noescape.
36603660
Printer << "@noescape ";
3661+
} else if (info.isExplicitlyEscaping()) {
3662+
Printer << "@escaping ";
3663+
}
36613664

36623665
if (Options.PrintFunctionRepresentationAttrs) {
36633666
// TODO: coalesce into a single convention attribute.

lib/AST/TypeRepr.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ void AttributedTypeRepr::printAttrs(ASTPrinter &Printer) const {
283283
case 2: Printer << "@autoclosure(escaping) "; break;
284284
case 3: Printer << "@autoclosure "; break;
285285
}
286+
if (Attrs.has(TAK_escaping)) Printer << "@escaping ";
286287
if (Attrs.has(TAK_thin)) Printer << "@thin ";
287288
if (Attrs.has(TAK_thick)) Printer << "@thick ";
288289
if (Attrs.convention.hasValue()) {

lib/Parse/ParseDecl.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,6 +1522,18 @@ bool Parser::parseTypeAttribute(TypeAttributes &Attributes, bool justChecking) {
15221522
diagnose(Loc, diag::attr_noescape_conflicts_escaping_autoclosure);
15231523
return false;
15241524
}
1525+
// You can't specify @noescape and @escaping together.
1526+
if (Attributes.has(TAK_escaping)) {
1527+
diagnose(Loc, diag::attr_escaping_conflicts_noescape);
1528+
return false;
1529+
}
1530+
break;
1531+
case TAK_escaping:
1532+
// You can't specify @noescape and @escaping together.
1533+
if (Attributes.has(TAK_noescape)) {
1534+
diagnose(Loc, diag::attr_escaping_conflicts_noescape);
1535+
return false;
1536+
}
15251537
break;
15261538
case TAK_out:
15271539
case TAK_in:

lib/Sema/CSApply.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5480,6 +5480,7 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
54805480
toEI.isNoReturn() | fromEI.isNoReturn(),
54815481
toEI.isAutoClosure(),
54825482
toEI.isNoEscape() | fromEI.isNoEscape(),
5483+
toEI.isExplicitlyEscaping() | fromEI.isExplicitlyEscaping(),
54835484
toEI.throws() & fromEI.throws());
54845485
auto newToType = FunctionType::get(fromFunc->getInput(),
54855486
fromFunc->getResult(), newEI);

0 commit comments

Comments
 (0)