| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 // TODO(jmesserly): this file needs to be refactored, it's a port from | 5 // TODO(jmesserly): this file needs to be refactored, it's a port from |
| 6 // package:dev_compiler's tests | 6 // package:dev_compiler's tests |
| 7 /// General type checking tests | 7 /// General type checking tests |
| 8 library analyzer.test.src.task.strong.checker_test; | 8 library analyzer.test.src.task.strong.checker_test; |
| 9 | 9 |
| 10 import 'package:unittest/unittest.dart'; | 10 import 'package:unittest/unittest.dart'; |
| (...skipping 666 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 677 Bottom f; | 677 Bottom f; |
| 678 f = /*warning:DOWN_CAST_COMPOSITE*/top; | 678 f = /*warning:DOWN_CAST_COMPOSITE*/top; |
| 679 f = /*warning:DOWN_CAST_COMPOSITE*/left; | 679 f = /*warning:DOWN_CAST_COMPOSITE*/left; |
| 680 f = /*warning:DOWN_CAST_COMPOSITE*/right; | 680 f = /*warning:DOWN_CAST_COMPOSITE*/right; |
| 681 f = bot; | 681 f = bot; |
| 682 } | 682 } |
| 683 } | 683 } |
| 684 '''); | 684 '''); |
| 685 }); | 685 }); |
| 686 | 686 |
| 687 test('dynamic functions - closures are not fuzzy', () { |
| 688 // Regression test for |
| 689 // https://github.com/dart-lang/sdk/issues/26118 |
| 690 checkFile(''' |
| 691 void test1() { |
| 692 void takesF(f(int x)) => null; |
| 693 takesF((dynamic y) => 3); |
| 694 } |
| 695 |
| 696 void test2() { |
| 697 int x; |
| 698 int f/*<T>*/(/*=T*/ t, callback(/*=T*/ x)) { return 3; } |
| 699 f(x, (y) => 3); |
| 700 } |
| 701 '''); |
| 702 }); |
| 703 |
| 687 test('dynamic - known functions', () { | 704 test('dynamic - known functions', () { |
| 688 // Our lattice should look like this: | 705 // Our lattice should look like this: |
| 689 // | 706 // |
| 690 // | 707 // |
| 691 // Bot -> Top | 708 // Bot -> Top |
| 692 // / \ | 709 // / \ |
| 693 // A -> Top Bot -> A | 710 // A -> Top Bot -> A |
| 694 // / \ / | 711 // / \ / |
| 695 // Top -> Top A -> A | 712 // Top -> Top A -> A |
| 696 // \ / | 713 // \ / |
| 697 // Top -> A | 714 // Top -> A |
| 698 // | 715 // |
| 716 // Note that downcasts of known functions are promoted to |
| 717 // static type errors, since they cannot succeed. |
| 718 // This makes some of what look like downcasts turn into |
| 719 // type errors below. |
| 699 checkFile(''' | 720 checkFile(''' |
| 700 class A {} | 721 class A {} |
| 701 | 722 |
| 702 typedef dynamic BotTop(dynamic x); | 723 typedef dynamic BotTop(dynamic x); |
| 703 typedef dynamic ATop(A x); | 724 typedef dynamic ATop(A x); |
| 704 typedef A BotA(dynamic x); | 725 typedef A BotA(dynamic x); |
| 705 typedef A AA(A x); | 726 typedef A AA(A x); |
| 706 typedef A TopA(Object x); | 727 typedef A TopA(Object x); |
| 707 typedef dynamic TopTop(Object x); | 728 typedef dynamic TopTop(Object x); |
| 708 | 729 |
| 709 dynamic aTop(A x) => x; | 730 dynamic aTop(A x) => x; |
| 710 A aa(A x) => x; | 731 A aa(A x) => x; |
| 711 dynamic topTop(dynamic x) => x; | 732 dynamic topTop(dynamic x) => x; |
| 712 A topA(dynamic x) => /*info:DYNAMIC_CAST*/x; | 733 A topA(dynamic x) => /*info:DYNAMIC_CAST*/x; |
| 713 | 734 void apply/*<T>*/(/*=T*/ f0, /*=T*/ f1, /*=T*/ f2, |
| 735 /*=T*/ f3, /*=T*/ f4, /*=T*/ f5) {} |
| 714 void main() { | 736 void main() { |
| 715 BotTop botTop; | 737 BotTop botTop; |
| 716 BotA botA; | 738 BotA botA; |
| 717 { | 739 { |
| 718 BotTop f; | 740 BotTop f; |
| 741 f = topA; |
| 719 f = topTop; | 742 f = topTop; |
| 743 f = aa; |
| 720 f = aTop; | 744 f = aTop; |
| 721 f = topA; | 745 f = botA; |
| 722 f = aa; | 746 f = botTop; |
| 747 apply/*<BotTop>*/( |
| 748 topA, |
| 749 topTop, |
| 750 aa, |
| 751 aTop, |
| 752 botA, |
| 753 botTop |
| 754 ); |
| 755 apply/*<BotTop>*/( |
| 756 (dynamic x) => new A(), |
| 757 (dynamic x) => (x as Object), |
| 758 (A x) => x, |
| 759 (A x) => null, |
| 760 botA, |
| 761 botTop |
| 762 ); |
| 723 } | 763 } |
| 724 { | 764 { |
| 725 ATop f; | 765 ATop f; |
| 766 f = topA; |
| 726 f = topTop; | 767 f = topTop; |
| 768 f = aa; |
| 727 f = aTop; | 769 f = aTop; |
| 728 f = topA; | 770 f = /*warning:DOWN_CAST_COMPOSITE should be severe:STATIC_TYPE_ERROR
*/botA; |
| 729 f = aa; | 771 f = /*warning:DOWN_CAST_COMPOSITE*/botTop; |
| 772 apply/*<ATop>*/( |
| 773 topA, |
| 774 topTop, |
| 775 aa, |
| 776 aTop, |
| 777 /*warning:DOWN_CAST_COMPOSITE should be severe:STATIC_TYPE_ERROR
*/botA, |
| 778 /*warning:DOWN_CAST_COMPOSITE*/botTop |
| 779 ); |
| 780 apply/*<ATop>*/( |
| 781 (dynamic x) => new A(), |
| 782 (dynamic x) => (x as Object), |
| 783 (A x) => x, |
| 784 (A x) => null, |
| 785 /*warning:DOWN_CAST_COMPOSITE should be severe:STATIC_TYPE_ERROR
*/botA, |
| 786 /*warning:DOWN_CAST_COMPOSITE*/botTop |
| 787 ); |
| 730 } | 788 } |
| 731 { | 789 { |
| 732 BotA f; | 790 BotA f; |
| 791 f = topA; |
| 733 f = /*severe:STATIC_TYPE_ERROR*/topTop; | 792 f = /*severe:STATIC_TYPE_ERROR*/topTop; |
| 793 f = aa; |
| 734 f = /*severe:STATIC_TYPE_ERROR*/aTop; | 794 f = /*severe:STATIC_TYPE_ERROR*/aTop; |
| 735 f = topA; | 795 f = botA; |
| 736 f = aa; | 796 f = /*warning:DOWN_CAST_COMPOSITE*/botTop; |
| 797 apply/*<BotA>*/( |
| 798 topA, |
| 799 /*severe:STATIC_TYPE_ERROR*/topTop, |
| 800 aa, |
| 801 /*severe:STATIC_TYPE_ERROR*/aTop, |
| 802 botA, |
| 803 /*warning:DOWN_CAST_COMPOSITE*/botTop |
| 804 ); |
| 805 apply/*<BotA>*/( |
| 806 (dynamic x) => new A(), |
| 807 /*severe:STATIC_TYPE_ERROR*/(dynamic x) => (x as Object), |
| 808 (A x) => x, |
| 809 /*severe:STATIC_TYPE_ERROR*/(A x) => (x as Object), |
| 810 botA, |
| 811 /*warning:DOWN_CAST_COMPOSITE*/botTop |
| 812 ); |
| 737 } | 813 } |
| 738 { | 814 { |
| 739 AA f; | 815 AA f; |
| 816 f = topA; |
| 740 f = /*severe:STATIC_TYPE_ERROR*/topTop; | 817 f = /*severe:STATIC_TYPE_ERROR*/topTop; |
| 741 f = /*severe:STATIC_TYPE_ERROR*/aTop; | |
| 742 f = topA; | |
| 743 f = aa; | 818 f = aa; |
| 819 f = /*severe:STATIC_TYPE_ERROR*/aTop; // known function |
| 820 f = /*warning:DOWN_CAST_COMPOSITE*/botA; |
| 821 f = /*warning:DOWN_CAST_COMPOSITE*/botTop; |
| 822 apply/*<AA>*/( |
| 823 topA, |
| 824 /*severe:STATIC_TYPE_ERROR*/topTop, |
| 825 aa, |
| 826 /*severe:STATIC_TYPE_ERROR*/aTop, // known function |
| 827 /*warning:DOWN_CAST_COMPOSITE*/botA, |
| 828 /*warning:DOWN_CAST_COMPOSITE*/botTop |
| 829 ); |
| 830 apply/*<AA>*/( |
| 831 (dynamic x) => new A(), |
| 832 /*severe:STATIC_TYPE_ERROR*/(dynamic x) => (x as Object), |
| 833 (A x) => x, |
| 834 /*severe:STATIC_TYPE_ERROR*/(A x) => (x as Object), // known fun
ction |
| 835 /*warning:DOWN_CAST_COMPOSITE*/botA, |
| 836 /*warning:DOWN_CAST_COMPOSITE*/botTop |
| 837 ); |
| 744 } | 838 } |
| 745 { | 839 { |
| 746 TopTop f; | 840 TopTop f; |
| 841 f = topA; |
| 747 f = topTop; | 842 f = topTop; |
| 748 f = /*severe:STATIC_TYPE_ERROR*/aTop; | |
| 749 f = topA; | |
| 750 f = /*severe:STATIC_TYPE_ERROR*/aa; | 843 f = /*severe:STATIC_TYPE_ERROR*/aa; |
| 844 f = /*severe:STATIC_TYPE_ERROR*/aTop; // known function |
| 845 f = /*warning:DOWN_CAST_COMPOSITE should be severe:STATIC_TYPE_ERROR
*/botA; |
| 846 f = /*warning:DOWN_CAST_COMPOSITE*/botTop; |
| 847 apply/*<TopTop>*/( |
| 848 topA, |
| 849 topTop, |
| 850 /*severe:STATIC_TYPE_ERROR*/aa, |
| 851 /*severe:STATIC_TYPE_ERROR*/aTop, // known function |
| 852 /*warning:DOWN_CAST_COMPOSITE should be severe:STATIC_TYPE_ERROR
*/botA, |
| 853 /*warning:DOWN_CAST_COMPOSITE*/botTop |
| 854 ); |
| 855 apply/*<TopTop>*/( |
| 856 (dynamic x) => new A(), |
| 857 (dynamic x) => (x as Object), |
| 858 /*severe:STATIC_TYPE_ERROR*/(A x) => x, |
| 859 /*severe:STATIC_TYPE_ERROR*/(A x) => (x as Object), // known fun
ction |
| 860 /*warning:DOWN_CAST_COMPOSITE should be severe:STATIC_TYPE_ERROR
*/botA, |
| 861 /*warning:DOWN_CAST_COMPOSITE*/botTop |
| 862 ); |
| 751 } | 863 } |
| 752 { | 864 { |
| 753 TopA f; | 865 TopA f; |
| 754 f = /*severe:STATIC_TYPE_ERROR*/topTop; | |
| 755 f = /*severe:STATIC_TYPE_ERROR*/aTop; | |
| 756 f = topA; | 866 f = topA; |
| 757 f = /*severe:STATIC_TYPE_ERROR*/aa; | 867 f = /*severe:STATIC_TYPE_ERROR*/topTop; // known function |
| 868 f = /*severe:STATIC_TYPE_ERROR*/aa; // known function |
| 869 f = /*severe:STATIC_TYPE_ERROR*/aTop; // known function |
| 870 f = /*warning:DOWN_CAST_COMPOSITE*/botA; |
| 871 f = /*warning:DOWN_CAST_COMPOSITE*/botTop; |
| 872 apply/*<TopA>*/( |
| 873 topA, |
| 874 /*severe:STATIC_TYPE_ERROR*/topTop, // known function |
| 875 /*severe:STATIC_TYPE_ERROR*/aa, // known function |
| 876 /*severe:STATIC_TYPE_ERROR*/aTop, // known function |
| 877 /*warning:DOWN_CAST_COMPOSITE*/botA, |
| 878 /*warning:DOWN_CAST_COMPOSITE*/botTop |
| 879 ); |
| 880 apply/*<TopA>*/( |
| 881 (dynamic x) => new A(), |
| 882 /*severe:STATIC_TYPE_ERROR*/(dynamic x) => (x as Object), // kno
wn function |
| 883 /*severe:STATIC_TYPE_ERROR*/(A x) => x, // known function |
| 884 /*severe:STATIC_TYPE_ERROR*/(A x) => (x as Object), // known fun
ction |
| 885 /*warning:DOWN_CAST_COMPOSITE*/botA, |
| 886 /*warning:DOWN_CAST_COMPOSITE*/botTop |
| 887 ); |
| 758 } | 888 } |
| 759 } | 889 } |
| 760 '''); | 890 '''); |
| 761 }); | 891 }); |
| 762 | 892 |
| 763 test('function literal variance', () { | 893 test('function literal variance', () { |
| 764 checkFile(''' | 894 checkFile(''' |
| 765 | 895 |
| 766 class A {} | 896 class A {} |
| 767 class B extends A {} | 897 class B extends A {} |
| (...skipping 572 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1340 void main() { | 1470 void main() { |
| 1341 nonGenericFn(x) => null; | 1471 nonGenericFn(x) => null; |
| 1342 { | 1472 { |
| 1343 /*=R*/ f/*<P, R>*/(/*=P*/ p) => null; | 1473 /*=R*/ f/*<P, R>*/(/*=P*/ p) => null; |
| 1344 /*=T*/ g/*<S, T>*/(/*=S*/ s) => null; | 1474 /*=T*/ g/*<S, T>*/(/*=S*/ s) => null; |
| 1345 | 1475 |
| 1346 var local = f; | 1476 var local = f; |
| 1347 local = g; // valid | 1477 local = g; // valid |
| 1348 | 1478 |
| 1349 // Non-generic function cannot subtype a generic one. | 1479 // Non-generic function cannot subtype a generic one. |
| 1350 local = /*severe:STATIC_TYPE_ERROR, warning:INVALID_ASSIGNMENT*/(x)
=> null; | 1480 local = /*warning:INVALID_ASSIGNMENT*/(x) => null; |
| 1351 local = /*warning:INVALID_ASSIGNMENT*/nonGenericFn; | 1481 local = /*warning:INVALID_ASSIGNMENT*/nonGenericFn; |
| 1352 } | 1482 } |
| 1353 { | 1483 { |
| 1354 Iterable/*<R>*/ f/*<P, R>*/(List/*<P>*/ p) => null; | 1484 Iterable/*<R>*/ f/*<P, R>*/(List/*<P>*/ p) => null; |
| 1355 List/*<T>*/ g/*<S, T>*/(Iterable/*<S>*/ s) => null; | 1485 List/*<T>*/ g/*<S, T>*/(Iterable/*<S>*/ s) => null; |
| 1356 | 1486 |
| 1357 var local = f; | 1487 var local = f; |
| 1358 local = g; // valid | 1488 local = g; // valid |
| 1359 | 1489 |
| 1360 var local2 = g; | 1490 var local2 = g; |
| 1361 local = local2; | 1491 local = local2; |
| 1362 local2 = /*severe:STATIC_TYPE_ERROR*/f; | 1492 local2 = /*severe:STATIC_TYPE_ERROR*/f; |
| 1363 local2 = /*warning:DOWN_CAST_COMPOSITE*/local; | 1493 local2 = /*warning:DOWN_CAST_COMPOSITE*/local; |
| 1364 | 1494 |
| 1365 // Non-generic function cannot subtype a generic one. | 1495 // Non-generic function cannot subtype a generic one. |
| 1366 local = /*severe:STATIC_TYPE_ERROR, warning:INVALID_ASSIGNMENT*/(x)
=> null; | 1496 local = /*warning:INVALID_ASSIGNMENT*/(x) => null; |
| 1367 local = /*warning:INVALID_ASSIGNMENT*/nonGenericFn; | 1497 local = /*warning:INVALID_ASSIGNMENT*/nonGenericFn; |
| 1368 } | 1498 } |
| 1369 } | 1499 } |
| 1370 '''); | 1500 '''); |
| 1371 }); | 1501 }); |
| 1372 }); | 1502 }); |
| 1373 | 1503 |
| 1374 test('Relaxed casts', () { | 1504 test('Relaxed casts', () { |
| 1375 checkFile(''' | 1505 checkFile(''' |
| 1376 | 1506 |
| (...skipping 1618 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2995 | 3125 |
| 2996 baz1() sync* { yield* /*info:DYNAMIC_CAST*/x; } | 3126 baz1() sync* { yield* /*info:DYNAMIC_CAST*/x; } |
| 2997 Iterable baz2() sync* { yield* /*info:DYNAMIC_CAST*/x; } | 3127 Iterable baz2() sync* { yield* /*info:DYNAMIC_CAST*/x; } |
| 2998 Iterable<int> baz3() sync* { yield* /*warning:DOWN_CAST_COMPOSITE*/x; } | 3128 Iterable<int> baz3() sync* { yield* /*warning:DOWN_CAST_COMPOSITE*/x; } |
| 2999 Iterable<int> baz4() sync* { yield* bar3(); } | 3129 Iterable<int> baz4() sync* { yield* bar3(); } |
| 3000 Iterable<int> baz5() sync* { yield* /*info:INFERRED_TYPE_ALLOCATION*/new
List(); } | 3130 Iterable<int> baz5() sync* { yield* /*info:INFERRED_TYPE_ALLOCATION*/new
List(); } |
| 3001 '''); | 3131 '''); |
| 3002 }); | 3132 }); |
| 3003 }); | 3133 }); |
| 3004 } | 3134 } |
| OLD | NEW |