df7c4bb439
This an experimental change; we haven't yet decided whether we want to allow this. See https://github.com/dart-lang/language/issues/2939 for discussion. Bug: https://github.com/dart-lang/language/issues/2939 Change-Id: If69ab66d18ae7ec3dfc79496ce13517ef1c15227 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/290907 Reviewed-by: Konstantin Shcheglov <scheglov@google.com> Commit-Queue: Paul Berry <paulberry@google.com> Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
32 lines
1008 B
Dart
32 lines
1008 B
Dart
// Copyright (c) 2023, 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.
|
|
|
|
// SharedOptions=--enable-experiment=patterns,records
|
|
|
|
/// Test that switch expressions are allowed to be empty, and that they have the
|
|
/// proper static type behavior.
|
|
///
|
|
/// Note that the runtime behavior only matters in mixed-mode programs, since in
|
|
/// fully sound programs an empty switch expression will be unreachable.
|
|
|
|
import '../static_type_helper.dart';
|
|
|
|
// Note: no subtypes.
|
|
sealed class A {}
|
|
|
|
Never emptySwitchOnSealedClass(A a) =>
|
|
(switch (a) { })..expectStaticType<Never>();
|
|
|
|
void unreachableAfterEmptySwitch(A a, int? i) {
|
|
if (i == null) {
|
|
(switch (a) { });
|
|
// Flow analysis should understand that since `switch (a) {}` has type
|
|
// `Never`, the rest of this block is unreachable.
|
|
}
|
|
// Hence, `i` is promoted to non-nullable.
|
|
i.isEven;
|
|
}
|
|
|
|
main() {}
|