-
Notifications
You must be signed in to change notification settings - Fork 28
#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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
LanguageFeatures/Extension-types/exhaustiveness_flow_analysis_A01_t01.dart
This file contains hidden or 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,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(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
LanguageFeatures/Extension-types/exhaustiveness_variable_A04_t01.dart
This file contains hidden or 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) 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>>; | ||
} | ||
} |
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
|
@@ -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] | ||
|
||
|
31 changes: 31 additions & 0 deletions
31
LanguageFeatures/Patterns/Exhaustiveness/exhaustiveness_sealed_A03_t01.dart
This file contains hidden or 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,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); | ||
eernstg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
} |
32 changes: 32 additions & 0 deletions
32
LanguageFeatures/Patterns/Exhaustiveness/exhaustiveness_sealed_A03_t02.dart
This file contains hidden or 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,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 | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.