Skip to content

Commit 99e45cf

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.
1 parent de63cec commit 99e45cf

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

include/swift/AST/DiagnosticsParse.def

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

1571+
// abi attribute
1572+
ERROR(attr_abi_incompatible_kind,none,
1573+
"cannot use %0 in '@abi'",
1574+
(DescriptiveDeclKind))
1575+
15711576
// availability
15721577
ERROR(attr_availability_platform,none,
15731578
"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
@@ -3357,9 +3357,18 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
33573357
}
33583358

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

33653374
break;

test/Macros/macro_expand.swift

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
// REQUIRES: swift_swift_parser, executable_test
2+
// REQUIRES: swift_feature_ABIAttribute
23

34
// RUN: %empty-directory(%t)
45
// RUN: %host-build-swift -swift-version 5 -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift
56

67
// Diagnostics testing
7-
// RUN: %target-typecheck-verify-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -DTEST_DIAGNOSTICS
8+
// RUN: %target-typecheck-verify-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -DTEST_DIAGNOSTICS -enable-experimental-feature ABIAttribute
89

910
// Diagnostics testing by importing macros from a module
1011
// RUN: %target-swift-frontend -swift-version 5 -emit-module -o %t/freestanding_macro_library.swiftmodule %S/Inputs/freestanding_macro_library.swift -module-name freestanding_macro_library -load-plugin-library %t/%target-library-name(MacroDefinition)
1112
// RUN: %target-swift-frontend -swift-version 5 -emit-module -o %t/freestanding_macro_library_2.swiftmodule %S/Inputs/freestanding_macro_library_2.swift -module-name freestanding_macro_library_2 -load-plugin-library %t/%target-library-name(MacroDefinition) -I %t
1213

13-
// RUN: %target-typecheck-verify-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -DTEST_DIAGNOSTICS -I %t -DIMPORT_MACRO_LIBRARY
14+
// RUN: %target-typecheck-verify-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -DTEST_DIAGNOSTICS -I %t -DIMPORT_MACRO_LIBRARY -enable-experimental-feature ABIAttribute
1415

15-
// RUN: not %target-swift-frontend -swift-version 5 -typecheck -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -DTEST_DIAGNOSTICS -serialize-diagnostics-path %t/macro_expand.dia %s -emit-macro-expansion-files no-diagnostics -Rmacro-loading > %t/macro-printing.txt
16+
// RUN: not %target-swift-frontend -swift-version 5 -typecheck -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -DTEST_DIAGNOSTICS -serialize-diagnostics-path %t/macro_expand.dia %s -emit-macro-expansion-files no-diagnostics -Rmacro-loading > %t/macro-printing.txt -enable-experimental-feature ABIAttribute
1617
// RUN: c-index-test -read-diagnostics %t/macro_expand.dia 2>&1 | %FileCheck -check-prefix CHECK-DIAGS -dump-input=always %s
1718

1819
// RUN: %FileCheck %s --check-prefix CHECK-MACRO-PRINTED < %t/macro-printing.txt
1920

20-
// RUN: not %target-swift-frontend -swift-version 5 -typecheck -diagnostic-style=swift -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -DTEST_DIAGNOSTICS %s > %t/pretty-macro-diagnostics.txt 2>&1
21+
// RUN: not %target-swift-frontend -swift-version 5 -typecheck -diagnostic-style=swift -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -DTEST_DIAGNOSTICS %s -enable-experimental-feature ABIAttribute > %t/pretty-macro-diagnostics.txt 2>&1
2122
// RUN: %FileCheck %s --check-prefix PRETTY-DIAGS < %t/pretty-macro-diagnostics.txt
2223

2324
// Debug info SIL testing
@@ -666,6 +667,29 @@ struct HasNestedType {
666667
#DefineComparableType
667668
}
668669

670+
#if swift(>=1.0) && TEST_DIAGNOSTICS
671+
// Test that macros can't be used in @abi
672+
673+
struct ABIAttrWithFreestandingMacro1 {
674+
// expected-error@+1 {{cannot use pound literal in '@abi'}}
675+
@abi(#varValue)
676+
#varValue
677+
// expected-note@-1 {{in expansion of macro 'varValue' here}}
678+
}
679+
680+
struct ABIAttrWithFreestandingMacro2 {
681+
// expected-error@+1 {{cannot use pound literal in '@abi'}}
682+
@abi(#varValue)
683+
var value: Int { 0 }
684+
}
685+
686+
struct ABIAttrWithFreestandingMacro3 {
687+
@abi(var value: Int)
688+
#varValue
689+
}
690+
691+
#endif
692+
669693
#if TEST_DIAGNOSTICS
670694
@freestanding(expression)
671695
macro missingMacro() = #externalMacro(module: "MacroDefinition", type: "BluhBlah")

test/attr/attr_abi.swift

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

0 commit comments

Comments
 (0)