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

Side by Side Diff: pkg/analyzer/test/generated/strong_mode_test.dart

Issue 2647833002: fix #28008, fix #28009 implement FutureOr<T> (Closed)
Patch Set: Merge remote-tracking branch 'origin/master' into 28008_futureort Created 3 years, 11 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
OLDNEW
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 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 verify([source]); 123 verify([source]);
124 CompilationUnit unit = analysisResult.unit; 124 CompilationUnit unit = analysisResult.unit;
125 List<Statement> statements = 125 List<Statement> statements =
126 AstFinder.getStatementsInTopLevelFunction(unit, "test"); 126 AstFinder.getStatementsInTopLevelFunction(unit, "test");
127 VariableDeclarationStatement stmt = statements[0]; 127 VariableDeclarationStatement stmt = statements[0];
128 VariableDeclaration decl = stmt.variables.variables[0]; 128 VariableDeclaration decl = stmt.variables.variables[0];
129 Expression call = decl.initializer; 129 Expression call = decl.initializer;
130 _isInt(call.staticType); 130 _isInt(call.staticType);
131 } 131 }
132 132
133 fail_futureOr_downwards7() async {
134 // Test that downwards inference incorporates bounds correctly
135 // when instantiating type variables.
136 // TODO(leafp): I think this should pass once the inference changes
137 // that jmesserly is adding are landed.
138 String code = r'''
139 import "dart:async";
140 T mk<T extends int>(T x) => null;
141 FutureOr<int> test() => mk(new Future.value(42));
142 ''';
143 Source source = addSource(code);
144 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
145 assertNoErrors(source);
146 verify([source]);
147 CompilationUnit unit = analysisResult.unit;
148 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
149 ExpressionFunctionBody body = test.functionExpression.body;
150 MethodInvocation invoke = body.expression;
151 _isFutureOfInt(invoke.staticType);
152 _isFutureOfInt(invoke.argumentList.arguments[0].staticType);
153 }
154
155 fail_futureOr_downwards8() async {
156 // Test that downwards inference incorporates bounds correctly
157 // when instantiating type variables.
158 // TODO(leafp): I think this should pass once the inference changes
159 // that jmesserly is adding are landed.
160 String code = r'''
161 import "dart:async";
162 T mk<T extends Future<Object>>(T x) => null;
163 FutureOr<int> test() => mk(new Future.value(42));
164 ''';
165 Source source = addSource(code);
166 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
167 assertNoErrors(source);
168 verify([source]);
169 CompilationUnit unit = analysisResult.unit;
170 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
171 ExpressionFunctionBody body = test.functionExpression.body;
172 MethodInvocation invoke = body.expression;
173 _isFutureOfInt(invoke.staticType);
174 _isFutureOfInt(invoke.argumentList.arguments[0].staticType);
175 }
176
133 fail_pinning_multipleConstraints1() async { 177 fail_pinning_multipleConstraints1() async {
134 // Test that downwards inference with two different downwards covariant 178 // Test that downwards inference with two different downwards covariant
135 // constraints on the same parameter correctly fails to infer when 179 // constraints on the same parameter correctly fails to infer when
136 // the types do not share a common subtype 180 // the types do not share a common subtype
137 String code = r''' 181 String code = r'''
138 class A<S, T> { 182 class A<S, T> {
139 S s; 183 S s;
140 T t; 184 T t;
141 } 185 }
142 class B<S> extends A<S, S> { B(S s); } 186 class B<S> extends A<S, S> { B(S s); }
(...skipping 773 matching lines...) Expand 10 before | Expand all | Expand 10 after
916 } 960 }
917 } 961 }
918 962
919 expect(functionReturnValue(0).staticType, typeProvider.intType); 963 expect(functionReturnValue(0).staticType, typeProvider.intType);
920 expect(functionReturnValue(1).staticType, typeProvider.intType); 964 expect(functionReturnValue(1).staticType, typeProvider.intType);
921 expect(functionReturnValue(2).staticType, typeProvider.intType); 965 expect(functionReturnValue(2).staticType, typeProvider.intType);
922 expect(functionReturnValue(3).staticType, typeProvider.dynamicType); 966 expect(functionReturnValue(3).staticType, typeProvider.dynamicType);
923 expect(functionReturnValue(4).staticType, typeProvider.stringType); 967 expect(functionReturnValue(4).staticType, typeProvider.stringType);
924 } 968 }
925 969
970 test_futureOr_downwards1() async {
971 // Test that downwards inference interacts correctly with FutureOr
972 // parameters.
973 String code = r'''
974 import "dart:async";
975 Future<T> mk<T>(FutureOr<T> x) => null;
976 Future<int> test() => mk(new Future<int>.value(42));
977 ''';
978 Source source = addSource(code);
979 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
980 assertNoErrors(source);
981 verify([source]);
982 CompilationUnit unit = analysisResult.unit;
983 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
984 ExpressionFunctionBody body = test.functionExpression.body;
985 MethodInvocation invoke = body.expression;
986 _isFutureOfInt(invoke.staticType);
987 }
988
989 test_futureOr_downwards2() async {
990 // Test that downwards inference interacts correctly with FutureOr
991 // parameters when the downwards context is FutureOr
992 String code = r'''
993 import "dart:async";
994 Future<T> mk<T>(FutureOr<T> x) => null;
995 FutureOr<int> test() => mk(new Future<int>.value(42));
996 ''';
997 Source source = addSource(code);
998 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
999 assertNoErrors(source);
1000 verify([source]);
1001 CompilationUnit unit = analysisResult.unit;
1002 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
1003 ExpressionFunctionBody body = test.functionExpression.body;
1004 MethodInvocation invoke = body.expression;
1005 _isFutureOfInt(invoke.staticType);
1006 }
1007
1008 test_futureOr_downwards3() async {
1009 // Test that downwards inference correctly propogates into
1010 // arguments.
1011 String code = r'''
1012 import "dart:async";
1013 Future<T> mk<T>(FutureOr<T> x) => null;
1014 Future<int> test() => mk(new Future.value(42));
1015 ''';
1016 Source source = addSource(code);
1017 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
1018 assertNoErrors(source);
1019 verify([source]);
1020 CompilationUnit unit = analysisResult.unit;
1021 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
1022 ExpressionFunctionBody body = test.functionExpression.body;
1023 MethodInvocation invoke = body.expression;
1024 _isFutureOfInt(invoke.staticType);
1025 _isFutureOfInt(invoke.argumentList.arguments[0].staticType);
1026 }
1027
1028 test_futureOr_downwards4() async {
1029 // Test that downwards inference interacts correctly with FutureOr
1030 // parameters when the downwards context is FutureOr
1031 String code = r'''
1032 import "dart:async";
1033 Future<T> mk<T>(FutureOr<T> x) => null;
1034 FutureOr<int> test() => mk(new Future.value(42));
1035 ''';
1036 Source source = addSource(code);
1037 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
1038 assertNoErrors(source);
1039 verify([source]);
1040 CompilationUnit unit = analysisResult.unit;
1041 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
1042 ExpressionFunctionBody body = test.functionExpression.body;
1043 MethodInvocation invoke = body.expression;
1044 _isFutureOfInt(invoke.staticType);
1045 _isFutureOfInt(invoke.argumentList.arguments[0].staticType);
1046 }
1047
1048 test_futureOr_downwards5() async {
1049 // Test that downwards inference correctly pins the type when it
1050 // comes from a FutureOr
1051 String code = r'''
1052 import "dart:async";
1053 Future<T> mk<T>(FutureOr<T> x) => null;
1054 FutureOr<num> test() => mk(new Future.value(42));
1055 ''';
1056 Source source = addSource(code);
1057 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
1058 assertNoErrors(source);
1059 verify([source]);
1060 CompilationUnit unit = analysisResult.unit;
1061 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
1062 ExpressionFunctionBody body = test.functionExpression.body;
1063 MethodInvocation invoke = body.expression;
1064 _isFutureOf([_isNum])(invoke.staticType);
1065 _isFutureOf([_isNum])(invoke.argumentList.arguments[0].staticType);
1066 }
1067
1068 test_futureOr_downwards6() async {
1069 // Test that downwards inference doesn't decompose FutureOr
1070 // when instantiating type variables.
1071 String code = r'''
1072 import "dart:async";
1073 T mk<T>(T x) => null;
1074 FutureOr<int> test() => mk(new Future.value(42));
1075 ''';
1076 Source source = addSource(code);
1077 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
1078 assertNoErrors(source);
1079 verify([source]);
1080 CompilationUnit unit = analysisResult.unit;
1081 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
1082 ExpressionFunctionBody body = test.functionExpression.body;
1083 MethodInvocation invoke = body.expression;
1084 _isFutureOfInt(invoke.staticType);
1085 _isFutureOfInt(invoke.argumentList.arguments[0].staticType);
1086 }
1087
1088 test_futureOr_downwards9() async {
1089 // Test that downwards inference decomposes correctly with
1090 // other composite types
1091 String code = r'''
1092 import "dart:async";
1093 List<T> mk<T>(T x) => null;
1094 FutureOr<List<int>> test() => mk(3);
1095 ''';
1096 Source source = addSource(code);
1097 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
1098 assertNoErrors(source);
1099 verify([source]);
1100 CompilationUnit unit = analysisResult.unit;
1101 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
1102 ExpressionFunctionBody body = test.functionExpression.body;
1103 MethodInvocation invoke = body.expression;
1104 _isListOf(_isInt)(invoke.staticType);
1105 _isInt(invoke.argumentList.arguments[0].staticType);
1106 }
1107
1108 test_futureOr_methods1() async {
1109 // Test that FutureOr has the Object methods
1110 String code = r'''
1111 import "dart:async";
1112 dynamic test(FutureOr<int> x) => x.toString();
1113 ''';
1114 Source source = addSource(code);
1115 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
1116 assertNoErrors(source);
1117 verify([source]);
1118 CompilationUnit unit = analysisResult.unit;
1119 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
1120 ExpressionFunctionBody body = test.functionExpression.body;
1121 MethodInvocation invoke = body.expression;
1122 _isString(invoke.staticType);
1123 }
1124
1125 test_futureOr_methods2() async {
1126 // Test that FutureOr does not have the constituent type methods
1127 String code = r'''
1128 import "dart:async";
1129 dynamic test(FutureOr<int> x) => x.abs();
1130 ''';
1131 Source source = addSource(code);
1132 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
1133 assertErrors(source, [StaticTypeWarningCode.UNDEFINED_METHOD]);
1134 verify([source]);
1135 CompilationUnit unit = analysisResult.unit;
1136 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
1137 ExpressionFunctionBody body = test.functionExpression.body;
1138 MethodInvocation invoke = body.expression;
1139 _isDynamic(invoke.staticType);
1140 }
1141
1142 test_futureOr_methods3() async {
1143 // Test that FutureOr does not have the Future type methods
1144 String code = r'''
1145 import "dart:async";
1146 dynamic test(FutureOr<int> x) => x.then((x) => x);
1147 ''';
1148 Source source = addSource(code);
1149 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
1150 assertErrors(source, [StaticTypeWarningCode.UNDEFINED_METHOD]);
1151 verify([source]);
1152 CompilationUnit unit = analysisResult.unit;
1153 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
1154 ExpressionFunctionBody body = test.functionExpression.body;
1155 MethodInvocation invoke = body.expression;
1156 _isDynamic(invoke.staticType);
1157 }
1158
1159 test_futureOr_methods4() async {
1160 // Test that FutureOr<dynamic> does not have all methods
1161 String code = r'''
1162 import "dart:async";
1163 dynamic test(FutureOr<dynamic> x) => x.abs();
1164 ''';
1165 Source source = addSource(code);
1166 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
1167 assertErrors(source, [StaticTypeWarningCode.UNDEFINED_METHOD]);
1168 verify([source]);
1169 CompilationUnit unit = analysisResult.unit;
1170 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
1171 ExpressionFunctionBody body = test.functionExpression.body;
1172 MethodInvocation invoke = body.expression;
1173 _isDynamic(invoke.staticType);
1174 }
1175
1176 test_futureOr_upwards1() async {
1177 // Test that upwards inference correctly prefers to instantiate type
1178 // variables with the "smaller" solution when both are possible.
1179 String code = r'''
1180 import "dart:async";
1181 Future<T> mk<T>(FutureOr<T> x) => null;
1182 dynamic test() => mk(new Future<int>.value(42));
1183 ''';
1184 Source source = addSource(code);
1185 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
1186 assertNoErrors(source);
1187 verify([source]);
1188 CompilationUnit unit = analysisResult.unit;
1189 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
1190 ExpressionFunctionBody body = test.functionExpression.body;
1191 MethodInvocation invoke = body.expression;
1192 _isFutureOfInt(invoke.staticType);
1193 }
1194
1195 test_futureOr_upwards2() async {
1196 // Test that upwards inference fails when the solution doesn't
1197 // match the bound.
1198 String code = r'''
1199 import "dart:async";
1200 Future<T> mk<T extends Future<Object>>(FutureOr<T> x) => null;
1201 dynamic test() => mk(new Future<int>.value(42));
1202 ''';
1203 Source source = addSource(code);
1204 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
1205 assertErrors(source, [StrongModeCode.COULD_NOT_INFER]);
1206 verify([source]);
1207 CompilationUnit unit = analysisResult.unit;
1208 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
1209 ExpressionFunctionBody body = test.functionExpression.body;
1210 MethodInvocation invoke = body.expression;
1211 _isFutureOf([_isObject])(invoke.staticType);
1212 }
1213
926 test_inference_hints() async { 1214 test_inference_hints() async {
927 Source source = addSource(r''' 1215 Source source = addSource(r'''
928 void main () { 1216 void main () {
929 var x = 3; 1217 var x = 3;
930 List<int> l0 = []; 1218 List<int> l0 = [];
931 } 1219 }
932 '''); 1220 ''');
933 await computeAnalysisResult(source); 1221 await computeAnalysisResult(source);
934 assertNoErrors(source); 1222 assertNoErrors(source);
935 verify([source]); 1223 verify([source]);
(...skipping 854 matching lines...) Expand 10 before | Expand all | Expand 10 after
1790 /** 2078 /**
1791 * Strong mode static analyzer end to end tests 2079 * Strong mode static analyzer end to end tests
1792 */ 2080 */
1793 @reflectiveTest 2081 @reflectiveTest
1794 class StrongModeStaticTypeAnalyzer2Test extends StaticTypeAnalyzer2TestShared { 2082 class StrongModeStaticTypeAnalyzer2Test extends StaticTypeAnalyzer2TestShared {
1795 void expectStaticInvokeType(String search, String type) { 2083 void expectStaticInvokeType(String search, String type) {
1796 var invocation = findIdentifier(search).parent as MethodInvocation; 2084 var invocation = findIdentifier(search).parent as MethodInvocation;
1797 expect(invocation.staticInvokeType.toString(), type); 2085 expect(invocation.staticInvokeType.toString(), type);
1798 } 2086 }
1799 2087
2088 fail_futureOr_promotion4() async {
2089 // Test that promotion from FutureOr<T> to T works for type
2090 // parameters T
2091 // TODO(leafp): When the restriction on is checks for generic methods
2092 // goes away this should pass.
2093 String code = r'''
2094 import "dart:async";
2095 dynamic test<T extends num>(FutureOr<T> x) => (x is T) &&
2096 (x.abs() == 0);
2097 ''';
2098 await resolveTestUnit(code);
2099 }
2100
1800 fail_genericMethod_tearoff_instantiated() async { 2101 fail_genericMethod_tearoff_instantiated() async {
1801 await resolveTestUnit(r''' 2102 await resolveTestUnit(r'''
1802 class C<E> { 2103 class C<E> {
1803 /*=T*/ f/*<T>*/(E e) => null; 2104 /*=T*/ f/*<T>*/(E e) => null;
1804 static /*=T*/ g/*<T>*/(/*=T*/ e) => null; 2105 static /*=T*/ g/*<T>*/(/*=T*/ e) => null;
1805 static final h = g; 2106 static final h = g;
1806 } 2107 }
1807 2108
1808 /*=T*/ topF/*<T>*/(/*=T*/ e) => null; 2109 /*=T*/ topF/*<T>*/(/*=T*/ e) => null;
1809 var topG = topF; 2110 var topG = topF;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1850 String code = r''' 2151 String code = r'''
1851 main() { 2152 main() {
1852 dynamic a = null; 2153 dynamic a = null;
1853 var foo = a.toString(); 2154 var foo = a.toString();
1854 } 2155 }
1855 '''; 2156 ''';
1856 await resolveTestUnit(code); 2157 await resolveTestUnit(code);
1857 expectInitializerType('foo', 'String', isNull); 2158 expectInitializerType('foo', 'String', isNull);
1858 } 2159 }
1859 2160
2161 test_futureOr_promotion1() async {
2162 // Test that promotion from FutureOr<T> to T works for concrete types
2163 String code = r'''
2164 import "dart:async";
2165 dynamic test(FutureOr<int> x) => (x is int) && (x.abs() == 0);
2166 ''';
2167 await resolveTestUnit(code);
2168 }
2169
2170 test_futureOr_promotion2() async {
2171 // Test that promotion from FutureOr<T> to Future<T> works for concrete
2172 // types
2173 String code = r'''
2174 import "dart:async";
2175 dynamic test(FutureOr<int> x) => (x is Future<int>) &&
2176 (x.then((x) => x) == null);
2177 ''';
2178 await resolveTestUnit(code);
2179 }
2180
2181 test_futureOr_promotion4() async {
2182 // Test that promotion from FutureOr<T> to Future<T> works for type
2183 // parameters T
2184 String code = r'''
2185 import "dart:async";
2186 dynamic test<T extends num>(FutureOr<T> x) => (x is Future<T>) &&
2187 (x.then((x) => x) == null);
2188 ''';
2189 await resolveTestUnit(code);
2190 }
2191
1860 test_genericFunction() async { 2192 test_genericFunction() async {
1861 await resolveTestUnit(r'/*=T*/ f/*<T>*/(/*=T*/ x) => null;'); 2193 await resolveTestUnit(r'/*=T*/ f/*<T>*/(/*=T*/ x) => null;');
1862 expectFunctionType('f', '<T>(T) → T', 2194 expectFunctionType('f', '<T>(T) → T',
1863 elementTypeParams: '[T]', typeFormals: '[T]'); 2195 elementTypeParams: '[T]', typeFormals: '[T]');
1864 SimpleIdentifier f = findIdentifier('f'); 2196 SimpleIdentifier f = findIdentifier('f');
1865 FunctionElementImpl e = f.staticElement; 2197 FunctionElementImpl e = f.staticElement;
1866 FunctionType ft = e.type.instantiate([typeProvider.stringType]); 2198 FunctionType ft = e.type.instantiate([typeProvider.stringType]);
1867 expect(ft.toString(), '(String) → String'); 2199 expect(ft.toString(), '(String) → String');
1868 } 2200 }
1869 2201
(...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after
2393 test_genericMethod_then() async { 2725 test_genericMethod_then() async {
2394 String code = r''' 2726 String code = r'''
2395 import 'dart:async'; 2727 import 'dart:async';
2396 String toString(int x) => x.toString(); 2728 String toString(int x) => x.toString();
2397 main() { 2729 main() {
2398 Future<int> bar = null; 2730 Future<int> bar = null;
2399 var foo = bar.then(toString); 2731 var foo = bar.then(toString);
2400 } 2732 }
2401 '''; 2733 ''';
2402 await resolveTestUnit(code); 2734 await resolveTestUnit(code);
2735
2403 expectInitializerType('foo', 'Future<String>', isNull); 2736 expectInitializerType('foo', 'Future<String>', isNull);
2404 } 2737 }
2405 2738
2406 test_genericMethod_then_prefixed() async { 2739 test_genericMethod_then_prefixed() async {
2407 String code = r''' 2740 String code = r'''
2408 import 'dart:async' as async; 2741 import 'dart:async' as async;
2409 String toString(int x) => x.toString(); 2742 String toString(int x) => x.toString();
2410 main() { 2743 main() {
2411 async.Future<int> bar = null; 2744 async.Future<int> bar = null;
2412 var foo = bar.then(toString); 2745 var foo = bar.then(toString);
(...skipping 668 matching lines...) Expand 10 before | Expand all | Expand 10 after
3081 var v = x; 3414 var v = x;
3082 v; // marker 3415 v; // marker
3083 } 3416 }
3084 int x = 3; 3417 int x = 3;
3085 '''; 3418 ''';
3086 CompilationUnit unit = await resolveSource(code); 3419 CompilationUnit unit = await resolveSource(code);
3087 assertPropagatedAssignedType(code, unit, typeProvider.intType, null); 3420 assertPropagatedAssignedType(code, unit, typeProvider.intType, null);
3088 assertTypeOfMarkedExpression(code, unit, typeProvider.intType, null); 3421 assertTypeOfMarkedExpression(code, unit, typeProvider.intType, null);
3089 } 3422 }
3090 } 3423 }
OLDNEW
« no previous file with comments | « pkg/analyzer/test/generated/resolver_test.dart ('k') | pkg/analyzer/test/src/context/mock_sdk.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698