Skip to content

Commit 437f017

Browse files
committed
Clean up the ty::Const::assert* methods
1 parent 4be0675 commit 437f017

File tree

6 files changed

+30
-50
lines changed

6 files changed

+30
-50
lines changed

src/librustc/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2368,7 +2368,7 @@ impl<'tcx> AdtDef {
23682368
match tcx.const_eval(param_env.and(cid)) {
23692369
Ok(val) => {
23702370
// FIXME: Find the right type and use it instead of `val.ty` here
2371-
if let Some(b) = val.assert_bits(tcx.global_tcx(), param_env.and(val.ty)) {
2371+
if let Some(b) = val.assert_bits(tcx.global_tcx(), val.ty) {
23722372
trace!("discriminants: {} ({:?})", b, repr_type);
23732373
Some(Discr {
23742374
val: b,

src/librustc/ty/sty.rs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::ty::{self, AdtDef, Discr, DefIdTree, TypeFlags, Ty, TyCtxt, TypeFolda
1515
use crate::ty::{List, TyS, ParamEnvAnd, ParamEnv};
1616
use crate::ty::layout::VariantIdx;
1717
use crate::util::captures::Captures;
18-
use crate::mir::interpret::{Scalar, Pointer};
18+
use crate::mir::interpret::Scalar;
1919

2020
use smallvec::SmallVec;
2121
use std::borrow::Cow;
@@ -2291,29 +2291,16 @@ impl<'tcx> Const<'tcx> {
22912291
}
22922292

22932293
#[inline]
2294-
pub fn to_bits(&self, tcx: TyCtxt<'tcx>, ty: ParamEnvAnd<'tcx, Ty<'tcx>>) -> Option<u128> {
2295-
if self.ty != ty.value {
2296-
return None;
2297-
}
2298-
let size = tcx.layout_of(ty).ok()?.size;
2299-
self.val.try_to_bits(size)
2300-
}
2301-
2302-
#[inline]
2303-
pub fn to_ptr(&self) -> Option<Pointer> {
2304-
self.val.try_to_ptr()
2305-
}
2306-
2307-
#[inline]
2308-
pub fn assert_bits(&self, tcx: TyCtxt<'tcx>, ty: ParamEnvAnd<'tcx, Ty<'tcx>>) -> Option<u128> {
2309-
assert_eq!(self.ty, ty.value);
2310-
let size = tcx.layout_of(ty).ok()?.size;
2294+
pub fn assert_bits(&self, tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<u128> {
2295+
assert_eq!(self.ty, ty);
2296+
let ty = tcx.lift_to_global(&ty).unwrap();
2297+
let size = tcx.layout_of(ParamEnv::empty().and(ty)).ok()?.size;
23112298
self.val.try_to_bits(size)
23122299
}
23132300

23142301
#[inline]
23152302
pub fn assert_bool(&self, tcx: TyCtxt<'tcx>) -> Option<bool> {
2316-
self.assert_bits(tcx, ParamEnv::empty().and(tcx.types.bool)).and_then(|v| match v {
2303+
self.assert_bits(tcx, tcx.types.bool).and_then(|v| match v {
23172304
0 => Some(false),
23182305
1 => Some(true),
23192306
_ => None,
@@ -2322,19 +2309,18 @@ impl<'tcx> Const<'tcx> {
23222309

23232310
#[inline]
23242311
pub fn assert_usize(&self, tcx: TyCtxt<'tcx>) -> Option<u64> {
2325-
self.assert_bits(tcx, ParamEnv::empty().and(tcx.types.usize)).map(|v| v as u64)
2312+
self.assert_bits(tcx, tcx.types.usize).map(|v| v as u64)
23262313
}
23272314

23282315
#[inline]
2329-
pub fn unwrap_bits(&self, tcx: TyCtxt<'tcx>, ty: ParamEnvAnd<'tcx, Ty<'tcx>>) -> u128 {
2316+
pub fn unwrap_bits(&self, tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> u128 {
23302317
self.assert_bits(tcx, ty).unwrap_or_else(||
2331-
bug!("expected bits of {}, got {:#?}", ty.value, self))
2318+
bug!("expected bits of {}, got {:#?}", ty, self))
23322319
}
23332320

23342321
#[inline]
23352322
pub fn unwrap_usize(&self, tcx: TyCtxt<'tcx>) -> u64 {
2336-
self.assert_usize(tcx).unwrap_or_else(||
2337-
bug!("expected constant usize, got {:#?}", self))
2323+
self.unwrap_bits(tcx, tcx.types.usize) as u64
23382324
}
23392325
}
23402326

src/librustc_mir/build/matches/test.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
109109

110110
match *match_pair.pattern.kind {
111111
PatternKind::Constant { value } => {
112-
let switch_ty = ty::ParamEnv::empty().and(switch_ty);
113112
indices.entry(value)
114113
.or_insert_with(|| {
115114
options.push(value.unwrap_bits(self.hir.tcx(), switch_ty));
@@ -653,11 +652,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
653652
use std::cmp::Ordering::*;
654653
use rustc::hir::RangeEnd::*;
655654

656-
let param_env = ty::ParamEnv::empty().and(test.ty);
657655
let tcx = self.hir.tcx();
658656

659-
let lo = compare_const_vals(tcx, test.lo, pat.hi, param_env)?;
660-
let hi = compare_const_vals(tcx, test.hi, pat.lo, param_env)?;
657+
let lo = compare_const_vals(tcx, test.lo, pat.hi, test.ty)?;
658+
let hi = compare_const_vals(tcx, test.hi, pat.lo, test.ty)?;
661659

662660
match (test.end, pat.end, lo, hi) {
663661
// pat < test
@@ -772,11 +770,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
772770
) -> Option<bool> {
773771
use std::cmp::Ordering::*;
774772

775-
let param_env = ty::ParamEnv::empty().and(range.ty);
776773
let tcx = self.hir.tcx();
777774

778-
let a = compare_const_vals(tcx, range.lo, value, param_env)?;
779-
let b = compare_const_vals(tcx, value, range.hi, param_env)?;
775+
let a = compare_const_vals(tcx, range.lo, value, range.ty)?;
776+
let b = compare_const_vals(tcx, value, range.hi, range.ty)?;
780777

781778
match (b, range.end) {
782779
(Less, _) |

src/librustc_mir/hair/pattern/_match.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ impl<'tcx> IntRange<'tcx> {
856856
}
857857
ConstantValue(val) if is_integral(val.ty) => {
858858
let ty = val.ty;
859-
if let Some(val) = val.assert_bits(tcx, ty::ParamEnv::empty().and(ty)) {
859+
if let Some(val) = val.assert_bits(tcx, ty) {
860860
let bias = IntRange::signed_bias(tcx, ty);
861861
let val = val ^ bias;
862862
Some(IntRange { range: val..=val, ty })
@@ -873,8 +873,8 @@ impl<'tcx> IntRange<'tcx> {
873873
match pat.kind {
874874
box PatternKind::Constant { value } => break ConstantValue(value),
875875
box PatternKind::Range(PatternRange { lo, hi, ty, end }) => break ConstantRange(
876-
lo.to_bits(tcx, ty::ParamEnv::empty().and(ty)).unwrap(),
877-
hi.to_bits(tcx, ty::ParamEnv::empty().and(ty)).unwrap(),
876+
lo.unwrap_bits(tcx, ty),
877+
hi.unwrap_bits(tcx, ty),
878878
ty,
879879
end,
880880
),
@@ -1327,8 +1327,8 @@ fn pat_constructors<'tcx>(cx: &mut MatchCheckCtxt<'_, 'tcx>,
13271327
PatternKind::Constant { value } => Some(vec![ConstantValue(value)]),
13281328
PatternKind::Range(PatternRange { lo, hi, ty, end }) =>
13291329
Some(vec![ConstantRange(
1330-
lo.to_bits(cx.tcx, ty::ParamEnv::empty().and(ty)).unwrap(),
1331-
hi.to_bits(cx.tcx, ty::ParamEnv::empty().and(ty)).unwrap(),
1330+
lo.unwrap_bits(cx.tcx, ty),
1331+
hi.unwrap_bits(cx.tcx, ty),
13321332
ty,
13331333
end,
13341334
)]),
@@ -1464,7 +1464,7 @@ fn slice_pat_covered_by_const<'tcx>(
14641464
{
14651465
match pat.kind {
14661466
box PatternKind::Constant { value } => {
1467-
let b = value.unwrap_bits(tcx, ty::ParamEnv::empty().and(pat.ty));
1467+
let b = value.unwrap_bits(tcx, pat.ty);
14681468
assert_eq!(b as u8 as u128, b);
14691469
if b as u8 != *ch {
14701470
return Ok(false);
@@ -1641,9 +1641,9 @@ fn constructor_covered_by_range<'tcx>(
16411641
_ => bug!("`constructor_covered_by_range` called with {:?}", pat),
16421642
};
16431643
trace!("constructor_covered_by_range {:#?}, {:#?}, {:#?}, {}", ctor, from, to, ty);
1644-
let cmp_from = |c_from| compare_const_vals(tcx, c_from, from, ty::ParamEnv::empty().and(ty))
1644+
let cmp_from = |c_from| compare_const_vals(tcx, c_from, from, ty)
16451645
.map(|res| res != Ordering::Less);
1646-
let cmp_to = |c_to| compare_const_vals(tcx, c_to, to, ty::ParamEnv::empty().and(ty));
1646+
let cmp_to = |c_to| compare_const_vals(tcx, c_to, to, ty);
16471647
macro_rules! some_or_ok {
16481648
($e:expr) => {
16491649
match $e {

src/librustc_mir/hair/pattern/mod.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
446446
self.tcx,
447447
lo,
448448
hi,
449-
self.param_env.and(ty),
449+
ty,
450450
);
451451
match (end, cmp) {
452452
(RangeEnd::Excluded, Some(Ordering::Less)) =>
@@ -1451,7 +1451,7 @@ pub fn compare_const_vals<'tcx>(
14511451
tcx: TyCtxt<'tcx>,
14521452
a: &'tcx ty::Const<'tcx>,
14531453
b: &'tcx ty::Const<'tcx>,
1454-
ty: ty::ParamEnvAnd<'tcx, Ty<'tcx>>,
1454+
ty: Ty<'tcx>,
14551455
) -> Option<Ordering> {
14561456
trace!("compare_const_vals: {:?}, {:?}", a, b);
14571457

@@ -1466,15 +1466,13 @@ pub fn compare_const_vals<'tcx>(
14661466
let fallback = || from_bool(a == b);
14671467

14681468
// Use the fallback if any type differs
1469-
if a.ty != b.ty || a.ty != ty.value {
1469+
if a.ty != b.ty || a.ty != ty {
14701470
return fallback();
14711471
}
14721472

1473-
// FIXME: This should use assert_bits(ty) instead of use_bits
1474-
// but triggers possibly bugs due to mismatching of arrays and slices
1475-
if let (Some(a), Some(b)) = (a.to_bits(tcx, ty), b.to_bits(tcx, ty)) {
1473+
if let (Some(a), Some(b)) = (a.assert_bits(tcx, ty), b.assert_bits(tcx, ty)) {
14761474
use ::rustc_apfloat::Float;
1477-
return match ty.value.sty {
1475+
return match ty.sty {
14781476
ty::Float(ast::FloatTy::F32) => {
14791477
let l = ::rustc_apfloat::ieee::Single::from_bits(a);
14801478
let r = ::rustc_apfloat::ieee::Single::from_bits(b);
@@ -1497,7 +1495,7 @@ pub fn compare_const_vals<'tcx>(
14971495
}
14981496
}
14991497

1500-
if let ty::Str = ty.value.sty {
1498+
if let ty::Str = ty.sty {
15011499
match (a.val, b.val) {
15021500
(
15031501
ConstValue::Slice { data: alloc_a, start: offset_a, end: end_a },

src/librustc_mir/transform/simplify_branches.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! A pass that simplifies branches when their condition is known.
22
3-
use rustc::ty::{TyCtxt, ParamEnv};
3+
use rustc::ty::TyCtxt;
44
use rustc::mir::*;
55
use crate::transform::{MirPass, MirSource};
66

@@ -26,7 +26,6 @@ impl MirPass for SimplifyBranches {
2626
TerminatorKind::SwitchInt {
2727
discr: Operand::Constant(ref c), switch_ty, ref values, ref targets, ..
2828
} => {
29-
let switch_ty = ParamEnv::empty().and(switch_ty);
3029
let constant = c.literal.assert_bits(tcx, switch_ty);
3130
if let Some(constant) = constant {
3231
let (otherwise, targets) = targets.split_last().unwrap();

0 commit comments

Comments
 (0)