Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use type argument for Future of nullable value in test #681

Merged
merged 1 commit into from
Sep 25, 2023

Conversation

srawlins
Copy link
Member

@srawlins srawlins commented Sep 25, 2023

This is a weird one, so I'm going to try to carefully explain the situation and my steps. Here is the code in question:

void _testSimpleValue(String testName, Object? value, Object? expected) {
  // ...
  _testFunction('Future<$testName>', Future.value(value), expected);
  // -------------------------------------^ inferred type argument
  // ...
}

void _testFunction(String testName, Object? value, Object? expected) { /* ... */ }

This code works today. It appears that the type of Future.value(...) inferred by CFE is Future<Object?>. A Future<Object?> constructed with the value constructor, and an Object? argument is sound. However, in dart-lang/language#3304, it was decided that CFE infers the wrong type here, and should infer Future<Object>.

If and when CFE will change its behavior remains to be seen, but analyzer will start reporting an issue that an Object? argument was passed to a Future.value constructor for an argument type of Object, and that would be invalid. The situation there is that Future.value's single parameter is optional, so it's type is T?, but at runtime a null value will throw an exception, so we can pretend the parameter is both required and non-nullable when T is non-nullable.

So to prepare for a CFE change, and support analyzer's new warning, we can just put an explicit type argument.


  • I’ve reviewed the contributor guide and applied the relevant portions to this PR.
Contribution guidelines:

Note that many Dart repos have a weekly cadence for reviewing PRs - please allow for some latency before initial review feedback.

Copy link
Contributor

@jakemac53 jakemac53 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunate, but Future.value is just a horrible API unfortunately. We should have fixed it in Dart 3.0 😞 .

@srawlins srawlins merged commit c953043 into master Sep 25, 2023
12 checks passed
@srawlins srawlins deleted the fix-for-nullable-future branch September 25, 2023 21:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants