Skip to content

Commit 7af29e4

Browse files
committed
reflection: enable Base._which with non-base Compiler.MethodTableView (#59915)
When packages using non-Base compilers call `Base._which`, a method error occurs due to module context mismatch. Like other reflection functions, the module context of `mt::Compiler.MethodTableView` needs to be properly determined.
1 parent d698a0e commit 7af29e4

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

base/reflection.jl

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,14 +261,29 @@ function invoke_interp_compiler(interp, fname::Symbol, args...)
261261
T = typeof(interp)
262262
while true
263263
Tname = typename(T).name
264-
Tname === :Any && error("Expected Interpreter")
264+
Tname === :Any && error("Expected AbstractInterpreter")
265265
Tname === :AbstractInterpreter && break
266266
T = supertype(T)
267267
end
268268
return getglobal(typename(T).module, fname)(args...)
269269
end
270270
end
271271

272+
function invoke_mt_compiler(mt, fname::Symbol, args...)
273+
if mt === nothing
274+
return invoke_default_compiler(fname, args...)
275+
else
276+
T = typeof(mt)
277+
while true
278+
Tname = typename(T).name
279+
Tname === :Any && error("Expected MethodTableView")
280+
Tname === :MethodTableView && break
281+
T = supertype(T)
282+
end
283+
return getglobal(typename(T).module, fname)(args...)
284+
end
285+
end
286+
272287
"""
273288
code_typed_by_type(types::Type{<:Tuple}; ...)
274289
@@ -870,7 +885,7 @@ function _which(@nospecialize(tt::Type);
870885
world::UInt=get_world_counter(),
871886
raise::Bool=true)
872887
world == typemax(UInt) && error("code reflection cannot be used from generated functions")
873-
match, = invoke_default_compiler(:findsup_mt, tt, world, method_table)
888+
match, = invoke_mt_compiler(method_table, :findsup_mt, tt, world, method_table)
874889
if match === nothing
875890
raise && error("no unique matching method found for the specified argument types")
876891
return nothing

0 commit comments

Comments
 (0)