dart-lang / language

Design of the Dart language
Other
2.65k stars 202 forks source link

Inferencing implicit instance getters #1534

Open MatrixDev opened 3 years ago

MatrixDev commented 3 years ago

Consider following code (which is pretty common in most other languages):

class Example {
  final a = '';
  late final b = a; // b is dynamic
}

Currently Dart throws warning about inferencing variable b type and makes it dynamic: The type of 'b' can't be inferred because it refers to an instance getter, 'a', which has an implicit type.

I think Dart should properly inference type from variable a, especially taking to account that exactly the same code will work properly inside the function body:

void test() {
  final a = '';
  late final b = a; // b is String
}
Cat-sushi commented 3 years ago

See #field-promotion

leafpetersen commented 3 years ago

@scheglov @stereotype441 I think this may be one of the legacy top level inference warnings we discussed? I don't think the current spec requires this, and the CFE does not issue this warning.

MatrixDev commented 3 years ago

@leafpetersen it is not only a warning. actual type of variable is also wrong (aka dynamic).

leafpetersen commented 3 years ago

@scheglov @stereotype441 this looks like an example of a place where the analyzer is not yet implementing the top level inference spec. We should discuss whether this can be addressed, or whether we need to revisit the spec here.