OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 analyzer.test.generated.strong_mode_test; | 5 library analyzer.test.generated.strong_mode_test; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:analyzer/dart/ast/ast.dart'; | 9 import 'package:analyzer/dart/ast/ast.dart'; |
10 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; | 10 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; |
(...skipping 937 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
948 } | 948 } |
949 } | 949 } |
950 | 950 |
951 expect(functionReturnValue(0).staticType, typeProvider.intType); | 951 expect(functionReturnValue(0).staticType, typeProvider.intType); |
952 expect(functionReturnValue(1).staticType, typeProvider.intType); | 952 expect(functionReturnValue(1).staticType, typeProvider.intType); |
953 expect(functionReturnValue(2).staticType, typeProvider.intType); | 953 expect(functionReturnValue(2).staticType, typeProvider.intType); |
954 expect(functionReturnValue(3).staticType, typeProvider.dynamicType); | 954 expect(functionReturnValue(3).staticType, typeProvider.dynamicType); |
955 expect(functionReturnValue(4).staticType, typeProvider.stringType); | 955 expect(functionReturnValue(4).staticType, typeProvider.stringType); |
956 } | 956 } |
957 | 957 |
| 958 test_futureOr_assignFromFuture() async { |
| 959 // Test a Future<T> can be assigned to FutureOr<T>. |
| 960 MethodInvocation invoke = await _testFutureOr(r''' |
| 961 FutureOr<T> mk<T>(Future<T> x) => x; |
| 962 test() => mk(new Future<int>.value(42)); |
| 963 '''); |
| 964 _isFutureOrOfInt(invoke.staticType); |
| 965 } |
| 966 |
| 967 test_futureOr_assignFromValue() async { |
| 968 // Test a T can be assigned to FutureOr<T>. |
| 969 MethodInvocation invoke = await _testFutureOr(r''' |
| 970 FutureOr<T> mk<T>(T x) => x; |
| 971 test() => mk(42); |
| 972 '''); |
| 973 _isFutureOrOfInt(invoke.staticType); |
| 974 } |
| 975 |
| 976 test_futureOr_asyncExpressionBody() async { |
| 977 // A FutureOr<T> can be used as the expression body for an async function |
| 978 MethodInvocation invoke = await _testFutureOr(r''' |
| 979 Future<T> mk<T>(FutureOr<T> x) async => x; |
| 980 test() => mk(42); |
| 981 '''); |
| 982 _isFutureOfInt(invoke.staticType); |
| 983 } |
| 984 |
| 985 test_futureOr_asyncReturn() async { |
| 986 // A FutureOr<T> can be used as the return value for an async function |
| 987 MethodInvocation invoke = await _testFutureOr(r''' |
| 988 Future<T> mk<T>(FutureOr<T> x) async { return x; } |
| 989 test() => mk(42); |
| 990 '''); |
| 991 _isFutureOfInt(invoke.staticType); |
| 992 } |
| 993 |
| 994 test_futureOr_await() async { |
| 995 // Test a FutureOr<T> can be awaited. |
| 996 MethodInvocation invoke = await _testFutureOr(r''' |
| 997 Future<T> mk<T>(FutureOr<T> x) async => await x; |
| 998 test() => mk(42); |
| 999 '''); |
| 1000 _isFutureOfInt(invoke.staticType); |
| 1001 } |
| 1002 |
958 test_futureOr_downwards1() async { | 1003 test_futureOr_downwards1() async { |
959 // Test that downwards inference interacts correctly with FutureOr | 1004 // Test that downwards inference interacts correctly with FutureOr |
960 // parameters. | 1005 // parameters. |
961 MethodInvocation invoke = await _testFutureOr(r''' | 1006 MethodInvocation invoke = await _testFutureOr(r''' |
962 Future<T> mk<T>(FutureOr<T> x) => null; | 1007 Future<T> mk<T>(FutureOr<T> x) => null; |
963 Future<int> test() => mk(new Future<int>.value(42)); | 1008 Future<int> test() => mk(new Future<int>.value(42)); |
964 '''); | 1009 '''); |
965 _isFutureOfInt(invoke.staticType); | 1010 _isFutureOfInt(invoke.staticType); |
966 } | 1011 } |
967 | 1012 |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1083 // match the bound. | 1128 // match the bound. |
1084 MethodInvocation invoke = await _testFutureOr( | 1129 MethodInvocation invoke = await _testFutureOr( |
1085 r''' | 1130 r''' |
1086 Future<T> mk<T extends Future<Object>>(FutureOr<T> x) => null; | 1131 Future<T> mk<T extends Future<Object>>(FutureOr<T> x) => null; |
1087 dynamic test() => mk(new Future<int>.value(42)); | 1132 dynamic test() => mk(new Future<int>.value(42)); |
1088 ''', | 1133 ''', |
1089 errors: [StrongModeCode.COULD_NOT_INFER]); | 1134 errors: [StrongModeCode.COULD_NOT_INFER]); |
1090 _isFutureOf([_isObject])(invoke.staticType); | 1135 _isFutureOf([_isObject])(invoke.staticType); |
1091 } | 1136 } |
1092 | 1137 |
1093 test_futureOr_assignFromValue() async { | |
1094 // Test a T can be assigned to FutureOr<T>. | |
1095 MethodInvocation invoke = await _testFutureOr(r''' | |
1096 FutureOr<T> mk<T>(T x) => x; | |
1097 test() => mk(42); | |
1098 '''); | |
1099 _isFutureOrOfInt(invoke.staticType); | |
1100 } | |
1101 | |
1102 test_futureOr_assignFromFuture() async { | |
1103 // Test a Future<T> can be assigned to FutureOr<T>. | |
1104 MethodInvocation invoke = await _testFutureOr(r''' | |
1105 FutureOr<T> mk<T>(Future<T> x) => x; | |
1106 test() => mk(new Future<int>.value(42)); | |
1107 '''); | |
1108 _isFutureOrOfInt(invoke.staticType); | |
1109 } | |
1110 | |
1111 test_futureOr_await() async { | |
1112 // Test a FutureOr<T> can be awaited. | |
1113 MethodInvocation invoke = await _testFutureOr(r''' | |
1114 Future<T> mk<T>(FutureOr<T> x) async => await x; | |
1115 test() => mk(42); | |
1116 '''); | |
1117 _isFutureOfInt(invoke.staticType); | |
1118 } | |
1119 | |
1120 test_futureOr_asyncExpressionBody() async { | |
1121 // A FutureOr<T> can be used as the expression body for an async function | |
1122 MethodInvocation invoke = await _testFutureOr(r''' | |
1123 Future<T> mk<T>(FutureOr<T> x) async => x; | |
1124 test() => mk(42); | |
1125 '''); | |
1126 _isFutureOfInt(invoke.staticType); | |
1127 } | |
1128 | |
1129 test_futureOr_asyncReturn() async { | |
1130 // A FutureOr<T> can be used as the return value for an async function | |
1131 MethodInvocation invoke = await _testFutureOr(r''' | |
1132 Future<T> mk<T>(FutureOr<T> x) async { return x; } | |
1133 test() => mk(42); | |
1134 '''); | |
1135 _isFutureOfInt(invoke.staticType); | |
1136 } | |
1137 | |
1138 test_inference_hints() async { | 1138 test_inference_hints() async { |
1139 Source source = addSource(r''' | 1139 Source source = addSource(r''' |
1140 void main () { | 1140 void main () { |
1141 var x = 3; | 1141 var x = 3; |
1142 List<int> l0 = []; | 1142 List<int> l0 = []; |
1143 } | 1143 } |
1144 '''); | 1144 '''); |
1145 await computeAnalysisResult(source); | 1145 await computeAnalysisResult(source); |
1146 assertNoErrors(source); | 1146 assertNoErrors(source); |
1147 verify([source]); | 1147 verify([source]); |
(...skipping 1601 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2749 '''; | 2749 '''; |
2750 await resolveTestUnit(code); | 2750 await resolveTestUnit(code); |
2751 expectIdentifierType('ai', "A<dynamic>"); | 2751 expectIdentifierType('ai', "A<dynamic>"); |
2752 expectIdentifierType('bi', "B<num>"); | 2752 expectIdentifierType('bi', "B<num>"); |
2753 expectIdentifierType('ci', "C<int, B<int>, A<dynamic>>"); | 2753 expectIdentifierType('ci', "C<int, B<int>, A<dynamic>>"); |
2754 expectIdentifierType('aa', "A<dynamic>"); | 2754 expectIdentifierType('aa', "A<dynamic>"); |
2755 expectIdentifierType('bb', "B<num>"); | 2755 expectIdentifierType('bb', "B<num>"); |
2756 expectIdentifierType('cc', "C<int, B<int>, A<dynamic>>"); | 2756 expectIdentifierType('cc', "C<int, B<int>, A<dynamic>>"); |
2757 } | 2757 } |
2758 | 2758 |
| 2759 @failingTest |
| 2760 test_instantiateToBounds_class_error_extension_malbounded() async { |
| 2761 // Test that superclasses are strictly checked for malbounded default |
| 2762 // types |
| 2763 String code = r''' |
| 2764 class C<T0 extends List<T1>, T1 extends List<T0>> {} |
| 2765 class D extends C {} |
| 2766 '''; |
| 2767 await resolveTestUnit(code, noErrors: false); |
| 2768 assertErrors(testSource, [StrongModeCode.NO_DEFAULT_BOUNDS]); |
| 2769 } |
| 2770 |
| 2771 @failingTest |
| 2772 test_instantiateToBounds_class_error_instantiation_malbounded() async { |
| 2773 // Test that instance creations are strictly checked for malbounded default |
| 2774 // types |
| 2775 String code = r''' |
| 2776 class C<T0 extends List<T1>, T1 extends List<T0>> {} |
| 2777 void test() { |
| 2778 var c = new C(); |
| 2779 } |
| 2780 '''; |
| 2781 await resolveTestUnit(code, noErrors: false); |
| 2782 assertErrors(testSource, [StrongModeCode.NO_DEFAULT_BOUNDS]); |
| 2783 expectIdentifierType('c;', 'C<List<dynamic>, List<dynamic>>'); |
| 2784 } |
| 2785 |
2759 test_instantiateToBounds_class_error_recursion() async { | 2786 test_instantiateToBounds_class_error_recursion() async { |
2760 String code = r''' | 2787 String code = r''' |
2761 class C<T0 extends List<T1>, T1 extends List<T0>> {} | 2788 class C<T0 extends List<T1>, T1 extends List<T0>> {} |
2762 C c; | 2789 C c; |
2763 '''; | 2790 '''; |
2764 await resolveTestUnit(code, noErrors: false); | 2791 await resolveTestUnit(code, noErrors: false); |
2765 assertErrors(testSource, [StrongModeCode.NO_DEFAULT_BOUNDS]); | 2792 assertNoErrors(testSource); |
2766 expectIdentifierType('c;', 'C<dynamic, dynamic>'); | 2793 expectIdentifierType('c;', 'C<List<dynamic>, List<dynamic>>'); |
2767 } | 2794 } |
2768 | 2795 |
2769 test_instantiateToBounds_class_error_recursion_self() async { | 2796 test_instantiateToBounds_class_error_recursion_self() async { |
2770 String code = r''' | 2797 String code = r''' |
2771 class C<T extends C<T>> {} | 2798 class C<T extends C<T>> {} |
2772 C c; | 2799 C c; |
2773 '''; | 2800 '''; |
2774 await resolveTestUnit(code, noErrors: false); | 2801 await resolveTestUnit(code, noErrors: false); |
2775 assertErrors(testSource, [StrongModeCode.NO_DEFAULT_BOUNDS]); | 2802 assertNoErrors(testSource); |
2776 expectIdentifierType('c;', 'C<dynamic>'); | 2803 expectIdentifierType('c;', 'C<C<dynamic>>'); |
2777 } | 2804 } |
2778 | 2805 |
2779 test_instantiateToBounds_class_error_recursion_self2() async { | 2806 test_instantiateToBounds_class_error_recursion_self2() async { |
2780 String code = r''' | 2807 String code = r''' |
2781 class A<E> {} | 2808 class A<E> {} |
2782 class C<T extends A<T>> {} | 2809 class C<T extends A<T>> {} |
2783 C c; | 2810 C c; |
2784 '''; | 2811 '''; |
2785 await resolveTestUnit(code, noErrors: false); | 2812 await resolveTestUnit(code, noErrors: false); |
2786 assertErrors(testSource, [StrongModeCode.NO_DEFAULT_BOUNDS]); | 2813 assertNoErrors(testSource); |
2787 expectIdentifierType('c;', 'C<dynamic>'); | 2814 expectIdentifierType('c;', 'C<A<dynamic>>'); |
2788 } | 2815 } |
2789 | 2816 |
2790 test_instantiateToBounds_class_error_typedef() async { | 2817 test_instantiateToBounds_class_error_typedef() async { |
2791 String code = r''' | 2818 String code = r''' |
2792 typedef T F<T>(T x); | 2819 typedef T F<T>(T x); |
2793 class C<T extends F<T>> {} | 2820 class C<T extends F<T>> {} |
2794 C c; | 2821 C c; |
2795 '''; | 2822 '''; |
2796 await resolveTestUnit(code, noErrors: false); | 2823 await resolveTestUnit(code, noErrors: false); |
2797 assertErrors(testSource, [StrongModeCode.NO_DEFAULT_BOUNDS]); | 2824 assertNoErrors(testSource); |
2798 expectIdentifierType('c;', 'C<dynamic>'); | 2825 expectIdentifierType('c;', 'C<(dynamic) → dynamic>'); |
2799 } | 2826 } |
2800 | 2827 |
2801 test_instantiateToBounds_class_ok_implicitDynamic_multi() async { | 2828 test_instantiateToBounds_class_ok_implicitDynamic_multi() async { |
2802 String code = r''' | 2829 String code = r''' |
2803 class C<T0 extends Map<T1, T2>, T1 extends List, T2 extends int> {} | 2830 class C<T0 extends Map<T1, T2>, T1 extends List, T2 extends int> {} |
2804 C c; | 2831 C c; |
2805 '''; | 2832 '''; |
2806 await resolveTestUnit(code); | 2833 await resolveTestUnit(code); |
2807 assertNoErrors(testSource); | 2834 assertNoErrors(testSource); |
2808 expectIdentifierType( | 2835 expectIdentifierType( |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2863 } | 2890 } |
2864 '''; | 2891 '''; |
2865 await resolveTestUnit(code); | 2892 await resolveTestUnit(code); |
2866 assertNoErrors(testSource); | 2893 assertNoErrors(testSource); |
2867 expectIdentifierType('a;', 'A<dynamic>'); | 2894 expectIdentifierType('a;', 'A<dynamic>'); |
2868 expectIdentifierType('b;', 'B<num>'); | 2895 expectIdentifierType('b;', 'B<num>'); |
2869 expectIdentifierType('c;', 'C<List<int>>'); | 2896 expectIdentifierType('c;', 'C<List<int>>'); |
2870 expectIdentifierType('d;', 'D<A<dynamic>>'); | 2897 expectIdentifierType('d;', 'D<A<dynamic>>'); |
2871 } | 2898 } |
2872 | 2899 |
| 2900 @failingTest |
| 2901 test_instantiateToBounds_generic_function_error_malbounded() async { |
| 2902 // Test that generic methods are strictly checked for malbounded default |
| 2903 // types |
| 2904 String code = r''' |
| 2905 T0 f<T0 extends List<T1>, T1 extends List<T0>>() {} |
| 2906 void g() { |
| 2907 var c = f(); |
| 2908 return; |
| 2909 } |
| 2910 '''; |
| 2911 await resolveTestUnit(code, noErrors: false); |
| 2912 assertErrors(testSource, [StrongModeCode.NO_DEFAULT_BOUNDS]); |
| 2913 expectIdentifierType('c;', 'List<dynamic>'); |
| 2914 } |
| 2915 |
2873 test_instantiateToBounds_method_ok_referenceOther_before() async { | 2916 test_instantiateToBounds_method_ok_referenceOther_before() async { |
2874 String code = r''' | 2917 String code = r''' |
2875 class C<T> { | 2918 class C<T> { |
2876 void m<S0 extends T, S1 extends List<S0>>(S0 p0, S1 p1) {} | 2919 void m<S0 extends T, S1 extends List<S0>>(S0 p0, S1 p1) {} |
2877 | 2920 |
2878 void main() { | 2921 void main() { |
2879 m(null, null); | 2922 m(null, null); |
2880 } | 2923 } |
2881 } | 2924 } |
2882 '''; | 2925 '''; |
(...skipping 543 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3426 var v = x; | 3469 var v = x; |
3427 v; // marker | 3470 v; // marker |
3428 } | 3471 } |
3429 int x = 3; | 3472 int x = 3; |
3430 '''; | 3473 '''; |
3431 CompilationUnit unit = await resolveSource(code); | 3474 CompilationUnit unit = await resolveSource(code); |
3432 assertPropagatedAssignedType(code, unit, typeProvider.intType, null); | 3475 assertPropagatedAssignedType(code, unit, typeProvider.intType, null); |
3433 assertTypeOfMarkedExpression(code, unit, typeProvider.intType, null); | 3476 assertTypeOfMarkedExpression(code, unit, typeProvider.intType, null); |
3434 } | 3477 } |
3435 } | 3478 } |
OLD | NEW |