Skip to content

Commit d4adba2

Browse files
committed
[Macros] Granular diagnostic when macro type is not found in a plugin
rdar://115571427
1 parent 0fe9c92 commit d4adba2

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

tools/swift-plugin-server/Sources/swift-plugin-server/swift-plugin-server.swift

+18-9
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,13 @@ final class SwiftPluginServer {
2626
}
2727
}
2828

29+
struct LoadedLibraryPlugin {
30+
var libraryPath: String
31+
var handle: UnsafeMutableRawPointer
32+
}
33+
2934
/// Loaded dylib handles associated with the module name.
30-
var loadedLibraryPlugins: [String: UnsafeMutableRawPointer] = [:]
35+
var loadedLibraryPlugins: [String: LoadedLibraryPlugin] = [:]
3136

3237
/// Resolved cached macros.
3338
var resolvedMacros: [MacroRef: Macro.Type] = [:]
@@ -50,33 +55,37 @@ extension SwiftPluginServer: PluginProvider {
5055
guard let dlHandle = PluginServer_load(libraryPath, &errorMessage) else {
5156
throw PluginServerError(message: String(cString: errorMessage!))
5257
}
53-
loadedLibraryPlugins[moduleName] = dlHandle
58+
loadedLibraryPlugins[moduleName] = LoadedLibraryPlugin(
59+
libraryPath: libraryPath,
60+
handle: dlHandle
61+
)
5462
}
5563

5664
/// Lookup a loaded macro by a pair of module name and type name.
57-
func resolveMacro(moduleName: String, typeName: String) -> Macro.Type? {
65+
func resolveMacro(moduleName: String, typeName: String) throws -> Macro.Type {
5866
if let resolved = resolvedMacros[.init(moduleName, typeName)] {
5967
return resolved
6068
}
6169

6270
// Find 'dlopen'ed library for the module name.
63-
guard let dlHandle = loadedLibraryPlugins[moduleName] else {
64-
return nil
71+
guard let plugin = loadedLibraryPlugins[moduleName] else {
72+
// NOTE: This should be unreachable. Compiler should not use this server
73+
// unless the plugin loading succeeded.
74+
throw PluginServerError(message: "(plugin-server) plugin not loaded for module '\(moduleName)'")
6575
}
6676

6777
// Lookup the type metadata.
6878
var errorMessage: UnsafePointer<CChar>?
6979
guard let macroTypePtr = PluginServer_lookupMacroTypeMetadataByExternalName(
70-
moduleName, typeName, dlHandle, &errorMessage
80+
moduleName, typeName, plugin.handle, &errorMessage
7181
) else {
72-
// FIXME: Propagate error message?
73-
return nil
82+
throw PluginServerError(message: "macro implementation type '\(moduleName).\(typeName)' could not be found in library plugin '\(plugin.libraryPath)'")
7483
}
7584

7685
// THe type must be a 'Macro' type.
7786
let macroType = unsafeBitCast(macroTypePtr, to: Any.Type.self)
7887
guard let macro = macroType as? Macro.Type else {
79-
return nil
88+
throw PluginServerError(message: "type '\(moduleName).\(typeName)' is not a valid macro implementation type in library plugin '\(plugin.libraryPath)'")
8089
}
8190

8291
// Cache the resolved type.

0 commit comments

Comments
 (0)