Skip to content

#2420. Add more exhaustiveness tests #2513

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 4 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2024, 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 Extension type erasure is not used during flow analysis
///
/// @description Check that an extension type erasure is not used during flow
/// analysis that isn't concerned with run-time type tests
/// @author [email protected]

// SharedOptions=--enable-experiment=inline-class

extension type const ET1(int value) {
void confirmET1() {}
}
extension type const ET2(int value) implements int {
void confirmET2() {}
}

void main() {
int i = 42;
switch (i) {
case ET1 v:
v.confirmET1(); // This confirms that `ET1` is not erased to int
}
switch (i) {
case ET2 v:
v.confirmET2();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2024, 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 Exhaustiveness of a variable pattern is determined by the static
/// type of the corresponding variable.
///
/// @description Check static type of a variable pattern
/// @author [email protected]

// SharedOptions=--enable-experiment=inline-class

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

extension type ET(int i) {}
extension type TO(int i) implements ET {}

main() {
ET x = ET(1);
switch (x) {
case int y:
x.expectStaticType<Exactly<ET>>;
y.expectStaticType<Exactly<int>>;
case TO z: // Unreachable, it's Ok
x.expectStaticType<Exactly<TO>>;
z.expectStaticType<Exactly<TO>>;
}
switch (42) {
case ET y:
x.expectStaticType<Exactly<ET>>;
y.expectStaticType<Exactly<ET>>;
case TO z: // Unreachable, it's Ok
x.expectStaticType<Exactly<ET>>;
z.expectStaticType<Exactly<TO>>;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,7 @@ class LastPersonOnEarth<T> implements Jack<T>, Queen<T>, King<T> {

LastPersonOnEarth(this.suit, {this.oneEyed = false});
}

sealed class SClass {}
class B1Class extends SClass {}
class B2Class extends SClass {}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/// matched type are always exhaustive
///
/// @description Check that it is a compile-time error if the matched value type
/// of a switch expression or stetement is a sealed class and the set of cases
/// of a switch expression or statement is a sealed class and the set of cases
/// is not exhaustive
/// @author [email protected]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2024, 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 Switch statements and expressions with a sealed class as a
/// matched type are always exhaustive
///
/// @description Check that the flow analysis considers a case reachable even in
/// the case where it accepts only objects of an unrelated type.
/// @author [email protected]

class A {}

class B {}

void main() {
int x;
A a = A();
switch (a) {
case B():
print(
'''x is not assigned here. This case looks impossible, but there can
be a hidden subtype of A and B in some other library''');
case _:
x = 1;
}
print(x);
// ^
// [analyzer] unspecified
// [cfe] unspecified
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2024, 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 Switch statements and expressions with a sealed class as a
/// matched type are always exhaustive
///
/// @description Check that the flow analysis considers a case reachable even in
/// the case where it accepts only objects of an unrelated type.
/// @author [email protected]

import 'exhaustiveness_lib.dart';

class C {}
class D extends C implements B1Class {}

void main() {
int x;
SClass s = D();
switch (s) {
case C():
print('x is not assigned here');
case B1Class():
x = 1;
case B2Class():
x = 2;
}
print(x);
// ^
// [analyzer] unspecified
// [cfe] unspecified
}