-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Emit unreachable arm warning for OR match arms (#6505)
## Description This PR fixes #6388. If a whole OR match arm is unreachable a warning will be emitted like in this case: ```Sway match v { 1 => (), 2 => (), 1 | 2 => (), <<<--- Warning emitted here. _ => (), }; ``` The PR strictly fixes the issue reported in #6388. It does not fix a previously known, more general issue, described in #5097 where we expect warnings to be emitted also on individual elements of an OR arm, even if the whole arm is reachable. E.g.: ```Sway match v { 1 => (), 2 => (), 1 | 3 => (), <<<--- Warning should be emitted here that `1` is not reachable. 2 | 4 => (), <<<--- Warning should be emitted here that `2` is not reachable. _ => (), }; ``` Fixing this issue requires more invasive restructuring of the propagation of the collected witness information and will be done in a separate PR. Closes #6388. ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [x] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers.
- Loading branch information
Showing
5 changed files
with
80 additions
and
6 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
8 changes: 8 additions & 0 deletions
8
...2e_vm_tests/test_programs/should_pass/language/match_expressions_unreachable_or/Forc.lock
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,8 @@ | ||
[[package]] | ||
name = "core" | ||
source = "path+from-root-7FA7C6C8132DFBE7" | ||
|
||
[[package]] | ||
name = "match_expressions_unreachable_or" | ||
source = "member" | ||
dependencies = ["core"] |
8 changes: 8 additions & 0 deletions
8
...2e_vm_tests/test_programs/should_pass/language/match_expressions_unreachable_or/Forc.toml
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,8 @@ | ||
[project] | ||
authors = ["Fuel Labs <[email protected]>"] | ||
license = "Apache-2.0" | ||
name = "match_expressions_unreachable_or" | ||
entry = "main.sw" | ||
|
||
[dependencies] | ||
core = { path = "../../../../../../../sway-lib-core" } |
42 changes: 42 additions & 0 deletions
42
..._vm_tests/test_programs/should_pass/language/match_expressions_unreachable_or/src/main.sw
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 @@ | ||
// This test proves that https://github.com/FuelLabs/sway/issues/6388 is fixed. | ||
script; | ||
|
||
struct S { | ||
x: bool | ||
} | ||
|
||
fn main() { | ||
let v = 0u64; | ||
// We want to have only one error in the case below, | ||
// for the whole or-pattern elements and not for individual | ||
// parts. | ||
match v { | ||
1 => (), | ||
1 | 1 => (), | ||
_ => (), | ||
}; | ||
|
||
match v { | ||
1 => (), | ||
2 => (), | ||
1 | 2 => (), | ||
_ => (), | ||
}; | ||
|
||
// TODO: Once https://github.com/FuelLabs/sway/issues/5097 is fixed, the below examples | ||
// will also emit warnings for unreacahbility of individual or-pattern elements. | ||
// Extend filecheck checks to cover those warning. | ||
|
||
match v { | ||
1 => (), | ||
2 => (), | ||
1 | 3 => (), | ||
2 | 4 => (), | ||
_ => (), | ||
}; | ||
|
||
let s = S { x: false }; | ||
let _x = match s { | ||
S { x } | S { x } => { x }, | ||
}; | ||
} |
16 changes: 16 additions & 0 deletions
16
...2e_vm_tests/test_programs/should_pass/language/match_expressions_unreachable_or/test.toml
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,16 @@ | ||
category = "compile" | ||
expected_warnings = 2 | ||
|
||
#check: $()warning | ||
#sameln: $()Match arm is unreachable | ||
#check: $()1 => (), | ||
#nextln: $()Preceding match arms already match all the values that `1 | 1` can match. | ||
#check: $()1 | 1 => (), | ||
#nextln: $()Match arm `1 | 1` is unreachable. | ||
|
||
#check: $()warning | ||
#sameln: $()Match arm is unreachable | ||
#check: $()2 => (), | ||
#nextln: $()Preceding match arms already match all the values that `1 | 2` can match. | ||
#check: $()1 | 2 => (), | ||
#nextln: $()Match arm `1 | 2` is unreachable. |