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

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

Issue 2456803004: fixes #27586, prefer context type in generic inference (Closed)
Patch Set: fix Created 3 years, 9 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 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 _isInstantiationOf(_hasElementOf(typeProvider.futureOrType)); 85 _isInstantiationOf(_hasElementOf(typeProvider.futureOrType));
86 _isFutureOfDynamic = _isFutureOf([_isDynamic]); 86 _isFutureOfDynamic = _isFutureOf([_isDynamic]);
87 _isFutureOfInt = _isFutureOf([_isInt]); 87 _isFutureOfInt = _isFutureOf([_isInt]);
88 _isFutureOfNull = _isFutureOf([_isNull]); 88 _isFutureOfNull = _isFutureOf([_isNull]);
89 _isFutureOrOfInt = _isFutureOrOf([_isInt]); 89 _isFutureOrOfInt = _isFutureOrOf([_isInt]);
90 _isStreamOf = _isInstantiationOf(_hasElementOf(typeProvider.streamType)); 90 _isStreamOf = _isInstantiationOf(_hasElementOf(typeProvider.streamType));
91 } 91 }
92 return result; 92 return result;
93 } 93 }
94 94
95 fail_constrainedByBounds3() async {
96 // Test that upwards inference with two type variables does
97 // not propogate from the constrained variable to the unconstrained
98 // variable if they are ordered right to left, and that if the result
99 // is not a valid instantiation an error is issued
100 String code = r'''
101 T f<T extends S, S extends int>(S x) => null;
102 void test() { var x = f(3); }
103 ''';
104 Source source = addSource(code);
105 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
106 assertErrors(
107 source, [StaticTypeWarningCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS]);
108 verify([source]);
109 CompilationUnit unit = analysisResult.unit;
110 List<Statement> statements =
111 AstFinder.getStatementsInTopLevelFunction(unit, "test");
112 VariableDeclarationStatement stmt = statements[0];
113 VariableDeclaration decl = stmt.variables.variables[0];
114 Expression call = decl.initializer;
115 _isDynamic(call.staticType);
116 }
117
118 fail_constrainedByBounds5() async {
119 // Test that upwards inference with two type variables does not
120 // propogate from the constrained variable to the unconstrained
121 // variable if they are ordered right to left, when the variable
122 // appears co and contra variantly, and that an error is issued
123 // for the non-matching bound.
124 String code = r'''
125 typedef To Func1<From, To>(From x);
126 T f<T extends Func1<S, S>, S>(S x) => null;
127 void test() { var x = f(3)(4); }
128 ''';
129 Source source = addSource(code);
130 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
131 assertErrors(
132 source, [StaticTypeWarningCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS]);
133 verify([source]);
134 CompilationUnit unit = analysisResult.unit;
135 List<Statement> statements =
136 AstFinder.getStatementsInTopLevelFunction(unit, "test");
137 VariableDeclarationStatement stmt = statements[0];
138 VariableDeclaration decl = stmt.variables.variables[0];
139 Expression call = decl.initializer;
140 _isInt(call.staticType);
141 }
142
143 fail_futureOr_downwards7() async {
144 // Test that downwards inference incorporates bounds correctly
145 // when instantiating type variables.
146 // TODO(leafp): I think this should pass once the inference changes
147 // that jmesserly is adding are landed.
148 MethodInvocation invoke = await _testFutureOr(r'''
149 T mk<T extends int>(T x) => null;
150 FutureOr<int> test() => mk(new Future.value(42));
151 ''');
152 _isFutureOfInt(invoke.staticType);
153 _isFutureOfInt(invoke.argumentList.arguments[0].staticType);
154 }
155
156 fail_futureOr_downwards8() async {
157 // Test that downwards inference incorporates bounds correctly
158 // when instantiating type variables.
159 // TODO(leafp): I think this should pass once the inference changes
160 // that jmesserly is adding are landed.
161 MethodInvocation invoke = await _testFutureOr(r'''
162 T mk<T extends Future<Object>>(T x) => null;
163 FutureOr<int> test() => mk(new Future.value(42));
164 ''');
165 _isFutureOfInt(invoke.staticType);
166 _isFutureOfInt(invoke.argumentList.arguments[0].staticType);
167 }
168
169 fail_pinning_multipleConstraints1() async {
170 // Test that downwards inference with two different downwards covariant
171 // constraints on the same parameter correctly fails to infer when
172 // the types do not share a common subtype
173 String code = r'''
174 class A<S, T> {
175 S s;
176 T t;
177 }
178 class B<S> extends A<S, S> { B(S s); }
179 A<int, String> test() => new B(3);
180 ''';
181 Source source = addSource(code);
182 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
183 assertErrors(source, [StrongModeCode.COULD_NOT_INFER]);
184 verify([source]);
185 CompilationUnit unit = analysisResult.unit;
186 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
187 ExpressionFunctionBody body = test.functionExpression.body;
188 DartType type = body.expression.staticType;
189
190 Element elementB = AstFinder.getClass(unit, "B").element;
191
192 _isInstantiationOf(_hasElement(elementB))([_isDynamic])(type);
193 }
194
195 fail_pinning_multipleConstraints2() async {
196 // Test that downwards inference with two identical downwards covariant
197 // constraints on the same parameter correctly infers and pins the type
198 String code = r'''
199 class A<S, T> {
200 S s;
201 T t;
202 }
203 class B<S> extends A<S, S> { B(S s); }
204 A<num, num> test() => new B(3);
205 ''';
206 Source source = addSource(code);
207 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
208 assertNoErrors(source);
209 verify([source]);
210 CompilationUnit unit = analysisResult.unit;
211 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
212 ExpressionFunctionBody body = test.functionExpression.body;
213 DartType type = body.expression.staticType;
214
215 Element elementB = AstFinder.getClass(unit, "B").element;
216
217 _isInstantiationOf(_hasElement(elementB))([_isNum])(type);
218 }
219
220 fail_pinning_multipleConstraints3() async {
221 // Test that downwards inference with two different downwards covariant
222 // constraints on the same parameter correctly fails to infer when
223 // the types do not share a common subtype, but do share a common supertype
224 String code = r'''
225 class A<S, T> {
226 S s;
227 T t;
228 }
229 class B<S> extends A<S, S> { B(S s); }
230 A<int, double> test() => new B(3);
231 ''';
232 Source source = addSource(code);
233 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
234 assertErrors(source, [
235 StrongModeCode.COULD_NOT_INFER,
236 StaticTypeWarningCode.RETURN_OF_INVALID_TYPE
237 ]);
238 verify([source]);
239 CompilationUnit unit = analysisResult.unit;
240 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
241 ExpressionFunctionBody body = test.functionExpression.body;
242 DartType type = body.expression.staticType;
243
244 Element elementB = AstFinder.getClass(unit, "B").element;
245
246 _isInstantiationOf(_hasElement(elementB))([_isDynamic])(type);
247 }
248
249 fail_returnType_variance2() async {
250 // Check that downwards inference correctly pins a type parameter
251 // when the parameter is constrained in a covariant position
252 String code = r'''
253 typedef To Func1<From, To>(From x);
254 Func1<String, T> f<T>(T x) => null;
255 Func1<String, num> test() => f(42);
256 ''';
257 Source source = addSource(code);
258 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
259 assertNoErrors(source);
260 verify([source]);
261 CompilationUnit unit = analysisResult.unit;
262 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
263 ExpressionFunctionBody body = test.functionExpression.body;
264 MethodInvocation invoke = body.expression;
265 _isFunction2Of(_isNum, _isFunction2Of(_isString, _isNum))(
266 invoke.staticInvokeType);
267 }
268
269 fail_returnType_variance6() async {
270 // Check that pinning works correctly with a partial type
271 // when the return type uses the variable in a covariant position
272 String code = r'''
273 typedef To Func1<From, To>(From x);
274 Func1<String, T> f<T>(T x) => null;
275 T g<T, S>(Func1<S, T> f) => null;
276 num test() => g(f(3));
277 ''';
278 Source source = addSource(code);
279 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
280 assertNoErrors(source);
281 verify([source]);
282 CompilationUnit unit = analysisResult.unit;
283 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
284 ExpressionFunctionBody body = test.functionExpression.body;
285 MethodInvocation call = body.expression;
286 _isNum(call.staticType);
287 _isFunction2Of(_isFunction2Of(_isString, _isNum), _isNum)(
288 call.staticInvokeType);
289 }
290
291 @override 95 @override
292 void setUp() { 96 void setUp() {
293 super.setUp(); 97 super.setUp();
294 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); 98 AnalysisOptionsImpl options = new AnalysisOptionsImpl();
295 options.strongMode = true; 99 options.strongMode = true;
296 resetWith(options: options); 100 resetWith(options: options);
297 } 101 }
298 102
299 test_async_method_propagation() async { 103 test_async_method_propagation() async {
300 String code = r''' 104 String code = r'''
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
511 List<Statement> statements = 315 List<Statement> statements =
512 AstFinder.getStatementsInTopLevelFunction(unit, "test"); 316 AstFinder.getStatementsInTopLevelFunction(unit, "test");
513 VariableDeclarationStatement stmt = statements[0]; 317 VariableDeclarationStatement stmt = statements[0];
514 VariableDeclaration decl = stmt.variables.variables[0]; 318 VariableDeclaration decl = stmt.variables.variables[0];
515 Expression call = decl.initializer; 319 Expression call = decl.initializer;
516 _isInt(call.staticType); 320 _isInt(call.staticType);
517 } 321 }
518 322
519 test_constrainedByBounds2() async { 323 test_constrainedByBounds2() async {
520 // Test that upwards inference with two type variables does 324 // Test that upwards inference with two type variables does
521 // not propogate from the constrained variable to the unconstrained 325 // propogate from the constrained variable to the unconstrained
522 // variable if they are ordered right to left. 326 // variable if they are ordered right to left.
523 String code = r''' 327 String code = r'''
524 T f<T extends S, S>(S x) => null; 328 T f<T extends S, S>(S x) => null;
525 void test() { var x = f(3); } 329 void test() { var x = f(3); }
526 '''; 330 ''';
527 Source source = addSource(code); 331 Source source = addSource(code);
528 TestAnalysisResult analysisResult = await computeAnalysisResult(source); 332 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
529 assertNoErrors(source); 333 assertNoErrors(source);
530 verify([source]); 334 verify([source]);
531 CompilationUnit unit = analysisResult.unit; 335 CompilationUnit unit = analysisResult.unit;
532 List<Statement> statements = 336 List<Statement> statements =
533 AstFinder.getStatementsInTopLevelFunction(unit, "test"); 337 AstFinder.getStatementsInTopLevelFunction(unit, "test");
534 VariableDeclarationStatement stmt = statements[0]; 338 VariableDeclarationStatement stmt = statements[0];
535 VariableDeclaration decl = stmt.variables.variables[0]; 339 VariableDeclaration decl = stmt.variables.variables[0];
536 Expression call = decl.initializer; 340 Expression call = decl.initializer;
537 _isDynamic(call.staticType); 341 _isInt(call.staticType);
342 }
343
344 test_constrainedByBounds3() async {
345 Source source = addSource(r'''
346 T f<T extends S, S extends int>(S x) => null;
347 void test() { var x = f(3); }
348 ''');
349 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
350 assertNoErrors(source);
351 verify([source]);
352 CompilationUnit unit = analysisResult.unit;
353 List<Statement> statements =
354 AstFinder.getStatementsInTopLevelFunction(unit, "test");
355 VariableDeclarationStatement stmt = statements[0];
356 VariableDeclaration decl = stmt.variables.variables[0];
357 Expression call = decl.initializer;
358 _isInt(call.staticType);
538 } 359 }
539 360
540 test_constrainedByBounds4() async { 361 test_constrainedByBounds4() async {
541 // Test that upwards inference with two type variables correctly 362 // Test that upwards inference with two type variables correctly
542 // propogates from the constrained variable to the unconstrained 363 // propogates from the constrained variable to the unconstrained
543 // variable if they are ordered left to right, when the variable 364 // variable if they are ordered left to right, when the variable
544 // appears co and contra variantly 365 // appears co and contra variantly
545 String code = r''' 366 String code = r'''
546 typedef To Func1<From, To>(From x); 367 typedef To Func1<From, To>(From x);
547 T f<S, T extends Func1<S, S>>(S x) => null; 368 T f<S, T extends Func1<S, S>>(S x) => null;
548 void test() { var x = f(3)(4); } 369 void test() { var x = f(3)(4); }
549 '''; 370 ''';
550 Source source = addSource(code); 371 Source source = addSource(code);
551 TestAnalysisResult analysisResult = await computeAnalysisResult(source); 372 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
552 assertNoErrors(source); 373 assertNoErrors(source);
553 verify([source]); 374 verify([source]);
554 CompilationUnit unit = analysisResult.unit; 375 CompilationUnit unit = analysisResult.unit;
555 List<Statement> statements = 376 List<Statement> statements =
556 AstFinder.getStatementsInTopLevelFunction(unit, "test"); 377 AstFinder.getStatementsInTopLevelFunction(unit, "test");
557 VariableDeclarationStatement stmt = statements[0]; 378 VariableDeclarationStatement stmt = statements[0];
558 VariableDeclaration decl = stmt.variables.variables[0]; 379 VariableDeclaration decl = stmt.variables.variables[0];
559 Expression call = decl.initializer; 380 Expression call = decl.initializer;
560 _isInt(call.staticType); 381 _isInt(call.staticType);
561 } 382 }
562 383
384 test_constrainedByBounds5() async {
385 // Test that upwards inference with two type variables does not
386 // propogate from the constrained variable to the unconstrained
387 // variable if they are ordered right to left, when the variable
388 // appears co and contra variantly, and that an error is issued
389 // for the non-matching bound.
390 String code = r'''
391 typedef To Func1<From, To>(From x);
392 T f<T extends Func1<S, S>, S>(S x) => null;
393 void test() { var x = f(3)(4); }
394 ''';
395 Source source = addSource(code);
396 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
397 assertErrors(source, [StrongModeCode.COULD_NOT_INFER]);
398 verify([source]);
399 CompilationUnit unit = analysisResult.unit;
400 List<Statement> statements =
401 AstFinder.getStatementsInTopLevelFunction(unit, "test");
402 VariableDeclarationStatement stmt = statements[0];
403 VariableDeclaration decl = stmt.variables.variables[0];
404 Expression call = decl.initializer;
405 _isDynamic(call.staticType);
406 }
407
563 test_constructorInitializer_propagation() async { 408 test_constructorInitializer_propagation() async {
564 String code = r''' 409 String code = r'''
565 class A { 410 class A {
566 List<String> x; 411 List<String> x;
567 A() : this.x = []; 412 A() : this.x = [];
568 } 413 }
569 '''; 414 ''';
570 CompilationUnit unit = await resolveSource(code); 415 CompilationUnit unit = await resolveSource(code);
571 ConstructorDeclaration constructor = 416 ConstructorDeclaration constructor =
572 AstFinder.getConstructorInClass(unit, "A", null); 417 AstFinder.getConstructorInClass(unit, "A", null);
(...skipping 484 matching lines...) Expand 10 before | Expand all | Expand 10 after
1057 _isFutureOf([_isNum])(invoke.argumentList.arguments[0].staticType); 902 _isFutureOf([_isNum])(invoke.argumentList.arguments[0].staticType);
1058 } 903 }
1059 904
1060 test_futureOr_downwards6() async { 905 test_futureOr_downwards6() async {
1061 // Test that downwards inference doesn't decompose FutureOr 906 // Test that downwards inference doesn't decompose FutureOr
1062 // when instantiating type variables. 907 // when instantiating type variables.
1063 MethodInvocation invoke = await _testFutureOr(r''' 908 MethodInvocation invoke = await _testFutureOr(r'''
1064 T mk<T>(T x) => null; 909 T mk<T>(T x) => null;
1065 FutureOr<int> test() => mk(new Future.value(42)); 910 FutureOr<int> test() => mk(new Future.value(42));
1066 '''); 911 ''');
912 _isFutureOrOfInt(invoke.staticType);
913 _isFutureOfInt(invoke.argumentList.arguments[0].staticType);
914 }
915
916 test_futureOr_downwards7() async {
917 // Test that downwards inference incorporates bounds correctly
918 // when instantiating type variables.
919 MethodInvocation invoke = await _testFutureOr(r'''
920 T mk<T extends Future<int>>(T x) => null;
921 FutureOr<int> test() => mk(new Future.value(42));
922 ''');
923 _isFutureOfInt(invoke.staticType);
924 _isFutureOfInt(invoke.argumentList.arguments[0].staticType);
925 }
926
927 test_futureOr_downwards8() async {
928 // Test that downwards inference incorporates bounds correctly
929 // when instantiating type variables.
930 // TODO(leafp): I think this should pass once the inference changes
931 // that jmesserly is adding are landed.
932 MethodInvocation invoke = await _testFutureOr(r'''
933 T mk<T extends Future<Object>>(T x) => null;
934 FutureOr<int> test() => mk(new Future.value(42));
935 ''');
1067 _isFutureOfInt(invoke.staticType); 936 _isFutureOfInt(invoke.staticType);
1068 _isFutureOfInt(invoke.argumentList.arguments[0].staticType); 937 _isFutureOfInt(invoke.argumentList.arguments[0].staticType);
1069 } 938 }
1070 939
1071 test_futureOr_downwards9() async { 940 test_futureOr_downwards9() async {
1072 // Test that downwards inference decomposes correctly with 941 // Test that downwards inference decomposes correctly with
1073 // other composite types 942 // other composite types
1074 MethodInvocation invoke = await _testFutureOr(r''' 943 MethodInvocation invoke = await _testFutureOr(r'''
1075 List<T> mk<T>(T x) => null; 944 List<T> mk<T>(T x) => null;
1076 FutureOr<List<int>> test() => mk(3); 945 FutureOr<List<int>> test() => mk(3);
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1118 } 987 }
1119 988
1120 test_futureOr_no_return() async { 989 test_futureOr_no_return() async {
1121 MethodInvocation invoke = await _testFutureOr(r''' 990 MethodInvocation invoke = await _testFutureOr(r'''
1122 FutureOr<T> mk<T>(Future<T> x) => x; 991 FutureOr<T> mk<T>(Future<T> x) => x;
1123 Future<int> f; 992 Future<int> f;
1124 test() => f.then((int x) {}); 993 test() => f.then((int x) {});
1125 '''); 994 ''');
1126 _isFunction2Of(_isInt, _isNull)( 995 _isFunction2Of(_isInt, _isNull)(
1127 invoke.argumentList.arguments[0].staticType); 996 invoke.argumentList.arguments[0].staticType);
1128 _isFutureOfDynamic(invoke.staticType); 997 _isFutureOfNull(invoke.staticType);
1129 } 998 }
1130 999
1131 test_futureOr_no_return_value() async { 1000 test_futureOr_no_return_value() async {
1132 MethodInvocation invoke = await _testFutureOr(r''' 1001 MethodInvocation invoke = await _testFutureOr(r'''
1133 FutureOr<T> mk<T>(Future<T> x) => x; 1002 FutureOr<T> mk<T>(Future<T> x) => x;
1134 Future<int> f; 1003 Future<int> f;
1135 test() => f.then((int x) {return;}); 1004 test() => f.then((int x) {return;});
1136 '''); 1005 ''');
1137 _isFunction2Of(_isInt, _isNull)( 1006 _isFunction2Of(_isInt, _isNull)(
1138 invoke.argumentList.arguments[0].staticType); 1007 invoke.argumentList.arguments[0].staticType);
1139 _isFutureOfDynamic(invoke.staticType); 1008 _isFutureOfNull(invoke.staticType);
1140 } 1009 }
1141 1010
1142 test_futureOr_return_null() async { 1011 test_futureOr_return_null() async {
1143 MethodInvocation invoke = await _testFutureOr(r''' 1012 MethodInvocation invoke = await _testFutureOr(r'''
1144 FutureOr<T> mk<T>(Future<T> x) => x; 1013 FutureOr<T> mk<T>(Future<T> x) => x;
1145 Future<int> f; 1014 Future<int> f;
1146 test() => f.then((int x) {}); 1015 test() => f.then((int x) {});
1147 '''); 1016 ''');
1148 _isFunction2Of(_isInt, _isNull)( 1017 _isFunction2Of(_isInt, _isNull)(
1149 invoke.argumentList.arguments[0].staticType); 1018 invoke.argumentList.arguments[0].staticType);
1150 _isFutureOfDynamic(invoke.staticType); 1019 _isFutureOfNull(invoke.staticType);
1151 } 1020 }
1152 1021
1153 test_futureOr_upwards1() async { 1022 test_futureOr_upwards1() async {
1154 // Test that upwards inference correctly prefers to instantiate type 1023 // Test that upwards inference correctly prefers to instantiate type
1155 // variables with the "smaller" solution when both are possible. 1024 // variables with the "smaller" solution when both are possible.
1156 MethodInvocation invoke = await _testFutureOr(r''' 1025 MethodInvocation invoke = await _testFutureOr(r'''
1157 Future<T> mk<T>(FutureOr<T> x) => null; 1026 Future<T> mk<T>(FutureOr<T> x) => null;
1158 dynamic test() => mk(new Future<int>.value(42)); 1027 dynamic test() => mk(new Future<int>.value(42));
1159 '''); 1028 ''');
1160 _isFutureOfInt(invoke.staticType); 1029 _isFutureOfInt(invoke.staticType);
1161 } 1030 }
1162 1031
1163 test_futureOr_upwards2() async { 1032 test_futureOr_upwards2() async {
1164 // Test that upwards inference fails when the solution doesn't 1033 // Test that upwards inference fails when the solution doesn't
1165 // match the bound. 1034 // match the bound.
1166 MethodInvocation invoke = await _testFutureOr( 1035 MethodInvocation invoke = await _testFutureOr(
1167 r''' 1036 r'''
1168 Future<T> mk<T extends Future<Object>>(FutureOr<T> x) => null; 1037 Future<T> mk<T extends Future<Object>>(FutureOr<T> x) => null;
1169 dynamic test() => mk(new Future<int>.value(42)); 1038 dynamic test() => mk(new Future<int>.value(42));
1170 ''', 1039 ''',
1171 errors: [StrongModeCode.COULD_NOT_INFER]); 1040 errors: [StrongModeCode.COULD_NOT_INFER]);
1172 _isFutureOf([_isObject])(invoke.staticType); 1041 _isFutureOfInt(invoke.staticType);
1173 } 1042 }
1174 1043
1175 test_futureOrNull_no_return() async { 1044 test_futureOrNull_no_return() async {
1176 MethodInvocation invoke = await _testFutureOr(r''' 1045 MethodInvocation invoke = await _testFutureOr(r'''
1177 FutureOr<T> mk<T>(Future<T> x) => x; 1046 FutureOr<T> mk<T>(Future<T> x) => x;
1178 Future<int> f; 1047 Future<int> f;
1179 test() => f.then<Null>((int x) {}); 1048 test() => f.then<Null>((int x) {});
1180 '''); 1049 ''');
1181 _isFunction2Of(_isInt, _isNull)( 1050 _isFunction2Of(_isInt, _isNull)(
1182 invoke.argumentList.arguments[0].staticType); 1051 invoke.argumentList.arguments[0].staticType);
(...skipping 15 matching lines...) Expand all
1198 MethodInvocation invoke = await _testFutureOr(r''' 1067 MethodInvocation invoke = await _testFutureOr(r'''
1199 FutureOr<T> mk<T>(Future<T> x) => x; 1068 FutureOr<T> mk<T>(Future<T> x) => x;
1200 Future<int> f; 1069 Future<int> f;
1201 test() => f.then<Null>((int x) {}); 1070 test() => f.then<Null>((int x) {});
1202 '''); 1071 ''');
1203 _isFunction2Of(_isInt, _isNull)( 1072 _isFunction2Of(_isInt, _isNull)(
1204 invoke.argumentList.arguments[0].staticType); 1073 invoke.argumentList.arguments[0].staticType);
1205 _isFutureOfNull(invoke.staticType); 1074 _isFutureOfNull(invoke.staticType);
1206 } 1075 }
1207 1076
1077 test_inferConstructor_unknownTypeLowerBound() async {
1078 Source source = addSource(r'''
1079 class C<T> {
1080 C(void callback(List<T> a));
1081 }
1082 test() {
1083 // downwards inference pushes List<?> and in parameter position this
1084 // becomes inferred as List<Null>.
1085 var c = new C((items) {});
1086 }
1087 ''');
1088 CompilationUnit unit = (await computeAnalysisResult(source)).unit;
1089 assertNoErrors(source);
1090 verify([source]);
1091 DartType cType = AstFinder
1092 .getTopLevelFunction(unit, "test")
1093 .element
1094 .localVariables[0]
1095 .type;
1096 Element elementC = AstFinder.getClass(unit, "C").element;
1097
1098 _isInstantiationOf(_hasElement(elementC))([_isDynamic])(cType);
1099 }
1100
1208 test_inference_hints() async { 1101 test_inference_hints() async {
1209 Source source = addSource(r''' 1102 Source source = addSource(r'''
1210 void main () { 1103 void main () {
1211 var x = 3; 1104 var x = 3;
1212 List<int> l0 = []; 1105 List<int> l0 = [];
1213 } 1106 }
1214 '''); 1107 ''');
1215 await computeAnalysisResult(source); 1108 await computeAnalysisResult(source);
1216 assertNoErrors(source); 1109 assertNoErrors(source);
1217 verify([source]); 1110 verify([source]);
(...skipping 529 matching lines...) Expand 10 before | Expand all | Expand 10 after
1747 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); 1640 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
1748 ExpressionFunctionBody body = test.functionExpression.body; 1641 ExpressionFunctionBody body = test.functionExpression.body;
1749 _isString(body.expression.staticType); 1642 _isString(body.expression.staticType);
1750 MethodInvocation invoke = body.expression; 1643 MethodInvocation invoke = body.expression;
1751 FunctionExpression function = invoke.argumentList.arguments[0]; 1644 FunctionExpression function = invoke.argumentList.arguments[0];
1752 ExecutableElement f0 = function.element; 1645 ExecutableElement f0 = function.element;
1753 FunctionType type = f0.type; 1646 FunctionType type = f0.type;
1754 _isFunction2Of(_isString, _isInt)(type); 1647 _isFunction2Of(_isString, _isInt)(type);
1755 } 1648 }
1756 1649
1650 test_pinning_multipleConstraints1() async {
1651 // Test that downwards inference with two different downwards covariant
1652 // constraints on the same parameter correctly fails to infer when
1653 // the types do not share a common subtype
1654 String code = r'''
1655 class A<S, T> {
1656 S s;
1657 T t;
1658 }
1659 class B<S> extends A<S, S> { B(S s); }
1660 A<int, String> test() => new B(3);
1661 ''';
1662 Source source = addSource(code);
1663 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
1664 assertErrors(source,
1665 [StrongModeCode.COULD_NOT_INFER, StrongModeCode.INVALID_CAST_LITERAL]);
1666 verify([source]);
1667 CompilationUnit unit = analysisResult.unit;
1668 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
1669 ExpressionFunctionBody body = test.functionExpression.body;
1670 DartType type = body.expression.staticType;
1671
1672 Element elementB = AstFinder.getClass(unit, "B").element;
1673
1674 _isInstantiationOf(_hasElement(elementB))([_isNull])(type);
1675 }
1676
1677 test_pinning_multipleConstraints2() async {
1678 // Test that downwards inference with two identical downwards covariant
1679 // constraints on the same parameter correctly infers and pins the type
1680 String code = r'''
1681 class A<S, T> {
1682 S s;
1683 T t;
1684 }
1685 class B<S> extends A<S, S> { B(S s); }
1686 A<num, num> test() => new B(3);
1687 ''';
1688 Source source = addSource(code);
1689 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
1690 assertNoErrors(source);
1691 verify([source]);
1692 CompilationUnit unit = analysisResult.unit;
1693 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
1694 ExpressionFunctionBody body = test.functionExpression.body;
1695 DartType type = body.expression.staticType;
1696
1697 Element elementB = AstFinder.getClass(unit, "B").element;
1698
1699 _isInstantiationOf(_hasElement(elementB))([_isNum])(type);
1700 }
1701
1702 test_pinning_multipleConstraints3() async {
1703 // Test that downwards inference with two different downwards covariant
1704 // constraints on the same parameter correctly fails to infer when
1705 // the types do not share a common subtype, but do share a common supertype
1706 String code = r'''
1707 class A<S, T> {
1708 S s;
1709 T t;
1710 }
1711 class B<S> extends A<S, S> { B(S s); }
1712 A<int, double> test() => new B(3);
1713 ''';
1714 Source source = addSource(code);
1715 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
1716 assertErrors(source, [
1717 StrongModeCode.COULD_NOT_INFER,
1718 StrongModeCode.INVALID_CAST_LITERAL,
1719 ]);
1720 verify([source]);
1721 CompilationUnit unit = analysisResult.unit;
1722 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
1723 ExpressionFunctionBody body = test.functionExpression.body;
1724 DartType type = body.expression.staticType;
1725
1726 Element elementB = AstFinder.getClass(unit, "B").element;
1727
1728 _isInstantiationOf(_hasElement(elementB))([_isNull])(type);
1729 }
1730
1757 test_pinning_multipleConstraints4() async { 1731 test_pinning_multipleConstraints4() async {
1758 // Test that downwards inference with two subtype related downwards 1732 // Test that downwards inference with two subtype related downwards
1759 // covariant constraints on the same parameter correctly infers and pins 1733 // covariant constraints on the same parameter correctly infers and pins
1760 // the type 1734 // the type
1761 String code = r''' 1735 String code = r'''
1762 class A<S, T> { 1736 class A<S, T> {
1763 S s; 1737 S s;
1764 T t; 1738 T t;
1765 } 1739 }
1766 class B<S> extends A<S, S> {} 1740 class B<S> extends A<S, S> {}
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
1924 assertNoErrors(source); 1898 assertNoErrors(source);
1925 verify([source]); 1899 verify([source]);
1926 CompilationUnit unit = analysisResult.unit; 1900 CompilationUnit unit = analysisResult.unit;
1927 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); 1901 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
1928 ExpressionFunctionBody body = test.functionExpression.body; 1902 ExpressionFunctionBody body = test.functionExpression.body;
1929 MethodInvocation invoke = body.expression; 1903 MethodInvocation invoke = body.expression;
1930 _isFunction2Of(_isNum, _isFunction2Of(_isNum, _isString))( 1904 _isFunction2Of(_isNum, _isFunction2Of(_isNum, _isString))(
1931 invoke.staticInvokeType); 1905 invoke.staticInvokeType);
1932 } 1906 }
1933 1907
1908 test_returnType_variance2() async {
1909 // Check that downwards inference correctly pins a type parameter
1910 // when the parameter is constrained in a covariant position
1911 String code = r'''
1912 typedef To Func1<From, To>(From x);
1913 Func1<String, T> f<T>(T x) => null;
1914 Func1<String, num> test() => f(42);
1915 ''';
1916 Source source = addSource(code);
1917 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
1918 assertNoErrors(source);
1919 verify([source]);
1920 CompilationUnit unit = analysisResult.unit;
1921 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
1922 ExpressionFunctionBody body = test.functionExpression.body;
1923 MethodInvocation invoke = body.expression;
1924 _isFunction2Of(_isNum, _isFunction2Of(_isString, _isNum))(
1925 invoke.staticInvokeType);
1926 }
1927
1934 test_returnType_variance3() async { 1928 test_returnType_variance3() async {
1935 // Check that the variance heuristic chooses the less precise type 1929 // Check that the variance heuristic chooses the most precise type
1936 // when the return type uses the variable in a contravariant position 1930 // when the return type uses the variable in a contravariant position
1937 // and there is no downwards constraint. 1931 // and there is no downwards constraint.
1938 String code = r''' 1932 String code = r'''
1939 typedef To Func1<From, To>(From x); 1933 typedef To Func1<From, To>(From x);
1940 Func1<T, String> f<T>(T x, g(T x)) => null; 1934 Func1<T, String> f<T>(T x, g(T x)) => null;
1941 dynamic test() => f(42, (num x) => x); 1935 dynamic test() => f(42, (num x) => x);
1942 '''; 1936 ''';
1943 Source source = addSource(code); 1937 Source source = addSource(code);
1944 TestAnalysisResult analysisResult = await computeAnalysisResult(source); 1938 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
1945 assertNoErrors(source); 1939 assertNoErrors(source);
1946 verify([source]); 1940 verify([source]);
1947 CompilationUnit unit = analysisResult.unit; 1941 CompilationUnit unit = analysisResult.unit;
1948 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); 1942 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
1949 ExpressionFunctionBody body = test.functionExpression.body; 1943 ExpressionFunctionBody body = test.functionExpression.body;
1950 FunctionType functionType = body.expression.staticType; 1944 FunctionType functionType = body.expression.staticType;
1951 DartType type = functionType.normalParameterTypes[0]; 1945 DartType type = functionType.normalParameterTypes[0];
1952 _isNum(type); 1946 _isInt(type);
1953 } 1947 }
1954 1948
1955 test_returnType_variance4() async { 1949 test_returnType_variance4() async {
1956 // Check that the variance heuristic chooses the more precise type 1950 // Check that the variance heuristic chooses the more precise type
1957 // when the return type uses the variable in a covariant position 1951 // when the return type uses the variable in a covariant position
1958 // and there is no downwards constraint 1952 // and there is no downwards constraint
1959 String code = r''' 1953 String code = r'''
1960 typedef To Func1<From, To>(From x); 1954 typedef To Func1<From, To>(From x);
1961 Func1<String, T> f<T>(T x, g(T x)) => null; 1955 Func1<String, T> f<T>(T x, g(T x)) => null;
1962 dynamic test() => f(42, (num x) => x); 1956 dynamic test() => f(42, (num x) => x);
(...skipping 25 matching lines...) Expand all
1988 verify([source]); 1982 verify([source]);
1989 CompilationUnit unit = analysisResult.unit; 1983 CompilationUnit unit = analysisResult.unit;
1990 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); 1984 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
1991 ExpressionFunctionBody body = test.functionExpression.body; 1985 ExpressionFunctionBody body = test.functionExpression.body;
1992 MethodInvocation call = body.expression; 1986 MethodInvocation call = body.expression;
1993 _isNum(call.staticType); 1987 _isNum(call.staticType);
1994 _isFunction2Of(_isFunction2Of(_isNum, _isString), _isNum)( 1988 _isFunction2Of(_isFunction2Of(_isNum, _isString), _isNum)(
1995 call.staticInvokeType); 1989 call.staticInvokeType);
1996 } 1990 }
1997 1991
1992 test_returnType_variance6() async {
1993 // Check that pinning works correctly with a partial type
1994 // when the return type uses the variable in a covariant position
1995 String code = r'''
1996 typedef To Func1<From, To>(From x);
1997 Func1<String, T> f<T>(T x) => null;
1998 T g<T, S>(Func1<S, T> f) => null;
1999 num test() => g(f(3));
2000 ''';
2001 Source source = addSource(code);
2002 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
2003 assertNoErrors(source);
2004 verify([source]);
2005 CompilationUnit unit = analysisResult.unit;
2006 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
2007 ExpressionFunctionBody body = test.functionExpression.body;
2008 MethodInvocation call = body.expression;
2009 _isNum(call.staticType);
2010 _isFunction2Of(_isFunction2Of(_isString, _isNum), _isNum)(
2011 call.staticInvokeType);
2012 }
2013
1998 test_superConstructorInvocation_propagation() async { 2014 test_superConstructorInvocation_propagation() async {
1999 String code = r''' 2015 String code = r'''
2000 class B { 2016 class B {
2001 B(List<String>); 2017 B(List<String>);
2002 } 2018 }
2003 class A extends B { 2019 class A extends B {
2004 A() : super([]); 2020 A() : super([]);
2005 } 2021 }
2006 '''; 2022 ''';
2007 CompilationUnit unit = await resolveSource(code); 2023 CompilationUnit unit = await resolveSource(code);
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after
2294 Expression exp = (statements[i] as ExpressionStatement).expression; 2310 Expression exp = (statements[i] as ExpressionStatement).expression;
2295 expect(exp.staticType, typeProvider.dynamicType); 2311 expect(exp.staticType, typeProvider.dynamicType);
2296 } 2312 }
2297 } 2313 }
2298 2314
2299 checkBody("C"); 2315 checkBody("C");
2300 checkBody("D"); 2316 checkBody("D");
2301 } 2317 }
2302 2318
2303 test_genericFunction_upwardsAndDownwards() async { 2319 test_genericFunction_upwardsAndDownwards() async {
2304 // Regression tests for https://github.com/dart-lang/sdk/issues/27151. 2320 // Regression tests for https://github.com/dart-lang/sdk/issues/27586.
2305 await resolveTestUnit(r'List<num> x = [1, 2];'); 2321 await resolveTestUnit(r'List<num> x = [1, 2];');
2306 expectInitializerType('x', 'List<int>'); 2322 expectInitializerType('x', 'List<num>');
2323 }
2324
2325 test_genericFunction_upwardsAndDownwards_Object() async {
2326 // Regression tests for https://github.com/dart-lang/sdk/issues/27625.
2327 await resolveTestUnit(r'''
2328 List<Object> aaa = [];
2329 List<Object> bbb = [1, 2, 3];
2330 List<Object> ccc = [null];
2331 List<Object> ddd = [1 as dynamic];
2332 List<Object> eee = [new Object()];
2333 ''');
2334 expectInitializerType('aaa', 'List<Object>');
2335 expectInitializerType('bbb', 'List<Object>');
2336 expectInitializerType('ccc', 'List<Object>');
2337 expectInitializerType('ddd', 'List<Object>');
2338 expectInitializerType('eee', 'List<Object>');
2307 } 2339 }
2308 2340
2309 test_genericMethod() async { 2341 test_genericMethod() async {
2310 await resolveTestUnit(r''' 2342 await resolveTestUnit(r'''
2311 class C<E> { 2343 class C<E> {
2312 List/*<T>*/ f/*<T>*/(E e) => null; 2344 List/*<T>*/ f/*<T>*/(E e) => null;
2313 } 2345 }
2314 main() { 2346 main() {
2315 C<String> cOfString; 2347 C<String> cOfString;
2316 } 2348 }
(...skipping 687 matching lines...) Expand 10 before | Expand all | Expand 10 after
3004 class C<T> { 3036 class C<T> {
3005 void m<S0 extends T, S1 extends List<S0>>(S0 p0, S1 p1) {} 3037 void m<S0 extends T, S1 extends List<S0>>(S0 p0, S1 p1) {}
3006 3038
3007 void main() { 3039 void main() {
3008 m(null, null); 3040 m(null, null);
3009 } 3041 }
3010 } 3042 }
3011 '''; 3043 ''';
3012 await resolveTestUnit(code); 3044 await resolveTestUnit(code);
3013 assertNoErrors(testSource); 3045 assertNoErrors(testSource);
3014 expectStaticInvokeType('m(null', '(T, List<T>) → void'); 3046 expectStaticInvokeType('m(null', '(Null, Null) → void');
3047 }
3048
3049 test_instantiateToBounds_method_ok_referenceOther_before2() async {
3050 String code = r'''
3051 class C<T> {
3052 Map<S0, S1> m<S0 extends T, S1 extends List<S0>>() => null;
3053
3054 void main() {
3055 m();
3056 }
3057 }
3058 ''';
3059 await resolveTestUnit(code);
3060 assertNoErrors(testSource);
3061 expectStaticInvokeType('m();', '() → Map<T, List<T>>');
3015 } 3062 }
3016 3063
3017 test_instantiateToBounds_method_ok_simpleBounds() async { 3064 test_instantiateToBounds_method_ok_simpleBounds() async {
3018 String code = r''' 3065 String code = r'''
3019 class C<T> { 3066 class C<T> {
3020 void m<S extends T>(S p0) {} 3067 void m<S extends T>(S p0) {}
3021 3068
3022 void main() { 3069 void main() {
3023 m(null); 3070 m(null);
3024 } 3071 }
3025 } 3072 }
3026 '''; 3073 ''';
3027 await resolveTestUnit(code); 3074 await resolveTestUnit(code);
3028 assertNoErrors(testSource); 3075 assertNoErrors(testSource);
3029 expectStaticInvokeType('m(null)', '(T) → void'); 3076 expectStaticInvokeType('m(null)', '(Null) → void');
3077 }
3078
3079 test_instantiateToBounds_method_ok_simpleBounds2() async {
3080 String code = r'''
3081 class C<T> {
3082 S m<S extends T>() => null;
3083
3084 void main() {
3085 m();
3086 }
3087 }
3088 ''';
3089 await resolveTestUnit(code);
3090 assertNoErrors(testSource);
3091 expectStaticInvokeType('m();', '() → T');
3030 } 3092 }
3031 3093
3032 test_notInstantiatedBound_direct_class_class() async { 3094 test_notInstantiatedBound_direct_class_class() async {
3033 String code = r''' 3095 String code = r'''
3034 class A<T extends int> {} 3096 class A<T extends int> {}
3035 class C<T extends A> {} 3097 class C<T extends A> {}
3036 '''; 3098 ''';
3037 await resolveTestUnit(code, noErrors: false); 3099 await resolveTestUnit(code, noErrors: false);
3038 assertErrors(testSource, [StrongModeCode.NOT_INSTANTIATED_BOUND]); 3100 assertErrors(testSource, [StrongModeCode.NOT_INSTANTIATED_BOUND]);
3039 } 3101 }
(...skipping 515 matching lines...) Expand 10 before | Expand all | Expand 10 after
3555 var v = x; 3617 var v = x;
3556 v; // marker 3618 v; // marker
3557 } 3619 }
3558 int x = 3; 3620 int x = 3;
3559 '''; 3621 ''';
3560 CompilationUnit unit = await resolveSource(code); 3622 CompilationUnit unit = await resolveSource(code);
3561 assertPropagatedAssignedType(code, unit, typeProvider.intType, null); 3623 assertPropagatedAssignedType(code, unit, typeProvider.intType, null);
3562 assertTypeOfMarkedExpression(code, unit, typeProvider.intType, null); 3624 assertTypeOfMarkedExpression(code, unit, typeProvider.intType, null);
3563 } 3625 }
3564 } 3626 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/summary/link.dart ('k') | pkg/analyzer/test/generated/type_system_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698