Skip to content

Commit 7f3201d

Browse files
committed
Auto merge of #30641 - tsion:match-range, r=eddyb
The previous version using `PartialOrd::le` was broken since it passed `T` arguments where `&T` was expected. It makes sense to use primitive comparisons since range patterns can only be used with chars and numeric types. r? @eddyb
2 parents f73c0a8 + 78526fc commit 7f3201d

File tree

3 files changed

+59
-23
lines changed

3 files changed

+59
-23
lines changed

src/librustc_mir/build/matches/test.rs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -185,28 +185,16 @@ impl<'a,'tcx> Builder<'a,'tcx> {
185185
}
186186

187187
TestKind::Range { ref lo, ref hi, ty } => {
188-
// Test `v` by computing `PartialOrd::le(lo, v) && PartialOrd::le(v, hi)`.
188+
// Test `val` by computing `lo <= val && val <= hi`, using primitive comparisons.
189189
let lo = self.literal_operand(test.span, ty.clone(), lo.clone());
190190
let hi = self.literal_operand(test.span, ty.clone(), hi.clone());
191-
let item_ref = self.hir.partial_le(ty);
191+
let val = Operand::Consume(lvalue.clone());
192192

193-
let lo_blocks = self.call_comparison_fn(block,
194-
test.span,
195-
item_ref.clone(),
196-
lo,
197-
Operand::Consume(lvalue.clone()));
193+
let fail = self.cfg.start_new_block();
194+
let block = self.compare(block, fail, test.span, BinOp::Le, lo, val.clone());
195+
let block = self.compare(block, fail, test.span, BinOp::Le, val, hi);
198196

199-
let hi_blocks = self.call_comparison_fn(lo_blocks[0],
200-
test.span,
201-
item_ref,
202-
Operand::Consume(lvalue.clone()),
203-
hi);
204-
205-
let failure = self.cfg.start_new_block();
206-
self.cfg.terminate(lo_blocks[1], Terminator::Goto { target: failure });
207-
self.cfg.terminate(hi_blocks[1], Terminator::Goto { target: failure });
208-
209-
vec![hi_blocks[0], failure]
197+
vec![block, fail]
210198
}
211199

212200
TestKind::Len { len, op } => {
@@ -240,6 +228,29 @@ impl<'a,'tcx> Builder<'a,'tcx> {
240228
}
241229
}
242230

231+
fn compare(&mut self,
232+
block: BasicBlock,
233+
fail_block: BasicBlock,
234+
span: Span,
235+
op: BinOp,
236+
left: Operand<'tcx>,
237+
right: Operand<'tcx>) -> BasicBlock {
238+
let bool_ty = self.hir.bool_ty();
239+
let result = self.temp(bool_ty);
240+
241+
// result = op(left, right)
242+
self.cfg.push_assign(block, span, &result, Rvalue::BinaryOp(op, left, right));
243+
244+
// branch based on result
245+
let target_block = self.cfg.start_new_block();
246+
self.cfg.terminate(block, Terminator::If {
247+
cond: Operand::Consume(result),
248+
targets: (target_block, fail_block)
249+
});
250+
251+
target_block
252+
}
253+
243254
fn call_comparison_fn(&mut self,
244255
block: BasicBlock,
245256
span: Span,

src/librustc_mir/hair/cx/mod.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,6 @@ impl<'a,'tcx:'a> Cx<'a, 'tcx> {
8888
self.cmp_method_ref(eq_def_id, "eq", ty)
8989
}
9090

91-
pub fn partial_le(&mut self, ty: Ty<'tcx>) -> ItemRef<'tcx> {
92-
let ord_def_id = self.tcx.lang_items.ord_trait().unwrap();
93-
self.cmp_method_ref(ord_def_id, "le", ty)
94-
}
95-
9691
pub fn num_variants(&mut self, adt_def: ty::AdtDef<'tcx>) -> usize {
9792
adt_def.variants.len()
9893
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(rustc_attrs)]
12+
13+
#[rustc_mir]
14+
pub fn foo(x: i8) -> i32 {
15+
match x {
16+
1...10 => 0,
17+
_ => 1,
18+
}
19+
}
20+
21+
fn main() {
22+
assert_eq!(foo(0), 1);
23+
assert_eq!(foo(1), 0);
24+
assert_eq!(foo(2), 0);
25+
assert_eq!(foo(5), 0);
26+
assert_eq!(foo(9), 0);
27+
assert_eq!(foo(10), 0);
28+
assert_eq!(foo(11), 1);
29+
assert_eq!(foo(20), 1);
30+
}

0 commit comments

Comments
 (0)