Skip to content

Commit

Permalink
#1401. Exhaustiveness tests added (#1974)
Browse files Browse the repository at this point in the history
Exhaustiveness tests added
  • Loading branch information
sgrekhov authored Apr 3, 2023
1 parent 56e7290 commit a6665ad
Show file tree
Hide file tree
Showing 23 changed files with 959 additions and 0 deletions.
45 changes: 45 additions & 0 deletions LanguageFeatures/Patterns/exhaustiveness_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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 But switch statements must only be exhaustive when the matched
/// value is an always-exhaustive type, defined as:
/// - bool
/// - Null
/// - A enum type
/// - A type whose declaration is marked sealed
/// - T? where T is always-exhaustive
/// - FutureOr<T> for some type T that is always-exhaustive
/// - A record type whose fields all have always-exhaustive types
/// - A type variable X with bound T where T is always-exhaustive
/// - A promoted type variable X & T where T is always-exhaustive
///
/// All other types are not always-exhaustive. Then:
/// - It is a compile-time error if the cases in a switch statement or switch
/// collection element are not exhaustive and the static type of the matched
/// value is an always-exhaustive type. There is no error if a switch
/// statement is not exhaustive when the type is not an always-exhaustive type
///
/// @description Check that it is a compile-time error if a switch statement is
/// not exhaustive. Test `bool`
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns

main() {
bool b = 1 > 2;
switch (b) {
//^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
case true:
}
switch (b) {
//^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
case false:
print("false");
break;
}
}
43 changes: 43 additions & 0 deletions LanguageFeatures/Patterns/exhaustiveness_A01_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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 But switch statements must only be exhaustive when the matched
/// value is an always-exhaustive type, defined as:
/// - bool
/// - Null
/// - A enum type
/// - A type whose declaration is marked sealed
/// - T? where T is always-exhaustive
/// - FutureOr<T> for some type T that is always-exhaustive
/// - A record type whose fields all have always-exhaustive types
/// - A type variable X with bound T where T is always-exhaustive
/// - A promoted type variable X & T where T is always-exhaustive
///
/// All other types are not always-exhaustive. Then:
/// - It is a compile-time error if the cases in a switch statement or switch
/// collection element are not exhaustive and the static type of the matched
/// value is an always-exhaustive type. There is no error if a switch
/// statement is not exhaustive when the type is not an always-exhaustive type
///
/// @description Check that it is no error if a switch statement is exhaustive.
/// Test `bool`
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns

main() {
bool b = 1 > 2;
switch (b) {
case true:
case false:
}
switch (b) {
case false:
print("false");
break;
case true:
print("true");
break;
}
}
35 changes: 35 additions & 0 deletions LanguageFeatures/Patterns/exhaustiveness_A01_t03.dart
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 But switch statements must only be exhaustive when the matched
/// value is an always-exhaustive type, defined as:
/// - bool
/// - Null
/// - A enum type
/// - A type whose declaration is marked sealed
/// - T? where T is always-exhaustive
/// - FutureOr<T> for some type T that is always-exhaustive
/// - A record type whose fields all have always-exhaustive types
/// - A type variable X with bound T where T is always-exhaustive
/// - A promoted type variable X & T where T is always-exhaustive
///
/// All other types are not always-exhaustive. Then:
/// - It is a compile-time error if the cases in a switch statement or switch
/// collection element are not exhaustive and the static type of the matched
/// value is an always-exhaustive type. There is no error if a switch
/// statement is not exhaustive when the type is not an always-exhaustive type
///
/// @description Check that it is a compile-time error if a switch statement is
/// not exhaustive. Test `null`
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns

main() {
switch (null) {
//^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}
}
36 changes: 36 additions & 0 deletions LanguageFeatures/Patterns/exhaustiveness_A01_t04.dart
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 But switch statements must only be exhaustive when the matched
/// value is an always-exhaustive type, defined as:
/// - bool
/// - Null
/// - A enum type
/// - A type whose declaration is marked sealed
/// - T? where T is always-exhaustive
/// - FutureOr<T> for some type T that is always-exhaustive
/// - A record type whose fields all have always-exhaustive types
/// - A type variable X with bound T where T is always-exhaustive
/// - A promoted type variable X & T where T is always-exhaustive
///
/// All other types are not always-exhaustive. Then:
/// - It is a compile-time error if the cases in a switch statement or switch
/// collection element are not exhaustive and the static type of the matched
/// value is an always-exhaustive type. There is no error if a switch
/// statement is not exhaustive when the type is not an always-exhaustive type
///
/// @description Check that it is no error if a switch statement is exhaustive.
/// Test `null`
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns

main() {
switch (null) {
case null:
}
switch (null) {
case Null():
}
}
42 changes: 42 additions & 0 deletions LanguageFeatures/Patterns/exhaustiveness_A01_t05.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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 But switch statements must only be exhaustive when the matched
/// value is an always-exhaustive type, defined as:
/// - bool
/// - Null
/// - A enum type
/// - A type whose declaration is marked sealed
/// - T? where T is always-exhaustive
/// - FutureOr<T> for some type T that is always-exhaustive
/// - A record type whose fields all have always-exhaustive types
/// - A type variable X with bound T where T is always-exhaustive
/// - A promoted type variable X & T where T is always-exhaustive
///
/// All other types are not always-exhaustive. Then:
/// - It is a compile-time error if the cases in a switch statement or switch
/// collection element are not exhaustive and the static type of the matched
/// value is an always-exhaustive type. There is no error if a switch
/// statement is not exhaustive when the type is not an always-exhaustive type
///
/// @description Check that it is a compile-time error if a switch statement is
/// not exhaustive. Test an enum type
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns

enum E {
e1,
e2
}

main() {
E e = E.e1;
switch (e) {
//^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
case E.e1:
}
}
40 changes: 40 additions & 0 deletions LanguageFeatures/Patterns/exhaustiveness_A01_t06.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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 But switch statements must only be exhaustive when the matched
/// value is an always-exhaustive type, defined as:
/// - bool
/// - Null
/// - A enum type
/// - A type whose declaration is marked sealed
/// - T? where T is always-exhaustive
/// - FutureOr<T> for some type T that is always-exhaustive
/// - A record type whose fields all have always-exhaustive types
/// - A type variable X with bound T where T is always-exhaustive
/// - A promoted type variable X & T where T is always-exhaustive
///
/// All other types are not always-exhaustive. Then:
/// - It is a compile-time error if the cases in a switch statement or switch
/// collection element are not exhaustive and the static type of the matched
/// value is an always-exhaustive type. There is no error if a switch
/// statement is not exhaustive when the type is not an always-exhaustive type
///
/// @description Check that it is no error if a switch statement is exhaustive.
/// Test an enum type
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns

enum E {
e1,
e2
}

main() {
E e = E.e1;
switch (e) {
case E.e1:
case E.e2:
}
}
41 changes: 41 additions & 0 deletions LanguageFeatures/Patterns/exhaustiveness_A01_t07.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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 But switch statements must only be exhaustive when the matched
/// value is an always-exhaustive type, defined as:
/// - bool
/// - Null
/// - A enum type
/// - A type whose declaration is marked sealed
/// - T? where T is always-exhaustive
/// - FutureOr<T> for some type T that is always-exhaustive
/// - A record type whose fields all have always-exhaustive types
/// - A type variable X with bound T where T is always-exhaustive
/// - A promoted type variable X & T where T is always-exhaustive
///
/// All other types are not always-exhaustive. Then:
/// - It is a compile-time error if the cases in a switch statement or switch
/// collection element are not exhaustive and the static type of the matched
/// value is an always-exhaustive type. There is no error if a switch
/// statement is not exhaustive when the type is not an always-exhaustive type
///
/// @description Check that it is a compile-time error if a switch statement is
/// not exhaustive. Test a type whose declaration is marked `sealed`
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns,class-modifiers

sealed class Sealed {}
class C1 extends Sealed {}
class C2 extends Sealed {}

main() {
Sealed s = C1();
switch (s) {
//^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
case C1 _:
}
}
43 changes: 43 additions & 0 deletions LanguageFeatures/Patterns/exhaustiveness_A01_t08.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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 But switch statements must only be exhaustive when the matched
/// value is an always-exhaustive type, defined as:
/// - bool
/// - Null
/// - A enum type
/// - A type whose declaration is marked sealed
/// - T? where T is always-exhaustive
/// - FutureOr<T> for some type T that is always-exhaustive
/// - A record type whose fields all have always-exhaustive types
/// - A type variable X with bound T where T is always-exhaustive
/// - A promoted type variable X & T where T is always-exhaustive
///
/// All other types are not always-exhaustive. Then:
/// - It is a compile-time error if the cases in a switch statement or switch
/// collection element are not exhaustive and the static type of the matched
/// value is an always-exhaustive type. There is no error if a switch
/// statement is not exhaustive when the type is not an always-exhaustive type
///
/// @description Check that it is no error if a switch statement is exhaustive.
/// Test a type whose declaration is marked `sealed`
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns,class-modifiers

sealed class Sealed {}
class C1 extends Sealed {}
class C2 extends Sealed {}

main() {
Sealed s = C1();
switch (s) {
case C1 _:
case C2 _:
}
switch (s) {
case C1():
case C2():
}
}
48 changes: 48 additions & 0 deletions LanguageFeatures/Patterns/exhaustiveness_A01_t09.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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 But switch statements must only be exhaustive when the matched
/// value is an always-exhaustive type, defined as:
/// - bool
/// - Null
/// - A enum type
/// - A type whose declaration is marked sealed
/// - T? where T is always-exhaustive
/// - FutureOr<T> for some type T that is always-exhaustive
/// - A record type whose fields all have always-exhaustive types
/// - A type variable X with bound T where T is always-exhaustive
/// - A promoted type variable X & T where T is always-exhaustive
///
/// All other types are not always-exhaustive. Then:
/// - It is a compile-time error if the cases in a switch statement or switch
/// collection element are not exhaustive and the static type of the matched
/// value is an always-exhaustive type. There is no error if a switch
/// statement is not exhaustive when the type is not an always-exhaustive type
///
/// @description Check that it is a compile-time error if a switch statement is
/// not exhaustive. Test a type `T?` where `T` is always-exhaustive
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns,class-modifiers

main() {
bool? b = 1 > 2;
if (b) {
b = null;
}
switch (b) {
//^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
case true:
case false:
}
switch (b) {
//^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
case true:
case null:
}
}
Loading

0 comments on commit a6665ad

Please sign in to comment.