Skip to content

Commit cd62c6d

Browse files
committed
[Macros] Fix a few issues with invalid macro expansion diagnostics, and add tests.
1 parent e417205 commit cd62c6d

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6910,7 +6910,7 @@ ERROR(external_macro_arg_not_type_name,none,
69106910
ERROR(attached_declaration_macro_not_supported,none,
69116911
"attached declaration macros are not yet supported", ())
69126912
ERROR(invalid_decl_in_macro_expansion,none,
6913-
"macro expansion cannot introduce %1",
6913+
"macro expansion cannot introduce %0",
69146914
(DescriptiveDeclKind))
69156915
ERROR(invalid_main_type_in_macro_expansion,none,
69166916
"macro expansion cannot introduce '@main' type'",

lib/Sema/TypeCheckMacros.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,13 +512,20 @@ static void validateMacroExpansion(SourceFile *expansionBuffer,
512512
isa<ExtensionDecl>(decl)) {
513513
decl->diagnose(diag::invalid_decl_in_macro_expansion,
514514
decl->getDescriptiveKind());
515+
decl->setInvalid();
516+
517+
if (auto *extension = dyn_cast<ExtensionDecl>(decl)) {
518+
extension->setExtendedNominal(nullptr);
519+
}
520+
521+
continue;
515522
}
516523

517524
// Diagnose `@main` types.
518-
if (auto *typeDecl = dyn_cast<TypeDecl>(decl)) {
519-
if (typeDecl->getAttrs().hasAttribute<MainTypeAttr>()) {
520-
typeDecl->diagnose(diag::invalid_main_type_in_macro_expansion);
521-
}
525+
if (auto *mainAttr = decl->getAttrs().getAttribute<MainTypeAttr>()) {
526+
ctx.Diags.diagnose(mainAttr->getLocation(),
527+
diag::invalid_main_type_in_macro_expansion);
528+
mainAttr->setInvalid();
522529
}
523530

524531
// Diagnose default literal type overrides.

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,27 @@ public struct AddCompletionHandler: PeerMacro {
764764
}
765765
}
766766

767+
public struct InvalidMacro: PeerMacro {
768+
public static func expansion(
769+
of node: AttributeSyntax,
770+
providingPeersOf declaration: some DeclSyntaxProtocol,
771+
in context: some MacroExpansionContext
772+
) throws -> [DeclSyntax] {
773+
return [
774+
"import Swift",
775+
"precedencegroup MyPrecedence {}",
776+
"@attached(member) macro myMacro()",
777+
"extension Int {}",
778+
"""
779+
@main
780+
struct MyMain {
781+
static func main() {}
782+
}
783+
""",
784+
]
785+
}
786+
}
787+
767788
public struct WrapInType: PeerMacro {
768789
public static func expansion(
769790
of node: AttributeSyntax,

test/Macros/macro_expand.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,39 @@
2626
// FIXME: Swift parser is not enabled on Linux CI yet.
2727
// REQUIRES: OS=macosx
2828

29+
#if TEST_DIAGNOSTICS
30+
@attached(peer)
31+
macro Invalid() = #externalMacro(module: "MacroDefinition", type: "InvalidMacro")
32+
33+
@Invalid
34+
struct Bad {}
35+
// expected-note@-1 7 {{in expansion of macro 'Invalid' here}}
36+
37+
// CHECK-DIAGS: error: macro expansion cannot introduce import
38+
// CHECK-DIAGS: CONTENTS OF FILE @__swiftmacro_9MacroUser3BadV7InvalidfMp_.swift
39+
// CHECK-DIAGS: import Swift
40+
41+
// CHECK-DIAGS: error: macro expansion cannot introduce precedence group
42+
// CHECK-DIAGS: CONTENTS OF FILE @__swiftmacro_9MacroUser3BadV7InvalidfMp_.swift
43+
// CHECK-DIAGS: precedencegroup MyPrecedence {}
44+
45+
// CHECK-DIAGS: error: macro expansion cannot introduce macro
46+
// CHECK-DIAGS: CONTENTS OF FILE @__swiftmacro_9MacroUser3BadV7InvalidfMp_.swift
47+
// CHECK-DIAGS: @attached(member) macro myMacro()
48+
49+
// CHECK-DIAGS: error: macro expansion cannot introduce extension
50+
// CHECK-DIAGS: CONTENTS OF FILE @__swiftmacro_9MacroUser3BadV7InvalidfMp_.swift
51+
// CHECK-DIAGS: extension Int {}
52+
53+
// CHECK-DIAGS: error: macro expansion cannot introduce '@main' type'
54+
// CHECK-DIAGS: CONTENTS OF FILE @__swiftmacro_9MacroUser3BadV7InvalidfMp_.swift
55+
// CHECK-DIAGS: @main
56+
57+
// CHECK-DIAGS: error: declaration name 'MyMain' is not covered by macro 'Invalid'
58+
// CHECK-DIAGS: CONTENTS OF FILE @__swiftmacro_9MacroUser3BadV7InvalidfMp_.swift
59+
// CHECK-DIAGS: struct MyMain {
60+
#endif
61+
2962
@freestanding(expression) macro customFileID() -> String = #externalMacro(module: "MacroDefinition", type: "FileIDMacro")
3063
@freestanding(expression) macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
3164
@freestanding(expression) macro fileID<T: ExpressibleByStringLiteral>() -> T = #externalMacro(module: "MacroDefinition", type: "FileIDMacro")

0 commit comments

Comments
 (0)