Skip to content

Commit 25bf069

Browse files
committed
[Legacy parser] No freestanding macros in @abi
SwiftSyntaxParser is already doing this, and we already diagnosed it in Sema anyway, so we’re just moving that diagnostic earlier so the ASTGen testing mode is happy. Also adding compiler tests for it. Macro-related tests are not included in this commit; they require matching swift-syntax changes which are being negotiated.
1 parent f01c0a7 commit 25bf069

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,11 @@ ERROR(attr_unsupported_on_target, none,
15711571
ERROR(attr_name_unsupported_on_target, none,
15721572
"attribute '%0' is unsupported on target '%1'", (StringRef, StringRef))
15731573

1574+
// abi attribute
1575+
ERROR(attr_abi_incompatible_kind,none,
1576+
"cannot use %0 in '@abi'",
1577+
(DescriptiveDeclKind))
1578+
15741579
// availability
15751580
ERROR(attr_availability_platform,none,
15761581
"expected platform name or '*' for '%0' attribute", (StringRef))

lib/Parse/ParseDecl.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3359,9 +3359,18 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
33593359
}
33603360

33613361
if (abiDecl) {
3362-
Attributes.add(new (Context) ABIAttr(abiDecl,
3363-
AtLoc, { Loc, rParenLoc },
3364-
/*implicit=*/false));
3362+
auto attr = new (Context) ABIAttr(abiDecl, AtLoc, { Loc, rParenLoc },
3363+
/*implicit=*/false);
3364+
3365+
// Diagnose syntactically invalid abiDecl kind here to match behavior of
3366+
// Swift parser.
3367+
if (!attr->canAppearOnDecl(abiDecl) && !isa<PatternBindingDecl>(abiDecl)){
3368+
diagnose(abiDecl->getLoc(), diag::attr_abi_incompatible_kind,
3369+
abiDecl->getDescriptiveKind());
3370+
attr->setInvalid();
3371+
}
3372+
3373+
Attributes.add(attr);
33653374
}
33663375

33673376
break;

test/attr/attr_abi.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,7 @@ struct CustomAttrPropertyWrapper {
12851285
}
12861286
12871287
// CustomAttr for attached macro -- see Macros/macro_expand_peers.swift
1288+
// Freestanding macro in @abi -- see Macros/macro_expand.swift
12881289
12891290
// CustomAttr for result builder -- banned in '@abi'
12901291
// Has no ABI impact on either a parameter or a decl.

0 commit comments

Comments
 (0)