Skip to content

Commit 2fdc875

Browse files
committed
Merge pull request #1088 from SquaredTiki/sr215-fix
[Parser] Fix-it for declaration attributes being applied to parameter types (SR-215)
2 parents deac756 + c4ee22c commit 2fdc875

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
@@ -259,6 +259,24 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc,
259259
if (Tok.is(tok::colon)) {
260260
param.ColonLoc = consumeToken();
261261

262+
// Check if token is @ sign ergo an attribute
263+
if (Tok.is(tok::at_sign)) {
264+
Token nextToken = peekToken();
265+
// Check if attribute is invalid type attribute
266+
// and actually a declaration attribute
267+
TypeAttrKind TK = TypeAttributes::getAttrKindFromString(nextToken.getText());
268+
DeclAttrKind DK = DeclAttribute::getAttrKindFromString(nextToken.getText());
269+
if ((TK == TAK_Count || (TK == TAK_noescape && !isInSILMode()))
270+
&& DK != DAK_Count
271+
&& DeclAttribute::getOptions(declKind) & OnParam) {
272+
SourceLoc AtLoc = consumeToken(tok::at_sign);
273+
SourceLoc AttrLoc = consumeToken(tok::identifier);
274+
diagnose(AtLoc, diag::decl_attribute_applied_to_type)
275+
.fixItRemove(SourceRange(AtLoc, AttrLoc))
276+
.fixItInsert(StartLoc, "@" + nextToken.getText().str()+" ");
277+
}
278+
}
279+
262280
auto type = parseType(diag::expected_parameter_type);
263281
status |= type;
264282
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)