Skip to content

Commit 0d81649

Browse files
committed
gccrs: Implement E0579 error checking in RangePattern compilation
Checks whether upper bound of range is not lower or equal to the lower bound. gcc/rust/ChangeLog: * backend/rust-compile-pattern.cc(compilePatternCheckExpr::visit(RangePattern)): Add E0579 check to ensure that lower bound is always below upper bound. Signed-off-by: Yap Zhi Heng <[email protected]>
1 parent f2f9466 commit 0d81649

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

gcc/rust/backend/rust-compile-pattern.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,30 @@ CompilePatternCheckExpr::visit (HIR::RangePattern &pattern)
159159
pattern.get_mappings (),
160160
pattern.get_locus (), ctx);
161161

162+
rust_assert (
163+
(TREE_CODE (upper) == REAL_CST && TREE_CODE (lower) == REAL_CST)
164+
|| (TREE_CODE (upper) == INTEGER_CST && TREE_CODE (lower) == INTEGER_CST));
165+
166+
bool error_E0579 = false;
167+
if (TREE_CODE (upper) == REAL_CST)
168+
{
169+
REAL_VALUE_TYPE upper_r = TREE_REAL_CST (upper);
170+
REAL_VALUE_TYPE lower_r = TREE_REAL_CST (lower);
171+
if (real_compare (GE_EXPR, &lower_r, &upper_r))
172+
error_E0579 = true;
173+
}
174+
else if (TREE_CODE (upper) == INTEGER_CST)
175+
{
176+
auto upper_wi = wi::to_wide (upper).to_shwi ();
177+
auto lower_wi = wi::to_wide (lower).to_shwi ();
178+
if (lower_wi >= upper_wi)
179+
error_E0579 = true;
180+
}
181+
182+
if (error_E0579)
183+
rust_error_at (pattern.get_locus (), ErrorCode::E0579,
184+
"lower range bound must be less than upper");
185+
162186
ComparisonOperator upper_cmp = pattern.is_inclusive_range ()
163187
? ComparisonOperator::LESS_OR_EQUAL
164188
: ComparisonOperator::LESS_THAN;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(exclusive_range_pattern)]
2+
3+
fn main() {
4+
let x = 3;
5+
6+
match x {
7+
0..-1 => 2, // { dg-error "lower range bound must be less than upper .E0579." }
8+
_ => 3,
9+
};
10+
}

0 commit comments

Comments
 (0)