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

Unified Diff: pkg/analyzer/test/src/task/strong/inferred_type_test.dart

Issue 2176693003: Issue a strong mode warning for type inference that is unsafe with AST-based summaries. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Address code review comments. Created 4 years, 5 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/analyzer/test/src/summary/resynthesize_ast_test.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analyzer/test/src/task/strong/inferred_type_test.dart
diff --git a/pkg/analyzer/test/src/task/strong/inferred_type_test.dart b/pkg/analyzer/test/src/task/strong/inferred_type_test.dart
index 9738a9b2cb6d2a622bae5cb22a3dc490db08e303..0f43df2e4d94e34be52cb00c72ed12494fa8aba0 100644
--- a/pkg/analyzer/test/src/task/strong/inferred_type_test.dart
+++ b/pkg/analyzer/test/src/task/strong/inferred_type_test.dart
@@ -62,7 +62,7 @@ main() {
var mainUnit = checkFile(r'''
import 'dart:async';
import 'dart:math' show Random;
-var f = /*info:INFERRED_TYPE_CLOSURE*/() async {
+var f = /*info:INFERRED_TYPE_CLOSURE,warning:UNSAFE_BLOCK_CLOSURE_INFERENCE*/() async {
if (new Random().nextBool()) {
return new Future<int>.value(1);
} else {
@@ -101,7 +101,7 @@ main() {
var mainUnit = checkFile(r'''
import 'dart:async';
import 'dart:math' show Random;
-var f = /*info:INFERRED_TYPE_CLOSURE*/() async {
+var f = /*info:INFERRED_TYPE_CLOSURE,warning:UNSAFE_BLOCK_CLOSURE_INFERENCE*/() async {
if (new Random().nextBool()) {
return 1;
} else {
@@ -140,7 +140,7 @@ main() {
var mainUnit = checkFile(r'''
import 'dart:async';
import 'dart:math' show Random;
-var f = /*info:INFERRED_TYPE_CLOSURE*/() async {
+var f = /*info:INFERRED_TYPE_CLOSURE,warning:UNSAFE_BLOCK_CLOSURE_INFERENCE*/() async {
if (new Random().nextBool()) {
return new Future<int>.value(1);
} else {
@@ -175,7 +175,7 @@ main() {
void test_blockBodiedLambdas_asyncStar_topLevel() {
var mainUnit = checkFile(r'''
import 'dart:async';
-var f = /*info:INFERRED_TYPE_CLOSURE*/() async* {
+var f = /*info:INFERRED_TYPE_CLOSURE,warning:UNSAFE_BLOCK_CLOSURE_INFERENCE*/() async* {
yield 1;
Stream<double> s;
yield* s;
@@ -198,7 +198,7 @@ test1() {
void test_blockBodiedLambdas_basic_topLevel() {
checkFile(r'''
List<int> o;
-var y = o.map(/*info:INFERRED_TYPE_CLOSURE*/(x) { return x + 1; });
+var y = o.map(/*info:INFERRED_TYPE_CLOSURE,warning:UNSAFE_BLOCK_CLOSURE_INFERENCE*/(x) { return x + 1; });
Iterable<int> z = y;
''');
}
@@ -223,7 +223,7 @@ main() async {
void test_blockBodiedLambdas_doesNotInferBottom_async_topLevel() {
var mainUnit = checkFile(r'''
import 'dart:async';
-var f = () async { return null; };
+var f = /*warning:UNSAFE_BLOCK_CLOSURE_INFERENCE*/() async { return null; };
''');
var f = mainUnit.topLevelVariables[0];
expect(f.type.toString(), '() → Future<dynamic>');
@@ -249,7 +249,7 @@ main() async {
void test_blockBodiedLambdas_doesNotInferBottom_asyncStar_topLevel() {
var mainUnit = checkFile(r'''
import 'dart:async';
-var f = () async* { yield null; };
+var f = /*warning:UNSAFE_BLOCK_CLOSURE_INFERENCE*/() async* { yield null; };
''');
var f = mainUnit.topLevelVariables[0];
expect(f.type.toString(), '() → Stream<dynamic>');
@@ -304,7 +304,7 @@ main() {
void test_blockBodiedLambdas_doesNotInferBottom_syncStar_topLevel() {
var mainUnit = checkFile(r'''
-var f = () sync* { yield null; };
+var f = /*warning:UNSAFE_BLOCK_CLOSURE_INFERENCE*/() sync* { yield null; };
''');
var f = mainUnit.topLevelVariables[0];
expect(f.type.toString(), '() → Iterable<dynamic>');
@@ -357,7 +357,7 @@ test2() {
checkFile(r'''
import 'dart:math' show Random;
List<num> o;
-var y = o.map(/*info:INFERRED_TYPE_CLOSURE*/(x) {
+var y = o.map(/*info:INFERRED_TYPE_CLOSURE,warning:UNSAFE_BLOCK_CLOSURE_INFERENCE*/(x) {
if (new Random().nextBool()) {
return x.toInt() + 1;
} else {
@@ -388,7 +388,7 @@ main() {
void test_blockBodiedLambdas_nestedLambdas_topLevel() {
// Original feature request: https://github.com/dart-lang/sdk/issues/25487
var mainUnit = checkFile(r'''
-var f = /*info:INFERRED_TYPE_CLOSURE*/() {
+var f = /*info:INFERRED_TYPE_CLOSURE,warning:UNSAFE_BLOCK_CLOSURE_INFERENCE*/() {
return /*info:INFERRED_TYPE_CLOSURE*/(int x) { return 2.0 * x; };
};
''');
@@ -440,7 +440,7 @@ main() {
void test_blockBodiedLambdas_syncStar_topLevel() {
var mainUnit = checkFile(r'''
-var f = /*info:INFERRED_TYPE_CLOSURE*/() sync* {
+var f = /*info:INFERRED_TYPE_CLOSURE,warning:UNSAFE_BLOCK_CLOSURE_INFERENCE*/() sync* {
yield 1;
yield* /*info:INFERRED_TYPE_LITERAL*/[3, 4.0];
};
@@ -3857,6 +3857,301 @@ class C {
// No type should be inferred for a because there is a circular reference
// between a and c.
}
+
+ void test_unsafeBlockClosureInference_closureCall() {
+ // TODO(paulberry): why is the call to the closure considered a
Leaf 2016/07/25 19:51:29 applying a function with arguments of type dynamic
+ // DYNAMIC_INVOKE?
+ var mainUnit = checkFile('''
+var v = /*info:DYNAMIC_INVOKE*/((x) => 1.0)(
+ /*info:INFERRED_TYPE_CLOSURE*/() { return 1; });
+''');
+ var v = mainUnit.topLevelVariables[0];
+ expect(v.name, 'v');
+ expect(v.type.toString(), 'double');
+ }
+
+ void test_unsafeBlockClosureInference_constructorCall_explicitDynamicParam() {
+ var mainUnit = checkFile('''
+class C<T> {
+ C(T x());
+}
+var v = new C<dynamic>(/*info:INFERRED_TYPE_CLOSURE*/() { return 1; });
+''');
+ var v = mainUnit.topLevelVariables[0];
+ expect(v.name, 'v');
+ expect(v.type.toString(), 'C<dynamic>');
+ }
+
+ void test_unsafeBlockClosureInference_constructorCall_explicitTypeParam() {
+ var mainUnit = checkFile('''
+class C<T> {
+ C(T x());
+}
+var v = new C<int>(/*info:INFERRED_TYPE_CLOSURE*/() { return 1; });
+''');
+ var v = mainUnit.topLevelVariables[0];
+ expect(v.name, 'v');
+ expect(v.type.toString(), 'C<int>');
+ }
+
+ void test_unsafeBlockClosureInference_constructorCall_implicitTypeParam() {
+ var mainUnit = checkFile('''
+class C<T> {
+ C(T x());
+}
+var v = /*info:INFERRED_TYPE_ALLOCATION*/new C(
+ /*info:INFERRED_TYPE_CLOSURE,warning:UNSAFE_BLOCK_CLOSURE_INFERENCE*/() {
+ return 1;
+ });
+''');
+ var v = mainUnit.topLevelVariables[0];
+ expect(v.name, 'v');
+ expect(v.type.toString(), 'C<int>');
+ }
+
+ void test_unsafeBlockClosureInference_constructorCall_noTypeParam() {
+ var mainUnit = checkFile('''
+class C {
+ C(x());
+}
+var v = new C(/*info:INFERRED_TYPE_CLOSURE*/() { return 1; });
+''');
+ var v = mainUnit.topLevelVariables[0];
+ expect(v.name, 'v');
+ expect(v.type.toString(), 'C');
+ }
+
+ void test_unsafeBlockClosureInference_functionCall_explicitDynamicParam() {
+ var mainUnit = checkFile('''
+dynamic /*=List<T>*/ f/*<T>*/(dynamic/*=T*/ g()) => <T>[g()];
+var v = f/*<dynamic>*/(/*info:INFERRED_TYPE_CLOSURE*/() { return 1; });
+''');
+ var v = mainUnit.topLevelVariables[0];
+ expect(v.name, 'v');
+ expect(v.type.toString(), 'List<dynamic>');
+ }
+
+ @failingTest
+ void
+ test_unsafeBlockClosureInference_functionCall_explicitDynamicParam_viaExpr1() {
+ // TODO(paulberry): for some reason (f/*<dynamic>) is nort properly
Leaf 2016/07/25 19:51:29 https://github.com/dart-lang/sdk/issues/25824
+ // resulting in an instantiated function type.
+ var mainUnit = checkFile('''
+dynamic /*=List<T>*/ f/*<T>*/(dynamic/*=T*/ g()) => <T>[g()];
+var v = (f/*<dynamic>*/)(/*info:INFERRED_TYPE_CLOSURE*/() { return 1; });
+''');
+ var v = mainUnit.topLevelVariables[0];
+ expect(v.name, 'v');
+ expect(v.type.toString(), 'List<dynamic>');
+ }
+
+ void
+ test_unsafeBlockClosureInference_functionCall_explicitDynamicParam_viaExpr2() {
+ var mainUnit = checkFile('''
+dynamic /*=List<T>*/ f/*<T>*/(dynamic/*=T*/ g()) => <T>[g()];
+var v = (f)/*<dynamic>*/(/*info:INFERRED_TYPE_CLOSURE*/() { return 1; });
+''');
+ var v = mainUnit.topLevelVariables[0];
+ expect(v.name, 'v');
+ expect(v.type.toString(), 'List<dynamic>');
+ }
+
+ void test_unsafeBlockClosureInference_functionCall_explicitTypeParam() {
+ var mainUnit = checkFile('''
+dynamic /*=List<T>*/ f/*<T>*/(dynamic/*=T*/ g()) => <T>[g()];
+var v = f/*<int>*/(/*info:INFERRED_TYPE_CLOSURE*/() { return 1; });
+''');
+ var v = mainUnit.topLevelVariables[0];
+ expect(v.name, 'v');
+ expect(v.type.toString(), 'List<int>');
+ }
+
+ @failingTest
+ void
+ test_unsafeBlockClosureInference_functionCall_explicitTypeParam_viaExpr1() {
+ // TODO(paulberry): for some reason (f/*<int>) is nort properly resulting
+ // in an instantiated function type.
+ var mainUnit = checkFile('''
+dynamic /*=List<T>*/ f/*<T>*/(dynamic/*=T*/ g()) => <T>[g()];
+var v = (f/*<int>*/)(/*info:INFERRED_TYPE_CLOSURE*/() { return 1; });
+''');
+ var v = mainUnit.topLevelVariables[0];
+ expect(v.name, 'v');
+ expect(v.type.toString(), 'List<int>');
+ }
+
+ void
+ test_unsafeBlockClosureInference_functionCall_explicitTypeParam_viaExpr2() {
+ var mainUnit = checkFile('''
+dynamic /*=List<T>*/ f/*<T>*/(dynamic/*=T*/ g()) => <T>[g()];
+var v = (f)/*<int>*/(/*info:INFERRED_TYPE_CLOSURE*/() { return 1; });
+''');
+ var v = mainUnit.topLevelVariables[0];
+ expect(v.name, 'v');
+ expect(v.type.toString(), 'List<int>');
+ }
+
+ void test_unsafeBlockClosureInference_functionCall_implicitTypeParam() {
+ var mainUnit = checkFile('''
+dynamic /*=List<T>*/ f/*<T>*/(dynamic/*=T*/ g()) => <T>[g()];
+var v = f(
+ /*info:INFERRED_TYPE_CLOSURE,warning:UNSAFE_BLOCK_CLOSURE_INFERENCE*/() {
+ return 1;
+ });
+''');
+ var v = mainUnit.topLevelVariables[0];
+ expect(v.name, 'v');
+ expect(v.type.toString(), 'List<int>');
+ }
+
+ void
+ test_unsafeBlockClosureInference_functionCall_implicitTypeParam_viaExpr() {
+ var mainUnit = checkFile('''
+dynamic /*=List<T>*/ f/*<T>*/(dynamic/*=T*/ g()) => <T>[g()];
+var v = (f)(
+ /*info:INFERRED_TYPE_CLOSURE,warning:UNSAFE_BLOCK_CLOSURE_INFERENCE*/() {
+ return 1;
+ });
+''');
+ var v = mainUnit.topLevelVariables[0];
+ expect(v.name, 'v');
+ expect(v.type.toString(), 'List<int>');
+ }
+
+ void test_unsafeBlockClosureInference_functionCall_noTypeParam() {
+ var mainUnit = checkFile('''
+double f(x) => 1.0;
+var v = f(/*info:INFERRED_TYPE_CLOSURE*/() { return 1; });
+''');
+ var v = mainUnit.topLevelVariables[0];
+ expect(v.name, 'v');
+ expect(v.type.toString(), 'double');
+ }
+
+ void test_unsafeBlockClosureInference_functionCall_noTypeParam_viaExpr() {
+ // TODO(paulberry): why is the call to f() considered a DYNAMIC_INVOKE?
+ var mainUnit = checkFile('''
+double f(x) => 1.0;
+var v = /*info:DYNAMIC_INVOKE*/(f)(
+ /*info:INFERRED_TYPE_CLOSURE*/() { return 1; });
+''');
+ var v = mainUnit.topLevelVariables[0];
+ expect(v.name, 'v');
+ expect(v.type.toString(), 'double');
+ }
+
+ void test_unsafeBlockClosureInference_inList_dynamic() {
+ var mainUnit = checkFile('''
+var v = <dynamic>[/*info:INFERRED_TYPE_CLOSURE*/() { return 1; }];
+''');
+ var v = mainUnit.topLevelVariables[0];
+ expect(v.name, 'v');
+ expect(v.type.toString(), 'List<dynamic>');
+ }
+
+ void test_unsafeBlockClosureInference_inList_typed() {
+ var mainUnit = checkFile('''
+typedef int F();
+var v = <F>[/*info:INFERRED_TYPE_CLOSURE*/() { return 1; }];
+''');
+ var v = mainUnit.topLevelVariables[0];
+ expect(v.name, 'v');
+ expect(v.type.toString(), 'List<() → int>');
+ }
+
+ void test_unsafeBlockClosureInference_inList_untyped() {
+ var mainUnit = checkFile('''
+var v = /*info:INFERRED_TYPE_LITERAL*/[
+ /*info:INFERRED_TYPE_CLOSURE,warning:UNSAFE_BLOCK_CLOSURE_INFERENCE*/() {
+ return 1;
+ }];
+''');
+ var v = mainUnit.topLevelVariables[0];
+ expect(v.name, 'v');
+ expect(v.type.toString(), 'List<() → int>');
+ }
+
+ void test_unsafeBlockClosureInference_inMap_dynamic() {
+ var mainUnit = checkFile('''
+var v = <int, dynamic>{1: /*info:INFERRED_TYPE_CLOSURE*/() { return 1; }};
+''');
+ var v = mainUnit.topLevelVariables[0];
+ expect(v.name, 'v');
+ expect(v.type.toString(), 'Map<int, dynamic>');
+ }
+
+ void test_unsafeBlockClosureInference_inMap_typed() {
+ var mainUnit = checkFile('''
+typedef int F();
+var v = <int, F>{1: /*info:INFERRED_TYPE_CLOSURE*/() { return 1; }};
+''');
+ var v = mainUnit.topLevelVariables[0];
+ expect(v.name, 'v');
+ expect(v.type.toString(), 'Map<int, () → int>');
+ }
+
+ void test_unsafeBlockClosureInference_inMap_untyped() {
+ var mainUnit = checkFile('''
+var v = /*info:INFERRED_TYPE_LITERAL*/{
+ 1: /*info:INFERRED_TYPE_CLOSURE,warning:UNSAFE_BLOCK_CLOSURE_INFERENCE*/() {
+ return 1;
+ }};
+''');
+ var v = mainUnit.topLevelVariables[0];
+ expect(v.name, 'v');
+ expect(v.type.toString(), 'Map<int, () → int>');
+ }
+
+ void test_unsafeBlockClosureInference_methodCall_explicitDynamicParam() {
+ var mainUnit = checkFile('''
+class C {
+ dynamic /*=List<T>*/ f/*<T>*/(dynamic/*=T*/ g()) => <T>[g()];
+}
+var v = new C().f/*<dynamic>*/(/*info:INFERRED_TYPE_CLOSURE*/() { return 1; });
+''');
+ var v = mainUnit.topLevelVariables[0];
+ expect(v.name, 'v');
+ expect(v.type.toString(), 'List<dynamic>');
+ }
+
+ void test_unsafeBlockClosureInference_methodCall_explicitTypeParam() {
+ var mainUnit = checkFile('''
+class C {
+ dynamic /*=List<T>*/ f/*<T>*/(dynamic/*=T*/ g()) => <T>[g()];
+}
+var v = new C().f/*<int>*/(/*info:INFERRED_TYPE_CLOSURE*/() { return 1; });
+''');
+ var v = mainUnit.topLevelVariables[0];
+ expect(v.name, 'v');
+ expect(v.type.toString(), 'List<int>');
+ }
+
+ void test_unsafeBlockClosureInference_methodCall_implicitTypeParam() {
+ var mainUnit = checkFile('''
+class C {
+ dynamic /*=List<T>*/ f/*<T>*/(dynamic/*=T*/ g()) => <T>[g()];
+}
+var v = new C().f(
+ /*info:INFERRED_TYPE_CLOSURE,warning:UNSAFE_BLOCK_CLOSURE_INFERENCE*/() {
+ return 1;
+ });
+''');
+ var v = mainUnit.topLevelVariables[0];
+ expect(v.name, 'v');
+ expect(v.type.toString(), 'List<int>');
+ }
+
+ void test_unsafeBlockClosureInference_methodCall_noTypeParam() {
+ var mainUnit = checkFile('''
+class C {
+ double f(x) => 1.0;
+}
+var v = new C().f(/*info:INFERRED_TYPE_CLOSURE*/() { return 1; });
+''');
+ var v = mainUnit.topLevelVariables[0];
+ expect(v.name, 'v');
+ expect(v.type.toString(), 'double');
+ }
}
@reflectiveTest
« no previous file with comments | « pkg/analyzer/test/src/summary/resynthesize_ast_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698