|
| 1 | +// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | +import 'package:expect/expect.dart'; |
| 7 | +import '../static_type_helper.dart'; |
| 8 | + |
| 9 | +// Testing the behavior of flatten (cf. SDK issue #49396). |
| 10 | + |
| 11 | +class A {} |
| 12 | + |
| 13 | +abstract class B<X> extends A implements Future<X> { |
| 14 | + Future<X> get fut; |
| 15 | + asStream() => fut.asStream(); |
| 16 | + catchError(error, {test}) => fut.catchError(error, test: test); |
| 17 | + then<R>(onValue, {onError}) => fut.then(onValue, onError: onError); |
| 18 | + timeout(timeLimit, {onTimeout}) => |
| 19 | + fut.timeout(timeLimit, onTimeout: onTimeout); |
| 20 | + whenComplete(action) => fut.whenComplete(action); |
| 21 | +} |
| 22 | + |
| 23 | +class C extends B<Object?> { |
| 24 | + final Future<Object?> fut = Future.value(CompletelyUnrelated()); |
| 25 | +} |
| 26 | + |
| 27 | +class CompletelyUnrelated {} |
| 28 | + |
| 29 | +class C1 extends A {} |
| 30 | + |
| 31 | +class C2 extends B<C1> { |
| 32 | + final Future<C1> fut = Future.value(C1()); |
| 33 | +} |
| 34 | + |
| 35 | +void main() async { |
| 36 | + final Object o = Future<Object?>.value(); |
| 37 | + var o2 = await o; // Remains a future. |
| 38 | + o2.expectStaticType<Exactly<Object>>(); |
| 39 | + Expect.type<Future<Object?>>(o2); |
| 40 | + Expect.identical(o, o2); |
| 41 | + |
| 42 | + final FutureOr<Object> x = Future<Object?>.value(); |
| 43 | + var x2 = await x; // Remains a future. |
| 44 | + x2.expectStaticType<Exactly<Object>>(); |
| 45 | + Expect.type<Future<Object?>>(x2); |
| 46 | + Expect.identical(x, x2); |
| 47 | + |
| 48 | + final FutureOr<Future<int>> y = Future<int>.value(1); |
| 49 | + var y2 = await y; // Remains a `Future<int>`. |
| 50 | + y2.expectStaticType<Exactly<Future<int>>>(); |
| 51 | + Expect.type<Future<int>>(y2); |
| 52 | + Expect.identical(y, y2); |
| 53 | + |
| 54 | + A a = C(); |
| 55 | + var a2 = await a; // Remains a `C` and a `Future<Object?>`. |
| 56 | + a2.expectStaticType<Exactly<A>>(); |
| 57 | + Expect.type<C>(a2); |
| 58 | + Expect.identical(a, a2); |
| 59 | + |
| 60 | + // Type variable; called with `X == Object`. |
| 61 | + Future<void> f1<X extends Object>(X x) async { |
| 62 | + var x2 = await x; // Remains a `Future<Object?>`. |
| 63 | + x2.expectStaticType<Exactly<X>>(); |
| 64 | + Expect.type<Future<Object?>>(x2); |
| 65 | + Expect.identical(x, x2); |
| 66 | + } |
| 67 | + |
| 68 | + await f1<Object>(Future<Object?>.value(null)); |
| 69 | + |
| 70 | + // Type variable; called with `X == A`. |
| 71 | + Future<void> f2<X extends A>(X x) async { |
| 72 | + var x2 = await x; // The future is awaited. |
| 73 | + x2.expectStaticType<Exactly<X>>(); |
| 74 | + Expect.type<C1>(x2); |
| 75 | + Expect.notType<C2>(x2); |
| 76 | + Expect.notIdentical(x, x2); |
| 77 | + } |
| 78 | + |
| 79 | + await f2<A>(C2()); |
| 80 | + |
| 81 | + // Type variable; called with `X == C2`. |
| 82 | + Future<void> f3<X extends A>(X x) async { |
| 83 | + var x2 = await x; // Remains a `C2` and a `Future<C1>`. |
| 84 | + x2.expectStaticType<Exactly<X>>(); |
| 85 | + Expect.type<C2>(x2); |
| 86 | + Expect.identical(x, x2); |
| 87 | + } |
| 88 | + |
| 89 | + await f3<C2>(C2()); |
| 90 | + |
| 91 | + // Type variable; value of `X` makes no difference. |
| 92 | + Future<void> f4<X extends Future<A>>(X x) async { |
| 93 | + var x2 = await x; // The future is awaited. |
| 94 | + x2.expectStaticType<Exactly<A>>(); |
| 95 | + Expect.type<C1>(x2); |
| 96 | + Expect.notType<C2>(x2); |
| 97 | + Expect.notIdentical(x, x2); |
| 98 | + } |
| 99 | + |
| 100 | + await f4<Future<A>>(C2()); |
| 101 | + await f4<C2>(C2()); |
| 102 | + |
| 103 | + // Type variable; value of `X` makes no difference. |
| 104 | + Future<void> f5<X extends FutureOr<A>>(X x) async { |
| 105 | + var x2 = await x; // The future is awaited. |
| 106 | + x2.expectStaticType<Exactly<A>>(); |
| 107 | + Expect.type<C1>(x2); |
| 108 | + Expect.notType<C2>(x2); |
| 109 | + Expect.notIdentical(x, x2); |
| 110 | + } |
| 111 | + |
| 112 | + await f5<A>(C2()); |
| 113 | + await f5<Future<A>>(C2()); |
| 114 | + await f5<Future<C1>>(C2()); |
| 115 | + await f5<C2>(C2()); |
| 116 | + |
| 117 | + // Promoted type variable; called with `X == Object`. |
| 118 | + Future<void> g1<X>(X x) async { |
| 119 | + if (x is Object) { |
| 120 | + var x2 = await x; // Remains a `Future<Object?>`. |
| 121 | + x2.expectStaticType<Exactly<X>>(); |
| 122 | + Expect.type<Future<Object?>>(x2); |
| 123 | + Expect.identical(x, x2); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + await g1<Object>(Future<Object?>.value(null)); |
| 128 | + |
| 129 | + // Promoted type variable; called with `X == Object?`. |
| 130 | + Future<void> g2<X>(X x) async { |
| 131 | + if (x is Object) { |
| 132 | + var x2 = await x; // The future is awaited. |
| 133 | + x2.expectStaticType<Exactly<X>>(); |
| 134 | + Expect.isNull(x2); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + await g2<Object?>(Future<Object?>.value(null)); |
| 139 | + |
| 140 | + // Promoted type variable; called with `X == A`. |
| 141 | + Future<void> g3<X>(X x) async { |
| 142 | + if (x is A) { |
| 143 | + var x2 = await x; // The future is awaited. |
| 144 | + x2.expectStaticType<Exactly<X>>(); |
| 145 | + Expect.type<C1>(x2); |
| 146 | + Expect.notType<C2>(x2); |
| 147 | + Expect.notIdentical(x, x2); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + await g3<A>(C2()); |
| 152 | + |
| 153 | + // Promoted type variable; called with `X == C2`. |
| 154 | + Future<void> g4<X>(X x) async { |
| 155 | + if (x is A) { |
| 156 | + var x2 = await x; // Remains a `C2` and a `Future<C1>`. |
| 157 | + x2.expectStaticType<Exactly<X>>(); |
| 158 | + Expect.type<C2>(x2); |
| 159 | + Expect.identical(x, x2); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + await g4<C2>(C2()); |
| 164 | + |
| 165 | + // Promoted type variable; the value of `X` does not matter. |
| 166 | + Future<void> g5<X>(X x) async { |
| 167 | + if (x is Future<A>) { |
| 168 | + var x2 = await x; // The future is awaited. |
| 169 | + x2.expectStaticType<Exactly<A>>(); |
| 170 | + Expect.type<C1>(x2); |
| 171 | + Expect.notType<C2>(x2); |
| 172 | + Expect.notIdentical(x, x2); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + await g5<A>(C2()); |
| 177 | + await g5<C2>(C2()); |
| 178 | + await g5<Future<A>>(C2()); |
| 179 | + |
| 180 | + // Promoted type variable; the value of `X` does not matter. |
| 181 | + Future<void> g6<X>(X x) async { |
| 182 | + if (X is FutureOr<A>) { |
| 183 | + var x2 = await x; // The future is awaited. |
| 184 | + x2.expectStaticType<Exactly<A>>(); |
| 185 | + Expect.type<C1>(x2); |
| 186 | + Expect.notType<C2>(x2); |
| 187 | + Expect.notIdentical(x, x2); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + await g6<A>(C2()); |
| 192 | + await g6<C2>(C2()); |
| 193 | + await g6<Future<A>>(C2()); |
| 194 | + await g6<Future<C1>>(C2()); |
| 195 | + |
| 196 | + // Promoted type variable; values of type variables makes no difference. |
| 197 | + Future<void> g7<X, Y extends Future<int>>(X x) async { |
| 198 | + if (x is Future<int> && x is Y) { |
| 199 | + // The promoted type of `x` is `X & Y`. |
| 200 | + var x2 = await x; // The future is awaited. |
| 201 | + x2.expectStaticType<Exactly<int>>(); |
| 202 | + Expect.equals(1, x2); |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + await g7<Object?, Object?>(Future<int>.value(1)); |
| 207 | + |
| 208 | + // S? bounded type: called with `X == Null`. |
| 209 | + Future<void> h1<X extends Future<A>?>(X x) { |
| 210 | + var x2 = await x; // Remains null. |
| 211 | + x2.expectStaticType<Exactly<A?>>(); |
| 212 | + Expect.identical(null, x2); |
| 213 | + } |
| 214 | + |
| 215 | + await h1<Null>(null); |
| 216 | + |
| 217 | + // S? bounded type: called with `X == Future<A>`. |
| 218 | + Future<void> h2<X extends Future<A>?>(X x) { |
| 219 | + var x2 = await x; // The future is awaited. |
| 220 | + x2.expectStaticType<Exactly<A?>>(); |
| 221 | + Expect.type<C1>(x2); |
| 222 | + Expect.notType<C2>(x2); |
| 223 | + Expect.notIdentical(x, x2); |
| 224 | + } |
| 225 | + |
| 226 | + await h2<Future<A>>(C2()); |
| 227 | + |
| 228 | + // S? bounded type: called with `X == Null`. |
| 229 | + Future<void> h3<X extends FutureOr<A>?>(X x) { |
| 230 | + var x2 = await x; // Remains null. |
| 231 | + x2.expectStaticType<Exactly<A?>>(); |
| 232 | + Expect.identical(null, x2); |
| 233 | + } |
| 234 | + |
| 235 | + await h3<Null>(null); |
| 236 | + |
| 237 | + // S? bounded type: called with `X == FutureOr<A>`. |
| 238 | + Future<void> h4<X extends FutureOr<A>?>(X x) { |
| 239 | + var x2 = await x; // The future is awaited. |
| 240 | + x2.expectStaticType<Exactly<A?>>(); |
| 241 | + Expect.type<C1>(x2); |
| 242 | + Expect.notType<C2>(x2); |
| 243 | + Expect.notIdentical(x, x2); |
| 244 | + } |
| 245 | + |
| 246 | + await h4<FutureOr<A>>(C2()); |
| 247 | +} |
0 commit comments