Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

datatype: handle concrete type intersections that happen #49017

Merged
merged 1 commit into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/datatype.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ jl_datatype_t *jl_new_uninitialized_datatype(void)
t->isprimitivetype = 0;
t->zeroinit = 0;
t->has_concrete_subtype = 1;
t->cached_by_hash = 0;
t->maybe_subtype_of_cache = 1;
t->ismutationfree = 0;
t->isidentityfree = 0;
t->name = NULL;
Expand Down
1 change: 0 additions & 1 deletion src/jl_exported_funcs.inc
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,6 @@
XX(jl_typename_str) \
XX(jl_typeof_str) \
XX(jl_types_equal) \
XX(jl_type_equality_is_identity) \
XX(jl_type_error) \
XX(jl_type_error_rt) \
XX(jl_type_intersection) \
Expand Down
241 changes: 163 additions & 78 deletions src/jltypes.c

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ typedef struct _jl_datatype_t {
uint16_t isbitstype:1; // relevant query for C-api and type-parameters
uint16_t zeroinit:1; // if one or more fields requires zero-initialization
uint16_t has_concrete_subtype:1; // If clear, no value will have this datatype
uint16_t cached_by_hash:1; // stored in hash-based set cache (instead of linear cache)
uint16_t maybe_subtype_of_cache:1; // Computational bit for has_concrete_supertype. See description in jltypes.c.
uint16_t isprimitivetype:1; // whether this is declared with 'primitive type' keyword (sized, no fields, and immutable)
uint16_t ismutationfree:1; // whether any mutable memory is reachable through this type (in the type or via fields)
uint16_t isidentityfree:1; // whether this type or any object reachable through its fields has non-content-based identity
Expand Down Expand Up @@ -1414,7 +1414,6 @@ STATIC_INLINE int jl_egal_(const jl_value_t *a JL_MAYBE_UNROOTED, const jl_value
#define jl_egal(a, b) jl_egal_((a), (b))

// type predicates and basic operations
JL_DLLEXPORT int jl_type_equality_is_identity(jl_value_t *t1, jl_value_t *t2) JL_NOTSAFEPOINT;
JL_DLLEXPORT int jl_has_free_typevars(jl_value_t *v) JL_NOTSAFEPOINT;
JL_DLLEXPORT int jl_has_typevar(jl_value_t *t, jl_tvar_t *v) JL_NOTSAFEPOINT;
JL_DLLEXPORT int jl_has_typevar_from_unionall(jl_value_t *t, jl_unionall_t *ua);
Expand Down
1 change: 1 addition & 0 deletions src/julia_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ void jl_init_main_module(void);
JL_DLLEXPORT int jl_is_submodule(jl_module_t *child, jl_module_t *parent) JL_NOTSAFEPOINT;
jl_array_t *jl_get_loaded_modules(void);
JL_DLLEXPORT int jl_datatype_isinlinealloc(jl_datatype_t *ty, int pointerfree);
int jl_type_equality_is_identity(jl_value_t *t1, jl_value_t *t2) JL_NOTSAFEPOINT;

void jl_eval_global_expr(jl_module_t *m, jl_expr_t *ex, int set_type);
jl_value_t *jl_toplevel_eval_flex(jl_module_t *m, jl_value_t *e, int fast, int expanded);
Expand Down
23 changes: 9 additions & 14 deletions src/subtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,8 @@ static int obviously_unequal(jl_value_t *a, jl_value_t *b)
if (ad->name != bd->name)
return 1;
int istuple = (ad->name == jl_tuple_typename);
if ((jl_is_concrete_type(a) || jl_is_concrete_type(b)) &&
jl_type_equality_is_identity(a, b)) {
if (!istuple && ad->name != jl_type_typename) // HACK: can't properly normalize Tuple{Float64} == Tuple{<:Float64} like types or Type{T} types
return 1;
}
if (jl_type_equality_is_identity(a, b))
return 1;
size_t i, np;
if (istuple) {
size_t na = jl_nparams(ad), nb = jl_nparams(bd);
Expand Down Expand Up @@ -395,10 +392,7 @@ static int obviously_disjoint(jl_value_t *a, jl_value_t *b, int specificity)
return 0;
if (specificity && a == (jl_value_t*)jl_typeofbottom_type)
return 0;
if (jl_is_concrete_type(a) && jl_is_concrete_type(b) &&
jl_type_equality_is_identity(a, b) &&
(((jl_datatype_t*)a)->name != jl_tuple_typename ||
((jl_datatype_t*)b)->name != jl_tuple_typename))
if (jl_is_concrete_type(a) && jl_is_concrete_type(b) && jl_type_equality_is_identity(a, b))
return 1;
if (jl_is_unionall(a)) a = jl_unwrap_unionall(a);
if (jl_is_unionall(b)) b = jl_unwrap_unionall(b);
Expand Down Expand Up @@ -1756,7 +1750,7 @@ static int obvious_subtype(jl_value_t *x, jl_value_t *y, jl_value_t *y0, int *su
if (jl_is_datatype(y)) {
int istuple = (((jl_datatype_t*)y)->name == jl_tuple_typename);
int iscov = istuple;
// TODO: this would be a nice fast-path to have, unfortuanately,
// TODO: this would be a nice fast-path to have, unfortunately,
// datatype allocation fails to correctly hash-cons them
// and the subtyping tests include tests for this case
//if (!iscov && ((jl_datatype_t*)y)->isconcretetype && !jl_is_type_type(x)) {
Expand Down Expand Up @@ -2243,7 +2237,7 @@ JL_DLLEXPORT int jl_isa(jl_value_t *x, jl_value_t *t)
return 0;
}
}
if (jl_is_concrete_type(t) && jl_type_equality_is_identity(jl_typeof(x), t))
if (jl_is_concrete_type(t))
return 0;
return jl_subtype(jl_typeof(x), t);
}
Expand Down Expand Up @@ -3725,6 +3719,7 @@ static jl_value_t *switch_union_tuple(jl_value_t *a, jl_value_t *b)

// `a` might have a non-empty intersection with some concrete type b even if !(a<:b) and !(b<:a)
// For example a=`Tuple{Type{<:Vector}}` and b=`Tuple{DataType}`
// TODO: this query is partly available memoized as jl_type_equality_is_identity
static int might_intersect_concrete(jl_value_t *a)
{
if (jl_is_unionall(a))
Expand Down Expand Up @@ -3774,9 +3769,9 @@ jl_value_t *jl_type_intersection_env_s(jl_value_t *a, jl_value_t *b, jl_svec_t *
*ans = a; sz = szb;
if (issubty) *issubty = 1;
}
else if (lta && ltb) {
goto bot;
}
// else if (lta && ltb) { // !jl_type_equality_is_identity known in this case because obviously_disjoint returned false
// goto bot;
// }
else if (jl_subtype(b, a)) {
*ans = b;
}
Expand Down
2 changes: 1 addition & 1 deletion test/specificity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ f27361(::M) where M <: Tuple{3} = nothing
@test length(methods(f27361)) == 2

# specificity of TypeofBottom
@test args_morespecific(Tuple{Core.TypeofBottom}, Tuple{DataType})
@test !args_morespecific(Tuple{DataType}, Tuple{Core.TypeofBottom})
@test args_morespecific(Tuple{Core.TypeofBottom}, Tuple{Type{<:Tuple}})

@test args_morespecific(Tuple{Type{Any}, Type}, Tuple{Type{T}, Type{T}} where T)
Expand Down