Skip to content

Commit 059bbd9

Browse files
committed
Add common struct for range
1 parent 2c1b1c2 commit 059bbd9

File tree

6 files changed

+48
-69
lines changed

6 files changed

+48
-69
lines changed

src/librustc_mir/build/matches/mod.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use build::{BlockAnd, BlockAndExtension, Builder};
1919
use build::{GuardFrame, GuardFrameLocal, LocalsForNode};
2020
use hair::*;
2121
use hair::pattern::PatternTypeProjections;
22-
use rustc::hir;
2322
use rustc::mir::*;
2423
use rustc::ty::{self, Ty};
2524
use rustc::ty::layout::VariantIdx;
@@ -681,12 +680,7 @@ enum TestKind<'tcx> {
681680
},
682681

683682
// test whether the value falls within an inclusive or exclusive range
684-
Range {
685-
lo: &'tcx ty::Const<'tcx>,
686-
hi: &'tcx ty::Const<'tcx>,
687-
ty: Ty<'tcx>,
688-
end: hir::RangeEnd,
689-
},
683+
Range(PatternRange<'tcx>),
690684

691685
// test length of the slice is equal to len
692686
Len {

src/librustc_mir/build/matches/simplify.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
107107
Err(match_pair)
108108
}
109109

110-
PatternKind::Range { lo, hi, ty, end } => {
110+
PatternKind::Range(PatternRange { lo, hi, ty, end }) => {
111111
let range = match ty.sty {
112112
ty::Char => {
113113
Some(('\u{0000}' as u128, '\u{10FFFF}' as u128, Size::from_bits(32)))

src/librustc_mir/build/matches/test.rs

+24-42
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,11 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
7272
}
7373
}
7474

75-
PatternKind::Range { lo, hi, ty, end } => {
76-
assert!(ty == match_pair.pattern.ty);
75+
PatternKind::Range(range) => {
76+
assert!(range.ty == match_pair.pattern.ty);
7777
Test {
7878
span: match_pair.pattern.span,
79-
kind: TestKind::Range {
80-
lo,
81-
hi,
82-
ty,
83-
end,
84-
},
79+
kind: TestKind::Range(range),
8580
}
8681
}
8782

@@ -137,9 +132,9 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
137132
PatternKind::Variant { .. } => {
138133
panic!("you should have called add_variants_to_switch instead!");
139134
}
140-
PatternKind::Range { ty, lo, hi, end } => {
135+
PatternKind::Range(range) => {
141136
// Check that none of the switch values are in the range.
142-
self.values_not_contained_in_range(ty, lo, hi, end, indices)
137+
self.values_not_contained_in_range(range, indices)
143138
.unwrap_or(false)
144139
}
145140
PatternKind::Slice { .. } |
@@ -381,7 +376,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
381376
}
382377
}
383378

384-
TestKind::Range { ref lo, ref hi, ty, ref end } => {
379+
TestKind::Range(PatternRange { ref lo, ref hi, ty, ref end }) => {
385380
// Test `val` by computing `lo <= val && val <= hi`, using primitive comparisons.
386381
let lo = self.literal_operand(test.span, ty.clone(), lo.clone());
387382
let hi = self.literal_operand(test.span, ty.clone(), hi.clone());
@@ -536,9 +531,9 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
536531
}
537532

538533
(&TestKind::SwitchInt { switch_ty: _, ref options, ref indices },
539-
&PatternKind::Range { ty, lo, hi, end }) => {
534+
&PatternKind::Range(range)) => {
540535
let not_contained = self
541-
.values_not_contained_in_range(ty, lo, hi, end, indices)
536+
.values_not_contained_in_range(range, indices)
542537
.unwrap_or(false);
543538

544539
if not_contained {
@@ -630,12 +625,9 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
630625
}
631626
}
632627

633-
(&TestKind::Range {
634-
lo: test_lo, hi: test_hi, ty: test_ty, end: test_end,
635-
}, &PatternKind::Range {
636-
lo: pat_lo, hi: pat_hi, ty: _, end: pat_end,
637-
}) => {
638-
if (test_lo, test_hi, test_end) == (pat_lo, pat_hi, pat_end) {
628+
(&TestKind::Range(test),
629+
&PatternKind::Range(pat)) => {
630+
if test == pat {
639631
resulting_candidates[0]
640632
.push(self.candidate_without_match_pair(
641633
match_pair_index,
@@ -648,13 +640,13 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
648640
use std::cmp::Ordering::*;
649641
use rustc::hir::RangeEnd::*;
650642

651-
let param_env = ty::ParamEnv::empty().and(test_ty);
643+
let param_env = ty::ParamEnv::empty().and(test.ty);
652644
let tcx = self.hir.tcx();
653645

654-
let lo = compare_const_vals(tcx, test_lo, pat_hi, param_env)?;
655-
let hi = compare_const_vals(tcx, test_hi, pat_lo, param_env)?;
646+
let lo = compare_const_vals(tcx, test.lo, pat.hi, param_env)?;
647+
let hi = compare_const_vals(tcx, test.hi, pat.lo, param_env)?;
656648

657-
match (test_end, pat_end, lo, hi) {
649+
match (test.end, pat.end, lo, hi) {
658650
// pat < test
659651
(_, _, Greater, _) |
660652
(_, Excluded, Equal, _) |
@@ -675,12 +667,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
675667
}
676668
}
677669

678-
(&TestKind::Range {
679-
lo, hi, ty, end
680-
}, &PatternKind::Constant {
681-
ref value
682-
}) => {
683-
if self.const_range_contains(ty, lo, hi, end, value) == Some(false) {
670+
(&TestKind::Range(range), &PatternKind::Constant { ref value }) => {
671+
if self.const_range_contains(range, value) == Some(false) {
684672
// `value` is not contained in the testing range,
685673
// so `value` can be matched only if this test fails.
686674
resulting_candidates[1].push(candidate.clone());
@@ -807,21 +795,18 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
807795

808796
fn const_range_contains(
809797
&self,
810-
ty: Ty<'tcx>,
811-
lo: &'tcx ty::Const<'tcx>,
812-
hi: &'tcx ty::Const<'tcx>,
813-
end: RangeEnd,
798+
range: PatternRange<'tcx>,
814799
value: &'tcx ty::Const<'tcx>,
815800
) -> Option<bool> {
816801
use std::cmp::Ordering::*;
817802

818-
let param_env = ty::ParamEnv::empty().and(ty);
803+
let param_env = ty::ParamEnv::empty().and(range.ty);
819804
let tcx = self.hir.tcx();
820805

821-
let a = compare_const_vals(tcx, lo, value, param_env)?;
822-
let b = compare_const_vals(tcx, value, hi, param_env)?;
806+
let a = compare_const_vals(tcx, range.lo, value, param_env)?;
807+
let b = compare_const_vals(tcx, value, range.hi, param_env)?;
823808

824-
match (b, end) {
809+
match (b, range.end) {
825810
(Less, _) |
826811
(Equal, RangeEnd::Included) if a != Greater => Some(true),
827812
_ => Some(false),
@@ -830,14 +815,11 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
830815

831816
fn values_not_contained_in_range(
832817
&self,
833-
ty: Ty<'tcx>,
834-
lo: &'tcx ty::Const<'tcx>,
835-
hi: &'tcx ty::Const<'tcx>,
836-
end: RangeEnd,
818+
range: PatternRange<'tcx>,
837819
indices: &FxHashMap<&'tcx ty::Const<'tcx>, usize>,
838820
) -> Option<bool> {
839821
for val in indices.keys() {
840-
if self.const_range_contains(ty, lo, hi, end, val)? {
822+
if self.const_range_contains(range, val)? {
841823
return Some(false);
842824
}
843825
}

src/librustc_mir/hair/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub mod cx;
2929
mod constant;
3030

3131
pub mod pattern;
32-
pub use self::pattern::{BindingMode, Pattern, PatternKind, FieldPattern};
32+
pub use self::pattern::{BindingMode, Pattern, PatternKind, PatternRange, FieldPattern};
3333
pub(crate) use self::pattern::{PatternTypeProjection, PatternTypeProjections};
3434

3535
mod util;

src/librustc_mir/hair/pattern/_match.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ use self::WitnessPreference::*;
173173
use rustc_data_structures::fx::FxHashMap;
174174
use rustc_data_structures::indexed_vec::Idx;
175175

176-
use super::{FieldPattern, Pattern, PatternKind};
176+
use super::{FieldPattern, Pattern, PatternKind, PatternRange};
177177
use super::{PatternFoldable, PatternFolder, compare_const_vals};
178178

179179
use rustc::hir::def_id::DefId;
@@ -554,12 +554,12 @@ impl<'tcx> Witness<'tcx> {
554554
_ => {
555555
match *ctor {
556556
ConstantValue(value) => PatternKind::Constant { value },
557-
ConstantRange(lo, hi, ty, end) => PatternKind::Range {
557+
ConstantRange(lo, hi, ty, end) => PatternKind::Range(PatternRange {
558558
lo: ty::Const::from_bits(cx.tcx, lo, ty::ParamEnv::empty().and(ty)),
559559
hi: ty::Const::from_bits(cx.tcx, hi, ty::ParamEnv::empty().and(ty)),
560560
ty,
561561
end,
562-
},
562+
}),
563563
_ => PatternKind::Wild,
564564
}
565565
}
@@ -820,7 +820,7 @@ impl<'tcx> IntRange<'tcx> {
820820
-> Option<IntRange<'tcx>> {
821821
Self::from_ctor(tcx, &match pat.kind {
822822
box PatternKind::Constant { value } => ConstantValue(value),
823-
box PatternKind::Range { lo, hi, ty, end } => ConstantRange(
823+
box PatternKind::Range(PatternRange { lo, hi, ty, end }) => ConstantRange(
824824
lo.to_bits(tcx, ty::ParamEnv::empty().and(ty)).unwrap(),
825825
hi.to_bits(tcx, ty::ParamEnv::empty().and(ty)).unwrap(),
826826
ty,
@@ -1259,7 +1259,7 @@ fn pat_constructors<'tcx>(cx: &mut MatchCheckCtxt<'_, 'tcx>,
12591259
Some(vec![Variant(adt_def.variants[variant_index].did)])
12601260
}
12611261
PatternKind::Constant { value } => Some(vec![ConstantValue(value)]),
1262-
PatternKind::Range { lo, hi, ty, end } =>
1262+
PatternKind::Range(PatternRange { lo, hi, ty, end }) =>
12631263
Some(vec![ConstantRange(
12641264
lo.to_bits(cx.tcx, ty::ParamEnv::empty().and(ty)).unwrap(),
12651265
hi.to_bits(cx.tcx, ty::ParamEnv::empty().and(ty)).unwrap(),
@@ -1556,7 +1556,7 @@ fn constructor_covered_by_range<'a, 'tcx>(
15561556
) -> Result<bool, ErrorReported> {
15571557
let (from, to, end, ty) = match pat.kind {
15581558
box PatternKind::Constant { value } => (value, value, RangeEnd::Included, value.ty),
1559-
box PatternKind::Range { lo, hi, ty, end } => (lo, hi, end, ty),
1559+
box PatternKind::Range(PatternRange { lo, hi, end, ty }) => (lo, hi, end, ty),
15601560
_ => bug!("`constructor_covered_by_range` called with {:?}", pat),
15611561
};
15621562
trace!("constructor_covered_by_range {:#?}, {:#?}, {:#?}, {}", ctor, from, to, ty);

src/librustc_mir/hair/pattern/mod.rs

+15-12
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,7 @@ pub enum PatternKind<'tcx> {
219219
value: &'tcx ty::Const<'tcx>,
220220
},
221221

222-
Range {
223-
lo: &'tcx ty::Const<'tcx>,
224-
hi: &'tcx ty::Const<'tcx>,
225-
ty: Ty<'tcx>,
226-
end: RangeEnd,
227-
},
222+
Range(PatternRange<'tcx>),
228223

229224
/// matches against a slice, checking the length and extracting elements.
230225
/// irrefutable when there is a slice pattern and both `prefix` and `suffix` are empty.
@@ -243,6 +238,14 @@ pub enum PatternKind<'tcx> {
243238
},
244239
}
245240

241+
#[derive(Clone, Copy, Debug, PartialEq)]
242+
pub struct PatternRange<'tcx> {
243+
pub lo: &'tcx ty::Const<'tcx>,
244+
pub hi: &'tcx ty::Const<'tcx>,
245+
pub ty: Ty<'tcx>,
246+
pub end: RangeEnd,
247+
}
248+
246249
impl<'tcx> fmt::Display for Pattern<'tcx> {
247250
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
248251
match *self.kind {
@@ -354,7 +357,7 @@ impl<'tcx> fmt::Display for Pattern<'tcx> {
354357
PatternKind::Constant { value } => {
355358
fmt_const_val(f, value)
356359
}
357-
PatternKind::Range { lo, hi, ty: _, end } => {
360+
PatternKind::Range(PatternRange { lo, hi, ty: _, end }) => {
358361
fmt_const_val(f, lo)?;
359362
match end {
360363
RangeEnd::Included => write!(f, "..=")?,
@@ -483,7 +486,7 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
483486
);
484487
match (end, cmp) {
485488
(RangeEnd::Excluded, Some(Ordering::Less)) =>
486-
PatternKind::Range { lo, hi, ty, end },
489+
PatternKind::Range(PatternRange { lo, hi, ty, end }),
487490
(RangeEnd::Excluded, _) => {
488491
span_err!(
489492
self.tcx.sess,
@@ -497,7 +500,7 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
497500
PatternKind::Constant { value: lo }
498501
}
499502
(RangeEnd::Included, Some(Ordering::Less)) => {
500-
PatternKind::Range { lo, hi, ty, end }
503+
PatternKind::Range(PatternRange { lo, hi, ty, end })
501504
}
502505
(RangeEnd::Included, _) => {
503506
let mut err = struct_span_err!(
@@ -1177,17 +1180,17 @@ impl<'tcx> PatternFoldable<'tcx> for PatternKind<'tcx> {
11771180
} => PatternKind::Constant {
11781181
value: value.fold_with(folder)
11791182
},
1180-
PatternKind::Range {
1183+
PatternKind::Range(PatternRange {
11811184
lo,
11821185
hi,
11831186
ty,
11841187
end,
1185-
} => PatternKind::Range {
1188+
}) => PatternKind::Range(PatternRange {
11861189
lo: lo.fold_with(folder),
11871190
hi: hi.fold_with(folder),
11881191
ty: ty.fold_with(folder),
11891192
end,
1190-
},
1193+
}),
11911194
PatternKind::Slice {
11921195
ref prefix,
11931196
ref slice,

0 commit comments

Comments
 (0)