Skip to content

Commit fcf0174

Browse files
vtjnashKristofferC
authored andcommitted
prevent allocation of Memory (layout and object) when not concrete (#58064)
Fix #54969 (cherry picked from commit cca5ac7)
1 parent 3c15710 commit fcf0174

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

src/datatype.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,13 @@ void jl_get_genericmemory_layout(jl_datatype_t *st)
496496
jl_value_t *kind = jl_tparam0(st);
497497
jl_value_t *eltype = jl_tparam1(st);
498498
jl_value_t *addrspace = jl_tparam2(st);
499-
if (!jl_is_typevar(eltype) && !jl_is_type(eltype)) {
499+
if (!st->isconcretetype) {
500+
// Since parent dt has an opaque layout, we may end up here being asked to copy that layout to subtypes,
501+
// but we don't actually want to do that unless this object is constructable (or at least has a layout).
502+
// The real layout is stored only on the wrapper.
503+
return;
504+
}
505+
if (!jl_is_type(eltype)) {
500506
// this is expected to have a layout, but since it is not constructable, we don't care too much what it is
501507
static const jl_datatype_layout_t opaque_ptr_layout = {0, 0, 1, -1, sizeof(void*), {0}};
502508
st->layout = &opaque_ptr_layout;

src/julia.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1335,7 +1335,7 @@ JL_DLLEXPORT jl_value_t *jl_unwrap_unionall(jl_value_t *v JL_PROPAGATES_ROOT) JL
13351335
#define jl_inlinedatatype_layout(t) (((jl_datatype_t*)t)->layout)
13361336
STATIC_INLINE const jl_datatype_layout_t *jl_datatype_layout(jl_datatype_t *t) JL_NOTSAFEPOINT
13371337
{
1338-
if (jl_is_layout_opaque(t->layout)) // e.g. GenericMemory
1338+
if (t->layout == NULL || jl_is_layout_opaque(t->layout)) // e.g. GenericMemory
13391339
t = (jl_datatype_t*)jl_unwrap_unionall(t->name->wrapper);
13401340
return t->layout;
13411341
}

test/core.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4904,6 +4904,9 @@ let ft = Base.datatype_fieldtypes
49044904
@test !isdefined(ft(B12238.body.body)[1], :instance) # has free type vars
49054905
end
49064906

4907+
# issue #54969
4908+
@test !isdefined(Memory.body, :instance)
4909+
49074910
# `where` syntax in constructor definitions
49084911
(A12238{T} where T<:Real)(x) = 0
49094912
@test A12238{<:Real}(0) == 0

0 commit comments

Comments
 (0)