dart-lang / dart-lang/language

How do we expect required context types to interact with `await`?

Open
#3,575 17 comments 0 reactions 0 assignees View on GitHub
type-inference
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

In #3471 , there is a proposal to require that all expressions be a subtype of their context type. This issue is to clarify the intended treatment of a specific corner of the language under this proposal.

Consider the following program:

```dart
void main() async {
dynamic x = Future.value(3);
int y = await x;
}
```

This program passes static analysis currently. I believe that the current treatment chooses `FutureOr` for the context type when do inference on `x`. This has no effect, the static type of `x` remains `dynamic`. The static type of `await x` is therefore `dynamic` as well, and there is an implicit downcast to `int` inserted at the assignment to `y`. Semantically, it is as if we had written `int y = (await x) as int`.

This program also runs to completion currently. I believe that the semantic interpretation of `await x` in this case is essentially to check whether `x is Future` and if so to unwrap the value from the `Future` and otherwise to simply return the value. In this case, the instance check returns true, the value is unwrapped, and `await x` evaluates to `3` and the implicit cast succeeds.

My natural interpretation of the proposal in #3471 is that we would change the above as follows.

Statically, we would instead say that since `x` does not satisfy its context type, we should insert a cast to the context type (here `FutureOr` ) inside of the `await`. The result would be that semantically, it is as if we had written `await (x as FutureOr)`. The static type of `await x` would then be `int`, and no implicit downcast outside of the `await x` would be inserted.

Assuming the above, then the runtime behavior of the above program will change. The cast inserted on `x` will fail, and the program above will terminate with a cast failure.

Is my interpretation of the intended semantics above correct? Is this change expected/desired? If so, do we have any data on how breaking it would be?

cc @dart-lang/language-team

Contributor guide

Open the contributing guide

Research direction

Start by reading the proposal in #3471 and the example involving dynamic x, await, FutureOr, and the assignment to y. Trace the stated static and runtime interpretations of await, then review the discussion and existing comments. Done means the intended semantics, compatibility impact, and whether the behavior change is desired are clarified.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.