dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.25k stars 1.58k forks source link

A value of type 'int' can't be assigned to a parameter of type 'FutureOr<int>'. #35612

Closed Keksinautin closed 5 years ago

Keksinautin commented 5 years ago

In this code

import 'dart:async';

class A {
  final FutureOr<int> a;

  const A({this.a: 2});
}

void main() async {
  final b = const A(); // Here we have an error;
  ///But in this case it works well
  //final b = new A();

  print(b.a);
}

I have a error: A value of type 'int' can't be assigned to a parameter of type 'FutureOr<int>'. I think this is a very strange result.

https://gist.github.com/Keksinautin/5366adf409b6762440cdf3c60ab5404b

vsmenon commented 5 years ago

I can repro this with the dartanalyzer. If I run the above directly on the dart VM, it works fine.

stereotype441 commented 5 years ago

Marking as P1 since this seems like a pretty fundamental type system problem (possibly affecting soundness). If we investigate and find that it only fails in rare corner cases, we may downgrade to P2.

bwilkerson commented 5 years ago

Makes me wonder whether the code I added to unwrap FutureOr in https://dart-review.googlesource.com/c/sdk/+/88741 needed to be added to the assignability testing methods.

bwilkerson commented 5 years ago

After looking more closely at the original case, it seems more likely to me that the bug is in the constant evaluation engine, which would somewhat limit the impact of the bug.

scheglov commented 5 years ago

I tried to fix this, but hit once more type inference limitation.

test.dart
--------------
import 'test2.dart';

main() {
  const C();
}

test2.dart
--------------
class C {
  final List<int> values;

  const C({this.values: const []});
}

Produces: error: A value of type 'List<dynamic>' can't be assigned to a parameter of type 'List<int>'. (const_constructor_param_type_mismatch at [test] bin/test.dart:4)

srawlins commented 5 years ago

This was fixed somewhere along the way. In dart 2.5.2, there are no warnings in the original example, nor in @scheglov's recent example with List<int>. dart2js and the VM successfully execute as well.

scheglov commented 5 years ago

It was fixed with summary2.