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

Unified Diff: test/checker/inferred_type_test.dart

Issue 1011933002: Handle type-inference on fields, consts, and inferable overrides (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 5 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 side-by-side diff with in-line comments
Download patch
« lib/src/checker/resolver.dart ('K') | « lib/src/testing.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/checker/inferred_type_test.dart
diff --git a/test/checker/inferred_type_test.dart b/test/checker/inferred_type_test.dart
index a492a20a047493dee3c518801a355f494f8b7b75..8b3008ce3adced3980828596d2d06c8e3831890f 100644
--- a/test/checker/inferred_type_test.dart
+++ b/test/checker/inferred_type_test.dart
@@ -161,35 +161,6 @@ main() {
}
'''
});
-
- // Allowed with special flag. Note, while the flag is generally not stable,
- // we can use it in this test because it is stable within a library (order
- // matches program order).
- testChecker({
- '/main.dart': '''
- var x = 2;
- var y = x;
-
- test1() {
- x = /*severe:StaticTypeError*/"hi";
- y = /*severe:StaticTypeError*/"hi";
- }
- '''
- }, inferInNonStableOrder: true);
-
- testChecker({
- '/main.dart': '''
- class A {
- static var x = 2;
- static var y = A.x;
- }
-
- test1() {
- A.x = /*severe:StaticTypeError*/"hi";
- A.y = /*severe:StaticTypeError*/"hi";
- }
- '''
- }, inferInNonStableOrder: true);
});
test('not ok to infer from variables in non-cycle libs', () {
@@ -260,15 +231,15 @@ main() {
testChecker({
'/a.dart': '''
import 'main.dart';
- var x = 2;
+ var x = 2; // ok to infer
''',
'/main.dart': '''
import 'a.dart';
- var y = x;
+ var y = x; // not ok to infer yet
test1() {
int t = 3;
- t = /*info:DownCast*/x;
+ t = x;
t = /*info:DownCast*/y;
}
'''
@@ -285,7 +256,7 @@ main() {
test1() {
int t = 3;
- t = /*info:DownCast*/A.x;
+ t = A.x;
t = /*info:DownCast*/A.y;
}
'''
@@ -348,7 +319,27 @@ main() {
}, inferStaticsFromIdentifiers: true);
});
- test('don\'t infer on cycles', () {
+ test('inference uses declared types', () {
+ testChecker({
+ '/main.dart': '''
+ int w = 0;
+ var x = 0;
+
+ var y = w; // y can be inferred because w is typed int.
+ var z = x; // z cannot, because x would be inferred.
+
+ test1() {
+ int a;
+ a = w;
+ a = x;
+ a = y;
+ a = /*info:DownCast*/z;
+ }
+ '''
+ }, inferStaticsFromIdentifiers: true);
+ });
+
+ test('inference in cycles is deterministic', () {
testChecker({
'/a.dart': '''
import 'b.dart';
@@ -372,15 +363,20 @@ main() {
}
''',
'/e.dart': '''
- part "e2.dart";
+ import 'a.dart';
+ part 'e2.dart';
class E {
static final e1 = 1;
- final e2 = 1;
+ static final e2 = F.f1;
+ static final e3 = A.a1;
+ final e4 = 1;
+ final e5 = new F().f2;
+ final e6 = new A().a2;
}
''',
'/f.dart': '''
- part "f2.dart";
+ part 'f2.dart';
''',
'/e2.dart': '''
class F {
@@ -406,21 +402,25 @@ main() {
x = A.a1;
x = new A().a2;
- // inference here or in c.dart is disabled because of the cycle
- x = /*info:DownCast*/C.c1;
- x = /*info:DownCast*/D.d1;
+ // Within a cycle we allow inference when the RHS is well known, but
+ // not when it depends on other fields within the cycle
+ x = C.c1;
+ x = D.d1;
x = /*info:DownCast*/D.d2;
- x = /*info:DownCast*/new C().c2;
- x = /*info:DownCast*/new D().d3;
+ x = new C().c2;
+ x = new D().d3;
x = /*info:DownCast*/new D().d4;
- // inference in e.dart and f.dart is disabled because they contains
- // parts
- x = /*info:DownCast*/E.e1;
- x = /*info:DownCast*/new E().e2;
- x = /*info:DownCast*/F.f1;
- x = /*info:DownCast*/new F().f2;
+ // Similarly if the library contains parts.
+ x = E.e1;
+ x = /*info:DownCast*/E.e2;
+ x = E.e3;
+ x = new E().e4;
+ x = /*info:DownCast*/new E().e5;
+ x = new E().e6;
+ x = F.f1;
+ x = new F().f2;
}
'''
}, inferStaticsFromIdentifiers: true);
@@ -444,6 +444,7 @@ main() {
var g = -3;
var h = new A() + 3;
var i = - new A();
+ var j = null as B;
test1() {
a = /*severe:StaticTypeError*/"hi";
@@ -465,6 +466,9 @@ main() {
h = /*severe:StaticTypeError*/false;
h = new B();
i = false;
+ j = new B();
+ j = /*severe:StaticTypeError*/false;
+ j = /*severe:StaticTypeError*/[];
}
'''
});
@@ -812,8 +816,33 @@ main() {
}, inferFromOverrides: true);
});
- // TODO(sigmund): enable this test, it's currently flaky (see issue #48).
- skip_test('infer types on generic instantiations in library cycle', () {
+ test('infer type regardless of declaration order or cycles', () {
+ testChecker({
+ '/b.dart': '''
+ import 'main.dart';
+
+ class B extends A { }
+ ''',
+ '/main.dart': '''
+ import 'b.dart';
+ class C extends B {
+ get x;
+ }
+ class A {
+ int get x;
+ }
+ foo () {
+ int y = new C().x;
+ String y = /*severe:StaticTypeError*/new C().x;
+ }
+ '''
+ }, inferFromOverrides: true);
+ });
+
+ // Note: this is a regression test for a non-deterministic behavior we used to
+ // have with inference in library cycles. If you see this test flake out,
+ // change `test` to `skip_test` and reopen bug #48.
+ test('infer types on generic instantiations in library cycle', () {
testChecker({
'/a.dart': '''
import 'main.dart';
« lib/src/checker/resolver.dart ('K') | « lib/src/testing.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698