-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
subtype.c
5175 lines (4940 loc) · 188 KB
/
subtype.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is a part of Julia. License is MIT: https://julialang.org/license
/*
subtyping predicate
Uses the algorithm described in section 4.2.2 of https://github.com/JeffBezanson/phdthesis/
This code adds the following features to the core algorithm:
- Type variables can be restricted to range over only concrete types.
This is done by returning false if such a variable's lower bound is not concrete.
- Diagonal rule: a type variable is concrete if it occurs more than once in
covariant position, and never in invariant position. This sounds like a syntactic
property, but actually isn't since it depends on which occurrences of a type
variable the algorithm actually uses.
- Unconstrained type vars (Bottom<:T<:Any) can match non-type values.
- Vararg types have an int-valued length parameter N (in `Vararg{T,N}`).
- Type{T}<:S if isa(T,S). Existing code assumes this, but it's not strictly
correct since a type can equal `T` without having the same representation.
- Free type variables are tolerated. This can hopefully be removed after a
deprecation period.
*/
#include <stdlib.h>
#include <string.h>
#ifdef _OS_WINDOWS_
#include <malloc.h>
#endif
#include "julia.h"
#include "julia_internal.h"
#include "julia_assert.h"
#ifdef __cplusplus
extern "C" {
#endif
// stack of bits to keep track of which combination of Union components we are
// looking at (0 for Union.a, 1 for Union.b). forall_exists_subtype and
// exists_subtype loop over all combinations by updating a binary count in
// this structure.
// Union type decision points are discovered while the algorithm works.
// If a new Union decision is encountered, the `more` flag is set to tell
// the forall/exists loop to grow the stack.
// TODO: the stack probably needs to be artificially large because of some
// deeper problem (see #21191) and could be shrunk once that is fixed
typedef struct {
int16_t depth;
int16_t more;
int16_t used;
uint32_t stack[100]; // stack of bits represented as a bit vector
} jl_unionstate_t;
typedef struct {
int16_t depth;
int16_t more;
int16_t used;
void *stack;
} jl_saved_unionstate_t;
// Linked list storing the type variable environment. A new jl_varbinding_t
// is pushed for each UnionAll type we encounter. `lb` and `ub` are updated
// during the computation.
// Most of the complexity is due to the "diagonal rule", requiring us to
// identify which type vars range over only concrete types.
typedef struct jl_varbinding_t {
jl_tvar_t *var;
jl_value_t *lb;
jl_value_t *ub;
int8_t right; // whether this variable came from the right side of `A <: B`
int8_t occurs; // occurs in any position
int8_t occurs_inv; // occurs in invariant position
int8_t occurs_cov; // # of occurrences in covariant position
int8_t concrete; // 1 if another variable has a constraint forcing this one to be concrete
int8_t max_offset; // record the maximum positive offset of the variable (up to 32)
// max_offset < 0 if this variable occurs outside VarargNum.
// constraintkind: in covariant position, we try three different ways to compute var ∩ type:
// let ub = var.ub ∩ type
// 0 - var.ub <: type ? var : ub
// 1 - var.ub = ub; return var
// 2 - var.lb = lb; return ub
int8_t constraintkind;
int8_t intvalued; // intvalued: must be integer-valued; i.e. occurs as N in Vararg{_,N}
int8_t limited;
int8_t intersected; // whether this variable has been intersected
int16_t depth0; // # of invariant constructors nested around the UnionAll type for this var
// array of typevars that our bounds depend on, whose UnionAlls need to be
// moved outside ours.
jl_array_t *innervars;
struct jl_varbinding_t *prev;
} jl_varbinding_t;
// subtype algorithm state
typedef struct jl_stenv_t {
// N.B.: varbindings are created on the stack and rooted there
jl_varbinding_t *vars; // type variable environment
jl_unionstate_t Lunions; // union state for unions on the left of A <: B
jl_unionstate_t Runions; // union state for unions on the right
// N.B.: envout is gc-rooted
jl_value_t **envout; // for passing caller the computed bounds of right-side variables
int envsz; // length of envout
int envidx; // current index in envout
int invdepth; // current number of invariant constructors we're nested in
int ignore_free; // treat free vars as black boxes; used during intersection
int intersection; // true iff subtype is being called from intersection
int emptiness_only; // true iff intersection only needs to test for emptiness
int triangular; // when intersecting Ref{X} with Ref{<:Y}
// Used to represent the length difference between 2 vararg.
// intersect(X, Y) ==> X = Y + Loffset
int Loffset;
} jl_stenv_t;
// state manipulation utilities
// look up a type variable in an environment
#ifdef __clang_gcanalyzer__
static jl_varbinding_t *lookup(jl_stenv_t *e, jl_tvar_t *v) JL_GLOBALLY_ROOTED JL_NOTSAFEPOINT;
#else
static jl_varbinding_t *lookup(jl_stenv_t *e, jl_tvar_t *v) JL_GLOBALLY_ROOTED JL_NOTSAFEPOINT
{
jl_varbinding_t *b = e->vars;
while (b != NULL) {
if (b->var == v) return b;
b = b->prev;
}
return b;
}
#endif
static int statestack_get(jl_unionstate_t *st, int i) JL_NOTSAFEPOINT
{
assert(i >= 0 && i < sizeof(st->stack) * 8);
// get the `i`th bit in an array of 32-bit words
return (st->stack[i>>5] & (1u<<(i&31))) != 0;
}
static void statestack_set(jl_unionstate_t *st, int i, int val) JL_NOTSAFEPOINT
{
assert(i >= 0 && i < sizeof(st->stack) * 8);
if (val)
st->stack[i>>5] |= (1u<<(i&31));
else
st->stack[i>>5] &= ~(1u<<(i&31));
}
#define push_unionstate(saved, src) \
do { \
(saved)->depth = (src)->depth; \
(saved)->more = (src)->more; \
(saved)->used = (src)->used; \
(saved)->stack = alloca(((src)->used+7)/8); \
memcpy((saved)->stack, &(src)->stack, ((src)->used+7)/8); \
} while (0);
#define pop_unionstate(dst, saved) \
do { \
(dst)->depth = (saved)->depth; \
(dst)->more = (saved)->more; \
(dst)->used = (saved)->used; \
memcpy(&(dst)->stack, (saved)->stack, ((saved)->used+7)/8); \
} while (0);
static int current_env_length(jl_stenv_t *e)
{
jl_varbinding_t *v = e->vars;
int len = 0;
while (v) {
len++;
v = v->prev;
}
return len;
}
typedef struct {
int8_t *buf;
int rdepth;
int8_t _space[32]; // == 8 * 4
jl_gcframe_t gcframe;
jl_value_t *roots[24]; // == 8 * 3
} jl_savedenv_t;
static void re_save_env(jl_stenv_t *e, jl_savedenv_t *se, int root)
{
jl_value_t **roots = NULL;
int nroots = 0;
if (root) {
if (se->gcframe.nroots == JL_GC_ENCODE_PUSHARGS(1)) {
jl_svec_t *sv = (jl_svec_t*)se->roots[0];
assert(jl_is_svec(sv));
roots = jl_svec_data(sv);
nroots = jl_svec_len(sv);
}
else {
roots = se->roots;
nroots = se->gcframe.nroots >> 2;
}
}
jl_varbinding_t *v = e->vars;
int i = 0, j = 0;
while (v != NULL) {
if (root) {
roots[i++] = v->lb;
roots[i++] = v->ub;
roots[i++] = (jl_value_t*)v->innervars;
}
se->buf[j++] = v->occurs;
se->buf[j++] = v->occurs_inv;
se->buf[j++] = v->occurs_cov;
se->buf[j++] = v->max_offset;
v = v->prev;
}
assert(i == nroots); (void)nroots;
se->rdepth = e->Runions.depth;
}
static void alloc_env(jl_stenv_t *e, jl_savedenv_t *se, int root)
{
jl_task_t *ct = jl_current_task;
int len = current_env_length(e);
se->gcframe.nroots = 0;
se->gcframe.prev = NULL;
se->roots[0] = NULL;
if (len > 8) {
if (root) {
se->gcframe.nroots = JL_GC_ENCODE_PUSHARGS(1);
se->gcframe.prev = ct->gcstack;
ct->gcstack = &se->gcframe;
jl_svec_t *sv = jl_alloc_svec(len * 3);
se->roots[0] = (jl_value_t*)sv;
}
}
else {
if (root && len) {
for (int i = 0; i < len * 3; i++)
se->roots[i] = NULL;
se->gcframe.nroots = JL_GC_ENCODE_PUSHARGS(len * 3);
se->gcframe.prev = ct->gcstack;
ct->gcstack = &se->gcframe;
}
}
se->buf = (len > 8 ? (int8_t*)malloc_s(len * 4) : se->_space);
#ifdef __clang_gcanalyzer__
memset(se->buf, 0, len * 3);
#endif
}
static void save_env(jl_stenv_t *e, jl_savedenv_t *se, int root)
{
alloc_env(e, se, root);
re_save_env(e, se, root);
}
static void free_env(jl_savedenv_t *se) JL_NOTSAFEPOINT
{
if (se->gcframe.nroots) {
assert(jl_current_task->gcstack == &se->gcframe);
JL_GC_POP();
}
if (se->buf != se->_space)
free(se->buf);
se->buf = NULL;
}
static void restore_env(jl_stenv_t *e, jl_savedenv_t *se, int root) JL_NOTSAFEPOINT
{
jl_value_t **roots = NULL;
int nroots = 0;
if (root) {
if (se->gcframe.nroots == JL_GC_ENCODE_PUSHARGS(1)) {
jl_svec_t *sv = (jl_svec_t*)se->roots[0];
assert(jl_is_svec(sv));
roots = jl_svec_data(sv);
nroots = jl_svec_len(sv);
}
else {
roots = se->roots;
nroots = se->gcframe.nroots >> 2;
}
}
jl_varbinding_t *v = e->vars;
int i = 0, j = 0;
while (v != NULL) {
if (root) {
v->lb = roots[i++];
v->ub = roots[i++];
v->innervars = (jl_array_t*)roots[i++];
}
v->occurs = se->buf[j++];
v->occurs_inv = se->buf[j++];
v->occurs_cov = se->buf[j++];
v->max_offset = se->buf[j++];
v = v->prev;
}
assert(i == nroots); (void)nroots;
e->Runions.depth = se->rdepth;
if (e->envout && e->envidx < e->envsz)
memset(&e->envout[e->envidx], 0, (e->envsz - e->envidx)*sizeof(void*));
}
static void clean_occurs(jl_stenv_t *e)
{
jl_varbinding_t *v = e->vars;
while (v) {
v->occurs = 0;
v = v->prev;
}
}
#define flip_offset(e) ((e)->Loffset *= -1)
// type utilities
// quickly test that two types are identical
static int obviously_egal(jl_value_t *a, jl_value_t *b) JL_NOTSAFEPOINT
{
if (a == (jl_value_t*)jl_typeofbottom_type->super)
a = (jl_value_t*)jl_typeofbottom_type; // supertype(typeof(Union{})) is equal to, although distinct from, itself
if (b == (jl_value_t*)jl_typeofbottom_type->super)
b = (jl_value_t*)jl_typeofbottom_type; // supertype(typeof(Union{})) is equal to, although distinct from, itself
if (a == b) return 1;
if (jl_typeof(a) != jl_typeof(b)) return 0;
if (jl_is_datatype(a)) {
jl_datatype_t *ad = (jl_datatype_t*)a;
jl_datatype_t *bd = (jl_datatype_t*)b;
if (ad->name != bd->name) return 0;
if (ad->isconcretetype || bd->isconcretetype) return 0;
size_t i, np = jl_nparams(ad);
if (np != jl_nparams(bd)) return 0;
for (i = 0; i < np; i++) {
if (!obviously_egal(jl_tparam(ad,i), jl_tparam(bd,i)))
return 0;
}
return 1;
}
if (jl_is_uniontype(a)) {
return obviously_egal(((jl_uniontype_t*)a)->a, ((jl_uniontype_t*)b)->a) &&
obviously_egal(((jl_uniontype_t*)a)->b, ((jl_uniontype_t*)b)->b);
}
if (jl_is_unionall(a)) {
return ((jl_unionall_t*)a)->var == ((jl_unionall_t*)b)->var &&
obviously_egal(((jl_unionall_t*)a)->body, ((jl_unionall_t*)b)->body);
}
if (jl_is_vararg(a)) {
jl_vararg_t *vma = (jl_vararg_t *)a;
jl_vararg_t *vmb = (jl_vararg_t *)b;
return obviously_egal(jl_unwrap_vararg(vma), jl_unwrap_vararg(vmb)) &&
((!vma->N && !vmb->N) || (vma->N && vmb->N && obviously_egal(vma->N, vmb->N)));
}
if (jl_is_typevar(a)) return 0;
return !jl_is_type(a) && jl_egal(a,b);
}
static int obviously_unequal(jl_value_t *a, jl_value_t *b)
{
if (a == (jl_value_t*)jl_typeofbottom_type->super)
a = (jl_value_t*)jl_typeofbottom_type; // supertype(typeof(Union{})) is equal to, although distinct from, itself
if (b == (jl_value_t*)jl_typeofbottom_type->super)
b = (jl_value_t*)jl_typeofbottom_type; // supertype(typeof(Union{})) is equal to, although distinct from, itself
if (a == b)
return 0;
if (jl_is_unionall(a))
a = jl_unwrap_unionall(a);
if (jl_is_unionall(b))
b = jl_unwrap_unionall(b);
if (jl_is_datatype(a)) {
if (b == jl_bottom_type)
return 1;
if (jl_is_datatype(b)) {
jl_datatype_t *ad = (jl_datatype_t*)a;
jl_datatype_t *bd = (jl_datatype_t*)b;
if (a == (jl_value_t*)jl_typeofbottom_type && bd->name == jl_type_typename)
return obviously_unequal(jl_bottom_type, jl_tparam(bd, 0));
if (ad->name == jl_type_typename && b == (jl_value_t*)jl_typeofbottom_type)
return obviously_unequal(jl_tparam(ad, 0), jl_bottom_type);
if (ad->name != bd->name)
return 1;
int istuple = (ad->name == jl_tuple_typename);
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);
if (jl_is_va_tuple(ad)) {
na -= 1;
if (jl_is_va_tuple(bd))
nb -= 1;
}
else if (jl_is_va_tuple(bd)) {
nb -= 1;
}
else if (na != nb) {
return 1;
}
np = na < nb ? na : nb;
}
else {
np = jl_nparams(ad);
if (np != jl_nparams(bd))
return 1;
}
for (i = 0; i < np; i++) {
if (obviously_unequal(jl_tparam(ad, i), jl_tparam(bd, i)))
return 1;
}
}
}
else if (a == jl_bottom_type && jl_is_datatype(b)) {
return 1;
}
if (jl_is_typevar(a) && jl_is_typevar(b) && obviously_unequal(((jl_tvar_t*)a)->ub, ((jl_tvar_t*)b)->ub))
return 1;
if (jl_is_long(a)) {
if (jl_is_long(b) && jl_unbox_long(a) != jl_unbox_long(b))
return 1;
}
else if (jl_is_long(b)) {
return 1;
}
if ((jl_is_symbol(a) || jl_is_symbol(b)) && a != b)
return 1;
return 0;
}
int jl_obviously_unequal(jl_value_t *a, jl_value_t *b)
{
return obviously_unequal(a, b);
}
static int in_union(jl_value_t *u, jl_value_t *x) JL_NOTSAFEPOINT
{
if (u == x) return 1;
if (!jl_is_uniontype(u)) return 0;
return in_union(((jl_uniontype_t*)u)->a, x) || in_union(((jl_uniontype_t*)u)->b, x);
}
static int obviously_in_union(jl_value_t *u, jl_value_t *x)
{
jl_value_t *a = NULL, *b = NULL;
if (jl_is_uniontype(x)) {
a = ((jl_uniontype_t*)x)->a;
b = ((jl_uniontype_t*)x)->b;
JL_GC_PUSH2(&a, &b);
int res = obviously_in_union(u, a) && obviously_in_union(u, b);
JL_GC_POP();
return res;
}
if (jl_is_uniontype(u)) {
a = ((jl_uniontype_t*)u)->a;
b = ((jl_uniontype_t*)u)->b;
JL_GC_PUSH2(&a, &b);
int res = obviously_in_union(a, x) || obviously_in_union(b, x);
JL_GC_POP();
return res;
}
return obviously_egal(u, x);
}
int obviously_disjoint(jl_value_t *a, jl_value_t *b, int specificity)
{
if (a == b || a == (jl_value_t*)jl_any_type || b == (jl_value_t*)jl_any_type)
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))
return 1;
if (jl_is_unionall(a)) a = jl_unwrap_unionall(a);
if (jl_is_unionall(b)) b = jl_unwrap_unionall(b);
if (jl_is_uniontype(a))
return obviously_disjoint(((jl_uniontype_t *)a)->a, b, specificity) &&
obviously_disjoint(((jl_uniontype_t *)a)->b, b, specificity);
if (jl_is_uniontype(b))
return obviously_disjoint(a, ((jl_uniontype_t *)b)->a, specificity) &&
obviously_disjoint(a, ((jl_uniontype_t *)b)->b, specificity);
if (jl_is_datatype(a) && jl_is_datatype(b)) {
jl_datatype_t *ad = (jl_datatype_t*)a, *bd = (jl_datatype_t*)b;
if (ad->name != bd->name) {
jl_datatype_t *temp = ad;
while (temp != jl_any_type && temp->name != bd->name)
temp = temp->super;
if (temp == jl_any_type) {
temp = bd;
while (temp != jl_any_type && temp->name != ad->name)
temp = temp->super;
if (temp == jl_any_type)
return 1;
bd = temp;
}
else {
ad = temp;
}
if (specificity) {
// account for declared subtypes taking priority (issue #21710)
return 0;
}
}
int istuple = (ad->name == jl_tuple_typename);
size_t np;
if (istuple) {
size_t na = jl_nparams(ad), nb = jl_nparams(bd);
if (jl_is_va_tuple(ad)) {
na -= 1;
if (jl_is_va_tuple(bd))
nb -= 1;
}
else if (jl_is_va_tuple(bd)) {
nb -= 1;
}
else if (!specificity && na != nb) {
// note: some disjoint types (e.g. tuples of different lengths) can be more specific
return 1;
}
np = na < nb ? na : nb;
}
else {
np = jl_nparams(ad);
}
size_t i;
for (i = 0; i < np; i++) {
jl_value_t *ai = jl_tparam(ad, i);
jl_value_t *bi = jl_tparam(bd, i);
if (jl_is_typevar(ai) || jl_is_typevar(bi))
continue; // it's possible that Union{} is in this intersection
if (jl_is_type(ai)) {
if (jl_is_type(bi)) {
if (istuple && (ai == jl_bottom_type || bi == jl_bottom_type))
; // TODO: this can return 1 if and when Tuple{Union{}} === Union{}
else if (obviously_disjoint(ai, bi, specificity))
return 1;
}
else if (ai != (jl_value_t*)jl_any_type) {
return 1;
}
}
else if (jl_is_type(bi)) {
if (bi != (jl_value_t*)jl_any_type)
return 1;
}
else if (!jl_egal(ai, bi)) {
return 1;
}
}
}
else if (a == jl_bottom_type || b == jl_bottom_type) {
return 1;
}
return 0;
}
jl_value_t *simple_union(jl_value_t *a, jl_value_t *b);
// compute a least upper bound of `a` and `b`
static jl_value_t *simple_join(jl_value_t *a, jl_value_t *b)
{
if (a == jl_bottom_type || b == (jl_value_t*)jl_any_type || obviously_egal(a, b))
return b;
if (b == jl_bottom_type || a == (jl_value_t*)jl_any_type)
return a;
if (!(jl_is_type(a) || jl_is_typevar(a)) || !(jl_is_type(b) || jl_is_typevar(b)))
return (jl_value_t*)jl_any_type;
if (jl_is_kind(a) && jl_is_type_type(b) && jl_typeof(jl_tparam0(b)) == a)
return a;
if (jl_is_kind(b) && jl_is_type_type(a) && jl_typeof(jl_tparam0(a)) == b)
return b;
if (jl_is_typevar(a) && obviously_egal(b, ((jl_tvar_t*)a)->lb))
return a;
if (jl_is_typevar(b) && obviously_egal(a, ((jl_tvar_t*)b)->lb))
return b;
return simple_union(a, b);
}
jl_value_t *simple_intersect(jl_value_t *a, jl_value_t *b, int overesi);
// Compute a greatest lower bound of `a` and `b`
// For the subtype path, we need to over-estimate this by returning `b` in many cases.
// But for `merge_env`, we'd better under-estimate and return a `Union{}`
static jl_value_t *simple_meet(jl_value_t *a, jl_value_t *b, int overesi)
{
if (a == (jl_value_t*)jl_any_type || b == jl_bottom_type || obviously_egal(a,b))
return b;
if (b == (jl_value_t*)jl_any_type || a == jl_bottom_type)
return a;
if (!(jl_is_type(a) || jl_is_typevar(a)) || !(jl_is_type(b) || jl_is_typevar(b)))
return jl_bottom_type;
if (jl_is_kind(a) && jl_is_type_type(b) && jl_typeof(jl_tparam0(b)) == a)
return b;
if (jl_is_kind(b) && jl_is_type_type(a) && jl_typeof(jl_tparam0(a)) == b)
return a;
if (jl_is_typevar(a) && obviously_egal(b, ((jl_tvar_t*)a)->ub))
return a;
if (jl_is_typevar(b) && obviously_egal(a, ((jl_tvar_t*)b)->ub))
return b;
return simple_intersect(a, b, overesi);
}
// main subtyping algorithm
static int subtype(jl_value_t *x, jl_value_t *y, jl_stenv_t *e, int param);
static int next_union_state(jl_stenv_t *e, int8_t R) JL_NOTSAFEPOINT
{
jl_unionstate_t *state = R ? &e->Runions : &e->Lunions;
if (state->more == 0)
return 0;
// reset `used` and let `pick_union_decision` clean the stack.
state->used = state->more;
statestack_set(state, state->used - 1, 1);
return 1;
}
static int pick_union_decision(jl_stenv_t *e, int8_t R) JL_NOTSAFEPOINT
{
jl_unionstate_t *state = R ? &e->Runions : &e->Lunions;
if (state->depth >= state->used) {
statestack_set(state, state->used, 0);
state->used++;
}
int ui = statestack_get(state, state->depth);
state->depth++;
if (ui == 0)
state->more = state->depth; // memorize that this was the deepest available choice
return ui;
}
static jl_value_t *pick_union_element(jl_value_t *u JL_PROPAGATES_ROOT, jl_stenv_t *e, int8_t R) JL_NOTSAFEPOINT
{
do {
if (pick_union_decision(e, R))
u = ((jl_uniontype_t*)u)->b;
else
u = ((jl_uniontype_t*)u)->a;
} while (jl_is_uniontype(u));
return u;
}
static int local_forall_exists_subtype(jl_value_t *x, jl_value_t *y, jl_stenv_t *e, int param, int limit_slow);
// subtype for variable bounds consistency check. needs its own forall/exists environment.
static int subtype_ccheck(jl_value_t *x, jl_value_t *y, jl_stenv_t *e)
{
if (jl_is_long(x) && jl_is_long(y))
return jl_unbox_long(x) == jl_unbox_long(y) + e->Loffset;
if (x == y)
return 1;
if (x == jl_bottom_type && jl_is_type(y))
return 1;
if (y == (jl_value_t*)jl_any_type && jl_is_type(x))
return 1;
if (jl_is_uniontype(x) && jl_egal(x, y))
return 1;
if (x == (jl_value_t*)jl_any_type && jl_is_datatype(y))
return 0;
jl_saved_unionstate_t oldLunions; push_unionstate(&oldLunions, &e->Lunions);
int sub = local_forall_exists_subtype(x, y, e, 0, 1);
pop_unionstate(&e->Lunions, &oldLunions);
return sub;
}
static int subtype_left_var(jl_value_t *x, jl_value_t *y, jl_stenv_t *e, int param)
{
if (jl_is_long(x) && jl_is_long(y))
return jl_unbox_long(x) == jl_unbox_long(y) + e->Loffset;
if (x == y && !(jl_is_unionall(y)))
return 1;
if (x == jl_bottom_type && jl_is_type(y))
return 1;
if (y == (jl_value_t*)jl_any_type && jl_is_type(x))
return 1;
if (jl_is_uniontype(x) && jl_egal(x, y))
return 1;
if (x == (jl_value_t*)jl_any_type && jl_is_datatype(y))
return 0;
return subtype(x, y, e, param);
}
// use the current context to record where a variable occurred, for the purpose
// of determining whether the variable is concrete.
static void record_var_occurrence(jl_varbinding_t *vb, jl_stenv_t *e, int param) JL_NOTSAFEPOINT
{
if (vb != NULL)
vb->occurs = 1;
if (vb != NULL && param) {
// saturate counters at 2; we don't need values bigger than that
if (param == 2 && e->invdepth > vb->depth0) {
if (vb->occurs_inv < 2)
vb->occurs_inv++;
}
else if (vb->occurs_cov < 2) {
vb->occurs_cov++;
}
// Always set `max_offset` to `-1` during the 1st round intersection.
// Would be recovered in `intersect_varargs`/`subtype_tuple_varargs` if needed.
if (!vb->intersected)
vb->max_offset = -1;
}
}
// is var x's quantifier outside y's in nesting order
static int var_outside(jl_stenv_t *e, jl_tvar_t *x, jl_tvar_t *y)
{
jl_varbinding_t *btemp = e->vars;
while (btemp != NULL) {
if (btemp->var == x) return 0;
if (btemp->var == y) return 1;
btemp = btemp->prev;
}
return 0;
}
static jl_value_t *intersect_aside(jl_value_t *x, jl_value_t *y, jl_stenv_t *e, int depth);
static int reachable_var(jl_value_t *x, jl_tvar_t *y, jl_stenv_t *e);
// check that type var `b` is <: `a`, and update b's upper bound.
static int var_lt(jl_tvar_t *b, jl_value_t *a, jl_stenv_t *e, int param)
{
jl_varbinding_t *bb = lookup(e, b);
if (bb == NULL)
return e->ignore_free || subtype_left_var(b->ub, a, e, param);
record_var_occurrence(bb, e, param);
assert(!jl_is_long(a) || e->Loffset == 0);
if (e->Loffset != 0 && !jl_is_typevar(a) &&
a != jl_bottom_type && a != (jl_value_t *)jl_any_type)
return 0;
if (!bb->right) // check ∀b . b<:a
return subtype_left_var(bb->ub, a, e, param);
if (bb->ub == a)
return 1;
if (!((bb->lb == jl_bottom_type && !jl_is_type(a) && !jl_is_typevar(a)) || subtype_ccheck(bb->lb, a, e)))
return 0;
// for this to work we need to compute issub(left,right) before issub(right,left),
// since otherwise the issub(a, bb.ub) check in var_gt becomes vacuous.
if (e->intersection) {
jl_value_t *ub = intersect_aside(a, bb->ub, e, bb->depth0);
JL_GC_PUSH1(&ub);
if (ub != (jl_value_t*)b && (!jl_is_typevar(ub) || !reachable_var(ub, b, e)))
bb->ub = ub;
JL_GC_POP();
}
else {
bb->ub = simple_meet(bb->ub, a, 1);
}
assert(bb->ub != (jl_value_t*)b);
if (jl_is_typevar(a)) {
jl_varbinding_t *aa = lookup(e, (jl_tvar_t*)a);
if (aa && !aa->right && in_union(bb->lb, a) && bb->depth0 != aa->depth0 && var_outside(e, b, (jl_tvar_t*)a)) {
// an "exists" var cannot equal a "forall" var inside it unless the forall
// var has equal bounds.
return subtype_left_var(aa->ub, aa->lb, e, param);
}
}
return 1;
}
// check that type var `b` is >: `a`, and update b's lower bound.
static int var_gt(jl_tvar_t *b, jl_value_t *a, jl_stenv_t *e, int param)
{
jl_varbinding_t *bb = lookup(e, b);
if (bb == NULL)
return e->ignore_free || subtype_left_var(a, b->lb, e, param);
record_var_occurrence(bb, e, param);
assert(!jl_is_long(a) || e->Loffset == 0);
if (e->Loffset != 0 && !jl_is_typevar(a) &&
a != jl_bottom_type && a != (jl_value_t *)jl_any_type)
return 0;
if (!bb->right) // check ∀b . b>:a
return subtype_left_var(a, bb->lb, e, param);
if (bb->lb == a)
return 1;
if (!((bb->ub == (jl_value_t*)jl_any_type && !jl_is_type(a) && !jl_is_typevar(a)) || subtype_ccheck(a, bb->ub, e)))
return 0;
jl_value_t *lb = simple_join(bb->lb, a);
JL_GC_PUSH1(&lb);
if (!e->intersection || !jl_is_typevar(lb) || !reachable_var(lb, b, e))
bb->lb = lb;
JL_GC_POP();
// this bound should not be directly circular
assert(bb->lb != (jl_value_t*)b);
if (jl_is_typevar(a)) {
jl_varbinding_t *aa = lookup(e, (jl_tvar_t*)a);
if (aa && !aa->right && bb->depth0 != aa->depth0 && param == 2 && var_outside(e, b, (jl_tvar_t*)a))
return subtype_left_var(aa->ub, aa->lb, e, param);
}
return 1;
}
static int subtype_var(jl_tvar_t *b, jl_value_t *a, jl_stenv_t *e, int R, int param)
{
if (e->intersection) {
jl_varbinding_t *bb = lookup(e, (jl_tvar_t*)b);
jl_value_t *bub = bb ? bb->ub : ((jl_tvar_t*)b)->ub;
jl_value_t *blb = bb ? bb->lb : ((jl_tvar_t*)b)->lb;
if (bub == blb && jl_is_typevar(bub)) {
int sub = subtype_var((jl_tvar_t *)bub, a, e, R, param);
return sub;
}
}
if (e->Loffset != 0 && jl_is_long(a)) {
int old_offset = R ? -e->Loffset : e->Loffset;
jl_value_t *na = jl_box_long(jl_unbox_long(a) + old_offset);
JL_GC_PUSH1(&na);
e->Loffset = 0;
int sub = R ? var_gt(b, na, e, param) : var_lt(b, na, e, param);
e->Loffset = R ? -old_offset : old_offset;
JL_GC_POP();
return sub;
}
return R ? var_gt(b, a, e, param) : var_lt(b, a, e, param);
}
// check that a type is concrete or quasi-concrete (Type{T}).
// this is used to check concrete typevars:
// issubtype is false if the lower bound of a concrete type var is not concrete.
int is_leaf_bound(jl_value_t *v) JL_NOTSAFEPOINT
{
if (v == jl_bottom_type)
return 1;
if (jl_is_datatype(v)) {
if (((jl_datatype_t*)v)->name->abstract) {
if (jl_is_type_type(v))
return 1;//!jl_has_free_typevars(jl_tparam0(v));
return 0;
}
return ((jl_datatype_t*)v)->isconcretetype;
}
return !jl_is_type(v) && !jl_is_typevar(v);
}
static int is_leaf_typevar(jl_tvar_t *v) JL_NOTSAFEPOINT
{
return is_leaf_bound(v->lb);
}
static jl_value_t *widen_Type(jl_value_t *t JL_PROPAGATES_ROOT) JL_NOTSAFEPOINT
{
if (jl_is_type_type(t) && !jl_is_typevar(jl_tparam0(t)))
return jl_typeof(jl_tparam0(t));
if (jl_is_uniontype(t)) {
jl_value_t *a = widen_Type(((jl_uniontype_t*)t)->a);
jl_value_t *b = widen_Type(((jl_uniontype_t*)t)->b);
if (a == b)
return a;
}
return t;
}
// convert a type with free variables to a typevar bounded by a UnionAll-wrapped
// version of that type.
// TODO: This loses some inference precision. For example in a case where a
// variable bound is `Vector{_}`, we could potentially infer `Type{Vector{_}} where _`,
// but this causes us to infer the larger `Type{T} where T<:Vector` instead.
// However this is needed because many contexts check `isa(sp, TypeVar)` to determine
// when a static parameter value is not known exactly.
static jl_value_t *fix_inferred_var_bound(jl_tvar_t *var, jl_value_t *ty JL_MAYBE_UNROOTED)
{
if (ty == NULL) // may happen if the user is intersecting with an incomplete type
return (jl_value_t*)var;
if (!jl_is_typevar(ty) && jl_has_free_typevars(ty)) {
jl_value_t *ans = ty;
jl_array_t *vs = NULL;
JL_GC_PUSH2(&ans, &vs);
vs = jl_find_free_typevars(ty);
int i;
for (i = 0; i < jl_array_nrows(vs); i++) {
ans = jl_type_unionall((jl_tvar_t*)jl_array_ptr_ref(vs, i), ans);
}
ans = (jl_value_t*)jl_new_typevar(var->name, jl_bottom_type, ans);
JL_GC_POP();
return ans;
}
return ty;
}
static int var_occurs_inside(jl_value_t *v, jl_tvar_t *var, int inside, int want_inv) JL_NOTSAFEPOINT;
typedef int (*tvar_callback)(void*, int8_t, jl_stenv_t *, int);
static int var_occurs_invariant(jl_value_t *v, jl_tvar_t *var) JL_NOTSAFEPOINT
{
return var_occurs_inside(v, var, 0, 1);
}
static jl_unionall_t *unalias_unionall(jl_unionall_t *u, jl_stenv_t *e)
{
jl_varbinding_t *btemp = e->vars;
// if the var for this unionall (based on identity) already appears somewhere
// in the environment, rename to get a fresh var.
JL_GC_PUSH1(&u);
while (btemp != NULL) {
if (btemp->var == u->var ||
// outer var can only refer to inner var if bounds changed
(btemp->lb != btemp->var->lb && jl_has_typevar(btemp->lb, u->var)) ||
(btemp->ub != btemp->var->ub && jl_has_typevar(btemp->ub, u->var))) {
u = jl_rename_unionall(u);
break;
}
btemp = btemp->prev;
}
JL_GC_POP();
return u;
}
static int subtype_unionall(jl_value_t *t, jl_unionall_t *u, jl_stenv_t *e, int8_t R, int param)
{
u = unalias_unionall(u, e);
jl_varbinding_t vb = { u->var, u->var->lb, u->var->ub, R, 0, 0, 0, 0, 0, 0, 0, 0, 0,
e->invdepth, NULL, e->vars };
JL_GC_PUSH4(&u, &vb.lb, &vb.ub, &vb.innervars);
e->vars = &vb;
int ans;
if (R) {
e->envidx++;
ans = subtype(t, u->body, e, param);
e->envidx--;
// widen Type{x} to typeof(x) in argument position
if (!vb.occurs_inv)
vb.lb = widen_Type(vb.lb);
}
else
ans = subtype(u->body, t, e, param);
// handle the "diagonal dispatch" rule, which says that a type var occurring more
// than once, and only in covariant position, is constrained to concrete types. E.g.
// ( Tuple{Int, Int} <: Tuple{T, T} where T) but
// !( Tuple{Int, String} <: Tuple{T, T} where T)
// Then check concreteness by checking that the lower bound is not an abstract type.
int diagonal = vb.occurs_cov > 1 && !var_occurs_invariant(u->body, u->var);
if (ans && (vb.concrete || (diagonal && is_leaf_typevar(u->var)))) {
if (vb.concrete && !diagonal && !is_leaf_bound(vb.ub)) {
// a non-diagonal var can only be a subtype of a diagonal var if its
// upper bound is concrete.
ans = 0;
}
else if (jl_is_typevar(vb.lb)) {
jl_tvar_t *v = (jl_tvar_t*)vb.lb;
jl_varbinding_t *vlb = lookup(e, v);
if (vlb)
vlb->concrete = 1;
}
else if (!is_leaf_bound(vb.lb)) {
ans = 0;
}
}
e->vars = vb.prev;
if (!ans) {
JL_GC_POP();
return 0;
}
jl_varbinding_t *btemp = e->vars;
if (vb.lb != vb.ub) {
while (btemp != NULL) {
jl_value_t *vu = btemp->ub;
jl_value_t *vl = btemp->lb;
// TODO: this takes a significant amount of time
if (btemp->depth0 != vb.depth0 &&
((vu != (jl_value_t*)vb.var && btemp->var->ub != vu && var_occurs_inside(vu, vb.var, 0, 0)) ||
(vl != (jl_value_t*)vb.var && btemp->var->lb != vl && var_occurs_inside(vl, vb.var, 0, 0)))) {
ans = 0; break;
}
btemp = btemp->prev;
}
}
// fill variable values into `envout` up to `envsz`
if (R && ans && e->envidx < e->envsz) {
jl_value_t *val;
if (vb.intvalued && vb.lb == (jl_value_t*)jl_any_type)
val = (jl_value_t*)jl_wrap_vararg(NULL, NULL, 0); // special token result that represents N::Int in the envout
else if (!vb.occurs_inv && vb.lb != jl_bottom_type)
val = is_leaf_bound(vb.lb) ? vb.lb : (jl_value_t*)jl_new_typevar(u->var->name, jl_bottom_type, vb.lb);
else if (vb.lb == vb.ub)
val = vb.lb;
else if (vb.lb != jl_bottom_type)
// TODO: for now return the least solution, which is what
// method parameters expect.
val = vb.lb;
else if (vb.lb == u->var->lb && vb.ub == u->var->ub)
val = (jl_value_t*)u->var;
else
val = (jl_value_t*)jl_new_typevar(u->var->name, vb.lb, vb.ub);
jl_value_t *oldval = e->envout[e->envidx];
// if we try to assign different variable values (due to checking
// multiple union members), consider the value unknown.
if (oldval && !jl_egal(oldval, val))
e->envout[e->envidx] = (jl_value_t*)u->var;
else
e->envout[e->envidx] = val;
// TODO: substitute the value (if any) of this variable into previous envout entries
}
JL_GC_POP();
return ans;
}
// check n <: (length of vararg type v)
static int check_vararg_length(jl_value_t *v, ssize_t n, jl_stenv_t *e)
{
jl_value_t *N = jl_unwrap_vararg_num(v);
// only do the check if N is free in the tuple type's last parameter
if (N) {
jl_value_t *nn = jl_box_long(n);
JL_GC_PUSH1(&nn);
e->invdepth++;