Skip to content

Commit 8e3f298

Browse files
authored
When defining singleton structs, don't reallocate the ->instance field (#37598)
Otherwise, we end up with two singletons for the type, which confuses many different parts of the runtime system. This happens quite frequently when using Revise, which likes to redefine types.
1 parent 2bd31a0 commit 8e3f298

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/datatype.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,11 @@ STATIC_INLINE int jl_is_datatype_make_singleton(jl_datatype_t *d)
224224
STATIC_INLINE void jl_maybe_allocate_singleton_instance(jl_datatype_t *st)
225225
{
226226
if (jl_is_datatype_make_singleton(st)) {
227-
st->instance = jl_gc_alloc(jl_get_ptls_states(), 0, st);
228-
jl_gc_wb(st, st->instance);
227+
// It's possible for st to already have an ->instance if it was redefined
228+
if (!st->instance) {
229+
st->instance = jl_gc_alloc(jl_get_ptls_states(), 0, st);
230+
jl_gc_wb(st, st->instance);
231+
}
229232
}
230233
}
231234

test/core.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7397,6 +7397,14 @@ let v = NullableHomogeneousPointerImmutable(),
73977397
@test r[] == 3
73987398
end
73997399

7400+
struct RedefinedSingleton
7401+
end
7402+
redefined_singleton_ref = Ref{Any}(RedefinedSingleton())
7403+
struct RedefinedSingleton
7404+
end
7405+
cmp_refs(a::Ref{Any}) = a[] === RedefinedSingleton()
7406+
@test cmp_refs(redefined_singleton_ref)
7407+
74007408
struct PointerNopadding{T}
74017409
a::Symbol
74027410
b::T

0 commit comments

Comments
 (0)