Skip to content

#3057. Add more tests for promotion in switch #3185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 1 addition & 37 deletions TypeSystem/flow-analysis/promotion_switch_A05_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@ test1(Object? x) {
}

test2(Object? x) {
switch (x) {
case num v:
case int _: // ignore: unreachable_switch_case
x.expectStaticType<Exactly<Object?>>();
case _:
x.expectStaticType<Exactly<Object?>>();
}
}

test3(Object? x) {
switch (x) {
case int v:
case num v:
Expand All @@ -43,17 +33,7 @@ test3(Object? x) {
}
}

test4(Object? x) {
switch (x) {
case num v:
case int v: // ignore: unreachable_switch_case
x.expectStaticType<Exactly<Object?>>();
case _:
x.expectStaticType<Exactly<Object?>>();
}
}

test5(Object? x) {
test3(Object? x) {
switch (x) {
case int _:
case num _:
Expand All @@ -63,27 +43,11 @@ test5(Object? x) {
}
}

test6(Object? x) {
switch (x) {
case num _:
case int _: // ignore: unreachable_switch_case
x.expectStaticType<Exactly<Object?>>();
case _:
x.expectStaticType<Exactly<Object?>>();
}
}

main() {
test1(1);
test1(null);
test2(1);
test2(null);
test3(1);
test3(null);
test4(1);
test4(null);
test5(1);
test5(null);
test6(1);
test6(null);
}
55 changes: 55 additions & 0 deletions TypeSystem/flow-analysis/promotion_switch_A05_t04.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) 2025, 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.

/// @assertion For now (May, 2025) promotion in switch statement and
/// expressions are not specified yet. Informal spec:
/// https://github.com/dart-lang/co19/pull/3169#issuecomment-2885167786
/// TODO (sgrekhov): update when specified
///
/// @description Checks that pattern match doesn't promote the variable when a
/// case scope is shared and cases have different types.
/// @author [email protected]

import '../../Utils/static_type_helper.dart';

test1(Object? x) {
if (x is Object) {
switch (x) {
case int v:
case num _:
x.expectStaticType<Exactly<Object>>();
case _:
x.expectStaticType<Exactly<Object>>();
}
}
}

test2(Object? x) {
switch (x) {
case num _ && int v:
case Object _ && int v: // ignore: unreachable_switch_case
x.expectStaticType<Exactly<int>>();
case _:
x.expectStaticType<Exactly<Object?>>();
}
}

test3(Object? x) {
switch (x) {
case num _ && int v:
case Object v && num _: // ignore: unreachable_switch_case
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, that's an interesting case, too!

x.expectStaticType<Exactly<num>>();
case _:
x.expectStaticType<Exactly<Object?>>();
}
}

main() {
test1(1);
test1(null);
test2(1);
test2(null);
test3(1);
test3(null);
}