@@ -12,13 +12,48 @@ public enum MacroRole {
12
12
case codeItem
13
13
}
14
14
15
+ extension MacroRole {
16
+ var protocolName : String {
17
+ switch self {
18
+ case . expression: return " ExpressionMacro "
19
+ case . declaration: return " DeclarationMacro "
20
+ case . accessor: return " AccessorMacro "
21
+ case . memberAttribute: return " MemberAttributeMacro "
22
+ case . member: return " MemberMacro "
23
+ case . peer: return " PeerMacro "
24
+ case . conformance: return " ConformanceMacro "
25
+ case . codeItem: return " CodeItemMacro "
26
+ }
27
+ }
28
+ }
29
+
15
30
/// Simple diagnostic message
16
- private enum MacroExpansionError : String , Error , CustomStringConvertible {
17
- case unmathedMacroRole = " macro doesn't conform to required macro role "
18
- case parentDeclGroupNil = " parent decl group is nil "
19
- case declarationNotDeclGroup = " declaration is not a decl group syntax "
20
- case declarationNotIdentified = " declaration is not a 'Identified' syntax "
21
- var description : String { self . rawValue }
31
+ private enum MacroExpansionError : Error , CustomStringConvertible {
32
+ case unmatchedMacroRole( Macro . Type , MacroRole )
33
+ case parentDeclGroupNil
34
+ case declarationNotDeclGroup
35
+ case declarationNotIdentified
36
+ case noFreestandingMacroRoles( Macro . Type )
37
+
38
+ var description : String {
39
+ switch self {
40
+ case . unmatchedMacroRole( let type, let role) :
41
+ return " macro implementation type ' \( type) ' doesn't conform to required protocol ' \( role. protocolName) ' "
42
+
43
+ case . parentDeclGroupNil:
44
+ return " parent decl group is nil "
45
+
46
+ case . declarationNotDeclGroup:
47
+ return " declaration is not a decl group syntax "
48
+
49
+ case . declarationNotIdentified:
50
+ return " declaration is not a 'Identified' syntax "
51
+
52
+ case . noFreestandingMacroRoles( let type) :
53
+ return " macro implementation type ' \( type) ' does not conform to any freestanding macro protocol "
54
+
55
+ }
56
+ }
22
57
}
23
58
24
59
/// Expand `@freestanding(XXX)` macros.
@@ -66,11 +101,9 @@ public func expandFreestandingMacro(
66
101
let rewritten = try codeItemMacroDef. expansion ( of: node, in: context)
67
102
expandedSyntax = Syntax ( CodeBlockItemListSyntax ( rewritten) )
68
103
69
- case ( . expression, _) , ( . declaration, _) , ( . codeItem, _) :
70
- throw MacroExpansionError . unmathedMacroRole
71
-
72
- case ( . accessor, _) , ( . memberAttribute, _) , ( . member, _) , ( . peer, _) , ( . conformance, _) , ( . codeItem, _) :
73
- fatalError ( " macro role \( macroRole) is not a freestanding macro " )
104
+ case ( . accessor, _) , ( . memberAttribute, _) , ( . member, _) , ( . peer, _) , ( . conformance, _) , ( . expression, _) , ( . declaration, _) ,
105
+ ( . codeItem, _) :
106
+ throw MacroExpansionError . unmatchedMacroRole ( definition, macroRole)
74
107
}
75
108
return expandedSyntax. formattedExpansion ( definition. formatMode)
76
109
}
@@ -91,7 +124,7 @@ public func inferFreestandingMacroRole(definition: Macro.Type) throws -> MacroRo
91
124
case is CodeItemMacro . Type : return . codeItem
92
125
93
126
default :
94
- throw MacroExpansionError . unmathedMacroRole
127
+ throw MacroExpansionError . noFreestandingMacroRoles ( definition )
95
128
}
96
129
}
97
130
@@ -251,7 +284,7 @@ public func expandAttachedMacro<Context: MacroExpansionContext>(
251
284
}
252
285
253
286
default :
254
- throw MacroExpansionError . unmathedMacroRole
287
+ throw MacroExpansionError . unmatchedMacroRole ( definition , macroRole )
255
288
}
256
289
} catch {
257
290
context. addDiagnostics ( from: error, node: attributeNode)
0 commit comments