Skip to content

Commit 82b13d1

Browse files
authored
Merge pull request #10784 from CodaFi/my-sweet-noescape
[SR-5296] @autoclosure and @NoEscape are not decl attributes
2 parents 9fb11c5 + b8069fb commit 82b13d1

20 files changed

+47
-282
lines changed

include/swift/AST/Attr.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,6 @@ SIMPLE_DECL_ATTR(_transparent, Transparent,
155155
OnFunc|OnConstructor|OnVar|UserInaccessible, 26)
156156
SIMPLE_DECL_ATTR(requires_stored_property_inits, RequiresStoredPropertyInits,
157157
OnClass, 27)
158-
DECL_ATTR(autoclosure, AutoClosure, OnParam, 28)
159-
SIMPLE_DECL_ATTR(noescape, NoEscape, OnParam, 29)
160158

161159
SIMPLE_DECL_ATTR(nonobjc, NonObjC,
162160
OnExtension | OnFunc | OnVar | OnSubscript | OnConstructor, 30)

include/swift/AST/Attr.h

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -215,20 +215,10 @@ class DeclAttribute : public AttributeBase {
215215
enum { NumAccessibilityAttrBits = NumDeclAttrBits + 3 };
216216
static_assert(NumAccessibilityAttrBits <= 32, "fits in an unsigned");
217217

218-
class AutoClosureAttrBitFields {
219-
friend class AutoClosureAttr;
220-
unsigned : NumDeclAttrBits;
221-
222-
unsigned Escaping : 1;
223-
};
224-
enum { NumAutoClosureAttrBits = NumDeclAttrBits + 1 };
225-
static_assert(NumAutoClosureAttrBits <= 32, "fits in an unsigned");
226-
227218
union {
228219
DeclAttrBitFields DeclAttrBits;
229220
ObjCAttrBitFields ObjCAttrBits;
230221
AccessibilityAttrBitFields AccessibilityAttrBits;
231-
AutoClosureAttrBitFields AutoClosureAttrBits;
232222
};
233223

234224
DeclAttribute *Next = nullptr;
@@ -916,24 +906,6 @@ class SetterAccessibilityAttr : public AbstractAccessibilityAttr {
916906
}
917907
};
918908

919-
/// Represents the autoclosure attribute.
920-
class AutoClosureAttr : public DeclAttribute {
921-
public:
922-
AutoClosureAttr(SourceLoc atLoc, SourceRange range, bool escaping,
923-
bool implicit = false)
924-
: DeclAttribute(DAK_AutoClosure, atLoc, range, implicit)
925-
{
926-
AutoClosureAttrBits.Escaping = escaping;
927-
}
928-
929-
/// Determine whether this autoclosure is escaping.
930-
bool isEscaping() const { return AutoClosureAttrBits.Escaping; }
931-
932-
static bool classof(const DeclAttribute *DA) {
933-
return DA->getKind() == DAK_AutoClosure;
934-
}
935-
};
936-
937909
/// Represents an inline attribute.
938910
class InlineAttr : public DeclAttribute {
939911
InlineKind Kind;

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -993,9 +993,6 @@ ERROR(missing_initializer_def,PointsToFirstBadToken,
993993
"initializer requires a body", ())
994994

995995
// Attributes
996-
ERROR(attr_decl_attr_now_on_type,none,
997-
"%0 is now an attribute on a parameter type, "
998-
"instead of on the parameter itself", (StringRef))
999996

1000997
ERROR(operator_not_func,none,
1001998
"operators must be declared with 'func'", ())
@@ -2183,20 +2180,15 @@ ERROR(rethrows_without_throwing_parameter,none,
21832180
"'rethrows' function must take a throwing function argument", ())
21842181

21852182
ERROR(autoclosure_function_type,none,
2186-
"@autoclosure may only be applied to values of function type",
2183+
"@autoclosure attribute only applies to function types",
21872184
())
21882185
ERROR(autoclosure_function_input_nonunit,none,
2189-
"autoclosure argument type must be '()'", ())
2186+
"argument type of @autoclosure parameter must be '()'", ())
21902187

21912188
// FIXME: drop these when we drop @noescape
2192-
ERROR(noescape_function_type,none,
2193-
"@noescape may only be applied to parameters of function type",
2194-
())
21952189
ERROR(noescape_implied_by_autoclosure,none,
21962190
"@noescape is implied by @autoclosure and should not be "
21972191
"redundantly specified", ())
2198-
ERROR(noescape_conflicts_escaping_autoclosure,none,
2199-
"@noescape conflicts with @autoclosure(escaping)", ())
22002192

22012193
ERROR(escaping_non_function_parameter,none,
22022194
"@escaping attribute may only be used in function parameter position", ())

include/swift/Migrator/FixitFilter.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ struct FixitFilter {
111111
Info.ID == diag::function_type_no_parens.ID ||
112112
Info.ID == diag::convert_let_to_var.ID ||
113113
Info.ID == diag::parameter_extraneous_double_up.ID ||
114-
Info.ID == diag::attr_decl_attr_now_on_type.ID ||
115114
Info.ID == diag::noescape_parameter.ID ||
116115
Info.ID == diag::where_inside_brackets.ID ||
117116
Info.ID == diag::selector_construction_suggest.ID ||

include/swift/Serialization/ModuleFormat.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const uint16_t VERSION_MAJOR = 0;
5454
/// in source control, you should also update the comment to briefly
5555
/// describe what change you made. The content of this comment isn't important;
5656
/// it just ensures a conflict if two people change the module format.
57-
const uint16_t VERSION_MINOR = 348; // Last change: 'inout' in parameter decls.
57+
const uint16_t VERSION_MINOR = 349; // Last change: '@autoclosure' and '@noescape' no longer decl attributes.
5858

5959
using DeclID = PointerEmbeddedInt<unsigned, 31>;
6060
using DeclIDField = BCFixed<31>;
@@ -1370,12 +1370,6 @@ namespace decls_block {
13701370

13711371
#undef BC_AVAIL_TUPLE
13721372

1373-
using AutoClosureDeclAttrLayout = BCRecordLayout<
1374-
AutoClosure_DECL_ATTR,
1375-
BCFixed<1>, // implicit flag
1376-
BCFixed<1> // escaping
1377-
>;
1378-
13791373
using ObjCDeclAttrLayout = BCRecordLayout<
13801374
ObjC_DECL_ATTR,
13811375
BCFixed<1>, // implicit flag

lib/AST/Attr.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -413,12 +413,7 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
413413
Printer << ")";
414414
break;
415415
}
416-
case DAK_AutoClosure:
417-
Printer.printAttrName("@autoclosure");
418-
if (cast<AutoClosureAttr>(this)->isEscaping())
419-
Printer << "(escaping)";
420-
break;
421-
416+
422417
case DAK_CDecl:
423418
Printer << "@_cdecl(\"" << cast<CDeclAttr>(this)->Name << "\")";
424419
break;
@@ -568,8 +563,6 @@ StringRef DeclAttribute::getAttrName() const {
568563
return "_semantics";
569564
case DAK_Available:
570565
return "availability";
571-
case DAK_AutoClosure:
572-
return "autoclosure";
573566
case DAK_ObjC:
574567
case DAK_ObjCRuntimeName:
575568
return "objc";

lib/ClangImporter/ImportType.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,15 +1652,13 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
16521652
return nullptr;
16531653

16541654
// Map __attribute__((noescape)) to @noescape.
1655-
bool addNoEscapeAttr = false;
16561655
if (param->hasAttr<clang::NoEscapeAttr>()) {
16571656
Type newParamTy = applyNoEscape(swiftParamTy);
16581657
if (newParamTy.getPointer() != swiftParamTy.getPointer()) {
16591658
swiftParamTy = newParamTy;
1660-
addNoEscapeAttr = true;
16611659
}
16621660
}
1663-
1661+
16641662
// Figure out the name for this parameter.
16651663
Identifier bodyName = importFullName(param, CurrentVersion)
16661664
.getDeclName()
@@ -1681,11 +1679,6 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
16811679
ImportedHeaderUnit);
16821680

16831681
paramInfo->setInterfaceType(swiftParamTy);
1684-
1685-
if (addNoEscapeAttr)
1686-
paramInfo->getAttrs().add(new (SwiftContext)
1687-
NoEscapeAttr(/*IsImplicit=*/false));
1688-
16891682
parameters.push_back(paramInfo);
16901683
++index;
16911684
}
@@ -2123,11 +2116,6 @@ Type ClangImporter::Implementation::importMethodType(
21232116
ImportedHeaderUnit);
21242117
paramInfo->setInterfaceType(dc->mapTypeOutOfContext(swiftParamTy));
21252118

2126-
if (addNoEscapeAttr) {
2127-
paramInfo->getAttrs().add(
2128-
new (SwiftContext) NoEscapeAttr(/*IsImplicit=*/false));
2129-
}
2130-
21312119
// Determine whether we have a default argument.
21322120
if (kind == SpecialMethodKind::Regular ||
21332121
kind == SpecialMethodKind::Constructor) {

lib/Parse/ParseDecl.cpp

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -537,36 +537,6 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,
537537
case DAK_Count:
538538
llvm_unreachable("DAK_Count should not appear in parsing switch");
539539

540-
case DAK_AutoClosure: {
541-
// If we don't have "(escaping", it's just a bare @autoclosure.
542-
if (Tok.isNot(tok::l_paren) || peekToken().getText() != "escaping") {
543-
if (!DiscardAttribute)
544-
Attributes.add(new (Context) AutoClosureAttr(AtLoc, Loc,
545-
/*escaping=*/false));
546-
break;
547-
}
548-
549-
// Consume the '('.
550-
SourceLoc lParenLoc = consumeToken(tok::l_paren);
551-
552-
// Consume the 'escaping'.
553-
(void)consumeToken();
554-
555-
// Parse the closing ')'.
556-
SourceLoc rParenLoc;
557-
parseMatchingToken(tok::r_paren, rParenLoc,
558-
diag::attr_autoclosure_expected_r_paren,
559-
lParenLoc);
560-
561-
// Add the attribute.
562-
if (!DiscardAttribute)
563-
Attributes.add(new (Context) AutoClosureAttr(AtLoc,
564-
SourceRange(Loc, rParenLoc),
565-
/*escaping=*/true));
566-
567-
break;
568-
}
569-
570540
case DAK_RawDocComment:
571541
case DAK_ObjCBridged:
572542
case DAK_ObjCRuntimeName:

lib/Parse/ParsePattern.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,7 @@ mapParsedParameters(Parser &parser,
345345
paramInfo.SpecifierLoc,
346346
argNameLoc, argName,
347347
paramNameLoc, paramName, Type(),
348-
parser.CurDeclContext);
349-
param->getAttrs() = paramInfo.Attrs;
350-
348+
parser.CurDeclContext);
351349
if (argNameLoc.isInvalid() && paramNameLoc.isInvalid())
352350
param->setImplicit();
353351

lib/Sema/TypeCheckAttr.cpp

Lines changed: 0 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,6 @@ class AttributeEarlyChecker : public AttributeVisitor<AttributeEarlyChecker> {
175175
TC.diagnose(attr->getLocation(), diag::alignment_not_power_of_two);
176176
}
177177

178-
void visitAutoClosureAttr(AutoClosureAttr *attr) {
179-
TC.checkAutoClosureAttr(cast<ParamDecl>(D), attr);
180-
}
181-
void visitNoEscapeAttr(NoEscapeAttr *attr) {
182-
TC.checkNoEscapeAttr(cast<ParamDecl>(D), attr);
183-
}
184-
185178
void visitTransparentAttr(TransparentAttr *attr);
186179
void visitMutationAttr(DeclAttribute *attr);
187180
void visitMutatingAttr(MutatingAttr *attr) { visitMutationAttr(attr); }
@@ -745,7 +738,6 @@ class AttributeChecker : public AttributeVisitor<AttributeChecker> {
745738
#define IGNORED_ATTR(CLASS) \
746739
void visit##CLASS##Attr(CLASS##Attr *) {}
747740

748-
IGNORED_ATTR(AutoClosure)
749741
IGNORED_ATTR(Alignment)
750742
IGNORED_ATTR(Convenience)
751743
IGNORED_ATTR(Dynamic)
@@ -760,7 +752,6 @@ class AttributeChecker : public AttributeVisitor<AttributeChecker> {
760752
IGNORED_ATTR(Lazy) // checked early.
761753
IGNORED_ATTR(LLDBDebuggerFunction)
762754
IGNORED_ATTR(Mutating)
763-
IGNORED_ATTR(NoEscape)
764755
IGNORED_ATTR(NonMutating)
765756
IGNORED_ATTR(NonObjC)
766757
IGNORED_ATTR(NoReturn)
@@ -1971,117 +1962,8 @@ void TypeChecker::checkTypeModifyingDeclAttributes(VarDecl *var) {
19711962

19721963
if (auto *attr = var->getAttrs().getAttribute<OwnershipAttr>())
19731964
checkOwnershipAttr(var, attr);
1974-
if (auto *attr = var->getAttrs().getAttribute<AutoClosureAttr>()) {
1975-
if (auto *pd = dyn_cast<ParamDecl>(var))
1976-
checkAutoClosureAttr(pd, attr);
1977-
else {
1978-
AttributeEarlyChecker Checker(*this, var);
1979-
Checker.diagnoseAndRemoveAttr(attr, diag::attr_only_one_decl_kind,
1980-
attr, "parameter");
1981-
}
1982-
}
1983-
if (auto *attr = var->getAttrs().getAttribute<NoEscapeAttr>()) {
1984-
if (auto *pd = dyn_cast<ParamDecl>(var))
1985-
checkNoEscapeAttr(pd, attr);
1986-
else {
1987-
AttributeEarlyChecker Checker(*this, var);
1988-
Checker.diagnoseAndRemoveAttr(attr, diag::attr_only_one_decl_kind,
1989-
attr, "parameter");
1990-
}
1991-
}
1992-
}
1993-
1994-
1995-
1996-
void TypeChecker::checkAutoClosureAttr(ParamDecl *PD, AutoClosureAttr *attr) {
1997-
// The paramdecl should have function type, and we restrict it to functions
1998-
// taking ().
1999-
auto *FTy = PD->getInterfaceType()->getAs<FunctionType>();
2000-
if (!FTy) {
2001-
diagnose(attr->getLocation(), diag::autoclosure_function_type);
2002-
attr->setInvalid();
2003-
return;
2004-
}
2005-
2006-
// Just stop if we've already applied this attribute.
2007-
if (FTy->isAutoClosure())
2008-
return;
2009-
2010-
// This decl attribute has been moved to being a type attribute.
2011-
auto text = attr->isEscaping() ? "@autoclosure @escaping " : "@autoclosure ";
2012-
diagnose(attr->getLocation(), diag::attr_decl_attr_now_on_type,
2013-
"@autoclosure")
2014-
.fixItRemove(attr->getRangeWithAt())
2015-
.fixItInsert(PD->getTypeLoc().getSourceRange().Start, text);
2016-
2017-
auto *FuncTyInput = FTy->getInput()->getAs<TupleType>();
2018-
if (!FuncTyInput || FuncTyInput->getNumElements() != 0) {
2019-
diagnose(attr->getLocation(), diag::autoclosure_function_input_nonunit);
2020-
attr->setInvalid();
2021-
return;
2022-
}
2023-
2024-
// Change the type to include the autoclosure bit.
2025-
PD->setType(
2026-
FTy->withExtInfo(FTy->getExtInfo().withIsAutoClosure(true)));
2027-
2028-
// And the interface type.
2029-
auto *IfaceFTy = PD->getInterfaceType()->getAs<FunctionType>();
2030-
PD->setInterfaceType(
2031-
IfaceFTy->withExtInfo(IfaceFTy->getExtInfo().withIsAutoClosure(true)));
2032-
2033-
// Autoclosure may imply noescape, so add a noescape attribute if this is a
2034-
// function parameter.
2035-
if (auto *NEAttr = PD->getAttrs().getAttribute<NoEscapeAttr>()) {
2036-
// If the parameter has both @noescape and @autoclosure, reject the
2037-
// explicit @noescape.
2038-
if (!NEAttr->isImplicit())
2039-
diagnose(NEAttr->getLocation(),
2040-
attr->isEscaping()
2041-
? diag::noescape_conflicts_escaping_autoclosure
2042-
: diag::noescape_implied_by_autoclosure)
2043-
.fixItRemove(NEAttr->getRange());
2044-
} else if (!attr->isEscaping()) {
2045-
auto *newAttr = new (Context) NoEscapeAttr(/*isImplicit*/true);
2046-
PD->getAttrs().add(newAttr);
2047-
checkNoEscapeAttr(PD, newAttr);
2048-
}
20491965
}
20501966

2051-
void TypeChecker::checkNoEscapeAttr(ParamDecl *PD, NoEscapeAttr *attr) {
2052-
// The paramdecl should have function type.
2053-
auto *FTy = PD->getInterfaceType()->getAs<FunctionType>();
2054-
if (FTy == nullptr) {
2055-
diagnose(attr->getLocation(), diag::noescape_function_type);
2056-
attr->setInvalid();
2057-
return;
2058-
}
2059-
2060-
// This range can be implicit e.g. if we're in the middle of diagnosing
2061-
// @autoclosure.
2062-
auto attrRemovalRange = attr->getRangeWithAt();
2063-
if (attrRemovalRange.isValid()) {
2064-
// Take the attribute, the '@', and the trailing space.
2065-
attrRemovalRange.End = attrRemovalRange.End.getAdvancedLoc(1);
2066-
}
2067-
2068-
// This decl attribute has been moved to being a type attribute.
2069-
if (!attr->isImplicit()) {
2070-
diagnose(attr->getLocation(), diag::attr_decl_attr_now_on_type, "@noescape")
2071-
.fixItRemove(attrRemovalRange)
2072-
.fixItInsert(PD->getTypeLoc().getSourceRange().Start, "@noescape ");
2073-
}
2074-
2075-
// Stop if we've already applied this attribute.
2076-
if (FTy->isNoEscape())
2077-
return;
2078-
2079-
// Change the type to include the noescape bit.
2080-
PD->setType(FunctionType::get(FTy->getInput(), FTy->getResult(),
2081-
FTy->getExtInfo().withNoEscape(true)));
2082-
}
2083-
2084-
20851967
void TypeChecker::checkOwnershipAttr(VarDecl *var, OwnershipAttr *attr) {
20861968
Type type = var->getType();
20871969
Type interfaceType = var->getInterfaceType();

lib/Sema/TypeCheckDecl.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6153,9 +6153,6 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
61536153
UNINTERESTING_ATTR(Specialize)
61546154

61556155
// These can't appear on overridable declarations.
6156-
UNINTERESTING_ATTR(AutoClosure)
6157-
UNINTERESTING_ATTR(NoEscape)
6158-
61596156
UNINTERESTING_ATTR(Prefix)
61606157
UNINTERESTING_ATTR(Postfix)
61616158
UNINTERESTING_ATTR(Infix)

0 commit comments

Comments
 (0)