Skip to content

Commit a39bb0b

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 3991949 commit a39bb0b

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
@@ -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
@@ -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
@@ -685,6 +686,29 @@ func testLocalAccessorMacroWithAutoclosure() {
685686
takesAutoclosure(1)
686687
}
687688

689+
#if swift(>=1.0) && TEST_DIAGNOSTICS
690+
// Test that macros can't be used in @abi
691+
692+
struct ABIAttrWithFreestandingMacro1 {
693+
// expected-error@+1 {{cannot use pound literal in '@abi'}}
694+
@abi(#varValue)
695+
#varValue
696+
// expected-note@-1 {{in expansion of macro 'varValue' here}}
697+
}
698+
699+
struct ABIAttrWithFreestandingMacro2 {
700+
// expected-error@+1 {{cannot use pound literal in '@abi'}}
701+
@abi(#varValue)
702+
var value: Int { 0 }
703+
}
704+
705+
struct ABIAttrWithFreestandingMacro3 {
706+
@abi(var value: Int)
707+
#varValue
708+
}
709+
710+
#endif
711+
688712
#if TEST_DIAGNOSTICS
689713
@freestanding(expression)
690714
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
@@ -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)