Closed
Description
Top level variable inference in the CFE seems to be going wrong in very non-local ways. For this program:
typedef T F<T>(T b);
class A<T> {
final F<T> f;
A(this.f);
}
int foo(int a) {
return 3;
}
var a = new A(foo);
// LINE 1: uncomment the next line and the definition of a (above) gets an error!
// var b = [a];
void main() {
print(a.runtimeType); // prints A<int>
// LINE 2: uncomment the next line, and the error message describes a as having type A<dynamic>
// String s = a;
// Line 3: uncomment the next line and there is no error message at all
// A<String> s = a;
}
This program correctly prints A<int>
.
- Uncommenting the line following LINE 1 causes the definition of
a
to suddenly become an error indicating thata
the type argument of the constructor is being inferred as dynamic:
leafp-macbookpro:dart leafp$ rundart.sh dart --preview-dart-2 ~/tmp/ddctest.dart
file:///Users/leafp/tmp/ddctest.dart:11:15: Error: A value of type '(dart.core::int) → dart.core::int' can't be assigned to a variable of type '(dynamic) → dynamic'.
Try changing the type of the left hand side, or casting the right hand side to '(dynamic) → dynamic'.
var a = new A(foo);
^
- Uncommenting the line following LINE 2 causes an error message that implies that
a
is being inferred as having typeA<dynamic>
.
file:///Users/leafp/tmp/ddctest.dart:17:14: Error: A value of type '#libnull::A<dynamic>' can't be assigned to a variable of type 'dart.core::String'.
Try changing the type of the left hand side, or casting the right hand side to 'dart.core::String'.
String s = a;
^
- Uncommenting the line following LINE 3 results in no error message (implying that
a
is incorrectly inferred asA<dynamic>
), but the runtime cast failure indicates that the reified type isA<int>
.
leafp-macbookpro:dart leafp$ rundart.sh dart --preview-dart-2 ~/tmp/ddctest.dart
A<int>
Unhandled exception:
type 'A<int>' is not a subtype of type 'A<String>' where
This showed up as an unexpected failure when testing dart2js with preview-dart-2 on internal code.