Skip to content

Commit 4b1c507

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 cf7938a commit 4b1c507

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
@@ -1562,6 +1562,11 @@ ERROR(attr_name_close_match, none,
15621562
ERROR(attr_unsupported_on_target, none,
15631563
"attribute '%0' is unsupported on target '%1'", (StringRef, StringRef))
15641564

1565+
// abi attribute
1566+
ERROR(attr_abi_incompatible_kind,none,
1567+
"cannot use %0 in '@abi'",
1568+
(DescriptiveDeclKind))
1569+
15651570
// availability
15661571
ERROR(attr_availability_platform,none,
15671572
"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
@@ -648,6 +649,29 @@ struct HasNestedType {
648649
#DefineComparableType
649650
}
650651

652+
#if swift(>=1.0) && TEST_DIAGNOSTICS
653+
// Test that macros can't be used in @abi
654+
655+
struct ABIAttrWithFreestandingMacro1 {
656+
// expected-error@+1 {{cannot use pound literal in '@abi'}}
657+
@abi(#varValue)
658+
#varValue
659+
// expected-note@-1 {{in expansion of macro 'varValue' here}}
660+
}
661+
662+
struct ABIAttrWithFreestandingMacro2 {
663+
// expected-error@+1 {{cannot use pound literal in '@abi'}}
664+
@abi(#varValue)
665+
var value: Int { 0 }
666+
}
667+
668+
struct ABIAttrWithFreestandingMacro3 {
669+
@abi(var value: Int)
670+
#varValue
671+
}
672+
673+
#endif
674+
651675
#if TEST_DIAGNOSTICS
652676
@freestanding(expression)
653677
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)