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

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

Issue 2667343005: Infer Null for return type of functions with empty returns. (Closed)
Patch Set: Avoid merge conflict Created 3 years, 10 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 21 matching lines...) Expand all
32 /** 32 /**
33 * Strong mode static analyzer local type inference tests 33 * Strong mode static analyzer local type inference tests
34 */ 34 */
35 @reflectiveTest 35 @reflectiveTest
36 class StrongModeLocalInferenceTest extends ResolverTestCase { 36 class StrongModeLocalInferenceTest extends ResolverTestCase {
37 TypeAssertions _assertions; 37 TypeAssertions _assertions;
38 38
39 Asserter<DartType> _isDynamic; 39 Asserter<DartType> _isDynamic;
40 Asserter<InterfaceType> _isFutureOfDynamic; 40 Asserter<InterfaceType> _isFutureOfDynamic;
41 Asserter<InterfaceType> _isFutureOfInt; 41 Asserter<InterfaceType> _isFutureOfInt;
42 Asserter<InterfaceType> _isFutureOfNull;
42 Asserter<InterfaceType> _isFutureOrOfInt; 43 Asserter<InterfaceType> _isFutureOrOfInt;
43 Asserter<DartType> _isInt; 44 Asserter<DartType> _isInt;
45 Asserter<DartType> _isNull;
44 Asserter<DartType> _isNum; 46 Asserter<DartType> _isNum;
45 Asserter<DartType> _isObject; 47 Asserter<DartType> _isObject;
46 Asserter<DartType> _isString; 48 Asserter<DartType> _isString;
47 49
48 AsserterBuilder2<Asserter<DartType>, Asserter<DartType>, DartType> 50 AsserterBuilder2<Asserter<DartType>, Asserter<DartType>, DartType>
49 _isFunction2Of; 51 _isFunction2Of;
50 AsserterBuilder<List<Asserter<DartType>>, InterfaceType> _isFutureOf; 52 AsserterBuilder<List<Asserter<DartType>>, InterfaceType> _isFutureOf;
51 AsserterBuilder<List<Asserter<DartType>>, InterfaceType> _isFutureOrOf; 53 AsserterBuilder<List<Asserter<DartType>>, InterfaceType> _isFutureOrOf;
52 AsserterBuilderBuilder<Asserter<DartType>, List<Asserter<DartType>>, DartType> 54 AsserterBuilderBuilder<Asserter<DartType>, List<Asserter<DartType>>, DartType>
53 _isInstantiationOf; 55 _isInstantiationOf;
54 AsserterBuilder<Asserter<DartType>, InterfaceType> _isListOf; 56 AsserterBuilder<Asserter<DartType>, InterfaceType> _isListOf;
55 AsserterBuilder2<Asserter<DartType>, Asserter<DartType>, InterfaceType> 57 AsserterBuilder2<Asserter<DartType>, Asserter<DartType>, InterfaceType>
56 _isMapOf; 58 _isMapOf;
57 AsserterBuilder<List<Asserter<DartType>>, InterfaceType> _isStreamOf; 59 AsserterBuilder<List<Asserter<DartType>>, InterfaceType> _isStreamOf;
58 AsserterBuilder<DartType, DartType> _isType; 60 AsserterBuilder<DartType, DartType> _isType;
59 61
60 AsserterBuilder<Element, DartType> _hasElement; 62 AsserterBuilder<Element, DartType> _hasElement;
61 AsserterBuilder<DartType, DartType> _hasElementOf; 63 AsserterBuilder<DartType, DartType> _hasElementOf;
62 64
63 @override 65 @override
64 Future<TestAnalysisResult> computeAnalysisResult(Source source) async { 66 Future<TestAnalysisResult> computeAnalysisResult(Source source) async {
65 TestAnalysisResult result = await super.computeAnalysisResult(source); 67 TestAnalysisResult result = await super.computeAnalysisResult(source);
66 if (_assertions == null) { 68 if (_assertions == null) {
67 _assertions = new TypeAssertions(typeProvider); 69 _assertions = new TypeAssertions(typeProvider);
68 _isType = _assertions.isType; 70 _isType = _assertions.isType;
69 _hasElement = _assertions.hasElement; 71 _hasElement = _assertions.hasElement;
70 _isInstantiationOf = _assertions.isInstantiationOf; 72 _isInstantiationOf = _assertions.isInstantiationOf;
71 _isInt = _assertions.isInt; 73 _isInt = _assertions.isInt;
74 _isNull = _assertions.isNull;
72 _isNum = _assertions.isNum; 75 _isNum = _assertions.isNum;
73 _isObject = _assertions.isObject; 76 _isObject = _assertions.isObject;
74 _isString = _assertions.isString; 77 _isString = _assertions.isString;
75 _isDynamic = _assertions.isDynamic; 78 _isDynamic = _assertions.isDynamic;
76 _isListOf = _assertions.isListOf; 79 _isListOf = _assertions.isListOf;
77 _isMapOf = _assertions.isMapOf; 80 _isMapOf = _assertions.isMapOf;
78 _isFunction2Of = _assertions.isFunction2Of; 81 _isFunction2Of = _assertions.isFunction2Of;
79 _hasElementOf = _assertions.hasElementOf; 82 _hasElementOf = _assertions.hasElementOf;
80 _isFutureOf = _isInstantiationOf(_hasElementOf(typeProvider.futureType)); 83 _isFutureOf = _isInstantiationOf(_hasElementOf(typeProvider.futureType));
81 _isFutureOrOf = 84 _isFutureOrOf =
82 _isInstantiationOf(_hasElementOf(typeProvider.futureOrType)); 85 _isInstantiationOf(_hasElementOf(typeProvider.futureOrType));
83 _isFutureOfDynamic = _isFutureOf([_isDynamic]); 86 _isFutureOfDynamic = _isFutureOf([_isDynamic]);
84 _isFutureOfInt = _isFutureOf([_isInt]); 87 _isFutureOfInt = _isFutureOf([_isInt]);
88 _isFutureOfNull = _isFutureOf([_isNull]);
85 _isFutureOrOfInt = _isFutureOrOf([_isInt]); 89 _isFutureOrOfInt = _isFutureOrOf([_isInt]);
86 _isStreamOf = _isInstantiationOf(_hasElementOf(typeProvider.streamType)); 90 _isStreamOf = _isInstantiationOf(_hasElementOf(typeProvider.streamType));
87 } 91 }
88 return result; 92 return result;
89 } 93 }
90 94
91 fail_constrainedByBounds3() async { 95 fail_constrainedByBounds3() async {
92 // Test that upwards inference with two type variables does 96 // Test that upwards inference with two type variables does
93 // not propogate from the constrained variable to the unconstrained 97 // not propogate from the constrained variable to the unconstrained
94 // variable if they are ordered right to left, and that if the result 98 // variable if they are ordered right to left, and that if the result
(...skipping 1011 matching lines...) Expand 10 before | Expand all | Expand 10 after
1106 test_futureOr_methods4() async { 1110 test_futureOr_methods4() async {
1107 // Test that FutureOr<dynamic> does not have all methods 1111 // Test that FutureOr<dynamic> does not have all methods
1108 MethodInvocation invoke = await _testFutureOr( 1112 MethodInvocation invoke = await _testFutureOr(
1109 r''' 1113 r'''
1110 dynamic test(FutureOr<dynamic> x) => x.abs(); 1114 dynamic test(FutureOr<dynamic> x) => x.abs();
1111 ''', 1115 ''',
1112 errors: [StaticTypeWarningCode.UNDEFINED_METHOD]); 1116 errors: [StaticTypeWarningCode.UNDEFINED_METHOD]);
1113 _isDynamic(invoke.staticType); 1117 _isDynamic(invoke.staticType);
1114 } 1118 }
1115 1119
1120 test_futureOr_no_return() async {
1121 MethodInvocation invoke = await _testFutureOr(r'''
1122 FutureOr<T> mk<T>(Future<T> x) => x;
1123 Future<int> f;
1124 test() => f.then((int x) {});
1125 ''');
1126 _isFunction2Of(_isInt, _isNull)(
1127 invoke.argumentList.arguments[0].staticType);
1128 _isFutureOfDynamic(invoke.staticType);
1129 }
1130
1131 test_futureOr_no_return_value() async {
1132 MethodInvocation invoke = await _testFutureOr(r'''
1133 FutureOr<T> mk<T>(Future<T> x) => x;
1134 Future<int> f;
1135 test() => f.then((int x) {return;});
1136 ''');
1137 _isFunction2Of(_isInt, _isNull)(
1138 invoke.argumentList.arguments[0].staticType);
1139 _isFutureOfDynamic(invoke.staticType);
1140 }
1141
1142 test_futureOr_return_null() async {
1143 MethodInvocation invoke = await _testFutureOr(r'''
1144 FutureOr<T> mk<T>(Future<T> x) => x;
1145 Future<int> f;
1146 test() => f.then((int x) {});
1147 ''');
1148 _isFunction2Of(_isInt, _isNull)(
1149 invoke.argumentList.arguments[0].staticType);
1150 _isFutureOfDynamic(invoke.staticType);
1151 }
1152
1116 test_futureOr_upwards1() async { 1153 test_futureOr_upwards1() async {
1117 // Test that upwards inference correctly prefers to instantiate type 1154 // Test that upwards inference correctly prefers to instantiate type
1118 // variables with the "smaller" solution when both are possible. 1155 // variables with the "smaller" solution when both are possible.
1119 MethodInvocation invoke = await _testFutureOr(r''' 1156 MethodInvocation invoke = await _testFutureOr(r'''
1120 Future<T> mk<T>(FutureOr<T> x) => null; 1157 Future<T> mk<T>(FutureOr<T> x) => null;
1121 dynamic test() => mk(new Future<int>.value(42)); 1158 dynamic test() => mk(new Future<int>.value(42));
1122 '''); 1159 ''');
1123 _isFutureOfInt(invoke.staticType); 1160 _isFutureOfInt(invoke.staticType);
1124 } 1161 }
1125 1162
1126 test_futureOr_upwards2() async { 1163 test_futureOr_upwards2() async {
1127 // Test that upwards inference fails when the solution doesn't 1164 // Test that upwards inference fails when the solution doesn't
1128 // match the bound. 1165 // match the bound.
1129 MethodInvocation invoke = await _testFutureOr( 1166 MethodInvocation invoke = await _testFutureOr(
1130 r''' 1167 r'''
1131 Future<T> mk<T extends Future<Object>>(FutureOr<T> x) => null; 1168 Future<T> mk<T extends Future<Object>>(FutureOr<T> x) => null;
1132 dynamic test() => mk(new Future<int>.value(42)); 1169 dynamic test() => mk(new Future<int>.value(42));
1133 ''', 1170 ''',
1134 errors: [StrongModeCode.COULD_NOT_INFER]); 1171 errors: [StrongModeCode.COULD_NOT_INFER]);
1135 _isFutureOf([_isObject])(invoke.staticType); 1172 _isFutureOf([_isObject])(invoke.staticType);
1136 } 1173 }
1137 1174
1175 test_futureOrNill_no_return_value() async {
Brian Wilkerson 2017/02/03 14:41:45 "Nill" --> "Null"
Leaf 2017/02/06 18:27:15 Done.
1176 MethodInvocation invoke = await _testFutureOr(r'''
1177 FutureOr<T> mk<T>(Future<T> x) => x;
1178 Future<int> f;
1179 test() => f.then<Null>((int x) {return;});
1180 ''');
1181 _isFunction2Of(_isInt, _isNull)(
1182 invoke.argumentList.arguments[0].staticType);
1183 _isFutureOfNull(invoke.staticType);
1184 }
1185
1186 test_futureOrNull_no_return() async {
1187 MethodInvocation invoke = await _testFutureOr(r'''
1188 FutureOr<T> mk<T>(Future<T> x) => x;
1189 Future<int> f;
1190 test() => f.then<Null>((int x) {});
1191 ''');
1192 _isFunction2Of(_isInt, _isNull)(
1193 invoke.argumentList.arguments[0].staticType);
1194 _isFutureOfNull(invoke.staticType);
1195 }
1196
1197 test_futureOrNull_return_null() async {
1198 MethodInvocation invoke = await _testFutureOr(r'''
1199 FutureOr<T> mk<T>(Future<T> x) => x;
1200 Future<int> f;
1201 test() => f.then<Null>((int x) {});
1202 ''');
1203 _isFunction2Of(_isInt, _isNull)(
1204 invoke.argumentList.arguments[0].staticType);
1205 _isFutureOfNull(invoke.staticType);
1206 }
1207
1138 test_inference_hints() async { 1208 test_inference_hints() async {
1139 Source source = addSource(r''' 1209 Source source = addSource(r'''
1140 void main () { 1210 void main () {
1141 var x = 3; 1211 var x = 3;
1142 List<int> l0 = []; 1212 List<int> l0 = [];
1143 } 1213 }
1144 '''); 1214 ''');
1145 await computeAnalysisResult(source); 1215 await computeAnalysisResult(source);
1146 assertNoErrors(source); 1216 assertNoErrors(source);
1147 verify([source]); 1217 verify([source]);
(...skipping 2321 matching lines...) Expand 10 before | Expand all | Expand 10 after
3469 var v = x; 3539 var v = x;
3470 v; // marker 3540 v; // marker
3471 } 3541 }
3472 int x = 3; 3542 int x = 3;
3473 '''; 3543 ''';
3474 CompilationUnit unit = await resolveSource(code); 3544 CompilationUnit unit = await resolveSource(code);
3475 assertPropagatedAssignedType(code, unit, typeProvider.intType, null); 3545 assertPropagatedAssignedType(code, unit, typeProvider.intType, null);
3476 assertTypeOfMarkedExpression(code, unit, typeProvider.intType, null); 3546 assertTypeOfMarkedExpression(code, unit, typeProvider.intType, null);
3477 } 3547 }
3478 } 3548 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698