Skip to content

Commit 8a83409

Browse files
committed
Use built-in comparisons for range matching in MIR.
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.
1 parent 6e2a64b commit 8a83409

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-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
}

0 commit comments

Comments
 (0)