Skip to content

Commit 9ac257b

Browse files
committed
[Macros] Diagnose conformance macros as being replaced by extension macros.
1 parent 178604e commit 9ac257b

File tree

5 files changed

+18
-10
lines changed

5 files changed

+18
-10
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7277,6 +7277,9 @@ ERROR(extension_macro_invalid_conformance,none,
72777277
ERROR(macro_attached_to_invalid_decl,none,
72787278
"'%0' macro cannot be attached to %1",
72797279
(StringRef, DescriptiveDeclKind))
7280+
ERROR(conformance_macro,none,
7281+
"conformance macros are replaced by extension macros",
7282+
())
72807283

72817284
ERROR(macro_resolve_circular_reference, none,
72827285
"circular reference resolving %select{freestanding|attached}0 macro %1",

lib/Sema/TypeCheckAttr.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7152,9 +7152,9 @@ void AttributeChecker::visitMacroRoleAttr(MacroRoleAttr *attr) {
71527152
case MacroRole::Peer:
71537153
break;
71547154
case MacroRole::Conformance:
7155-
if (!attr->getNames().empty())
7156-
diagnoseAndRemoveAttr(attr, diag::macro_cannot_introduce_names,
7157-
getMacroRoleString(attr->getMacroRole()));
7155+
diagnoseAndRemoveAttr(attr, diag::conformance_macro)
7156+
.fixItReplace(attr->getRange(),
7157+
"@attached(extension, conformances: <#Protocol#>)");
71587158
break;
71597159
case MacroRole::Extension:
71607160
break;

test/Macros/macro_expand_conformances.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@
1414
// RUN: %target-codesign %t/main
1515
// RUN: %target-run %t/main | %FileCheck %s
1616

17-
@attached(conformance)
17+
#if TEST_DIAGNOSTICS
18+
@attached(conformance) // expected-error{{conformance macros are replaced by extension macros}}
19+
macro InvalidEquatable() = #externalMacro(module: "MacroDefinition", type: "EquatableMacro")
20+
#endif
21+
22+
@attached(extension, conformances: Equatable)
1823
macro Equatable() = #externalMacro(module: "MacroDefinition", type: "EquatableMacro")
1924

20-
@attached(conformance)
25+
@attached(extension, conformances: Hashable)
2126
macro Hashable() = #externalMacro(module: "MacroDefinition", type: "HashableMacro")
2227

2328
#if MODULE_EXPORTING_TYPE
@@ -52,7 +57,7 @@ enum E {
5257
@Equatable struct Nested {}
5358
}
5459

55-
// CHECK-DUMP: @__swiftmacro_25macro_expand_conformances1S9EquatablefMc_.swift
60+
// CHECK-DUMP: @__swiftmacro_25macro_expand_conformances1S9EquatablefMe_.swift
5661
// CHECK-DUMP: extension S: Equatable {
5762
// CHECK-DUMP: }
5863

@@ -77,7 +82,7 @@ requireHashable(PublicEquatable())
7782
//expected-error@-1{{global function 'requireHashable' requires that 'PublicEquatable' conform to 'Hashable'}}
7883
#endif
7984

80-
@attached(conformance)
85+
@attached(extension, conformances: P)
8186
@attached(member, names: named(requirement))
8287
macro DelegatedConformance() = #externalMacro(module: "MacroDefinition", type: "DelegatedConformanceMacro")
8388

@@ -94,7 +99,7 @@ struct Wrapped: P {
9499
@DelegatedConformance
95100
struct Generic<Element> {}
96101

97-
// CHECK-DUMP: @__swiftmacro_25macro_expand_conformances7Generic20DelegatedConformancefMc_.swift
102+
// CHECK-DUMP: @__swiftmacro_25macro_expand_conformances7Generic20DelegatedConformancefMe_.swift
98103
// CHECK-DUMP: extension Generic: P where Element: P {
99104
// CHECK-DUMP: }
100105

test/Macros/option_set.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import Swift
66

77
@attached(member, names: named(RawValue), named(rawValue), named(`init`), arbitrary)
8-
@attached(conformance)
8+
@attached(extension, conformances: OptionSet)
99
public macro OptionSet<RawType>() =
1010
#externalMacro(module: "SwiftMacros", type: "OptionSetMacro")
1111

test/Macros/top_level_freestanding.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ macro Empty<T>(_ closure: () -> T) = #externalMacro(module: "MacroDefinition", t
117117
S(a: 10, b: 10)
118118
}
119119

120-
@attached(conformance)
120+
@attached(extension, conformances: Initializable)
121121
@attached(member, names: named(init))
122122
macro Initializable() = #externalMacro(module: "MacroDefinition", type: "InitializableMacro")
123123

0 commit comments

Comments
 (0)