@@ -322,7 +322,7 @@ static int obviously_disjoint(jl_value_t *a, jl_value_t *b, int specificity)
322
322
for (i = 0 ; i < np ; i ++ ) {
323
323
jl_value_t * ai = jl_tparam (ad ,i );
324
324
jl_value_t * bi = jl_tparam (bd ,i );
325
- if (!istuple && specificity ) {
325
+ if (!istuple && specificity && jl_has_free_typevars ( ai ) ) {
326
326
// X{<:SomeDataType} and X{Union{Y,Z,...}} need to be disjoint to
327
327
// avoid this transitivity problem:
328
328
// A = Tuple{Type{LinearIndices{N,R}}, LinearIndices{N}} where {N,R}
@@ -2585,9 +2585,8 @@ static int tuple_morespecific(jl_datatype_t *cdt, jl_datatype_t *pdt, int invari
2585
2585
return 1 ;
2586
2586
2587
2587
int cms = type_morespecific_ (ce , pe , invariant , env );
2588
- int eqv = !cms && eq_msp (ce , pe , env );
2589
2588
2590
- if (!cms && !eqv && ! sub_msp (ce , pe , env )) {
2589
+ if (!cms && !sub_msp (ce , pe , env )) {
2591
2590
/*
2592
2591
A bound vararg tuple can be more specific despite disjoint elements in order to
2593
2592
preserve transitivity. For example in
@@ -2600,7 +2599,8 @@ static int tuple_morespecific(jl_datatype_t *cdt, jl_datatype_t *pdt, int invari
2600
2599
}
2601
2600
2602
2601
// Tuple{..., T} not more specific than Tuple{..., Vararg{S}} if S is diagonal
2603
- if (eqv && i == clen - 1 && clen == plen && !cva && pva && jl_is_typevar (ce ) && jl_is_typevar (pe ) && !cdiag && pdiag )
2602
+ if (!cms && i == clen - 1 && clen == plen && !cva && pva && eq_msp (ce , pe , env ) &&
2603
+ jl_is_typevar (ce ) && jl_is_typevar (pe ) && !cdiag && pdiag )
2604
2604
return 0 ;
2605
2605
2606
2606
if (cms ) some_morespecific = 1 ;
@@ -2692,24 +2692,23 @@ static int num_occurs(jl_tvar_t *v, jl_typeenv_t *env)
2692
2692
return 0 ;
2693
2693
}
2694
2694
2695
+ #define HANDLE_UNIONALL_A \
2696
+ jl_unionall_t *ua = (jl_unionall_t*)a; \
2697
+ jl_typeenv_t newenv = { ua->var, 0x0, env }; \
2698
+ newenv.val = (jl_value_t*)(intptr_t)count_occurs(ua->body, ua->var); \
2699
+ return type_morespecific_(ua->body, b, invariant, &newenv)
2700
+
2701
+ #define HANDLE_UNIONALL_B \
2702
+ jl_unionall_t *ub = (jl_unionall_t*)b; \
2703
+ jl_typeenv_t newenv = { ub->var, 0x0, env }; \
2704
+ newenv.val = (jl_value_t*)(intptr_t)count_occurs(ub->body, ub->var); \
2705
+ return type_morespecific_(a, ub->body, invariant, &newenv)
2706
+
2695
2707
static int type_morespecific_ (jl_value_t * a , jl_value_t * b , int invariant , jl_typeenv_t * env )
2696
2708
{
2697
2709
if (a == b )
2698
2710
return 0 ;
2699
2711
2700
- if (jl_is_unionall (a )) {
2701
- jl_unionall_t * ua = (jl_unionall_t * )a ;
2702
- jl_typeenv_t newenv = { ua -> var , 0x0 , env };
2703
- newenv .val = (jl_value_t * )(intptr_t )count_occurs (ua -> body , ua -> var );
2704
- return type_morespecific_ (ua -> body , b , invariant , & newenv );
2705
- }
2706
- if (jl_is_unionall (b )) {
2707
- jl_unionall_t * ub = (jl_unionall_t * )b ;
2708
- jl_typeenv_t newenv = { ub -> var , 0x0 , env };
2709
- newenv .val = (jl_value_t * )(intptr_t )count_occurs (ub -> body , ub -> var );
2710
- return type_morespecific_ (a , ub -> body , invariant , & newenv );
2711
- }
2712
-
2713
2712
if (jl_is_tuple_type (a ) && jl_is_tuple_type (b )) {
2714
2713
// When one is JL_VARARG_BOUND and the other has fixed length,
2715
2714
// allow the argument length to fix the tvar
@@ -2727,7 +2726,15 @@ static int type_morespecific_(jl_value_t *a, jl_value_t *b, int invariant, jl_ty
2727
2726
return tuple_morespecific ((jl_datatype_t * )a , (jl_datatype_t * )b , invariant , env );
2728
2727
}
2729
2728
2729
+ if (!invariant ) {
2730
+ if ((jl_datatype_t * )a == jl_any_type ) return 0 ;
2731
+ if ((jl_datatype_t * )b == jl_any_type && !jl_is_typevar (a )) return 1 ;
2732
+ }
2733
+
2730
2734
if (jl_is_uniontype (a )) {
2735
+ if (jl_is_unionall (b )) {
2736
+ HANDLE_UNIONALL_B ;
2737
+ }
2731
2738
// Union a is more specific than b if some element of a is more specific than b, but
2732
2739
// not vice-versa.
2733
2740
if (sub_msp (b , a , env ))
@@ -2764,17 +2771,15 @@ static int type_morespecific_(jl_value_t *a, jl_value_t *b, int invariant, jl_ty
2764
2771
}
2765
2772
2766
2773
if (jl_is_uniontype (b )) {
2774
+ if (jl_is_unionall (a )) {
2775
+ HANDLE_UNIONALL_A ;
2776
+ }
2767
2777
jl_uniontype_t * u = (jl_uniontype_t * )b ;
2768
2778
if (type_morespecific_ (a , u -> a , invariant , env ) || type_morespecific_ (a , u -> b , invariant , env ))
2769
2779
return !type_morespecific_ (b , a , invariant , env );
2770
2780
return 0 ;
2771
2781
}
2772
2782
2773
- if (!invariant ) {
2774
- if ((jl_datatype_t * )a == jl_any_type ) return 0 ;
2775
- if ((jl_datatype_t * )b == jl_any_type ) return 1 ;
2776
- }
2777
-
2778
2783
if (jl_is_datatype (a ) && jl_is_datatype (b )) {
2779
2784
jl_datatype_t * tta = (jl_datatype_t * )a , * ttb = (jl_datatype_t * )b ;
2780
2785
// Type{Union{}} is more specific than other types, so TypeofBottom must be too
@@ -2796,13 +2801,20 @@ static int type_morespecific_(jl_value_t *a, jl_value_t *b, int invariant, jl_ty
2796
2801
for (size_t i = 0 ; i < jl_nparams (tta ); i ++ ) {
2797
2802
jl_value_t * apara = jl_tparam (tta ,i );
2798
2803
jl_value_t * bpara = jl_tparam (ttb ,i );
2799
- if (!jl_has_free_typevars (apara ) && !jl_has_free_typevars (bpara ) &&
2800
- !jl_types_equal (apara , bpara ))
2804
+ int afree = jl_has_free_typevars (apara );
2805
+ int bfree = jl_has_free_typevars (bpara );
2806
+ if (!afree && !bfree && !jl_types_equal (apara , bpara ))
2801
2807
return 0 ;
2802
- if (type_morespecific_ (apara , bpara , 1 , env ))
2808
+ if (type_morespecific_ (apara , bpara , 1 , env ) && ( jl_is_typevar ( apara ) || ! afree || bfree ) )
2803
2809
ascore += 1 ;
2804
- else if (type_morespecific_ (bpara , apara , 1 , env ))
2810
+ else if (type_morespecific_ (bpara , apara , 1 , env ) && ( jl_is_typevar ( bpara ) || ! bfree || afree ) )
2805
2811
bscore += 1 ;
2812
+ else if (eq_msp (apara , bpara , env )) {
2813
+ if (!afree && bfree )
2814
+ ascore += 1 ;
2815
+ else if (afree && !bfree )
2816
+ bscore += 1 ;
2817
+ }
2806
2818
if (jl_is_typevar (bpara ) && !jl_is_typevar (apara ) && !jl_is_type (apara ))
2807
2819
ascore1 = 1 ;
2808
2820
else if (jl_is_typevar (apara ) && !jl_is_typevar (bpara ) && !jl_is_type (bpara ))
@@ -2828,9 +2840,6 @@ static int type_morespecific_(jl_value_t *a, jl_value_t *b, int invariant, jl_ty
2828
2840
return 0 ;
2829
2841
return ascore > bscore || adiag > bdiag ;
2830
2842
}
2831
- else if (invariant ) {
2832
- return 0 ;
2833
- }
2834
2843
tta = tta -> super ; super = 1 ;
2835
2844
}
2836
2845
return 0 ;
@@ -2852,16 +2861,18 @@ static int type_morespecific_(jl_value_t *a, jl_value_t *b, int invariant, jl_ty
2852
2861
if (invariant ) {
2853
2862
if (((jl_tvar_t * )a )-> ub == jl_bottom_type )
2854
2863
return 1 ;
2855
- if (jl_has_free_typevars (b )) {
2856
- if (type_morespecific_ (((jl_tvar_t * )a )-> ub , b , 0 , env ) ||
2857
- eq_msp (((jl_tvar_t * )a )-> ub , b , env ))
2858
- return num_occurs ((jl_tvar_t * )a , env ) >= 2 ;
2859
- }
2860
- else {
2864
+ if (!jl_has_free_typevars (b ))
2861
2865
return 0 ;
2862
- }
2866
+ if (eq_msp (((jl_tvar_t * )a )-> ub , b , env ))
2867
+ return num_occurs ((jl_tvar_t * )a , env ) >= 2 ;
2868
+ }
2869
+ else {
2870
+ // need `{T,T} where T` more specific than `{Any, Any}`
2871
+ if (b == (jl_value_t * )jl_any_type && ((jl_tvar_t * )a )-> ub == (jl_value_t * )jl_any_type &&
2872
+ num_occurs ((jl_tvar_t * )a , env ) >= 2 )
2873
+ return 1 ;
2863
2874
}
2864
- return type_morespecific_ ((jl_value_t * )( (jl_tvar_t * )a )-> ub , b , 0 , env );
2875
+ return type_morespecific_ (((jl_tvar_t * )a )-> ub , b , 0 , env );
2865
2876
}
2866
2877
if (jl_is_typevar (b )) {
2867
2878
if (!jl_is_type (a ))
@@ -2873,12 +2884,24 @@ static int type_morespecific_(jl_value_t *a, jl_value_t *b, int invariant, jl_ty
2873
2884
if (type_morespecific_ (a , ((jl_tvar_t * )b )-> ub , 0 , env ) ||
2874
2885
eq_msp (a , ((jl_tvar_t * )b )-> ub , env ))
2875
2886
return num_occurs ((jl_tvar_t * )b , env ) < 2 ;
2887
+ return 0 ;
2876
2888
}
2877
2889
else {
2890
+ if (obviously_disjoint (a , ((jl_tvar_t * )b )-> ub , 1 ))
2891
+ return 0 ;
2892
+ if (type_morespecific_ (((jl_tvar_t * )b )-> ub , a , 0 , env ))
2893
+ return 0 ;
2878
2894
return 1 ;
2879
2895
}
2880
2896
}
2881
- return type_morespecific_ (a , (jl_value_t * )((jl_tvar_t * )b )-> ub , 0 , env );
2897
+ return type_morespecific_ (a , ((jl_tvar_t * )b )-> ub , 0 , env );
2898
+ }
2899
+
2900
+ if (jl_is_unionall (a )) {
2901
+ HANDLE_UNIONALL_A ;
2902
+ }
2903
+ if (jl_is_unionall (b )) {
2904
+ HANDLE_UNIONALL_B ;
2882
2905
}
2883
2906
2884
2907
return 0 ;
0 commit comments