From 548598ec26283fc1c026b8fc25570b8512eb2347 Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Mon, 29 Aug 2022 14:11:35 -0500 Subject: [PATCH] Allow redefinition of `invoke`d methods with new signatures (fixes #46503) --- src/gf.c | 10 +++++----- test/core.jl | 7 +++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/gf.c b/src/gf.c index 907adfea43dae..abc04f2e779ee 100644 --- a/src/gf.c +++ b/src/gf.c @@ -1867,14 +1867,14 @@ JL_DLLEXPORT void jl_method_table_insert(jl_methtable_t *mt, jl_method_t *method // invoke-dispatch, check invokeTypes for validity struct jl_typemap_assoc search = {invokeTypes, method->primary_world, NULL, 0, ~(size_t)0}; oldentry = jl_typemap_assoc_by_type(jl_atomic_load_relaxed(&mt->defs), &search, /*offs*/0, /*subtype*/0); - assert(oldentry); - if (oldentry->func.method == mi->def.method) { + if (oldentry && oldentry->func.method == mi->def.method) { + // We can safely keep this method jl_array_ptr_set(backedges, insb++, invokeTypes); jl_array_ptr_set(backedges, insb++, caller); - continue; + } else { + invalidate_method_instance(&do_nothing_with_codeinst, caller, max_world, 1); + invalidated = 1; } - invalidate_method_instance(&do_nothing_with_codeinst, caller, max_world, 1); - invalidated = 1; } } jl_array_del_end(backedges, nb - insb); diff --git a/test/core.jl b/test/core.jl index a0bd78df8e1ce..88635cf99bda1 100644 --- a/test/core.jl +++ b/test/core.jl @@ -7809,6 +7809,13 @@ import .Foo45350: x45350 f45350() = (global x45350 = 2) @test_throws ErrorException f45350() +# #46503 - redefine `invoke`d methods +foo46503(@nospecialize(a), b::Union{Vector{Any}, Float64, Nothing}) = rand() +foo46503(a::Int, b::Nothing) = @invoke foo46503(a::Any, b) +@test 0 <= foo46503(1, nothing) <= 1 +foo46503(@nospecialize(a), b::Union{Nothing, Float64}) = rand() + 10 +@test 10 <= foo46503(1, nothing) <= 11 + @testset "effect override on Symbol(::String)" begin @test Core.Compiler.is_foldable(Base.infer_effects(Symbol, (String,))) end