Skip to content

Commit b3f9e08

Browse files
authored
Merge pull request #71237 from ahoppen/ahoppen/attribute-parser-changes
Disallow spaces between `@`, attribute name and `(` in Swift 6
2 parents 65eaf95 + c6e425a commit b3f9e08

File tree

7 files changed

+149
-54
lines changed

7 files changed

+149
-54
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,13 @@ ERROR(type_attribute_applied_to_decl,none,
15141514
ERROR(decl_attribute_applied_to_type,none,
15151515
"attribute can only be applied to declarations, not types", ())
15161516

1517+
1518+
ERROR(attr_extra_whitespace_after_at,PointsToFirstBadToken,
1519+
"extraneous whitespace between '@' and attribute name", ())
1520+
1521+
ERROR(attr_extra_whitespace_before_lparen,PointsToFirstBadToken,
1522+
"extraneous whitespace between attribute name and '('", ())
1523+
15171524
ERROR(attr_expected_lparen,none,
15181525
"expected '(' in '%0' %select{attribute|modifier}1", (StringRef, bool))
15191526

include/swift/Parse/Parser.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,18 @@ class Parser {
602602
return true;
603603
}
604604

605+
/// Consume a '('. If it is not directly following the previous token, emit an
606+
/// error (Swift 6) or warning (Swift <6) that attribute name and parentheses
607+
/// must not be separated by a space.
608+
SourceLoc consumeAttributeLParen();
609+
610+
/// If the next token is a '(' that's not on a new line consume it, and error
611+
/// (Swift 6) or warn (Swift <6) that the attribute must not be separted from
612+
/// the '(' by a space.
613+
///
614+
/// If the next token is not '(' or it's on a new line, return false.
615+
bool consumeIfAttributeLParen();
616+
605617
bool consumeIfNotAtStartOfLine(tok K) {
606618
if (Tok.isAtStartOfLine()) return false;
607619
return consumeIf(K);
@@ -1150,6 +1162,7 @@ class Parser {
11501162

11511163
/// Parse a specific attribute.
11521164
ParserStatus parseDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,
1165+
SourceLoc AtEndLoc,
11531166
PatternBindingInitializer *&initContext,
11541167
bool isFromClangAttribute = false);
11551168

@@ -1259,6 +1272,7 @@ class Parser {
12591272
bool justChecking);
12601273

12611274
ParserStatus parseTypeAttribute(TypeOrCustomAttr &result, SourceLoc AtLoc,
1275+
SourceLoc AtEndLoc,
12621276
PatternBindingInitializer *&initContext,
12631277
bool justChecking = false);
12641278

lib/ClangImporter/ImportDecl.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8053,11 +8053,14 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
80538053
parser.consumeTokenWithoutFeedingReceiver();
80548054

80558055
bool hadError = false;
8056-
SourceLoc atLoc;
8057-
if (parser.consumeIf(tok::at_sign, atLoc)) {
8058-
hadError = parser.parseDeclAttribute(
8059-
MappedDecl->getAttrs(), atLoc, initContext,
8060-
/*isFromClangAttribute=*/true).isError();
8056+
if (parser.Tok.is(tok::at_sign)) {
8057+
SourceLoc atEndLoc = parser.Tok.getRange().getEnd();
8058+
SourceLoc atLoc = parser.consumeToken(tok::at_sign);
8059+
hadError = parser
8060+
.parseDeclAttribute(MappedDecl->getAttrs(), atLoc,
8061+
atEndLoc, initContext,
8062+
/*isFromClangAttribute=*/true)
8063+
.isError();
80618064
} else {
80628065
SourceLoc staticLoc;
80638066
StaticSpellingKind staticSpelling;

0 commit comments

Comments
 (0)