Skip to content

Commit 8d52886

Browse files
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

2 files changed

+3
-1
lines changed

gcc/rust/checks/errors/rust-hir-pattern-analysis.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1245,7 +1245,7 @@ lower_tuple_pattern (Resolver::TypeCheckContext *ctx,
12451245
variant->get_fields ().at (i++)->get_field_type ()));
12461246
}
12471247
while (i < variant->num_fields ()
1248-
- items_has_rest.get_upper_patterns ().size ())
1248+
- items_has_rest.get_upper_patterns ().size ())
12491249
{
12501250
fields.push_back (
12511251
DeconstructedPat::make_wildcard (pattern.get_locus ()));

gcc/testsuite/rust/compile/issue-4296.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![feature(no_core)]
2+
#![no_core]
13
// { dg-options "-w" }
24
pub fn main() {
35
let x = 3;

0 commit comments

Comments
 (0)