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

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

Issue 2755283002: fix `?` incorrectly being used in an implicit instantiation (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 1092 matching lines...) Expand 10 before | Expand all | Expand 10 after
1103 void main () { 1103 void main () {
1104 var x = 3; 1104 var x = 3;
1105 List<int> l0 = []; 1105 List<int> l0 = [];
1106 } 1106 }
1107 '''); 1107 ''');
1108 await computeAnalysisResult(source); 1108 await computeAnalysisResult(source);
1109 assertNoErrors(source); 1109 assertNoErrors(source);
1110 verify([source]); 1110 verify([source]);
1111 } 1111 }
1112 1112
1113 test_inferGenericInstantiation() async {
1114 // Verify that we don't infer '?` when we instantiate a generic function.
1115 var source = addSource(r'''
1116 T f<T>(T x(T t)) => x(null);
1117 S g<S>(S s) => s;
1118 test() {
1119 var h = f(g);
1120 }
1121 ''');
1122 var analysisResult = await computeAnalysisResult(source);
1123 assertNoErrors(source);
1124 verify([source]);
1125 var unit = analysisResult.unit;
1126 var h = (AstFinder.getStatementsInTopLevelFunction(unit, "test")[0]
1127 as VariableDeclarationStatement)
1128 .variables
1129 .variables[0];
1130 _isDynamic(h.element.type);
1131 var fCall = h.initializer as MethodInvocation;
1132 expect(
1133 fCall.staticInvokeType.toString(), '((dynamic) → dynamic) → dynamic');
1134 var g = fCall.argumentList.arguments[0];
1135 expect(g.staticType.toString(), '(dynamic) → dynamic');
1136 }
1137
1138 test_inferGenericInstantiation2() async {
1139 // Verify the behavior when we cannot infer an instantiation due to invalid
1140 // constraints from an outer generic method.
1141 var source = addSource(r'''
1142 T max<T extends num>(T x, T y) => x < y ? y : x;
1143 abstract class Iterable<T> {
1144 T get first;
1145 S fold<S>(S s, S f(S s, T t));
1146 }
1147 num test(Iterable values) => values.fold(values.first as num, max);
1148 ''');
1149 var analysisResult = await computeAnalysisResult(source);
1150 assertErrors(source, [
1151 StrongModeCode.COULD_NOT_INFER,
1152 StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE
1153 ]);
1154 verify([source]);
1155 var unit = analysisResult.unit;
1156 var fold = (AstFinder
1157 .getTopLevelFunction(unit, 'test')
1158 .functionExpression
1159 .body as ExpressionFunctionBody)
1160 .expression as MethodInvocation;
1161 expect(
1162 fold.staticInvokeType.toString(), '(num, (num, dynamic) → num) → num');
1163 var max = fold.argumentList.arguments[1];
1164 // TODO(jmesserly): arguably (num, num) → num is better here.
1165 expect(max.staticType.toString(), '(dynamic, dynamic) → dynamic');
1166 }
1167
1113 test_inferredFieldDeclaration_propagation() async { 1168 test_inferredFieldDeclaration_propagation() async {
1114 // Regression test for https://github.com/dart-lang/sdk/issues/25546 1169 // Regression test for https://github.com/dart-lang/sdk/issues/25546
1115 String code = r''' 1170 String code = r'''
1116 abstract class A { 1171 abstract class A {
1117 Map<int, List<int>> get map; 1172 Map<int, List<int>> get map;
1118 } 1173 }
1119 class B extends A { 1174 class B extends A {
1120 var map = { 42: [] }; 1175 var map = { 42: [] };
1121 } 1176 }
1122 class C extends A { 1177 class C extends A {
(...skipping 531 matching lines...) Expand 10 before | Expand all | Expand 10 after
1654 String code = r''' 1709 String code = r'''
1655 class A<S, T> { 1710 class A<S, T> {
1656 S s; 1711 S s;
1657 T t; 1712 T t;
1658 } 1713 }
1659 class B<S> extends A<S, S> { B(S s); } 1714 class B<S> extends A<S, S> { B(S s); }
1660 A<int, String> test() => new B(3); 1715 A<int, String> test() => new B(3);
1661 '''; 1716 ''';
1662 Source source = addSource(code); 1717 Source source = addSource(code);
1663 TestAnalysisResult analysisResult = await computeAnalysisResult(source); 1718 TestAnalysisResult analysisResult = await computeAnalysisResult(source);
1664 assertErrors(source,[StrongModeCode.INVALID_CAST_LITERAL]); 1719 assertErrors(source, [StrongModeCode.INVALID_CAST_LITERAL]);
1665 verify([source]); 1720 verify([source]);
1666 CompilationUnit unit = analysisResult.unit; 1721 CompilationUnit unit = analysisResult.unit;
1667 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); 1722 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test");
1668 ExpressionFunctionBody body = test.functionExpression.body; 1723 ExpressionFunctionBody body = test.functionExpression.body;
1669 DartType type = body.expression.staticType; 1724 DartType type = body.expression.staticType;
1670 1725
1671 Element elementB = AstFinder.getClass(unit, "B").element; 1726 Element elementB = AstFinder.getClass(unit, "B").element;
1672 1727
1673 _isInstantiationOf(_hasElement(elementB))([_isNull])(type); 1728 _isInstantiationOf(_hasElement(elementB))([_isNull])(type);
1674 } 1729 }
(...skipping 1940 matching lines...) Expand 10 before | Expand all | Expand 10 after
3615 var v = x; 3670 var v = x;
3616 v; // marker 3671 v; // marker
3617 } 3672 }
3618 int x = 3; 3673 int x = 3;
3619 '''; 3674 ''';
3620 CompilationUnit unit = await resolveSource(code); 3675 CompilationUnit unit = await resolveSource(code);
3621 assertPropagatedAssignedType(code, unit, typeProvider.intType, null); 3676 assertPropagatedAssignedType(code, unit, typeProvider.intType, null);
3622 assertTypeOfMarkedExpression(code, unit, typeProvider.intType, null); 3677 assertTypeOfMarkedExpression(code, unit, typeProvider.intType, null);
3623 } 3678 }
3624 } 3679 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/generated/type_system.dart ('k') | pkg/analyzer/test/src/task/strong/inferred_type_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698