Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2111)

Side by Side Diff: tests/compiler/dart2js/subtype_test.dart

Issue 13375005: Implement subtype for type variables. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rebased Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sdk/lib/_internal/compiler/implementation/dart_types.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library subtype_test; 5 library subtype_test;
6 6
7 import 'type_test_helper.dart'; 7 import 'type_test_helper.dart';
8 import '../../../sdk/lib/_internal/compiler/implementation/dart_types.dart'; 8 import '../../../sdk/lib/_internal/compiler/implementation/dart_types.dart';
9 import "../../../sdk/lib/_internal/compiler/implementation/elements/elements.dar t" 9 import "../../../sdk/lib/_internal/compiler/implementation/elements/elements.dar t"
10 show Element, ClassElement; 10 show Element, ClassElement;
11 11
12 void main() { 12 void main() {
13 testInterfaceSubtype(); 13 testInterfaceSubtype();
14 testCallableSubtype(); 14 testCallableSubtype();
15 testFunctionSubtyping(); 15 testFunctionSubtyping();
16 testTypedefSubtyping(); 16 testTypedefSubtyping();
17 testFunctionSubtypingOptional(); 17 testFunctionSubtypingOptional();
18 testTypedefSubtypingOptional(); 18 testTypedefSubtypingOptional();
19 testFunctionSubtypingNamed(); 19 testFunctionSubtypingNamed();
20 testTypedefSubtypingNamed(); 20 testTypedefSubtypingNamed();
21 testTypeVariableSubtype();
21 } 22 }
22 23
23 void testInterfaceSubtype() { 24 void testInterfaceSubtype() {
24 var env = new TypeEnvironment(r""" 25 var env = new TypeEnvironment(r"""
25 class A<T> {} 26 class A<T> {}
26 class B<T1, T2> extends A<T1> {} 27 class B<T1, T2> extends A<T1> {}
27 // TODO(johnniwinther): Inheritance with different type arguments is 28 // TODO(johnniwinther): Inheritance with different type arguments is
28 // currently not supported by the implementation. 29 // currently not supported by the implementation.
29 class C<T1, T2> extends B<T2, T1> /*implements A<A<T1>>*/ {} 30 class C<T1, T2> extends B<T2, T1> /*implements A<A<T1>>*/ {}
30 """); 31 """);
(...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after
485 expect(false, 'void___a_int', 'void___a_int_b_int'); 486 expect(false, 'void___a_int', 'void___a_int_b_int');
486 // Test ({int a,int b})->void <: ({int a})->void. 487 // Test ({int a,int b})->void <: ({int a})->void.
487 expect(true, 'void___a_int_b_int', 'void___a_int'); 488 expect(true, 'void___a_int_b_int', 'void___a_int');
488 // Test ({int a,int b,int c})->void <: ({int a,int c})->void. 489 // Test ({int a,int b,int c})->void <: ({int a,int c})->void.
489 expect(true, 'void___a_int_b_int_c_int', 'void___a_int_c_int'); 490 expect(true, 'void___a_int_b_int_c_int', 'void___a_int_c_int');
490 // Test ({int a,int b,int c})->void <: ({int b,int c})->void. 491 // Test ({int a,int b,int c})->void <: ({int b,int c})->void.
491 expect(true, 'void___a_int_b_int_c_int', 'void___b_int_c_int'); 492 expect(true, 'void___a_int_b_int_c_int', 'void___b_int_c_int');
492 // Test ({int a,int b,int c})->void <: ({int c})->void. 493 // Test ({int a,int b,int c})->void <: ({int c})->void.
493 expect(true, 'void___a_int_b_int_c_int', 'void___c_int'); 494 expect(true, 'void___a_int_b_int_c_int', 'void___c_int');
494 } 495 }
496
497 void testTypeVariableSubtype() {
498 var env = new TypeEnvironment(r"""
499 class A<T> {}
500 class B<T extends Object> {}
501 class C<T extends num> {}
502 class D<T extends int> {}
503 class E<T extends S, S extends num> {}
504 class F<T extends num, S extends T> {}
505 class G<T extends T> {}
506 class H<T extends S, S extends T> {}
507 class I<T extends S, S extends U, U extends T> {}
508 class J<T extends S, S extends U, U extends S> {}
509 """);
510
511 void expect(bool value, DartType T, DartType S) {
512 Expect.equals(value, env.isSubtype(T, S), '$T <: $S');
513 }
514
515 ClassElement A = env.getElement('A');
516 TypeVariableType A_T = A.thisType.typeArguments.head;
517 ClassElement B = env.getElement('B');
518 TypeVariableType B_T = B.thisType.typeArguments.head;
519 ClassElement C = env.getElement('C');
520 TypeVariableType C_T = C.thisType.typeArguments.head;
521 ClassElement D = env.getElement('D');
522 TypeVariableType D_T = D.thisType.typeArguments.head;
523 ClassElement E = env.getElement('E');
524 TypeVariableType E_T = E.thisType.typeArguments.head;
525 TypeVariableType E_S = E.thisType.typeArguments.tail.head;
526 ClassElement F = env.getElement('F');
527 TypeVariableType F_T = F.thisType.typeArguments.head;
528 TypeVariableType F_S = F.thisType.typeArguments.tail.head;
529 ClassElement G = env.getElement('G');
530 TypeVariableType G_T = G.thisType.typeArguments.head;
531 ClassElement H = env.getElement('H');
532 TypeVariableType H_T = H.thisType.typeArguments.head;
533 TypeVariableType H_S = H.thisType.typeArguments.tail.head;
534 ClassElement I = env.getElement('I');
535 TypeVariableType I_T = I.thisType.typeArguments.head;
536 TypeVariableType I_S = I.thisType.typeArguments.tail.head;
537 TypeVariableType I_U = I.thisType.typeArguments.tail.tail.head;
538 ClassElement J = env.getElement('J');
539 TypeVariableType J_T = J.thisType.typeArguments.head;
540 TypeVariableType J_S = J.thisType.typeArguments.tail.head;
541 TypeVariableType J_U = J.thisType.typeArguments.tail.tail.head;
542
543 DartType Object_ = env['Object'];
544 DartType num_ = env['num'];
545 DartType int_ = env['int'];
546 DartType String_ = env['String'];
547 DartType dynamic_ = env['dynamic'];
548
549 // class A<T> {}
550 expect(true, A_T, Object_);
551 expect(false, A_T, num_);
552 expect(false, A_T, int_);
553 expect(false, A_T, String_);
554 expect(true, A_T, dynamic_);
555 expect(true, A_T, A_T);
556 expect(false, A_T, B_T);
557
558 // class B<T extends Object> {}
559 expect(true, B_T, Object_);
560 expect(false, B_T, num_);
561 expect(false, B_T, int_);
562 expect(false, B_T, String_);
563 expect(true, B_T, dynamic_);
564 expect(true, B_T, B_T);
565 expect(false, B_T, A_T);
566
567 // class C<T extends num> {}
568 expect(true, C_T, Object_);
569 expect(true, C_T, num_);
570 expect(false, C_T, int_);
571 expect(false, C_T, String_);
572 expect(true, C_T, dynamic_);
573 expect(true, C_T, C_T);
574 expect(false, C_T, A_T);
575
576 // class D<T extends int> {}
577 expect(true, D_T, Object_);
578 expect(true, D_T, num_);
579 expect(true, D_T, int_);
580 expect(false, D_T, String_);
581 expect(true, D_T, dynamic_);
582 expect(true, D_T, D_T);
583 expect(false, D_T, A_T);
584
585 // class E<T extends S, S extends num> {}
586 expect(true, E_T, Object_);
587 expect(true, E_T, num_);
588 expect(false, E_T, int_);
589 expect(false, E_T, String_);
590 expect(true, E_T, dynamic_);
591 expect(true, E_T, E_T);
592 expect(true, E_T, E_S);
593 expect(false, E_T, A_T);
594
595 expect(true, E_S, Object_);
596 expect(true, E_S, num_);
597 expect(false, E_S, int_);
598 expect(false, E_S, String_);
599 expect(true, E_S, dynamic_);
600 expect(false, E_S, E_T);
601 expect(true, E_S, E_S);
602 expect(false, E_S, A_T);
603
604 // class F<T extends num, S extends T> {}
605 expect(true, F_T, Object_);
606 expect(true, F_T, num_);
607 expect(false, F_T, int_);
608 expect(false, F_T, String_);
609 expect(true, F_T, dynamic_);
610 expect(false, F_T, F_S);
611 expect(true, F_T, F_T);
612 expect(false, F_T, A_T);
613
614 expect(true, F_S, Object_);
615 expect(true, F_S, num_);
616 expect(false, F_S, int_);
617 expect(false, F_S, String_);
618 expect(true, F_S, dynamic_);
619 expect(true, F_S, F_S);
620 expect(true, F_S, F_T);
621 expect(false, F_S, A_T);
622
623 // class G<T extends T> {}
624 expect(true, G_T, Object_);
625 expect(false, G_T, num_);
626 expect(false, G_T, int_);
627 expect(false, G_T, String_);
628 expect(true, G_T, dynamic_);
629 expect(true, G_T, G_T);
630 expect(false, G_T, A_T);
631
632 // class H<T extends S, S extends T> {}
633 expect(true, H_T, Object_);
634 expect(false, H_T, num_);
635 expect(false, H_T, int_);
636 expect(false, H_T, String_);
637 expect(true, H_T, dynamic_);
638 expect(true, H_T, H_T);
639 expect(true, H_T, H_S);
640 expect(false, H_T, A_T);
641
642 expect(true, H_S, Object_);
643 expect(false, H_S, num_);
644 expect(false, H_S, int_);
645 expect(false, H_S, String_);
646 expect(true, H_S, dynamic_);
647 expect(true, H_S, H_T);
648 expect(true, H_S, H_S);
649 expect(false, H_S, A_T);
650
651 // class I<T extends S, S extends U, U extends T> {}
652 expect(true, I_T, Object_);
653 expect(false, I_T, num_);
654 expect(false, I_T, int_);
655 expect(false, I_T, String_);
656 expect(true, I_T, dynamic_);
657 expect(true, I_T, I_T);
658 expect(true, I_T, I_S);
659 expect(true, I_T, I_U);
660 expect(false, I_T, A_T);
661
662 expect(true, I_S, Object_);
663 expect(false, I_S, num_);
664 expect(false, I_S, int_);
665 expect(false, I_S, String_);
666 expect(true, I_S, dynamic_);
667 expect(true, I_S, I_T);
668 expect(true, I_S, I_S);
669 expect(true, I_S, I_U);
670 expect(false, I_S, A_T);
671
672 expect(true, I_U, Object_);
673 expect(false, I_U, num_);
674 expect(false, I_U, int_);
675 expect(false, I_U, String_);
676 expect(true, I_U, dynamic_);
677 expect(true, I_U, I_T);
678 expect(true, I_U, I_S);
679 expect(true, I_U, I_U);
680 expect(false, I_U, A_T);
681
682 // class J<T extends S, S extends U, U extends S> {}
683 expect(true, J_T, Object_);
684 expect(false, J_T, num_);
685 expect(false, J_T, int_);
686 expect(false, J_T, String_);
687 expect(true, J_T, dynamic_);
688 expect(true, J_T, J_T);
689 expect(true, J_T, J_S);
690 expect(true, J_T, J_U);
691 expect(false, J_T, A_T);
692
693 expect(true, J_S, Object_);
694 expect(false, J_S, num_);
695 expect(false, J_S, int_);
696 expect(false, J_S, String_);
697 expect(true, J_S, dynamic_);
698 expect(false, J_S, J_T);
699 expect(true, J_S, J_S);
700 expect(true, J_S, J_U);
701 expect(false, J_S, A_T);
702
703 expect(true, J_U, Object_);
704 expect(false, J_U, num_);
705 expect(false, J_U, int_);
706 expect(false, J_U, String_);
707 expect(true, J_U, dynamic_);
708 expect(false, J_U, J_T);
709 expect(true, J_U, J_S);
710 expect(true, J_U, J_U);
711 expect(false, J_U, A_T);
712 }
OLDNEW
« no previous file with comments | « sdk/lib/_internal/compiler/implementation/dart_types.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698