Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #17764, type problem due to object_id collision after workspace()
Browse files Browse the repository at this point in the history
In the type cache, make ordered comparison of types not depend on object_id.

Clarify help for `object_id`, since it technically can have collisions.

While I'm at it, remove unnecessary call to re-sort `tn->linearcache`
(it is not in sorted order).
JeffBezanson committed Aug 3, 2016
1 parent b27039f commit bdbf1ea
Showing 4 changed files with 38 additions and 8 deletions.
2 changes: 1 addition & 1 deletion base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
@@ -6416,7 +6416,7 @@ ctranspose!
"""
object_id(x)
Get a unique integer id for `x`. `object_id(x)==object_id(y)` if and only if `is(x,y)`.
Get a hash value for `x` based on object identity. `object_id(x)==object_id(y)` if `x === y`.
"""
object_id

1 change: 0 additions & 1 deletion src/dump.c
Original file line number Diff line number Diff line change
@@ -2033,7 +2033,6 @@ static void jl_restore_system_image_from_stream(ios_t *f)
tn->cache = (jl_svec_t*)jl_deserialize_value(&s, NULL); jl_gc_wb(tn, tn->cache);
tn->linearcache = (jl_svec_t*)jl_deserialize_value(&s, NULL); jl_gc_wb(tn, tn->linearcache);
jl_resort_type_cache(tn->cache);
jl_resort_type_cache(tn->linearcache);
}

jl_core_module = (jl_module_t*)jl_get_global(jl_main_module,
34 changes: 28 additions & 6 deletions src/jltypes.c
Original file line number Diff line number Diff line change
@@ -1891,14 +1891,36 @@ static int typekey_compare(jl_datatype_t *tt, jl_value_t **key, size_t n)
for(j=0; j < n; j++) {
jl_value_t *kj = key[j], *tj = jl_svecref(tt->parameters,j);
if (tj != kj) {
int dtt = jl_is_datatype(tj);
int dtk = jl_is_datatype(kj);
if (!dtt && !dtk && jl_egal(tj, kj))
if (!jl_is_datatype(tj)) {
if (!dtk) {
if (jl_egal(tj, kj))
continue;
return (jl_object_id(kj) < jl_object_id(tj) ? -1 : 1);
}
else {
return 1;
}
}
else if (!dtk) {
return -1;
}
jl_datatype_t *dt = (jl_datatype_t*)tj;
jl_datatype_t *dk = (jl_datatype_t*)kj;
if (dk->uid != dt->uid) {
return dk->uid < dt->uid ? -1 : 1;
}
else if (dk->uid != 0) {
continue;
uintptr_t tid = (dtt && ((jl_datatype_t*)tj)->uid ? ((jl_datatype_t*)tj)->uid : jl_object_id(tj));
uintptr_t kid = (dtk && ((jl_datatype_t*)kj)->uid ? ((jl_datatype_t*)kj)->uid : jl_object_id(kj));
if (kid != tid)
return kid < tid ? -1 : 1;
}
else if (dk->name->hash != dt->name->hash) {
return dk->name->hash < dt->name->hash ? -1 : 1;
}
else {
int cmp = typekey_compare(dt, jl_svec_data(dk->parameters), jl_nparams(dk));
if (cmp != 0)
return cmp;
}
}
}
return 0;
9 changes: 9 additions & 0 deletions test/workspace.jl
Original file line number Diff line number Diff line change
@@ -15,3 +15,12 @@ show(io, Pair)
"""
exename = Base.julia_cmd()
run(`$exename --startup-file=no -e $script`)

# issue #17764
script2 = """
type Foo end
workspace()
type Foo end
@assert Tuple{Type{LastMain.Foo}} !== Tuple{Type{Main.Foo}}
"""
run(`$exename --startup-file=no -e $script2`)

0 comments on commit bdbf1ea

Please sign in to comment.