Closed
Description
If switch statement variable is assigned in when
part then no promotion occurs if case scope is shared. Why there is no promotion in test2()
below?
test1(Object? x) {
switch (x) {
case int v when (x = 42) > 0:
x.expectStaticType<Exactly<int>>(); // Ok, promoted
case _:
x.expectStaticType<Exactly<Object?>>();
}
}
test2(Object? x) {
switch (x) {
case int v when (x = 42) > 0:
case int v:
x.expectStaticType<Exactly<Object?>>(); // No promotion
case _:
x.expectStaticType<Exactly<Object?>>();
}
}
T expectStaticType<R extends Exactly<T>>() {
return this;
}
typedef Exactly<T> = T Function(T);
Dart SDK version: 3.9.0-95.0.dev (dev) (Sun May 4 21:03:05 2025 -0700) on "windows_x64"