Skip to content

Using a toplevel variable changes inference results in CFE #32866

Closed
@leafpetersen

Description

@leafpetersen

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>.

  1. Uncommenting the line following LINE 1 causes the definition of a to suddenly become an error indicating that a 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);
              ^
  1. Uncommenting the line following LINE 2 causes an error message that implies that a is being inferred as having type A<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;
             ^
  1. Uncommenting the line following LINE 3 results in no error message (implying that a is incorrectly inferred as A<dynamic>), but the runtime cast failure indicates that the reified type is A<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.

cc @srawlins @sigmundch @stereotype441

Metadata

Metadata

Assignees

Labels

P1A high priority bug; for example, a single project is unusable or has many test failurescustomer-dart2jslegacy-area-front-endLegacy: Use area-dart-model instead.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions