Skip to content

Commit d4c6693

Browse files
Lishin1215CohenArthur
authored andcommitted
gccrs: Fix ICE on exclusive_range_pattern lowering
gcc/rust/ChangeLog: * backend/rust-compile-pattern.cc (CompilePatternCheckExpr::visit): Check upper compare operator. * hir/rust-ast-lower-pattern.cc (ASTLoweringPattern::visit): Handle lowering of exclusive range pattern. * hir/tree/rust-hir-pattern.h (class RangePattern): Add support for exclusive ranges in HIR representation. gcc/testsuite/ChangeLog: * rust/compile/issue-3928.rs: New test. Signed-off-by: lishin <[email protected]>
1 parent 85ec714 commit d4c6693

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,15 @@ CompilePatternCheckExpr::visit (HIR::RangePattern &pattern)
158158
pattern.get_mappings (),
159159
pattern.get_locus (), ctx);
160160

161+
ComparisonOperator upper_cmp = pattern.is_inclusive_range ()
162+
? ComparisonOperator::LESS_OR_EQUAL
163+
: ComparisonOperator::LESS_THAN;
161164
tree check_lower
162165
= Backend::comparison_expression (ComparisonOperator::GREATER_OR_EQUAL,
163166
match_scrutinee_expr, lower,
164167
pattern.get_locus ());
165168
tree check_upper
166-
= Backend::comparison_expression (ComparisonOperator::LESS_OR_EQUAL,
167-
match_scrutinee_expr, upper,
169+
= Backend::comparison_expression (upper_cmp, match_scrutinee_expr, upper,
168170
pattern.get_locus ());
169171
check_expr = Backend::arithmetic_or_logical_expression (
170172
ArithmeticOrLogicalOperator::BITWISE_AND, check_lower, check_upper,

gcc/rust/hir/rust-ast-lower-pattern.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,6 @@ ASTLoweringPattern::visit (AST::LiteralPattern &pattern)
268268
void
269269
ASTLoweringPattern::visit (AST::RangePattern &pattern)
270270
{
271-
if (pattern.get_range_kind () == AST::RangeKind::EXCLUDED)
272-
rust_unreachable (); // Not supported yet
273271
auto upper_bound = lower_range_pattern_bound (pattern.get_upper_bound ());
274272
auto lower_bound = lower_range_pattern_bound (pattern.get_lower_bound ());
275273

@@ -278,9 +276,11 @@ ASTLoweringPattern::visit (AST::RangePattern &pattern)
278276
mappings.get_next_hir_id (crate_num),
279277
UNKNOWN_LOCAL_DEFID);
280278

281-
translated
282-
= new HIR::RangePattern (mapping, std::move (lower_bound),
283-
std::move (upper_bound), pattern.get_locus ());
279+
bool is_inclusive = (pattern.get_range_kind () == AST::RangeKind::INCLUDED);
280+
281+
translated = new HIR::RangePattern (mapping, std::move (lower_bound),
282+
std::move (upper_bound),
283+
pattern.get_locus (), is_inclusive);
284284
}
285285

286286
void

gcc/rust/hir/tree/rust-hir-pattern.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ class RangePattern : public Pattern
350350
/* location only stored to avoid a dereference - lower pattern should give
351351
* correct location so maybe change in future */
352352
location_t locus;
353+
bool is_inclusive;
353354
Analysis::NodeMapping mappings;
354355

355356
public:
@@ -359,18 +360,18 @@ class RangePattern : public Pattern
359360
RangePattern (Analysis::NodeMapping mappings,
360361
std::unique_ptr<RangePatternBound> lower,
361362
std::unique_ptr<RangePatternBound> upper, location_t locus,
362-
bool has_ellipsis_syntax = false)
363+
bool is_inclusive, bool has_ellipsis_syntax = false)
363364
: lower (std::move (lower)), upper (std::move (upper)),
364365
has_ellipsis_syntax (has_ellipsis_syntax), locus (locus),
365-
mappings (mappings)
366+
is_inclusive (is_inclusive), mappings (mappings)
366367
{}
367368

368369
// Copy constructor with clone
369370
RangePattern (RangePattern const &other)
370371
: lower (other.lower->clone_range_pattern_bound ()),
371372
upper (other.upper->clone_range_pattern_bound ()),
372373
has_ellipsis_syntax (other.has_ellipsis_syntax), locus (other.locus),
373-
mappings (other.mappings)
374+
is_inclusive (other.is_inclusive), mappings (other.mappings)
374375
{}
375376

376377
// Overloaded assignment operator to clone
@@ -380,6 +381,7 @@ class RangePattern : public Pattern
380381
upper = other.upper->clone_range_pattern_bound ();
381382
has_ellipsis_syntax = other.has_ellipsis_syntax;
382383
locus = other.locus;
384+
is_inclusive = other.is_inclusive;
383385
mappings = other.mappings;
384386

385387
return *this;
@@ -395,6 +397,7 @@ class RangePattern : public Pattern
395397
void accept_vis (HIRPatternVisitor &vis) override;
396398

397399
bool get_has_ellipsis_syntax () { return has_ellipsis_syntax; };
400+
bool is_inclusive_range () const { return is_inclusive; }
398401

399402
const Analysis::NodeMapping &get_mappings () const override final
400403
{
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// { dg-do compile }
2+
// { dg-options "-fsyntax-only" }
3+
4+
#![feature(exclusive_range_pattern)]
5+
6+
fn Foo() {
7+
let x = 1u32;
8+
9+
match x {
10+
3..-1 => 4,
11+
};
12+
}

0 commit comments

Comments
 (0)