Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions gcc/rust/backend/rust-compile-pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ compile_range_pattern_bound (HIR::RangePatternBound &bound,

HIR::LiteralExpr litexpr (mappings, ref.get_literal (), locus,
std::vector<AST::Attribute> ());
if (ref.get_has_minus ())
litexpr.set_negative ();

result = CompileExpr::Compile (litexpr, ctx);
}
Expand Down Expand Up @@ -159,6 +161,30 @@ CompilePatternCheckExpr::visit (HIR::RangePattern &pattern)
pattern.get_mappings (),
pattern.get_locus (), ctx);

rust_assert (
(TREE_CODE (upper) == REAL_CST && TREE_CODE (lower) == REAL_CST)
|| (TREE_CODE (upper) == INTEGER_CST && TREE_CODE (lower) == INTEGER_CST));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does 1f32..1.2f32 break anything?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Triggers an error that shouldn't be triggered lol, maybe we can create a new issue for it.

error: mismatched types, expected ‘f32’ but got ‘<integer>’ [E0308]
    7 |         1f32..1.2f32 => 1,
      |         ^~~~

Anyways, 1.0f32..1.2f32 compiles properly and does not trigger E0579 but -1.2f32..-1.0f32 triggers E0579... not sure what went wrong here :/


bool error_E0579 = false;
if (TREE_CODE (upper) == REAL_CST)
{
REAL_VALUE_TYPE upper_r = TREE_REAL_CST (upper);
REAL_VALUE_TYPE lower_r = TREE_REAL_CST (lower);
if (real_compare (GE_EXPR, &lower_r, &upper_r))
error_E0579 = true;
}
else if (TREE_CODE (upper) == INTEGER_CST)
{
auto upper_wi = wi::to_wide (upper).to_shwi ();
auto lower_wi = wi::to_wide (lower).to_shwi ();
if (lower_wi >= upper_wi)
error_E0579 = true;
}

if (error_E0579)
rust_error_at (pattern.get_locus (), ErrorCode::E0579,
"lower range bound must be less than upper");

ComparisonOperator upper_cmp = pattern.is_inclusive_range ()
? ComparisonOperator::LESS_OR_EQUAL
: ComparisonOperator::LESS_THAN;
Expand Down
10 changes: 10 additions & 0 deletions gcc/testsuite/rust/compile/issue-3659.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![feature(exclusive_range_pattern)]

fn main() {
let x = 3;

match x {
0..-1 => 2, // { dg-error "lower range bound must be less than upper .E0579." }
_ => 3,
};
}
10 changes: 10 additions & 0 deletions gcc/testsuite/rust/compile/issue-4242.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![feature(exclusive_range_pattern)]

fn main() {
let x = 1;

match x {
-55..0 => 2,
-99..-55 => 3,
};
}
11 changes: 11 additions & 0 deletions gcc/testsuite/rust/execute/torture/issue-4242.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![feature(exclusive_range_pattern)]

fn main() -> i32 {
let x = -77;

match x {
-55..99 => 1,
-99..-55 => 0, // the correct case
_ => 1,
}
}
Loading