diff --git a/base/loading.jl b/base/loading.jl index eebadb92928f4..cdd66414c8dae 100644 --- a/base/loading.jl +++ b/base/loading.jl @@ -2284,20 +2284,20 @@ precompile(mi::Core.MethodInstance, world::UInt=get_world_counter()) = (ccall(:jl_compile_method_instance, Cvoid, (Any, Any, UInt), mi, C_NULL, world); return true) """ - precompile(m::Method, argtypes::Tuple{Vararg{Any}}) + precompile(f, argtypes::Tuple{Vararg{Any}}, m::Method) Precompile a specific method for the given argument types. This may be used to precompile -a different method than the one that would ordinarily be chosen by dispatch. +a different method than the one that would ordinarily be chosen by dispatch, for example +mimicking `invoke`. """ -function precompile(m::Method, @nospecialize(argtypes::Tuple)) +function precompile(@nospecialize(f), @nospecialize(argtypes::Tuple), m::Method) world = get_world_counter() - f = getglobal(m.module, m.name) matches = _methods(f, argtypes, -1, world)::Vector for mtch in matches mtch = mtch::Core.MethodMatch if mtch.method == m mi = Core.Compiler.specialize_method(m, mtch.spec_types, mtch.sparams) - return precompile(mi) + return precompile(mi, world) end end return false diff --git a/test/precompile.jl b/test/precompile.jl index 02936bd1e838f..7b3a3184bac08 100644 --- a/test/precompile.jl +++ b/test/precompile.jl @@ -972,7 +972,7 @@ precompile_test_harness("invoke") do dir invokeme(x) = 1 invokeme(::Int) = 2 m_any, m_int = sort(collect(methods(invokeme)); by=m->m.line) - precompile(m_any, (Int,)) + precompile(invokeme, (Int,), m_any) @test m_any.specializations[1].specTypes === Tuple{typeof(invokeme), Int} @test isempty(m_int.specializations) end