| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library analyzer.test.generated.strong_mode_test; | 5 library analyzer.test.generated.strong_mode_test; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analyzer/dart/ast/ast.dart'; | 9 import 'package:analyzer/dart/ast/ast.dart'; |
| 10 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; | 10 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; |
| 11 import 'package:analyzer/dart/element/element.dart'; | 11 import 'package:analyzer/dart/element/element.dart'; |
| 12 import 'package:analyzer/dart/element/type.dart'; | 12 import 'package:analyzer/dart/element/type.dart'; |
| 13 import 'package:analyzer/src/dart/element/element.dart'; | 13 import 'package:analyzer/src/dart/element/element.dart'; |
| 14 import 'package:analyzer/src/error/codes.dart'; | 14 import 'package:analyzer/src/error/codes.dart'; |
| 15 import 'package:analyzer/src/generated/engine.dart'; | 15 import 'package:analyzer/src/generated/engine.dart'; |
| 16 import 'package:analyzer/src/generated/source_io.dart'; | 16 import 'package:analyzer/src/generated/source_io.dart'; |
| 17 import 'package:front_end/src/base/errors.dart'; |
| 17 import 'package:test/test.dart'; | 18 import 'package:test/test.dart'; |
| 18 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 19 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 19 | 20 |
| 20 import '../utils.dart'; | 21 import '../utils.dart'; |
| 21 import 'resolver_test_case.dart'; | 22 import 'resolver_test_case.dart'; |
| 22 | 23 |
| 23 main() { | 24 main() { |
| 24 defineReflectiveSuite(() { | 25 defineReflectiveSuite(() { |
| 25 defineReflectiveTests(StrongModeLocalInferenceTest); | 26 defineReflectiveTests(StrongModeLocalInferenceTest); |
| 26 defineReflectiveTests(StrongModeStaticTypeAnalyzer2Test); | 27 defineReflectiveTests(StrongModeStaticTypeAnalyzer2Test); |
| 27 defineReflectiveTests(StrongModeTypePropagationTest); | 28 defineReflectiveTests(StrongModeTypePropagationTest); |
| 28 }); | 29 }); |
| 29 } | 30 } |
| 30 | 31 |
| 31 /** | 32 /** |
| 32 * Strong mode static analyzer local type inference tests | 33 * Strong mode static analyzer local type inference tests |
| 33 */ | 34 */ |
| 34 @reflectiveTest | 35 @reflectiveTest |
| 35 class StrongModeLocalInferenceTest extends ResolverTestCase { | 36 class StrongModeLocalInferenceTest extends ResolverTestCase { |
| 36 TypeAssertions _assertions; | 37 TypeAssertions _assertions; |
| 37 | 38 |
| 38 Asserter<DartType> _isDynamic; | 39 Asserter<DartType> _isDynamic; |
| 39 Asserter<InterfaceType> _isFutureOfDynamic; | 40 Asserter<InterfaceType> _isFutureOfDynamic; |
| 40 Asserter<InterfaceType> _isFutureOfInt; | 41 Asserter<InterfaceType> _isFutureOfInt; |
| 42 Asserter<InterfaceType> _isFutureOrOfInt; |
| 41 Asserter<DartType> _isInt; | 43 Asserter<DartType> _isInt; |
| 42 Asserter<DartType> _isNum; | 44 Asserter<DartType> _isNum; |
| 43 Asserter<DartType> _isObject; | 45 Asserter<DartType> _isObject; |
| 44 Asserter<DartType> _isString; | 46 Asserter<DartType> _isString; |
| 45 | 47 |
| 46 AsserterBuilder2<Asserter<DartType>, Asserter<DartType>, DartType> | 48 AsserterBuilder2<Asserter<DartType>, Asserter<DartType>, DartType> |
| 47 _isFunction2Of; | 49 _isFunction2Of; |
| 48 AsserterBuilder<List<Asserter<DartType>>, InterfaceType> _isFutureOf; | 50 AsserterBuilder<List<Asserter<DartType>>, InterfaceType> _isFutureOf; |
| 51 AsserterBuilder<List<Asserter<DartType>>, InterfaceType> _isFutureOrOf; |
| 49 AsserterBuilderBuilder<Asserter<DartType>, List<Asserter<DartType>>, DartType> | 52 AsserterBuilderBuilder<Asserter<DartType>, List<Asserter<DartType>>, DartType> |
| 50 _isInstantiationOf; | 53 _isInstantiationOf; |
| 51 AsserterBuilder<Asserter<DartType>, InterfaceType> _isListOf; | 54 AsserterBuilder<Asserter<DartType>, InterfaceType> _isListOf; |
| 52 AsserterBuilder2<Asserter<DartType>, Asserter<DartType>, InterfaceType> | 55 AsserterBuilder2<Asserter<DartType>, Asserter<DartType>, InterfaceType> |
| 53 _isMapOf; | 56 _isMapOf; |
| 54 AsserterBuilder<List<Asserter<DartType>>, InterfaceType> _isStreamOf; | 57 AsserterBuilder<List<Asserter<DartType>>, InterfaceType> _isStreamOf; |
| 55 AsserterBuilder<DartType, DartType> _isType; | 58 AsserterBuilder<DartType, DartType> _isType; |
| 56 | 59 |
| 57 AsserterBuilder<Element, DartType> _hasElement; | 60 AsserterBuilder<Element, DartType> _hasElement; |
| 58 AsserterBuilder<DartType, DartType> _hasElementOf; | 61 AsserterBuilder<DartType, DartType> _hasElementOf; |
| 59 | 62 |
| 60 @override | 63 @override |
| 61 Future<TestAnalysisResult> computeAnalysisResult(Source source) async { | 64 Future<TestAnalysisResult> computeAnalysisResult(Source source) async { |
| 62 TestAnalysisResult result = await super.computeAnalysisResult(source); | 65 TestAnalysisResult result = await super.computeAnalysisResult(source); |
| 63 if (_assertions == null) { | 66 if (_assertions == null) { |
| 64 _assertions = new TypeAssertions(typeProvider); | 67 _assertions = new TypeAssertions(typeProvider); |
| 65 _isType = _assertions.isType; | 68 _isType = _assertions.isType; |
| 66 _hasElement = _assertions.hasElement; | 69 _hasElement = _assertions.hasElement; |
| 67 _isInstantiationOf = _assertions.isInstantiationOf; | 70 _isInstantiationOf = _assertions.isInstantiationOf; |
| 68 _isInt = _assertions.isInt; | 71 _isInt = _assertions.isInt; |
| 69 _isNum = _assertions.isNum; | 72 _isNum = _assertions.isNum; |
| 70 _isObject = _assertions.isObject; | 73 _isObject = _assertions.isObject; |
| 71 _isString = _assertions.isString; | 74 _isString = _assertions.isString; |
| 72 _isDynamic = _assertions.isDynamic; | 75 _isDynamic = _assertions.isDynamic; |
| 73 _isListOf = _assertions.isListOf; | 76 _isListOf = _assertions.isListOf; |
| 74 _isMapOf = _assertions.isMapOf; | 77 _isMapOf = _assertions.isMapOf; |
| 75 _isFunction2Of = _assertions.isFunction2Of; | 78 _isFunction2Of = _assertions.isFunction2Of; |
| 76 _hasElementOf = _assertions.hasElementOf; | 79 _hasElementOf = _assertions.hasElementOf; |
| 77 _isFutureOf = _isInstantiationOf(_hasElementOf(typeProvider.futureType)); | 80 _isFutureOf = _isInstantiationOf(_hasElementOf(typeProvider.futureType)); |
| 81 _isFutureOrOf = |
| 82 _isInstantiationOf(_hasElementOf(typeProvider.futureOrType)); |
| 78 _isFutureOfDynamic = _isFutureOf([_isDynamic]); | 83 _isFutureOfDynamic = _isFutureOf([_isDynamic]); |
| 79 _isFutureOfInt = _isFutureOf([_isInt]); | 84 _isFutureOfInt = _isFutureOf([_isInt]); |
| 85 _isFutureOrOfInt = _isFutureOrOf([_isInt]); |
| 80 _isStreamOf = _isInstantiationOf(_hasElementOf(typeProvider.streamType)); | 86 _isStreamOf = _isInstantiationOf(_hasElementOf(typeProvider.streamType)); |
| 81 } | 87 } |
| 82 return result; | 88 return result; |
| 83 } | 89 } |
| 84 | 90 |
| 85 fail_constrainedByBounds3() async { | 91 fail_constrainedByBounds3() async { |
| 86 // Test that upwards inference with two type variables does | 92 // Test that upwards inference with two type variables does |
| 87 // not propogate from the constrained variable to the unconstrained | 93 // not propogate from the constrained variable to the unconstrained |
| 88 // variable if they are ordered right to left, and that if the result | 94 // variable if they are ordered right to left, and that if the result |
| 89 // is not a valid instantiation an error is issued | 95 // is not a valid instantiation an error is issued |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 VariableDeclaration decl = stmt.variables.variables[0]; | 134 VariableDeclaration decl = stmt.variables.variables[0]; |
| 129 Expression call = decl.initializer; | 135 Expression call = decl.initializer; |
| 130 _isInt(call.staticType); | 136 _isInt(call.staticType); |
| 131 } | 137 } |
| 132 | 138 |
| 133 fail_futureOr_downwards7() async { | 139 fail_futureOr_downwards7() async { |
| 134 // Test that downwards inference incorporates bounds correctly | 140 // Test that downwards inference incorporates bounds correctly |
| 135 // when instantiating type variables. | 141 // when instantiating type variables. |
| 136 // TODO(leafp): I think this should pass once the inference changes | 142 // TODO(leafp): I think this should pass once the inference changes |
| 137 // that jmesserly is adding are landed. | 143 // that jmesserly is adding are landed. |
| 138 String code = r''' | 144 MethodInvocation invoke = await _testFutureOr(r''' |
| 139 import "dart:async"; | |
| 140 T mk<T extends int>(T x) => null; | 145 T mk<T extends int>(T x) => null; |
| 141 FutureOr<int> test() => mk(new Future.value(42)); | 146 FutureOr<int> test() => mk(new Future.value(42)); |
| 142 '''; | 147 '''); |
| 143 Source source = addSource(code); | |
| 144 TestAnalysisResult analysisResult = await computeAnalysisResult(source); | |
| 145 assertNoErrors(source); | |
| 146 verify([source]); | |
| 147 CompilationUnit unit = analysisResult.unit; | |
| 148 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); | |
| 149 ExpressionFunctionBody body = test.functionExpression.body; | |
| 150 MethodInvocation invoke = body.expression; | |
| 151 _isFutureOfInt(invoke.staticType); | 148 _isFutureOfInt(invoke.staticType); |
| 152 _isFutureOfInt(invoke.argumentList.arguments[0].staticType); | 149 _isFutureOfInt(invoke.argumentList.arguments[0].staticType); |
| 153 } | 150 } |
| 154 | 151 |
| 155 fail_futureOr_downwards8() async { | 152 fail_futureOr_downwards8() async { |
| 156 // Test that downwards inference incorporates bounds correctly | 153 // Test that downwards inference incorporates bounds correctly |
| 157 // when instantiating type variables. | 154 // when instantiating type variables. |
| 158 // TODO(leafp): I think this should pass once the inference changes | 155 // TODO(leafp): I think this should pass once the inference changes |
| 159 // that jmesserly is adding are landed. | 156 // that jmesserly is adding are landed. |
| 160 String code = r''' | 157 MethodInvocation invoke = await _testFutureOr(r''' |
| 161 import "dart:async"; | |
| 162 T mk<T extends Future<Object>>(T x) => null; | 158 T mk<T extends Future<Object>>(T x) => null; |
| 163 FutureOr<int> test() => mk(new Future.value(42)); | 159 FutureOr<int> test() => mk(new Future.value(42)); |
| 164 '''; | 160 '''); |
| 165 Source source = addSource(code); | |
| 166 TestAnalysisResult analysisResult = await computeAnalysisResult(source); | |
| 167 assertNoErrors(source); | |
| 168 verify([source]); | |
| 169 CompilationUnit unit = analysisResult.unit; | |
| 170 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); | |
| 171 ExpressionFunctionBody body = test.functionExpression.body; | |
| 172 MethodInvocation invoke = body.expression; | |
| 173 _isFutureOfInt(invoke.staticType); | 161 _isFutureOfInt(invoke.staticType); |
| 174 _isFutureOfInt(invoke.argumentList.arguments[0].staticType); | 162 _isFutureOfInt(invoke.argumentList.arguments[0].staticType); |
| 175 } | 163 } |
| 176 | 164 |
| 177 fail_pinning_multipleConstraints1() async { | 165 fail_pinning_multipleConstraints1() async { |
| 178 // Test that downwards inference with two different downwards covariant | 166 // Test that downwards inference with two different downwards covariant |
| 179 // constraints on the same parameter correctly fails to infer when | 167 // constraints on the same parameter correctly fails to infer when |
| 180 // the types do not share a common subtype | 168 // the types do not share a common subtype |
| 181 String code = r''' | 169 String code = r''' |
| 182 class A<S, T> { | 170 class A<S, T> { |
| (...skipping 780 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 963 expect(functionReturnValue(0).staticType, typeProvider.intType); | 951 expect(functionReturnValue(0).staticType, typeProvider.intType); |
| 964 expect(functionReturnValue(1).staticType, typeProvider.intType); | 952 expect(functionReturnValue(1).staticType, typeProvider.intType); |
| 965 expect(functionReturnValue(2).staticType, typeProvider.intType); | 953 expect(functionReturnValue(2).staticType, typeProvider.intType); |
| 966 expect(functionReturnValue(3).staticType, typeProvider.dynamicType); | 954 expect(functionReturnValue(3).staticType, typeProvider.dynamicType); |
| 967 expect(functionReturnValue(4).staticType, typeProvider.stringType); | 955 expect(functionReturnValue(4).staticType, typeProvider.stringType); |
| 968 } | 956 } |
| 969 | 957 |
| 970 test_futureOr_downwards1() async { | 958 test_futureOr_downwards1() async { |
| 971 // Test that downwards inference interacts correctly with FutureOr | 959 // Test that downwards inference interacts correctly with FutureOr |
| 972 // parameters. | 960 // parameters. |
| 973 String code = r''' | 961 MethodInvocation invoke = await _testFutureOr(r''' |
| 974 import "dart:async"; | |
| 975 Future<T> mk<T>(FutureOr<T> x) => null; | 962 Future<T> mk<T>(FutureOr<T> x) => null; |
| 976 Future<int> test() => mk(new Future<int>.value(42)); | 963 Future<int> test() => mk(new Future<int>.value(42)); |
| 977 '''; | 964 '''); |
| 978 Source source = addSource(code); | |
| 979 TestAnalysisResult analysisResult = await computeAnalysisResult(source); | |
| 980 assertNoErrors(source); | |
| 981 verify([source]); | |
| 982 CompilationUnit unit = analysisResult.unit; | |
| 983 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); | |
| 984 ExpressionFunctionBody body = test.functionExpression.body; | |
| 985 MethodInvocation invoke = body.expression; | |
| 986 _isFutureOfInt(invoke.staticType); | 965 _isFutureOfInt(invoke.staticType); |
| 987 } | 966 } |
| 988 | 967 |
| 989 test_futureOr_downwards2() async { | 968 test_futureOr_downwards2() async { |
| 990 // Test that downwards inference interacts correctly with FutureOr | 969 // Test that downwards inference interacts correctly with FutureOr |
| 991 // parameters when the downwards context is FutureOr | 970 // parameters when the downwards context is FutureOr |
| 992 String code = r''' | 971 MethodInvocation invoke = await _testFutureOr(r''' |
| 993 import "dart:async"; | |
| 994 Future<T> mk<T>(FutureOr<T> x) => null; | 972 Future<T> mk<T>(FutureOr<T> x) => null; |
| 995 FutureOr<int> test() => mk(new Future<int>.value(42)); | 973 FutureOr<int> test() => mk(new Future<int>.value(42)); |
| 996 '''; | 974 '''); |
| 997 Source source = addSource(code); | |
| 998 TestAnalysisResult analysisResult = await computeAnalysisResult(source); | |
| 999 assertNoErrors(source); | |
| 1000 verify([source]); | |
| 1001 CompilationUnit unit = analysisResult.unit; | |
| 1002 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); | |
| 1003 ExpressionFunctionBody body = test.functionExpression.body; | |
| 1004 MethodInvocation invoke = body.expression; | |
| 1005 _isFutureOfInt(invoke.staticType); | 975 _isFutureOfInt(invoke.staticType); |
| 1006 } | 976 } |
| 1007 | 977 |
| 1008 test_futureOr_downwards3() async { | 978 test_futureOr_downwards3() async { |
| 1009 // Test that downwards inference correctly propogates into | 979 // Test that downwards inference correctly propogates into |
| 1010 // arguments. | 980 // arguments. |
| 1011 String code = r''' | 981 MethodInvocation invoke = await _testFutureOr(r''' |
| 1012 import "dart:async"; | |
| 1013 Future<T> mk<T>(FutureOr<T> x) => null; | 982 Future<T> mk<T>(FutureOr<T> x) => null; |
| 1014 Future<int> test() => mk(new Future.value(42)); | 983 Future<int> test() => mk(new Future.value(42)); |
| 1015 '''; | 984 '''); |
| 1016 Source source = addSource(code); | |
| 1017 TestAnalysisResult analysisResult = await computeAnalysisResult(source); | |
| 1018 assertNoErrors(source); | |
| 1019 verify([source]); | |
| 1020 CompilationUnit unit = analysisResult.unit; | |
| 1021 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); | |
| 1022 ExpressionFunctionBody body = test.functionExpression.body; | |
| 1023 MethodInvocation invoke = body.expression; | |
| 1024 _isFutureOfInt(invoke.staticType); | 985 _isFutureOfInt(invoke.staticType); |
| 1025 _isFutureOfInt(invoke.argumentList.arguments[0].staticType); | 986 _isFutureOfInt(invoke.argumentList.arguments[0].staticType); |
| 1026 } | 987 } |
| 1027 | 988 |
| 1028 test_futureOr_downwards4() async { | 989 test_futureOr_downwards4() async { |
| 1029 // Test that downwards inference interacts correctly with FutureOr | 990 // Test that downwards inference interacts correctly with FutureOr |
| 1030 // parameters when the downwards context is FutureOr | 991 // parameters when the downwards context is FutureOr |
| 1031 String code = r''' | 992 MethodInvocation invoke = await _testFutureOr(r''' |
| 1032 import "dart:async"; | |
| 1033 Future<T> mk<T>(FutureOr<T> x) => null; | 993 Future<T> mk<T>(FutureOr<T> x) => null; |
| 1034 FutureOr<int> test() => mk(new Future.value(42)); | 994 FutureOr<int> test() => mk(new Future.value(42)); |
| 1035 '''; | 995 '''); |
| 1036 Source source = addSource(code); | |
| 1037 TestAnalysisResult analysisResult = await computeAnalysisResult(source); | |
| 1038 assertNoErrors(source); | |
| 1039 verify([source]); | |
| 1040 CompilationUnit unit = analysisResult.unit; | |
| 1041 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); | |
| 1042 ExpressionFunctionBody body = test.functionExpression.body; | |
| 1043 MethodInvocation invoke = body.expression; | |
| 1044 _isFutureOfInt(invoke.staticType); | 996 _isFutureOfInt(invoke.staticType); |
| 1045 _isFutureOfInt(invoke.argumentList.arguments[0].staticType); | 997 _isFutureOfInt(invoke.argumentList.arguments[0].staticType); |
| 1046 } | 998 } |
| 1047 | 999 |
| 1048 test_futureOr_downwards5() async { | 1000 test_futureOr_downwards5() async { |
| 1049 // Test that downwards inference correctly pins the type when it | 1001 // Test that downwards inference correctly pins the type when it |
| 1050 // comes from a FutureOr | 1002 // comes from a FutureOr |
| 1051 String code = r''' | 1003 MethodInvocation invoke = await _testFutureOr(r''' |
| 1052 import "dart:async"; | |
| 1053 Future<T> mk<T>(FutureOr<T> x) => null; | 1004 Future<T> mk<T>(FutureOr<T> x) => null; |
| 1054 FutureOr<num> test() => mk(new Future.value(42)); | 1005 FutureOr<num> test() => mk(new Future.value(42)); |
| 1055 '''; | 1006 '''); |
| 1056 Source source = addSource(code); | |
| 1057 TestAnalysisResult analysisResult = await computeAnalysisResult(source); | |
| 1058 assertNoErrors(source); | |
| 1059 verify([source]); | |
| 1060 CompilationUnit unit = analysisResult.unit; | |
| 1061 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); | |
| 1062 ExpressionFunctionBody body = test.functionExpression.body; | |
| 1063 MethodInvocation invoke = body.expression; | |
| 1064 _isFutureOf([_isNum])(invoke.staticType); | 1007 _isFutureOf([_isNum])(invoke.staticType); |
| 1065 _isFutureOf([_isNum])(invoke.argumentList.arguments[0].staticType); | 1008 _isFutureOf([_isNum])(invoke.argumentList.arguments[0].staticType); |
| 1066 } | 1009 } |
| 1067 | 1010 |
| 1068 test_futureOr_downwards6() async { | 1011 test_futureOr_downwards6() async { |
| 1069 // Test that downwards inference doesn't decompose FutureOr | 1012 // Test that downwards inference doesn't decompose FutureOr |
| 1070 // when instantiating type variables. | 1013 // when instantiating type variables. |
| 1071 String code = r''' | 1014 MethodInvocation invoke = await _testFutureOr(r''' |
| 1072 import "dart:async"; | |
| 1073 T mk<T>(T x) => null; | 1015 T mk<T>(T x) => null; |
| 1074 FutureOr<int> test() => mk(new Future.value(42)); | 1016 FutureOr<int> test() => mk(new Future.value(42)); |
| 1075 '''; | 1017 '''); |
| 1076 Source source = addSource(code); | |
| 1077 TestAnalysisResult analysisResult = await computeAnalysisResult(source); | |
| 1078 assertNoErrors(source); | |
| 1079 verify([source]); | |
| 1080 CompilationUnit unit = analysisResult.unit; | |
| 1081 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); | |
| 1082 ExpressionFunctionBody body = test.functionExpression.body; | |
| 1083 MethodInvocation invoke = body.expression; | |
| 1084 _isFutureOfInt(invoke.staticType); | 1018 _isFutureOfInt(invoke.staticType); |
| 1085 _isFutureOfInt(invoke.argumentList.arguments[0].staticType); | 1019 _isFutureOfInt(invoke.argumentList.arguments[0].staticType); |
| 1086 } | 1020 } |
| 1087 | 1021 |
| 1088 test_futureOr_downwards9() async { | 1022 test_futureOr_downwards9() async { |
| 1089 // Test that downwards inference decomposes correctly with | 1023 // Test that downwards inference decomposes correctly with |
| 1090 // other composite types | 1024 // other composite types |
| 1091 String code = r''' | 1025 MethodInvocation invoke = await _testFutureOr(r''' |
| 1092 import "dart:async"; | |
| 1093 List<T> mk<T>(T x) => null; | 1026 List<T> mk<T>(T x) => null; |
| 1094 FutureOr<List<int>> test() => mk(3); | 1027 FutureOr<List<int>> test() => mk(3); |
| 1095 '''; | 1028 '''); |
| 1096 Source source = addSource(code); | |
| 1097 TestAnalysisResult analysisResult = await computeAnalysisResult(source); | |
| 1098 assertNoErrors(source); | |
| 1099 verify([source]); | |
| 1100 CompilationUnit unit = analysisResult.unit; | |
| 1101 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); | |
| 1102 ExpressionFunctionBody body = test.functionExpression.body; | |
| 1103 MethodInvocation invoke = body.expression; | |
| 1104 _isListOf(_isInt)(invoke.staticType); | 1029 _isListOf(_isInt)(invoke.staticType); |
| 1105 _isInt(invoke.argumentList.arguments[0].staticType); | 1030 _isInt(invoke.argumentList.arguments[0].staticType); |
| 1106 } | 1031 } |
| 1107 | 1032 |
| 1108 test_futureOr_methods1() async { | 1033 test_futureOr_methods1() async { |
| 1109 // Test that FutureOr has the Object methods | 1034 // Test that FutureOr has the Object methods |
| 1110 String code = r''' | 1035 MethodInvocation invoke = await _testFutureOr(r''' |
| 1111 import "dart:async"; | |
| 1112 dynamic test(FutureOr<int> x) => x.toString(); | 1036 dynamic test(FutureOr<int> x) => x.toString(); |
| 1113 '''; | 1037 '''); |
| 1114 Source source = addSource(code); | |
| 1115 TestAnalysisResult analysisResult = await computeAnalysisResult(source); | |
| 1116 assertNoErrors(source); | |
| 1117 verify([source]); | |
| 1118 CompilationUnit unit = analysisResult.unit; | |
| 1119 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); | |
| 1120 ExpressionFunctionBody body = test.functionExpression.body; | |
| 1121 MethodInvocation invoke = body.expression; | |
| 1122 _isString(invoke.staticType); | 1038 _isString(invoke.staticType); |
| 1123 } | 1039 } |
| 1124 | 1040 |
| 1125 test_futureOr_methods2() async { | 1041 test_futureOr_methods2() async { |
| 1126 // Test that FutureOr does not have the constituent type methods | 1042 // Test that FutureOr does not have the constituent type methods |
| 1127 String code = r''' | 1043 MethodInvocation invoke = await _testFutureOr( |
| 1128 import "dart:async"; | 1044 r''' |
| 1129 dynamic test(FutureOr<int> x) => x.abs(); | 1045 dynamic test(FutureOr<int> x) => x.abs(); |
| 1130 '''; | 1046 ''', |
| 1131 Source source = addSource(code); | 1047 errors: [StaticTypeWarningCode.UNDEFINED_METHOD]); |
| 1132 TestAnalysisResult analysisResult = await computeAnalysisResult(source); | |
| 1133 assertErrors(source, [StaticTypeWarningCode.UNDEFINED_METHOD]); | |
| 1134 verify([source]); | |
| 1135 CompilationUnit unit = analysisResult.unit; | |
| 1136 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); | |
| 1137 ExpressionFunctionBody body = test.functionExpression.body; | |
| 1138 MethodInvocation invoke = body.expression; | |
| 1139 _isDynamic(invoke.staticType); | 1048 _isDynamic(invoke.staticType); |
| 1140 } | 1049 } |
| 1141 | 1050 |
| 1142 test_futureOr_methods3() async { | 1051 test_futureOr_methods3() async { |
| 1143 // Test that FutureOr does not have the Future type methods | 1052 // Test that FutureOr does not have the Future type methods |
| 1144 String code = r''' | 1053 MethodInvocation invoke = await _testFutureOr( |
| 1145 import "dart:async"; | 1054 r''' |
| 1146 dynamic test(FutureOr<int> x) => x.then((x) => x); | 1055 dynamic test(FutureOr<int> x) => x.then((x) => x); |
| 1147 '''; | 1056 ''', |
| 1148 Source source = addSource(code); | 1057 errors: [StaticTypeWarningCode.UNDEFINED_METHOD]); |
| 1149 TestAnalysisResult analysisResult = await computeAnalysisResult(source); | |
| 1150 assertErrors(source, [StaticTypeWarningCode.UNDEFINED_METHOD]); | |
| 1151 verify([source]); | |
| 1152 CompilationUnit unit = analysisResult.unit; | |
| 1153 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); | |
| 1154 ExpressionFunctionBody body = test.functionExpression.body; | |
| 1155 MethodInvocation invoke = body.expression; | |
| 1156 _isDynamic(invoke.staticType); | 1058 _isDynamic(invoke.staticType); |
| 1157 } | 1059 } |
| 1158 | 1060 |
| 1159 test_futureOr_methods4() async { | 1061 test_futureOr_methods4() async { |
| 1160 // Test that FutureOr<dynamic> does not have all methods | 1062 // Test that FutureOr<dynamic> does not have all methods |
| 1161 String code = r''' | 1063 MethodInvocation invoke = await _testFutureOr( |
| 1162 import "dart:async"; | 1064 r''' |
| 1163 dynamic test(FutureOr<dynamic> x) => x.abs(); | 1065 dynamic test(FutureOr<dynamic> x) => x.abs(); |
| 1164 '''; | 1066 ''', |
| 1165 Source source = addSource(code); | 1067 errors: [StaticTypeWarningCode.UNDEFINED_METHOD]); |
| 1166 TestAnalysisResult analysisResult = await computeAnalysisResult(source); | |
| 1167 assertErrors(source, [StaticTypeWarningCode.UNDEFINED_METHOD]); | |
| 1168 verify([source]); | |
| 1169 CompilationUnit unit = analysisResult.unit; | |
| 1170 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); | |
| 1171 ExpressionFunctionBody body = test.functionExpression.body; | |
| 1172 MethodInvocation invoke = body.expression; | |
| 1173 _isDynamic(invoke.staticType); | 1068 _isDynamic(invoke.staticType); |
| 1174 } | 1069 } |
| 1175 | 1070 |
| 1176 test_futureOr_upwards1() async { | 1071 test_futureOr_upwards1() async { |
| 1177 // Test that upwards inference correctly prefers to instantiate type | 1072 // Test that upwards inference correctly prefers to instantiate type |
| 1178 // variables with the "smaller" solution when both are possible. | 1073 // variables with the "smaller" solution when both are possible. |
| 1179 String code = r''' | 1074 MethodInvocation invoke = await _testFutureOr(r''' |
| 1180 import "dart:async"; | |
| 1181 Future<T> mk<T>(FutureOr<T> x) => null; | 1075 Future<T> mk<T>(FutureOr<T> x) => null; |
| 1182 dynamic test() => mk(new Future<int>.value(42)); | 1076 dynamic test() => mk(new Future<int>.value(42)); |
| 1183 '''; | 1077 '''); |
| 1184 Source source = addSource(code); | |
| 1185 TestAnalysisResult analysisResult = await computeAnalysisResult(source); | |
| 1186 assertNoErrors(source); | |
| 1187 verify([source]); | |
| 1188 CompilationUnit unit = analysisResult.unit; | |
| 1189 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); | |
| 1190 ExpressionFunctionBody body = test.functionExpression.body; | |
| 1191 MethodInvocation invoke = body.expression; | |
| 1192 _isFutureOfInt(invoke.staticType); | 1078 _isFutureOfInt(invoke.staticType); |
| 1193 } | 1079 } |
| 1194 | 1080 |
| 1195 test_futureOr_upwards2() async { | 1081 test_futureOr_upwards2() async { |
| 1196 // Test that upwards inference fails when the solution doesn't | 1082 // Test that upwards inference fails when the solution doesn't |
| 1197 // match the bound. | 1083 // match the bound. |
| 1198 String code = r''' | 1084 MethodInvocation invoke = await _testFutureOr( |
| 1199 import "dart:async"; | 1085 r''' |
| 1200 Future<T> mk<T extends Future<Object>>(FutureOr<T> x) => null; | 1086 Future<T> mk<T extends Future<Object>>(FutureOr<T> x) => null; |
| 1201 dynamic test() => mk(new Future<int>.value(42)); | 1087 dynamic test() => mk(new Future<int>.value(42)); |
| 1202 '''; | 1088 ''', |
| 1203 Source source = addSource(code); | 1089 errors: [StrongModeCode.COULD_NOT_INFER]); |
| 1204 TestAnalysisResult analysisResult = await computeAnalysisResult(source); | |
| 1205 assertErrors(source, [StrongModeCode.COULD_NOT_INFER]); | |
| 1206 verify([source]); | |
| 1207 CompilationUnit unit = analysisResult.unit; | |
| 1208 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); | |
| 1209 ExpressionFunctionBody body = test.functionExpression.body; | |
| 1210 MethodInvocation invoke = body.expression; | |
| 1211 _isFutureOf([_isObject])(invoke.staticType); | 1090 _isFutureOf([_isObject])(invoke.staticType); |
| 1212 } | 1091 } |
| 1213 | 1092 |
| 1093 test_futureOr_assignFromValue() async { |
| 1094 // Test a T can be assigned to FutureOr<T>. |
| 1095 MethodInvocation invoke = await _testFutureOr(r''' |
| 1096 FutureOr<T> mk<T>(T x) => x; |
| 1097 test() => mk(42); |
| 1098 '''); |
| 1099 _isFutureOrOfInt(invoke.staticType); |
| 1100 } |
| 1101 |
| 1102 test_futureOr_assignFromFuture() async { |
| 1103 // Test a Future<T> can be assigned to FutureOr<T>. |
| 1104 MethodInvocation invoke = await _testFutureOr(r''' |
| 1105 FutureOr<T> mk<T>(Future<T> x) => x; |
| 1106 test() => mk(new Future<int>.value(42)); |
| 1107 '''); |
| 1108 _isFutureOrOfInt(invoke.staticType); |
| 1109 } |
| 1110 |
| 1111 test_futureOr_await() async { |
| 1112 // Test a FutureOr<T> can be awaited. |
| 1113 MethodInvocation invoke = await _testFutureOr(r''' |
| 1114 Future<T> mk<T>(FutureOr<T> x) async => await x; |
| 1115 test() => mk(42); |
| 1116 '''); |
| 1117 _isFutureOfInt(invoke.staticType); |
| 1118 } |
| 1119 |
| 1120 test_futureOr_asyncExpressionBody() async { |
| 1121 // A FutureOr<T> can be used as the expression body for an async function |
| 1122 MethodInvocation invoke = await _testFutureOr(r''' |
| 1123 Future<T> mk<T>(FutureOr<T> x) async => x; |
| 1124 test() => mk(42); |
| 1125 '''); |
| 1126 _isFutureOfInt(invoke.staticType); |
| 1127 } |
| 1128 |
| 1129 test_futureOr_asyncReturn() async { |
| 1130 // A FutureOr<T> can be used as the return value for an async function |
| 1131 MethodInvocation invoke = await _testFutureOr(r''' |
| 1132 Future<T> mk<T>(FutureOr<T> x) async { return x; } |
| 1133 test() => mk(42); |
| 1134 '''); |
| 1135 _isFutureOfInt(invoke.staticType); |
| 1136 } |
| 1137 |
| 1214 test_inference_hints() async { | 1138 test_inference_hints() async { |
| 1215 Source source = addSource(r''' | 1139 Source source = addSource(r''' |
| 1216 void main () { | 1140 void main () { |
| 1217 var x = 3; | 1141 var x = 3; |
| 1218 List<int> l0 = []; | 1142 List<int> l0 = []; |
| 1219 } | 1143 } |
| 1220 '''); | 1144 '''); |
| 1221 await computeAnalysisResult(source); | 1145 await computeAnalysisResult(source); |
| 1222 assertNoErrors(source); | 1146 assertNoErrors(source); |
| 1223 verify([source]); | 1147 verify([source]); |
| (...skipping 842 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2066 Expression exp = stmt.expression; | 1990 Expression exp = stmt.expression; |
| 2067 typeTest(exp.staticType); | 1991 typeTest(exp.staticType); |
| 2068 } | 1992 } |
| 2069 | 1993 |
| 2070 check("f0", _isListOf(_isDynamic)); | 1994 check("f0", _isListOf(_isDynamic)); |
| 2071 check("f1", _isListOf(_isDynamic)); | 1995 check("f1", _isListOf(_isDynamic)); |
| 2072 | 1996 |
| 2073 check("f2", _isListOf(_isInt)); | 1997 check("f2", _isListOf(_isInt)); |
| 2074 check("f3", _isListOf((DartType type) => _isListOf(_isInt)(type))); | 1998 check("f3", _isListOf((DartType type) => _isListOf(_isInt)(type))); |
| 2075 } | 1999 } |
| 2000 |
| 2001 /// Helper method for testing `FutureOr<T>`. |
| 2002 /// |
| 2003 /// Validates that [code] produces [errors]. It should define a function |
| 2004 /// "test", whose body is an expression that invokes a method. Returns that |
| 2005 /// invocation. |
| 2006 Future<MethodInvocation> _testFutureOr(String code, |
| 2007 {List<ErrorCode> errors}) async { |
| 2008 Source source = addSource(""" |
| 2009 import "dart:async"; |
| 2010 $code"""); |
| 2011 TestAnalysisResult analysisResult = await computeAnalysisResult(source); |
| 2012 |
| 2013 if (errors == null) { |
| 2014 assertNoErrors(source); |
| 2015 } else { |
| 2016 assertErrors(source, errors); |
| 2017 } |
| 2018 verify([source]); |
| 2019 FunctionDeclaration test = |
| 2020 AstFinder.getTopLevelFunction(analysisResult.unit, "test"); |
| 2021 ExpressionFunctionBody body = test.functionExpression.body; |
| 2022 return body.expression; |
| 2023 } |
| 2076 } | 2024 } |
| 2077 | 2025 |
| 2078 /** | 2026 /** |
| 2079 * Strong mode static analyzer end to end tests | 2027 * Strong mode static analyzer end to end tests |
| 2080 */ | 2028 */ |
| 2081 @reflectiveTest | 2029 @reflectiveTest |
| 2082 class StrongModeStaticTypeAnalyzer2Test extends StaticTypeAnalyzer2TestShared { | 2030 class StrongModeStaticTypeAnalyzer2Test extends StaticTypeAnalyzer2TestShared { |
| 2083 void expectStaticInvokeType(String search, String type) { | 2031 void expectStaticInvokeType(String search, String type) { |
| 2084 var invocation = findIdentifier(search).parent as MethodInvocation; | 2032 var invocation = findIdentifier(search).parent as MethodInvocation; |
| 2085 expect(invocation.staticInvokeType.toString(), type); | 2033 expect(invocation.staticInvokeType.toString(), type); |
| (...skipping 1328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3414 var v = x; | 3362 var v = x; |
| 3415 v; // marker | 3363 v; // marker |
| 3416 } | 3364 } |
| 3417 int x = 3; | 3365 int x = 3; |
| 3418 '''; | 3366 '''; |
| 3419 CompilationUnit unit = await resolveSource(code); | 3367 CompilationUnit unit = await resolveSource(code); |
| 3420 assertPropagatedAssignedType(code, unit, typeProvider.intType, null); | 3368 assertPropagatedAssignedType(code, unit, typeProvider.intType, null); |
| 3421 assertTypeOfMarkedExpression(code, unit, typeProvider.intType, null); | 3369 assertTypeOfMarkedExpression(code, unit, typeProvider.intType, null); |
| 3422 } | 3370 } |
| 3423 } | 3371 } |
| OLD | NEW |