-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Exhaustiveness tests added
- Loading branch information
Showing
23 changed files
with
959 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(): | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 _: | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(): | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
} | ||
} |
Oops, something went wrong.