dart-sdk/tests/language/await_type_test.dart
Lasse R.H. Nielsen 36a6b1b5ce Analyzer: Don't do recursive flattening of FutureOr in await.
The current behavior (removing all outer `FutureOr`s,
then calling *flatten* on the result) was never specified as correct behavior.

Since Dart 2.0, the await has not recursively flattened futures,
so the static analysis behavior did not match run-time behavior.


Change-Id: Ie1da323aedb4b0f51e2c1e3eebe84552576c5f37
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/121846
Commit-Queue: Lasse R.H. Nielsen <lrn@google.com>
Reviewed-by: Leaf Petersen <leafp@google.com>
2020-01-28 11:16:07 +00:00

88 lines
2 KiB
Dart

// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "dart:async";
import "package:expect/expect.dart";
import "package:async_helper/async_helper.dart";
void main() async {
asyncStart();
var fi = Future<int>.value(1);
var ffi = Future<Future<int>>.value(fi);
// Sanity check.
Expect.equals(type<int>(), typeOf(1));
{
int v = 1; // // Variable with type T.
var f = await v; // // Variable with type FLATTEN(T).
Expect.equals(type<int>(), typeOf(f)); // Dynamic check of static type of f.
int s = f; // // Static check upper bound of f.
f = 1; // // Static check lower bound of f.
s.toString(); // // Avoid "unused variable" warning.
}
{
Future<int> v = fi;
var f = await v;
Expect.equals(type<int>(), typeOf(f));
int s = f;
f = 1;
s.toString();
}
{
Future<Future<int>> v = ffi;
var f = await v;
Expect.equals(type<Future<int>>(), typeOf(f));
Future<int> s = f;
f = fi;
s.toString();
}
{
FutureOr<int> v = 1;
var f = await v;
Expect.equals(type<int>(), typeOf(f));
int s = f;
f = 1;
s.toString();
}
{
FutureOr<Future<int>> v = fi;
var f = await v;
Expect.equals(type<Future<int>>(), typeOf(f));
Future<int> s = f;
f = fi;
s.toString();
}
{
Future<FutureOr<int>> v = fi;
var f = await v;
Expect.equals(type<FutureOr<int>>(), typeOf(f));
FutureOr<int> s = f;
f = 1;
f = fi;
s.toString();
}
{
FutureOr<FutureOr<int>> v = 1;
var f = await v;
Expect.equals(type<FutureOr<int>>(), typeOf(f));
FutureOr<int> s = f;
f = 1;
f = fi;
s.toString();
}
asyncEnd();
}
Type type<T>() => T;
Type typeOf<T>(T of) => T;