Skip to content

Commit

Permalink
ensure that instances are only created for types with unique ids
Browse files Browse the repository at this point in the history
fix #16301
doesn't completely fix #12096, but avoids the symptoms
  • Loading branch information
vtjnash committed May 16, 2016
1 parent 97dc858 commit 0c78b78
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/interpreter.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ static jl_value_t *eval(jl_value_t *e, jl_value_t **locals, jl_lambda_info_t *la
jl_rethrow();
}
jl_compute_field_offsets(dt);
if (para == (jl_value_t*)jl_emptysvec && jl_is_datatype_singleton(dt)) {
if (para == (jl_value_t*)jl_emptysvec && jl_is_datatype_make_singleton(dt)) {
dt->instance = newstruct(dt);
jl_gc_wb(dt, dt->instance);
}
Expand Down
7 changes: 4 additions & 3 deletions src/jltypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2183,7 +2183,7 @@ static jl_value_t *inst_datatype(jl_datatype_t *dt, jl_svec_t *p, jl_value_t **i
jl_precompute_memoized_dt(ndt);

// assign uid as early as possible
if (cacheable && !ndt->abstract && ndt->uid==0)
if (cacheable && !ndt->abstract)
ndt->uid = jl_assign_type_uid();

if (istuple)
Expand All @@ -2207,7 +2207,7 @@ static jl_value_t *inst_datatype(jl_datatype_t *dt, jl_svec_t *p, jl_value_t **i
else {
jl_compute_field_offsets(ndt);
}
if (jl_is_datatype_singleton(ndt)) {
if (jl_is_datatype_make_singleton(ndt)) {
ndt->instance = newstruct(ndt);
jl_gc_wb(ndt, ndt->instance);
}
Expand Down Expand Up @@ -3416,7 +3416,8 @@ void jl_init_types(void)

jl_tupletype_t *empty_tuple_type = jl_apply_tuple_type(jl_emptysvec);
empty_tuple_type->uid = jl_assign_type_uid();
jl_emptytuple = ((jl_datatype_t*)empty_tuple_type)->instance;
jl_emptytuple = newstruct(empty_tuple_type);
empty_tuple_type->instance = jl_emptytuple;

// non-primitive definitions follow
jl_int32_type = NULL;
Expand Down
10 changes: 7 additions & 3 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -898,9 +898,13 @@ STATIC_INLINE int jl_isbits(void *t) // corresponding to isbits() in julia

STATIC_INLINE int jl_is_datatype_singleton(jl_datatype_t *d)
{
return (d->instance != NULL ||
(!d->abstract && d->size == 0 && d != jl_sym_type && d->name != jl_array_typename &&
(d->name->names == jl_emptysvec || !d->mutabl)));
return (d->instance != NULL);
}

STATIC_INLINE int jl_is_datatype_make_singleton(jl_datatype_t *d)
{
return (!d->abstract && d->size == 0 && d != jl_sym_type && d->name != jl_array_typename &&
d->uid != 0 && (d->name->names == jl_emptysvec || !d->mutabl));
}

STATIC_INLINE int jl_is_abstracttype(void *v)
Expand Down
9 changes: 9 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4127,3 +4127,12 @@ let a = Any[]
@test (push!(a,10); f()) - (push!(a,2); f()) == 8
@test a == [10, 2]
end

# issue #12096
let a = Val{Val{TypeVar(:_, Int, true)}},
b = Val{Val{TypeVar(:_, Int)}}

@test !isdefined(a, :instance)
@test isdefined(b, :instance)
@test isleaftype(b)
end

0 comments on commit 0c78b78

Please sign in to comment.