Commit 8d52886
committed
checks/errors: Fix non-exhaustive pattern matching for literals
The compiler previously treated literal patterns (like 0, 'a', or true) as
wildcards, causing the exhaustiveness checker to incorrectly accept incomplete
match blocks.
This patch modifies the pattern analysis to lower literals into integer ranges.
It introduces a new helper which uses
(for -fno-exceptions compatibility) to parse integers. It also updates
to correctly handle finite boolean universes and default
other scalars (char, int) to wildcards without incorrectly inserting struct
constructors.
Additionally, and
are updated to handle constructors, preventing Internal Compiler
Errors (ICEs). Unicode and hex escape patterns in characters currently emit
a sorry diagnostic as they are not yet fully supported.
Fixes #4296
gcc/rust/ChangeLog:
* checks/errors/rust-hir-pattern-analysis.h (Constructor::make_int_range):
Add helper to create integer range constructors.
* checks/errors/rust-hir-pattern-analysis.cc (lower_literal_pattern):
New helper function to lower literals to integer ranges.
(lower_pattern): Use new lower_literal_pattern helper.
(parse_check_literal): Refactor for readability and base detection.
(split_constructors): Handle integer, bool, and char types correctly
and remove redundant struct constructors for primitives.
(WitnessPat::to_string): Handle INT_RANGE to prevent ICE.
(WitnessMatrix::apply_constructor): Handle INT_RANGE to prevent ICE.
gcc/testsuite/ChangeLog:
* rust/compile/issue-4296.rs: New test.
* rust/compile/issue-3929-2.rs: Add wildcard arm to match block.
* rust/execute/torture/struct-pattern-match.rs: Add wildcard arm.
Signed-off-by: Jayant Chauhan <[email protected]>1 parent 54daa1b commit 8d52886
File tree
2 files changed
+3
-1
lines changed- gcc
- rust/checks/errors
- testsuite/rust/compile
2 files changed
+3
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1245 | 1245 | | |
1246 | 1246 | | |
1247 | 1247 | | |
1248 | | - | |
| 1248 | + | |
1249 | 1249 | | |
1250 | 1250 | | |
1251 | 1251 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
2 | 4 | | |
3 | 5 | | |
| |||
0 commit comments