Skip to content

Commit 7d67fc5

Browse files
committed
Added fix-it for declaration attributes being applied to parameter types
1 parent 5da0906 commit 7d67fc5

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lib/Parse/ParsePattern.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,24 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc,
244244

245245
// (':' type)?
246246
if (consumeIf(tok::colon)) {
247+
// Check if token is @ sign ergo an attribute
248+
if (Tok.is(tok::at_sign)) {
249+
Token nextToken = peekToken();
250+
// Check if attribute is invalid type attribute
251+
// and actually a declaration attribute
252+
TypeAttrKind TK = TypeAttributes::getAttrKindFromString(nextToken.getText());
253+
DeclAttrKind DK = DeclAttribute::getAttrKindFromString(nextToken.getText());
254+
if ((TK == TAK_Count || (TK == TAK_noescape && !isInSILMode()))
255+
&& DK != DAK_Count
256+
&& DeclAttribute::getOptions(DK) & AccessibilityAttr::OnParam) {
257+
SourceLoc AtLoc = consumeToken(tok::at_sign);
258+
SourceLoc AttrLoc = consumeToken(tok::identifier);
259+
diagnose(AtLoc, diag::decl_attribute_applied_to_type)
260+
.fixItRemove(SourceRange(AtLoc, AttrLoc))
261+
.fixItInsert(StartLoc, "@" + nextToken.getText().str()+" ");
262+
}
263+
}
264+
247265
auto type = parseType(diag::expected_parameter_type);
248266
status |= type;
249267
param.Type = type.getPtrOrNull();

test/attr/attr_noescape.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
@noescape var fn : () -> Int = { 4 } // expected-error {{@noescape may only be used on 'parameter' declarations}} {{1-11=}}
44

5+
func appliedToType(g: @noescape ()->Void) { g() } // expected-error {{attribute can only be applied to declarations, not types}} {{20-20=@noescape }} {{23-33=}}
6+
57
func doesEscape(fn : () -> Int) {}
68

79
func takesGenericClosure<T>(a : Int, @noescape _ fn : () -> T) {}

0 commit comments

Comments
 (0)