Skip to content

Commit 304fe78

Browse files
adienesKristofferC
authored andcommitted
fix some crashy behavior of invoke with builtins (#59451)
builtins have empty method tables so `invoke` will segfault or otherwise crash (see some tests). this PR attempts to fix that (cherry picked from commit 1811564)
1 parent 3157245 commit 304fe78

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/gf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ static int speccache_eq(size_t idx, const void *ty, jl_value_t *data, uint_t hv)
150150
// get or create the MethodInstance for a specialization
151151
static jl_method_instance_t *jl_specializations_get_linfo_(jl_method_t *m JL_PROPAGATES_ROOT, jl_value_t *type, jl_svec_t *sparams, jl_method_instance_t *mi_insert)
152152
{
153-
if (m->sig == (jl_value_t*)jl_anytuple_type && jl_atomic_load_relaxed(&m->unspecialized) != NULL && m != jl_opaque_closure_method && !m->is_for_opaque_closure)
153+
if (m->source == NULL && m->generator == NULL && jl_atomic_load_relaxed(&m->unspecialized) != NULL && m != jl_opaque_closure_method && !m->is_for_opaque_closure)
154154
return jl_atomic_load_relaxed(&m->unspecialized); // handle builtin methods
155155
jl_value_t *ut = jl_is_unionall(type) ? jl_unwrap_unionall(type) : type;
156156
JL_TYPECHK(specializations, datatype, ut);

test/core.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2816,6 +2816,14 @@ const T24460 = Tuple{T,T} where T
28162816
g24460() = invoke(f24460, T24460, 1, 2)
28172817
@test @inferred(g24460()) === 2.0
28182818

2819+
@testset "invoke with builtins" begin
2820+
@test invoke(getfield, Tuple{Any, Symbol}, (a = 42,), :a) == 42
2821+
@test invoke(setfield!, Tuple{Any, Symbol, Any}, Base.RefValue(1), :x, 2) == 2
2822+
@test invoke(isdefined, Tuple{Any, Symbol}, (a = 1,), :a) == true
2823+
@test invoke(isdefined, Tuple{Any, Symbol}, (a = 1,), :b) == false
2824+
@test invoke(invoke, Tuple{Any, Type, Vararg}, sin, Tuple{Real}, 0) == 0.0
2825+
end
2826+
28192827
# issue #30679
28202828
@noinline function f30679(::DataType)
28212829
b = IOBuffer()

0 commit comments

Comments
 (0)