Skip to content

Commit d3574a3

Browse files
committed
[Macros] Granular diagnostics when macro type is not found in a plugin
rdar://115571427
1 parent 9f09285 commit d3574a3

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

Sources/SwiftCompilerPlugin/CompilerPlugin.swift

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public protocol CompilerPlugin {
6363
}
6464

6565
extension CompilerPlugin {
66-
func resolveMacro(moduleName: String, typeName: String) -> Macro.Type? {
66+
func resolveMacro(moduleName: String, typeName: String) throws -> Macro.Type {
6767
let qualifedName = "\(moduleName).\(typeName)"
6868

6969
for type in providingMacros {
@@ -74,12 +74,14 @@ extension CompilerPlugin {
7474
return type
7575
}
7676
}
77-
return nil
77+
78+
let pluginPath = CommandLine.arguments.first ?? Bundle.main.executablePath ?? ProcessInfo.processInfo.processName
79+
throw CompilerPluginError(message: "macro implementation type '\(moduleName).\(typeName)' could not be found in executable plugin '\(pluginPath)'")
7880
}
7981

8082
// @testable
8183
public func _resolveMacro(moduleName: String, typeName: String) -> Macro.Type? {
82-
resolveMacro(moduleName: moduleName, typeName: typeName)
84+
try? resolveMacro(moduleName: moduleName, typeName: typeName)
8385
}
8486
}
8587

@@ -88,8 +90,8 @@ struct MacroProviderAdapter<Plugin: CompilerPlugin>: PluginProvider {
8890
init(plugin: Plugin) {
8991
self.plugin = plugin
9092
}
91-
func resolveMacro(moduleName: String, typeName: String) -> Macro.Type? {
92-
plugin.resolveMacro(moduleName: moduleName, typeName: typeName)
93+
func resolveMacro(moduleName: String, typeName: String) throws -> Macro.Type {
94+
try plugin.resolveMacro(moduleName: moduleName, typeName: typeName)
9395
}
9496
}
9597

@@ -242,3 +244,10 @@ private extension FileHandle {
242244
}
243245
}
244246
}
247+
248+
struct CompilerPluginError: Error, CustomStringConvertible {
249+
var description: String
250+
init(message: String) {
251+
self.description = message
252+
}
253+
}

Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public enum PluginFeature: String {
2020
/// A type that provides the actual plugin functions.
2121
public protocol PluginProvider {
2222
/// Resolve macro type by the module name and the type name.
23-
func resolveMacro(moduleName: String, typeName: String) -> Macro.Type?
23+
func resolveMacro(moduleName: String, typeName: String) throws -> Macro.Type
2424

2525
/// Load dynamic link library at `libraryPath`. Implementations can use
2626
/// `moduleName` to associate the loaded library with it.

Sources/SwiftCompilerPluginMessageHandling/Macros.swift

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import SwiftSyntaxMacros
1919

2020
extension CompilerPluginMessageHandler {
2121
/// Get concrete macro type from a pair of module name and type name.
22-
private func resolveMacro(_ ref: PluginMessage.MacroReference) -> Macro.Type? {
23-
provider.resolveMacro(moduleName: ref.moduleName, typeName: ref.typeName)
22+
private func resolveMacro(_ ref: PluginMessage.MacroReference) throws -> Macro.Type {
23+
try provider.resolveMacro(moduleName: ref.moduleName, typeName: ref.typeName)
2424
}
2525

2626
/// Expand `@freestainding(XXX)` macros.
@@ -43,10 +43,7 @@ extension CompilerPluginMessageHandler {
4343
guard let macroSyntax = syntax.asProtocol(FreestandingMacroExpansionSyntax.self) else {
4444
throw MacroExpansionError.freestandingMacroSyntaxIsNotMacro
4545
}
46-
guard let macroDefinition = resolveMacro(macro) else {
47-
throw MacroExpansionError.macroTypeNotFound(macro)
48-
}
49-
46+
let macroDefinition = try resolveMacro(macro)
5047
let macroRole: MacroRole
5148
if let pluginMacroRole {
5249
macroRole = MacroRole(messageMacroRole: pluginMacroRole)
@@ -113,9 +110,7 @@ extension CompilerPluginMessageHandler {
113110
// TODO: Make this a 'String?' and remove non-'hasExpandMacroResult' branches.
114111
let expandedSources: [String]?
115112
do {
116-
guard let macroDefinition = resolveMacro(macro) else {
117-
throw MacroExpansionError.macroTypeNotFound(macro)
118-
}
113+
let macroDefinition = try resolveMacro(macro)
119114
let role = MacroRole(messageMacroRole: macroRole)
120115

121116
let expansions = SwiftSyntaxMacroExpansion.expandAttachedMacroWithoutCollapsing(

0 commit comments

Comments
 (0)