Skip to content

Commit

Permalink
dart-lang#1401. Exhaustiveness tests fixed and new ones added (dart-l…
Browse files Browse the repository at this point in the history
…ang#2019)

Update and fix exhaustiveness tests. In particular, test exhaustiveness of custom classes that implement `List`, including implementations whose `length` can be negative.
  • Loading branch information
sgrekhov authored Apr 11, 2023
1 parent e34d7b5 commit c01fbd7
Show file tree
Hide file tree
Showing 21 changed files with 450 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/// always exhaustive
///
/// @description Check that it is no compile-time error if a matched type of a
/// switch expression is an enum and all cases are exhaustive
/// switch expression is an enum and the set of cases is exhaustive
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/// always exhaustive
///
/// @description Check that it is no compile-time error if a matched type of a
/// switch expression is an enum and all cases are exhaustive
/// switch expression is an enum and the set of cases is exhaustive
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/// always exhaustive
///
/// @description Check that it is no compile-time error if a matched type of a
/// switch expression is an enum and all cases are exhaustive
/// switch expression is an enum and the set of cases is exhaustive
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/// always exhaustive
///
/// @description Check that it is a compile-time error if a matched type of a
/// switch expression is an enum and not all cases are exhaustive
/// switch expression is an enum and the set of cases is not exhaustive
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ class Queen<T> extends Face<T> {
class King<T> extends Face<T> {
King(super.suit);
}

// The very last person will have to play all the roles.
class LastPersonOnEarth<T> implements Jack<T>, Queen<T>, King<T> {
final Suit suit;
final bool oneEyed;

LastPersonOnEarth(this.suit, {this.oneEyed = false});
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
// SharedOptions=--enable-experiment=patterns

import "../../../Utils/expect.dart";

String test(List<bool> l) {
// ^^^^
// [analyzer] unspecified
Expand All @@ -24,6 +22,9 @@ String test(List<bool> l) {
case [_, _, ...]:
return "ok";
}
// There is no return statement here, and static analysis doesn't know if the
// switch statement exhaustive or not, so an error above occurs because function
// return type cannot be null
}

main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ String test1(List<int> l) =>
[] => "0",
[_] => "1",
[_, _] => "2",
[_, ..., _, _, _] => "2+"
[_, ..., _, _, _] => "4+"
};

String test2(List<int> l) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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.

/// @assertion Switch expression with a list as a matched type can be exhaustive
///
/// @description Check that a custom implementation of `List` can be exhaustive
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns

import "dart:collection";
import "../../../Utils/expect.dart";

class MyList<T> extends ListBase<T> {
List<T> _inner = [];

MyList(this._inner);

@override
int get length {
return _inner.length;
}

@override
void set length(int newLength) {
_inner.length = newLength;
}

@override
T operator [](int index) {
return _inner[index];
}

@override
void operator []=(int index, T value) {
_inner[index] = value;
}
}

String test(MyList<int> l) =>
switch (l) {
[] => "0",
[_] => "1",
[_, _] => "2",
[_, _, ...] => "2+"
};

main() {
Expect.equals("0", test(MyList<int>([])));
Expect.equals("1", test(MyList<int>([1])));
Expect.equals("2", test(MyList<int>([1, 2])));
Expect.equals("2+", test(MyList<int>([1, 2, 3])));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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.

/// @assertion Switch expression with a list as a matched type can be exhaustive
///
/// @description Check that a custom implementation of `List` can be exhaustive
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns

import "dart:collection";
import "../../../Utils/expect.dart";

class MyList<T> with ListMixin<T> implements List<T> {
List<T> _inner = [];

MyList(this._inner);

@override
int get length {
return _inner.length;
}

@override
void set length(int newLength) {
_inner.length = newLength;
}

@override
T operator [](int index) {
return _inner[index];
}

@override
void operator []=(int index, T value) {
_inner[index] = value;
}
}

String test(MyList<int> l) =>
switch (l) {
[] => "0",
[_] => "1",
[_, _] => "2",
[_, _, ...] => "2+"
};

main() {
Expect.equals("0", test(MyList<int>([])));
Expect.equals("1", test(MyList<int>([1])));
Expect.equals("2", test(MyList<int>([1, 2])));
Expect.equals("2+", test(MyList<int>([1, 2, 3])));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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.

/// @assertion Switch expression with a list as a matched type can be exhaustive
///
/// @description Check that an implementation of `List` with a negative length
/// can be exhaustive
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns

import "dart:collection";
import "../../../Utils/expect.dart";

class MisbehavingList<T> extends ListBase<T> {
List<T> _inner = [];

MisbehavingList(this._inner);

@override
int get length {
return (-1) * _inner.length;
}

@override
void set length(int newLength) {
_inner.length = newLength;
}

@override
T operator [](int index) {
return _inner[index];
}

@override
void operator []=(int index, T value) {
_inner[index] = value;
}
}

String test(MisbehavingList<int> ml) =>
switch (ml) {
[] => "0",
[_] => "1",
[_, _] => "2",
[_, _, ...] => "2+"
};

main() {
Expect.equals("0", test(MisbehavingList<int>([])));
Expect.equals("0", test(MisbehavingList<int>([1])));
Expect.equals("0", test(MisbehavingList<int>([1, 2])));
Expect.equals("0", test(MisbehavingList<int>([1, 2, 3])));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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.

/// @assertion Switch expression with a list as a matched type can be exhaustive
///
/// @description Check that an implementation of `List` with a negative length
/// can be exhaustive
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns

import "dart:collection";
import "../../../Utils/expect.dart";

class MisbehavingList<T> with ListMixin<T> implements List<T> {
List<T> _inner = [];

MisbehavingList(this._inner);

@override
int get length {
return (-1) * _inner.length;
}

@override
void set length(int newLength) {
_inner.length = newLength;
}

@override
T operator [](int index) {
return _inner[index];
}

@override
void operator []=(int index, T value) {
_inner[index] = value;
}
}

String test(MisbehavingList<int> ml) =>
switch (ml) {
[] => "0",
[_] => "1",
[_, _] => "2",
[_, _, ...] => "2+"
};

main() {
Expect.equals("0", test(MisbehavingList<int>([])));
Expect.equals("0", test(MisbehavingList<int>([1])));
Expect.equals("0", test(MisbehavingList<int>([1, 2])));
Expect.equals("0", test(MisbehavingList<int>([1, 2, 3])));
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// SharedOptions=--enable-experiment=patterns

String test1(Map<bool, bool> m) =>
switch (m) {
switch (m) { // Empty map is not matched
//^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
Expand All @@ -35,6 +35,8 @@ String test2(Map<bool, bool> m) {
case {false: false}:
return "case-4";
}
// There is no return statement here, switch statement is not exhaustive, so an
// error above occurs because function return type cannot be null
}

main() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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.

/// @assertion Switch statements and expressions with map as a matched type can
/// never be exhaustive
///
/// @description Check that map pattern cannot be exhaustive
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns

String test1(Map<bool, bool> m) =>
switch (m) {
//^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
Map(length: <= 0) => "match"
};

String test2(Map<bool, bool> m) {
// ^^^^^
// [analyzer] unspecified
// [cfe] unspecified
switch (m) {
case Map(length: <= 0):
return "match";
}
// There is no return statement here, switch statement is not exhaustive, so an
// error above occurs because function return type cannot be null
}

main() {
print(test1({true: false}));
print(test2({true: false}));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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.

/// @assertion Switch statements and expressions with a sealed class as a
/// matched type are always exhaustive
///
/// @description Check that it is a compile-time error if matched type of a
/// switch expression is a sealed class and cases are not exhaustive
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns,class-modifiers

import "exhaustiveness_lib.dart";

String test1(Face face) => switch (face) {
// ^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
LastPersonOnEarth _ => 'LastPersonOnEarth'
};

String test2(Face face) => switch (face) {
// ^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
LastPersonOnEarth _ => 'Jack',
Queen _ => 'Queen',
King _ => 'King'
};

main() {
test1(King(Suit.club));
test2(King(Suit.club));
}
2 changes: 2 additions & 0 deletions LanguageFeatures/Patterns/Exhaustiveness/guard_A01_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ String testEnum1(Suit suit) {
case Suit.spade:
return "exhaustive";
}
// There is no return statement here, switch statement is not exhaustive, so an
// error above occurs because function return type cannot be null
}

String testEnum2(Suit suit) => switch (suit) {
Expand Down
Loading

0 comments on commit c01fbd7

Please sign in to comment.