Skip to content

Commit 9e2c020

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 <yapzhhg@gmail.com>
1 parent dcd4d2f commit 9e2c020

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ compile_range_pattern_bound (HIR::RangePatternBound &bound,
119119

120120
HIR::LiteralExpr litexpr (mappings, ref.get_literal (), locus,
121121
std::vector<AST::Attribute> ());
122-
if (ref.get_has_minus())
123-
litexpr.set_negative();
122+
if (ref.get_has_minus ())
123+
+litexpr.set_negative ();
124124

125125
result = CompileExpr::Compile (litexpr, ctx);
126126
}
@@ -161,6 +161,30 @@ CompilePatternCheckExpr::visit (HIR::RangePattern &pattern)
161161
pattern.get_mappings (),
162162
pattern.get_locus (), ctx);
163163

164+
rust_assert (
165+
(TREE_CODE (upper) == REAL_CST && TREE_CODE (lower) == REAL_CST)
166+
|| (TREE_CODE (upper) == INTEGER_CST && TREE_CODE (lower) == INTEGER_CST));
167+
168+
bool error_E0579 = false;
169+
if (TREE_CODE (upper) == REAL_CST)
170+
{
171+
REAL_VALUE_TYPE upper_r = TREE_REAL_CST (upper);
172+
REAL_VALUE_TYPE lower_r = TREE_REAL_CST (lower);
173+
if (real_compare (GE_EXPR, &lower_r, &upper_r))
174+
error_E0579 = true;
175+
}
176+
else if (TREE_CODE (upper) == INTEGER_CST)
177+
{
178+
auto upper_wi = wi::to_wide (upper).to_shwi ();
179+
auto lower_wi = wi::to_wide (lower).to_shwi ();
180+
if (lower_wi >= upper_wi)
181+
error_E0579 = true;
182+
}
183+
184+
if (error_E0579)
185+
rust_error_at (pattern.get_locus (), ErrorCode::E0579,
186+
"lower range bound must be less than upper");
187+
164188
ComparisonOperator upper_cmp = pattern.is_inclusive_range ()
165189
? ComparisonOperator::LESS_OR_EQUAL
166190
: 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)