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

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 { 95 @override
96 // Test that upwards inference with two type variables does 96 void setUp() {
97 // not propogate from the constrained variable to the unconstrained 97 super.setUp();
98 // variable if they are ordered right to left, and that if the result 98 AnalysisOptionsImpl options = new AnalysisOptionsImpl();
99 // is not a valid instantiation an error is issued 99 options.strongMode = true;
100 String code = r''' 100 resetWith(options: options);
101 T f<T extends S, S extends int>(S x) => null; 101 }
102 void test() { var x = f(3); } 102
103 '''; 103 test_constrainedByBounds3() async {
Leaf 2017/03/14 22:37:40 Sort methods in file?
Jennifer Messerly 2017/03/14 23:49:09 I can do that. It will destroy the diff, but that
104 Source source = addSource(code); 104 Source source = addSource(r'''
105 T f<T extends S, S extends int>(S x) => null;
106 void test() { var x = f(3); }
107 ''');
105 TestAnalysisResult analysisResult = await computeAnalysisResult(source); 108 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
106 assertErrors( 109 assertNoErrors(source);
107 source, [StaticTypeWarningCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS]);
108 verify([source]); 110 verify([source]);
109 CompilationUnit unit = analysisResult.unit; 111 CompilationUnit unit = analysisResult.unit;
110 List<Statement> statements = 112 List<Statement> statements =
111 AstFinder.getStatementsInTopLevelFunction(unit, "test"); 113 AstFinder.getStatementsInTopLevelFunction(unit, "test");
112 VariableDeclarationStatement stmt = statements[0]; 114 VariableDeclarationStatement stmt = statements[0];
113 VariableDeclaration decl = stmt.variables.variables[0]; 115 VariableDeclaration decl = stmt.variables.variables[0];
114 Expression call = decl.initializer; 116 Expression call = decl.initializer;
115 _isDynamic(call.staticType); 117 _isInt(call.staticType);
116 } 118 }
117 119
118 fail_constrainedByBounds5() async { 120 test_constrainedByBounds5() async {
119 // Test that upwards inference with two type variables does not 121 // Test that upwards inference with two type variables does not
120 // propogate from the constrained variable to the unconstrained 122 // propogate from the constrained variable to the unconstrained
121 // variable if they are ordered right to left, when the variable 123 // variable if they are ordered right to left, when the variable
122 // appears co and contra variantly, and that an error is issued 124 // appears co and contra variantly, and that an error is issued
123 // for the non-matching bound. 125 // for the non-matching bound.
124 String code = r''' 126 String code = r'''
125 typedef To Func1<From, To>(From x); 127 typedef To Func1<From, To>(From x);
126 T f<T extends Func1<S, S>, S>(S x) => null; 128 T f<T extends Func1<S, S>, S>(S x) => null;
127 void test() { var x = f(3)(4); } 129 void test() { var x = f(3)(4); }
128 '''; 130 ''';
129 Source source = addSource(code); 131 Source source = addSource(code);
130 TestAnalysisResult analysisResult = await computeAnalysisResult(source); 132 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
131 assertErrors( 133 assertErrors(source, [StrongModeCode.COULD_NOT_INFER]);
132 source, [StaticTypeWarningCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS]);
133 verify([source]); 134 verify([source]);
134 CompilationUnit unit = analysisResult.unit; 135 CompilationUnit unit = analysisResult.unit;
135 List<Statement> statements = 136 List<Statement> statements =
136 AstFinder.getStatementsInTopLevelFunction(unit, "test"); 137 AstFinder.getStatementsInTopLevelFunction(unit, "test");
137 VariableDeclarationStatement stmt = statements[0]; 138 VariableDeclarationStatement stmt = statements[0];
138 VariableDeclaration decl = stmt.variables.variables[0]; 139 VariableDeclaration decl = stmt.variables.variables[0];
139 Expression call = decl.initializer; 140 Expression call = decl.initializer;
140 _isInt(call.staticType); 141 _isDynamic(call.staticType);
141 } 142 }
142 143
143 fail_futureOr_downwards7() async { 144 test_futureOr_downwards7() async {
144 // Test that downwards inference incorporates bounds correctly 145 // Test that downwards inference incorporates bounds correctly
145 // when instantiating type variables. 146 // 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''' 147 MethodInvocation invoke = await _testFutureOr(r'''
149 T mk<T extends int>(T x) => null; 148 T mk<T extends Future<int>>(T x) => null;
150 FutureOr<int> test() => mk(new Future.value(42)); 149 FutureOr<int> test() => mk(new Future.value(42));
151 '''); 150 ''');
152 _isFutureOfInt(invoke.staticType); 151 _isFutureOfInt(invoke.staticType);
153 _isFutureOfInt(invoke.argumentList.arguments[0].staticType); 152 _isFutureOfInt(invoke.argumentList.arguments[0].staticType);
154 } 153 }
155 154
156 fail_futureOr_downwards8() async { 155 test_futureOr_downwards8() async {
157 // Test that downwards inference incorporates bounds correctly 156 // Test that downwards inference incorporates bounds correctly
158 // when instantiating type variables. 157 // when instantiating type variables.
159 // TODO(leafp): I think this should pass once the inference changes 158 // TODO(leafp): I think this should pass once the inference changes
160 // that jmesserly is adding are landed. 159 // that jmesserly is adding are landed.
161 MethodInvocation invoke = await _testFutureOr(r''' 160 MethodInvocation invoke = await _testFutureOr(r'''
162 T mk<T extends Future<Object>>(T x) => null; 161 T mk<T extends Future<Object>>(T x) => null;
163 FutureOr<int> test() => mk(new Future.value(42)); 162 FutureOr<int> test() => mk(new Future.value(42));
164 '''); 163 ''');
165 _isFutureOfInt(invoke.staticType); 164 _isFutureOfInt(invoke.staticType);
166 _isFutureOfInt(invoke.argumentList.arguments[0].staticType); 165 _isFutureOfInt(invoke.argumentList.arguments[0].staticType);
167 } 166 }
168 167
169 fail_pinning_multipleConstraints1() async { 168 test_pinning_multipleConstraints1() async {
170 // Test that downwards inference with two different downwards covariant 169 // Test that downwards inference with two different downwards covariant
171 // constraints on the same parameter correctly fails to infer when 170 // constraints on the same parameter correctly fails to infer when
172 // the types do not share a common subtype 171 // the types do not share a common subtype
173 String code = r''' 172 String code = r'''
174 class A<S, T> { 173 class A<S, T> {
175 S s; 174 S s;
176 T t; 175 T t;
177 } 176 }
178 class B<S> extends A<S, S> { B(S s); } 177 class B<S> extends A<S, S> { B(S s); }
179 A<int, String> test() => new B(3); 178 A<int, String> test() => new B(3);
180 '''; 179 ''';
181 Source source = addSource(code); 180 Source source = addSource(code);
182 TestAnalysisResult analysisResult = await computeAnalysisResult(source); 181 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
183 assertErrors(source, [StrongModeCode.COULD_NOT_INFER]); 182 assertErrors(source,
183 [StrongModeCode.COULD_NOT_INFER, StrongModeCode.INVALID_CAST_LITERAL]);
184 verify([source]); 184 verify([source]);
185 CompilationUnit unit = analysisResult.unit; 185 CompilationUnit unit = analysisResult.unit;
186 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); 186 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
187 ExpressionFunctionBody body = test.functionExpression.body; 187 ExpressionFunctionBody body = test.functionExpression.body;
188 DartType type = body.expression.staticType; 188 DartType type = body.expression.staticType;
189 189
190 Element elementB = AstFinder.getClass(unit, "B").element; 190 Element elementB = AstFinder.getClass(unit, "B").element;
191 191
192 _isInstantiationOf(_hasElement(elementB))([_isDynamic])(type); 192 _isInstantiationOf(_hasElement(elementB))([_isNull])(type);
193 } 193 }
194 194
195 fail_pinning_multipleConstraints2() async { 195 test_pinning_multipleConstraints2() async {
196 // Test that downwards inference with two identical downwards covariant 196 // Test that downwards inference with two identical downwards covariant
197 // constraints on the same parameter correctly infers and pins the type 197 // constraints on the same parameter correctly infers and pins the type
198 String code = r''' 198 String code = r'''
199 class A<S, T> { 199 class A<S, T> {
200 S s; 200 S s;
201 T t; 201 T t;
202 } 202 }
203 class B<S> extends A<S, S> { B(S s); } 203 class B<S> extends A<S, S> { B(S s); }
204 A<num, num> test() => new B(3); 204 A<num, num> test() => new B(3);
205 '''; 205 ''';
206 Source source = addSource(code); 206 Source source = addSource(code);
207 TestAnalysisResult analysisResult = await computeAnalysisResult(source); 207 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
208 assertNoErrors(source); 208 assertNoErrors(source);
209 verify([source]); 209 verify([source]);
210 CompilationUnit unit = analysisResult.unit; 210 CompilationUnit unit = analysisResult.unit;
211 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); 211 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
212 ExpressionFunctionBody body = test.functionExpression.body; 212 ExpressionFunctionBody body = test.functionExpression.body;
213 DartType type = body.expression.staticType; 213 DartType type = body.expression.staticType;
214 214
215 Element elementB = AstFinder.getClass(unit, "B").element; 215 Element elementB = AstFinder.getClass(unit, "B").element;
216 216
217 _isInstantiationOf(_hasElement(elementB))([_isNum])(type); 217 _isInstantiationOf(_hasElement(elementB))([_isNum])(type);
218 } 218 }
219 219
220 fail_pinning_multipleConstraints3() async { 220 test_pinning_multipleConstraints3() async {
221 // Test that downwards inference with two different downwards covariant 221 // Test that downwards inference with two different downwards covariant
222 // constraints on the same parameter correctly fails to infer when 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 223 // the types do not share a common subtype, but do share a common supertype
224 String code = r''' 224 String code = r'''
225 class A<S, T> { 225 class A<S, T> {
226 S s; 226 S s;
227 T t; 227 T t;
228 } 228 }
229 class B<S> extends A<S, S> { B(S s); } 229 class B<S> extends A<S, S> { B(S s); }
230 A<int, double> test() => new B(3); 230 A<int, double> test() => new B(3);
231 '''; 231 ''';
232 Source source = addSource(code); 232 Source source = addSource(code);
233 TestAnalysisResult analysisResult = await computeAnalysisResult(source); 233 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
234 assertErrors(source, [ 234 assertErrors(source, [
235 StrongModeCode.COULD_NOT_INFER, 235 StrongModeCode.COULD_NOT_INFER,
236 StaticTypeWarningCode.RETURN_OF_INVALID_TYPE 236 StrongModeCode.INVALID_CAST_LITERAL,
237 ]); 237 ]);
238 verify([source]); 238 verify([source]);
239 CompilationUnit unit = analysisResult.unit; 239 CompilationUnit unit = analysisResult.unit;
240 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); 240 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
241 ExpressionFunctionBody body = test.functionExpression.body; 241 ExpressionFunctionBody body = test.functionExpression.body;
242 DartType type = body.expression.staticType; 242 DartType type = body.expression.staticType;
243 243
244 Element elementB = AstFinder.getClass(unit, "B").element; 244 Element elementB = AstFinder.getClass(unit, "B").element;
245 245
246 _isInstantiationOf(_hasElement(elementB))([_isDynamic])(type); 246 _isInstantiationOf(_hasElement(elementB))([_isNull])(type);
247 } 247 }
248 248
249 fail_returnType_variance2() async { 249 test_returnType_variance2() async {
250 // Check that downwards inference correctly pins a type parameter 250 // Check that downwards inference correctly pins a type parameter
251 // when the parameter is constrained in a covariant position 251 // when the parameter is constrained in a covariant position
252 String code = r''' 252 String code = r'''
253 typedef To Func1<From, To>(From x); 253 typedef To Func1<From, To>(From x);
254 Func1<String, T> f<T>(T x) => null; 254 Func1<String, T> f<T>(T x) => null;
255 Func1<String, num> test() => f(42); 255 Func1<String, num> test() => f(42);
256 '''; 256 ''';
257 Source source = addSource(code); 257 Source source = addSource(code);
258 TestAnalysisResult analysisResult = await computeAnalysisResult(source); 258 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
259 assertNoErrors(source); 259 assertNoErrors(source);
260 verify([source]); 260 verify([source]);
261 CompilationUnit unit = analysisResult.unit; 261 CompilationUnit unit = analysisResult.unit;
262 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); 262 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
263 ExpressionFunctionBody body = test.functionExpression.body; 263 ExpressionFunctionBody body = test.functionExpression.body;
264 MethodInvocation invoke = body.expression; 264 MethodInvocation invoke = body.expression;
265 _isFunction2Of(_isNum, _isFunction2Of(_isString, _isNum))( 265 _isFunction2Of(_isNum, _isFunction2Of(_isString, _isNum))(
266 invoke.staticInvokeType); 266 invoke.staticInvokeType);
267 } 267 }
268 268
269 fail_returnType_variance6() async { 269 test_returnType_variance6() async {
270 // Check that pinning works correctly with a partial type 270 // Check that pinning works correctly with a partial type
271 // when the return type uses the variable in a covariant position 271 // when the return type uses the variable in a covariant position
272 String code = r''' 272 String code = r'''
273 typedef To Func1<From, To>(From x); 273 typedef To Func1<From, To>(From x);
274 Func1<String, T> f<T>(T x) => null; 274 Func1<String, T> f<T>(T x) => null;
275 T g<T, S>(Func1<S, T> f) => null; 275 T g<T, S>(Func1<S, T> f) => null;
276 num test() => g(f(3)); 276 num test() => g(f(3));
277 '''; 277 ''';
278 Source source = addSource(code); 278 Source source = addSource(code);
279 TestAnalysisResult analysisResult = await computeAnalysisResult(source); 279 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
280 assertNoErrors(source); 280 assertNoErrors(source);
281 verify([source]); 281 verify([source]);
282 CompilationUnit unit = analysisResult.unit; 282 CompilationUnit unit = analysisResult.unit;
283 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); 283 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
284 ExpressionFunctionBody body = test.functionExpression.body; 284 ExpressionFunctionBody body = test.functionExpression.body;
285 MethodInvocation call = body.expression; 285 MethodInvocation call = body.expression;
286 _isNum(call.staticType); 286 _isNum(call.staticType);
287 _isFunction2Of(_isFunction2Of(_isString, _isNum), _isNum)( 287 _isFunction2Of(_isFunction2Of(_isString, _isNum), _isNum)(
288 call.staticInvokeType); 288 call.staticInvokeType);
289 } 289 }
290 290
291 @override
292 void setUp() {
293 super.setUp();
294 AnalysisOptionsImpl options = new AnalysisOptionsImpl();
295 options.strongMode = true;
296 resetWith(options: options);
297 }
298
299 test_async_method_propagation() async { 291 test_async_method_propagation() async {
300 String code = r''' 292 String code = r'''
301 import "dart:async"; 293 import "dart:async";
302 class A { 294 class A {
303 Future f0() => new Future.value(3); 295 Future f0() => new Future.value(3);
304 Future f1() async => new Future.value(3); 296 Future f1() async => new Future.value(3);
305 Future f2() async => await new Future.value(3); 297 Future f2() async => await new Future.value(3);
306 298
307 Future<int> f3() => new Future.value(3); 299 Future<int> f3() => new Future.value(3);
308 Future<int> f4() async => new Future.value(3); 300 Future<int> f4() async => new Future.value(3);
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
512 AstFinder.getStatementsInTopLevelFunction(unit, "test"); 504 AstFinder.getStatementsInTopLevelFunction(unit, "test");
513 VariableDeclarationStatement stmt = statements[0]; 505 VariableDeclarationStatement stmt = statements[0];
514 VariableDeclaration decl = stmt.variables.variables[0]; 506 VariableDeclaration decl = stmt.variables.variables[0];
515 Expression call = decl.initializer; 507 Expression call = decl.initializer;
516 _isInt(call.staticType); 508 _isInt(call.staticType);
517 } 509 }
518 510
519 test_constrainedByBounds2() async { 511 test_constrainedByBounds2() async {
520 // Test that upwards inference with two type variables does 512 // Test that upwards inference with two type variables does
521 // not propogate from the constrained variable to the unconstrained 513 // not propogate from the constrained variable to the unconstrained
522 // variable if they are ordered right to left. 514 // variable if they are ordered right to left.
Leaf 2017/03/14 22:37:40 Looks like your code now handles right to left? U
Jennifer Messerly 2017/03/14 23:49:09 Done.
523 String code = r''' 515 String code = r'''
524 T f<T extends S, S>(S x) => null; 516 T f<T extends S, S>(S x) => null;
525 void test() { var x = f(3); } 517 void test() { var x = f(3); }
526 '''; 518 ''';
527 Source source = addSource(code); 519 Source source = addSource(code);
528 TestAnalysisResult analysisResult = await computeAnalysisResult(source); 520 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
529 assertNoErrors(source); 521 assertNoErrors(source);
530 verify([source]); 522 verify([source]);
531 CompilationUnit unit = analysisResult.unit; 523 CompilationUnit unit = analysisResult.unit;
532 List<Statement> statements = 524 List<Statement> statements =
533 AstFinder.getStatementsInTopLevelFunction(unit, "test"); 525 AstFinder.getStatementsInTopLevelFunction(unit, "test");
534 VariableDeclarationStatement stmt = statements[0]; 526 VariableDeclarationStatement stmt = statements[0];
535 VariableDeclaration decl = stmt.variables.variables[0]; 527 VariableDeclaration decl = stmt.variables.variables[0];
536 Expression call = decl.initializer; 528 Expression call = decl.initializer;
537 _isDynamic(call.staticType); 529 _isInt(call.staticType);
538 } 530 }
539 531
540 test_constrainedByBounds4() async { 532 test_constrainedByBounds4() async {
541 // Test that upwards inference with two type variables correctly 533 // Test that upwards inference with two type variables correctly
542 // propogates from the constrained variable to the unconstrained 534 // propogates from the constrained variable to the unconstrained
543 // variable if they are ordered left to right, when the variable 535 // variable if they are ordered left to right, when the variable
544 // appears co and contra variantly 536 // appears co and contra variantly
545 String code = r''' 537 String code = r'''
546 typedef To Func1<From, To>(From x); 538 typedef To Func1<From, To>(From x);
547 T f<S, T extends Func1<S, S>>(S x) => null; 539 T f<S, T extends Func1<S, S>>(S x) => null;
(...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after
1057 _isFutureOf([_isNum])(invoke.argumentList.arguments[0].staticType); 1049 _isFutureOf([_isNum])(invoke.argumentList.arguments[0].staticType);
1058 } 1050 }
1059 1051
1060 test_futureOr_downwards6() async { 1052 test_futureOr_downwards6() async {
1061 // Test that downwards inference doesn't decompose FutureOr 1053 // Test that downwards inference doesn't decompose FutureOr
1062 // when instantiating type variables. 1054 // when instantiating type variables.
1063 MethodInvocation invoke = await _testFutureOr(r''' 1055 MethodInvocation invoke = await _testFutureOr(r'''
1064 T mk<T>(T x) => null; 1056 T mk<T>(T x) => null;
1065 FutureOr<int> test() => mk(new Future.value(42)); 1057 FutureOr<int> test() => mk(new Future.value(42));
1066 '''); 1058 ''');
1067 _isFutureOfInt(invoke.staticType); 1059 _isFutureOrOfInt(invoke.staticType);
1068 _isFutureOfInt(invoke.argumentList.arguments[0].staticType); 1060 _isFutureOfInt(invoke.argumentList.arguments[0].staticType);
1069 } 1061 }
1070 1062
1071 test_futureOr_downwards9() async { 1063 test_futureOr_downwards9() async {
1072 // Test that downwards inference decomposes correctly with 1064 // Test that downwards inference decomposes correctly with
1073 // other composite types 1065 // other composite types
1074 MethodInvocation invoke = await _testFutureOr(r''' 1066 MethodInvocation invoke = await _testFutureOr(r'''
1075 List<T> mk<T>(T x) => null; 1067 List<T> mk<T>(T x) => null;
1076 FutureOr<List<int>> test() => mk(3); 1068 FutureOr<List<int>> test() => mk(3);
1077 '''); 1069 ''');
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1118 } 1110 }
1119 1111
1120 test_futureOr_no_return() async { 1112 test_futureOr_no_return() async {
1121 MethodInvocation invoke = await _testFutureOr(r''' 1113 MethodInvocation invoke = await _testFutureOr(r'''
1122 FutureOr<T> mk<T>(Future<T> x) => x; 1114 FutureOr<T> mk<T>(Future<T> x) => x;
1123 Future<int> f; 1115 Future<int> f;
1124 test() => f.then((int x) {}); 1116 test() => f.then((int x) {});
1125 '''); 1117 ''');
1126 _isFunction2Of(_isInt, _isNull)( 1118 _isFunction2Of(_isInt, _isNull)(
1127 invoke.argumentList.arguments[0].staticType); 1119 invoke.argumentList.arguments[0].staticType);
1128 _isFutureOfDynamic(invoke.staticType); 1120 _isFutureOfNull(invoke.staticType);
1129 } 1121 }
1130 1122
1131 test_futureOr_no_return_value() async { 1123 test_futureOr_no_return_value() async {
1132 MethodInvocation invoke = await _testFutureOr(r''' 1124 MethodInvocation invoke = await _testFutureOr(r'''
1133 FutureOr<T> mk<T>(Future<T> x) => x; 1125 FutureOr<T> mk<T>(Future<T> x) => x;
1134 Future<int> f; 1126 Future<int> f;
1135 test() => f.then((int x) {return;}); 1127 test() => f.then((int x) {return;});
1136 '''); 1128 ''');
1137 _isFunction2Of(_isInt, _isNull)( 1129 _isFunction2Of(_isInt, _isNull)(
1138 invoke.argumentList.arguments[0].staticType); 1130 invoke.argumentList.arguments[0].staticType);
1139 _isFutureOfDynamic(invoke.staticType); 1131 _isFutureOfNull(invoke.staticType);
1140 } 1132 }
1141 1133
1142 test_futureOr_return_null() async { 1134 test_futureOr_return_null() async {
1143 MethodInvocation invoke = await _testFutureOr(r''' 1135 MethodInvocation invoke = await _testFutureOr(r'''
1144 FutureOr<T> mk<T>(Future<T> x) => x; 1136 FutureOr<T> mk<T>(Future<T> x) => x;
1145 Future<int> f; 1137 Future<int> f;
1146 test() => f.then((int x) {}); 1138 test() => f.then((int x) {});
1147 '''); 1139 ''');
1148 _isFunction2Of(_isInt, _isNull)( 1140 _isFunction2Of(_isInt, _isNull)(
1149 invoke.argumentList.arguments[0].staticType); 1141 invoke.argumentList.arguments[0].staticType);
1150 _isFutureOfDynamic(invoke.staticType); 1142 _isFutureOfNull(invoke.staticType);
1151 } 1143 }
1152 1144
1153 test_futureOr_upwards1() async { 1145 test_futureOr_upwards1() async {
1154 // Test that upwards inference correctly prefers to instantiate type 1146 // Test that upwards inference correctly prefers to instantiate type
1155 // variables with the "smaller" solution when both are possible. 1147 // variables with the "smaller" solution when both are possible.
1156 MethodInvocation invoke = await _testFutureOr(r''' 1148 MethodInvocation invoke = await _testFutureOr(r'''
1157 Future<T> mk<T>(FutureOr<T> x) => null; 1149 Future<T> mk<T>(FutureOr<T> x) => null;
1158 dynamic test() => mk(new Future<int>.value(42)); 1150 dynamic test() => mk(new Future<int>.value(42));
1159 '''); 1151 ''');
1160 _isFutureOfInt(invoke.staticType); 1152 _isFutureOfInt(invoke.staticType);
1161 } 1153 }
1162 1154
1163 test_futureOr_upwards2() async { 1155 test_futureOr_upwards2() async {
1164 // Test that upwards inference fails when the solution doesn't 1156 // Test that upwards inference fails when the solution doesn't
1165 // match the bound. 1157 // match the bound.
1166 MethodInvocation invoke = await _testFutureOr( 1158 MethodInvocation invoke = await _testFutureOr(
1167 r''' 1159 r'''
1168 Future<T> mk<T extends Future<Object>>(FutureOr<T> x) => null; 1160 Future<T> mk<T extends Future<Object>>(FutureOr<T> x) => null;
1169 dynamic test() => mk(new Future<int>.value(42)); 1161 dynamic test() => mk(new Future<int>.value(42));
1170 ''', 1162 ''',
1171 errors: [StrongModeCode.COULD_NOT_INFER]); 1163 errors: [StrongModeCode.COULD_NOT_INFER]);
1172 _isFutureOf([_isObject])(invoke.staticType); 1164 _isFutureOfInt(invoke.staticType);
1173 } 1165 }
1174 1166
1175 test_futureOrNull_no_return() async { 1167 test_futureOrNull_no_return() async {
1176 MethodInvocation invoke = await _testFutureOr(r''' 1168 MethodInvocation invoke = await _testFutureOr(r'''
1177 FutureOr<T> mk<T>(Future<T> x) => x; 1169 FutureOr<T> mk<T>(Future<T> x) => x;
1178 Future<int> f; 1170 Future<int> f;
1179 test() => f.then<Null>((int x) {}); 1171 test() => f.then<Null>((int x) {});
1180 '''); 1172 ''');
1181 _isFunction2Of(_isInt, _isNull)( 1173 _isFunction2Of(_isInt, _isNull)(
1182 invoke.argumentList.arguments[0].staticType); 1174 invoke.argumentList.arguments[0].staticType);
(...skipping 705 matching lines...) Expand 10 before | Expand all | Expand 10 after
1888 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); 1880 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
1889 ExpressionFunctionBody body = test.functionExpression.body; 1881 ExpressionFunctionBody body = test.functionExpression.body;
1890 FunctionType functionType = body.expression.staticType; 1882 FunctionType functionType = body.expression.staticType;
1891 DartType type = functionType.normalParameterTypes[0]; 1883 DartType type = functionType.normalParameterTypes[0];
1892 1884
1893 Element elementA = AstFinder.getClass(unit, "A").element; 1885 Element elementA = AstFinder.getClass(unit, "A").element;
1894 1886
1895 _isInstantiationOf(_hasElement(elementA))([_isNum, _isNum])(type); 1887 _isInstantiationOf(_hasElement(elementA))([_isNum, _isNum])(type);
1896 } 1888 }
1897 1889
1890 test_inferConstructor_unknownTypeLowerBound() async {
1891 Source source = addSource(r'''
1892 class C<T> {
1893 C(void callback(List<T> a));
1894 }
1895 test() {
1896 // downwards inference pushes List<?> and in parameter position this
1897 // becomes inferred as List<Null>.
1898 var c = new C((items) {});
1899 }
1900 ''');
1901 CompilationUnit unit = (await computeAnalysisResult(source)).unit;
1902 assertNoErrors(source);
1903 verify([source]);
1904 DartType cType = AstFinder
1905 .getTopLevelFunction(unit, "test")
1906 .element
1907 .localVariables[0]
1908 .type;
1909 Element elementC = AstFinder.getClass(unit, "C").element;
1910
1911 _isInstantiationOf(_hasElement(elementC))([_isNull])(cType);
1912 }
1913
1914 test_inferConstructor_varianceForCallback2() async {
1915 Source source = addSource(r'''
1916 class C<T> {
1917 C(void callback(List<T> a));
1918 }
1919 test() {
1920 var c = new C((List items) {});
1921 }
1922 ''');
1923 CompilationUnit unit = (await computeAnalysisResult(source)).unit;
1924 assertNoErrors(source);
1925 verify([source]);
1926 DartType cType = AstFinder
1927 .getTopLevelFunction(unit, "test")
1928 .element
1929 .localVariables[0]
1930 .type;
1931 Element elementC = AstFinder.getClass(unit, "C").element;
1932
1933 _isInstantiationOf(_hasElement(elementC))([_isDynamic])(cType);
1934 }
1935
1936
1898 test_redirectingConstructor_propagation() async { 1937 test_redirectingConstructor_propagation() async {
1899 String code = r''' 1938 String code = r'''
1900 class A { 1939 class A {
1901 A() : this.named([]); 1940 A() : this.named([]);
1902 A.named(List<String> x); 1941 A.named(List<String> x);
1903 } 1942 }
1904 '''; 1943 ''';
1905 CompilationUnit unit = await resolveSource(code); 1944 CompilationUnit unit = await resolveSource(code);
1906 1945
1907 ConstructorDeclaration constructor = 1946 ConstructorDeclaration constructor =
(...skipping 17 matching lines...) Expand all
1925 verify([source]); 1964 verify([source]);
1926 CompilationUnit unit = analysisResult.unit; 1965 CompilationUnit unit = analysisResult.unit;
1927 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); 1966 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
1928 ExpressionFunctionBody body = test.functionExpression.body; 1967 ExpressionFunctionBody body = test.functionExpression.body;
1929 MethodInvocation invoke = body.expression; 1968 MethodInvocation invoke = body.expression;
1930 _isFunction2Of(_isNum, _isFunction2Of(_isNum, _isString))( 1969 _isFunction2Of(_isNum, _isFunction2Of(_isNum, _isString))(
1931 invoke.staticInvokeType); 1970 invoke.staticInvokeType);
1932 } 1971 }
1933 1972
1934 test_returnType_variance3() async { 1973 test_returnType_variance3() async {
1935 // Check that the variance heuristic chooses the less precise type 1974 // Check that the variance heuristic chooses the most precise type
1936 // when the return type uses the variable in a contravariant position 1975 // when the return type uses the variable in a contravariant position
1937 // and there is no downwards constraint. 1976 // and there is no downwards constraint.
1938 String code = r''' 1977 String code = r'''
1939 typedef To Func1<From, To>(From x); 1978 typedef To Func1<From, To>(From x);
1940 Func1<T, String> f<T>(T x, g(T x)) => null; 1979 Func1<T, String> f<T>(T x, g(T x)) => null;
1941 dynamic test() => f(42, (num x) => x); 1980 dynamic test() => f(42, (num x) => x);
1942 '''; 1981 ''';
1943 Source source = addSource(code); 1982 Source source = addSource(code);
1944 TestAnalysisResult analysisResult = await computeAnalysisResult(source); 1983 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
1945 assertNoErrors(source); 1984 assertNoErrors(source);
1946 verify([source]); 1985 verify([source]);
1947 CompilationUnit unit = analysisResult.unit; 1986 CompilationUnit unit = analysisResult.unit;
1948 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); 1987 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
1949 ExpressionFunctionBody body = test.functionExpression.body; 1988 ExpressionFunctionBody body = test.functionExpression.body;
1950 FunctionType functionType = body.expression.staticType; 1989 FunctionType functionType = body.expression.staticType;
1951 DartType type = functionType.normalParameterTypes[0]; 1990 DartType type = functionType.normalParameterTypes[0];
1952 _isNum(type); 1991 _isInt(type);
1953 } 1992 }
1954 1993
1955 test_returnType_variance4() async { 1994 test_returnType_variance4() async {
1956 // Check that the variance heuristic chooses the more precise type 1995 // Check that the variance heuristic chooses the more precise type
1957 // when the return type uses the variable in a covariant position 1996 // when the return type uses the variable in a covariant position
1958 // and there is no downwards constraint 1997 // and there is no downwards constraint
1959 String code = r''' 1998 String code = r'''
1960 typedef To Func1<From, To>(From x); 1999 typedef To Func1<From, To>(From x);
1961 Func1<String, T> f<T>(T x, g(T x)) => null; 2000 Func1<String, T> f<T>(T x, g(T x)) => null;
1962 dynamic test() => f(42, (num x) => x); 2001 dynamic test() => f(42, (num x) => x);
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after
2294 Expression exp = (statements[i] as ExpressionStatement).expression; 2333 Expression exp = (statements[i] as ExpressionStatement).expression;
2295 expect(exp.staticType, typeProvider.dynamicType); 2334 expect(exp.staticType, typeProvider.dynamicType);
2296 } 2335 }
2297 } 2336 }
2298 2337
2299 checkBody("C"); 2338 checkBody("C");
2300 checkBody("D"); 2339 checkBody("D");
2301 } 2340 }
2302 2341
2303 test_genericFunction_upwardsAndDownwards() async { 2342 test_genericFunction_upwardsAndDownwards() async {
2304 // Regression tests for https://github.com/dart-lang/sdk/issues/27151. 2343 // Regression tests for https://github.com/dart-lang/sdk/issues/27586.
2305 await resolveTestUnit(r'List<num> x = [1, 2];'); 2344 await resolveTestUnit(r'List<num> x = [1, 2];');
2306 expectInitializerType('x', 'List<int>'); 2345 expectInitializerType('x', 'List<num>');
2346 }
2347
2348 test_genericFunction_upwardsAndDownwards_Object() async {
2349 // Regression tests for https://github.com/dart-lang/sdk/issues/27625.
2350 await resolveTestUnit(r'''
2351 List<Object> aaa = [];
2352 List<Object> bbb = [1, 2, 3];
2353 List<Object> ccc = [null];
2354 List<Object> ddd = [1 as dynamic];
2355 List<Object> eee = [new Object()];
2356 ''');
2357 expectInitializerType('aaa', 'List<Object>');
2358 expectInitializerType('bbb', 'List<Object>');
2359 expectInitializerType('ccc', 'List<Object>');
2360 expectInitializerType('ddd', 'List<Object>');
2361 expectInitializerType('eee', 'List<Object>');
2307 } 2362 }
2308 2363
2309 test_genericMethod() async { 2364 test_genericMethod() async {
2310 await resolveTestUnit(r''' 2365 await resolveTestUnit(r'''
2311 class C<E> { 2366 class C<E> {
2312 List/*<T>*/ f/*<T>*/(E e) => null; 2367 List/*<T>*/ f/*<T>*/(E e) => null;
2313 } 2368 }
2314 main() { 2369 main() {
2315 C<String> cOfString; 2370 C<String> cOfString;
2316 } 2371 }
(...skipping 687 matching lines...) Expand 10 before | Expand all | Expand 10 after
3004 class C<T> { 3059 class C<T> {
3005 void m<S0 extends T, S1 extends List<S0>>(S0 p0, S1 p1) {} 3060 void m<S0 extends T, S1 extends List<S0>>(S0 p0, S1 p1) {}
3006 3061
3007 void main() { 3062 void main() {
3008 m(null, null); 3063 m(null, null);
3009 } 3064 }
3010 } 3065 }
3011 '''; 3066 ''';
3012 await resolveTestUnit(code); 3067 await resolveTestUnit(code);
3013 assertNoErrors(testSource); 3068 assertNoErrors(testSource);
3014 expectStaticInvokeType('m(null', '(T, List<T>) → void'); 3069 expectStaticInvokeType('m(null', '(Null, Null) → void');
3070 }
3071
3072 test_instantiateToBounds_method_ok_referenceOther_before2() async {
3073 String code = r'''
3074 class C<T> {
3075 Map<S0, S1> m<S0 extends T, S1 extends List<S0>>() => null;
3076
3077 void main() {
3078 m();
3079 }
3080 }
3081 ''';
3082 await resolveTestUnit(code);
3083 assertNoErrors(testSource);
3084 expectStaticInvokeType('m();', '() → Map<T, List<T>>');
3015 } 3085 }
3016 3086
3017 test_instantiateToBounds_method_ok_simpleBounds() async { 3087 test_instantiateToBounds_method_ok_simpleBounds() async {
3018 String code = r''' 3088 String code = r'''
3019 class C<T> { 3089 class C<T> {
3020 void m<S extends T>(S p0) {} 3090 void m<S extends T>(S p0) {}
3021 3091
3022 void main() { 3092 void main() {
3023 m(null); 3093 m(null);
3024 } 3094 }
3025 } 3095 }
3026 '''; 3096 ''';
3027 await resolveTestUnit(code); 3097 await resolveTestUnit(code);
3028 assertNoErrors(testSource); 3098 assertNoErrors(testSource);
3029 expectStaticInvokeType('m(null)', '(T) → void'); 3099 expectStaticInvokeType('m(null)', '(Null) → void');
3100 }
3101
3102 test_instantiateToBounds_method_ok_simpleBounds2() async {
3103 String code = r'''
3104 class C<T> {
3105 S m<S extends T>() => null;
3106
3107 void main() {
3108 m();
3109 }
3110 }
3111 ''';
3112 await resolveTestUnit(code);
3113 assertNoErrors(testSource);
3114 expectStaticInvokeType('m();', '() → T');
3030 } 3115 }
3031 3116
3032 test_notInstantiatedBound_direct_class_class() async { 3117 test_notInstantiatedBound_direct_class_class() async {
3033 String code = r''' 3118 String code = r'''
3034 class A<T extends int> {} 3119 class A<T extends int> {}
3035 class C<T extends A> {} 3120 class C<T extends A> {}
3036 '''; 3121 ''';
3037 await resolveTestUnit(code, noErrors: false); 3122 await resolveTestUnit(code, noErrors: false);
3038 assertErrors(testSource, [StrongModeCode.NOT_INSTANTIATED_BOUND]); 3123 assertErrors(testSource, [StrongModeCode.NOT_INSTANTIATED_BOUND]);
3039 } 3124 }
(...skipping 515 matching lines...) Expand 10 before | Expand all | Expand 10 after
3555 var v = x; 3640 var v = x;
3556 v; // marker 3641 v; // marker
3557 } 3642 }
3558 int x = 3; 3643 int x = 3;
3559 '''; 3644 ''';
3560 CompilationUnit unit = await resolveSource(code); 3645 CompilationUnit unit = await resolveSource(code);
3561 assertPropagatedAssignedType(code, unit, typeProvider.intType, null); 3646 assertPropagatedAssignedType(code, unit, typeProvider.intType, null);
3562 assertTypeOfMarkedExpression(code, unit, typeProvider.intType, null); 3647 assertTypeOfMarkedExpression(code, unit, typeProvider.intType, null);
3563 } 3648 }
3564 } 3649 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698