Skip to content

Commit f4f957d

Browse files
committed
Clear the ParamEnv where its information is irrelevant
1 parent 7710820 commit f4f957d

File tree

7 files changed

+43
-35
lines changed

7 files changed

+43
-35
lines changed

src/librustc/ty/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2357,7 +2357,7 @@ impl<'tcx> AdtDef {
23572357

23582358
#[inline]
23592359
pub fn eval_explicit_discr(&self, tcx: TyCtxt<'tcx>, expr_did: DefId) -> Option<Discr<'tcx>> {
2360-
let param_env = ParamEnv::empty();
2360+
let param_env = tcx.param_env(expr_did);
23612361
let repr_type = self.repr.discr_type();
23622362
let substs = InternalSubsts::identity_for_item(tcx.global_tcx(), expr_did);
23632363
let instance = ty::Instance::new(expr_did, substs);
@@ -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.try_eval_bits(tcx.global_tcx(), param_env.and(val.ty)) {
2371+
if let Some(b) = val.try_eval_bits(tcx.global_tcx(), param_env, val.ty) {
23722372
trace!("discriminants: {} ({:?})", b, repr_type);
23732373
Some(Discr {
23742374
val: b,

src/librustc/ty/sty.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,21 +2294,25 @@ impl<'tcx> Const<'tcx> {
22942294
pub fn try_eval_bits(
22952295
&self,
22962296
tcx: TyCtxt<'tcx>,
2297-
ty: ParamEnvAnd<'tcx, Ty<'tcx>>,
2297+
param_env: ParamEnv<'tcx>,
2298+
ty: Ty<'tcx>,
22982299
) -> Option<u128> {
2299-
assert_eq!(self.ty, ty.value);
2300-
let size = tcx.layout_of(ty).ok()?.size;
2300+
assert_eq!(self.ty, ty);
2301+
let size = tcx.layout_of(param_env.with_reveal_all().and(ty)).ok()?.size;
23012302
match self.val {
23022303
// FIXME(const_generics): this doesn't work right now,
23032304
// because it tries to relate an `Infer` to a `Param`.
23042305
ConstValue::Unevaluated(did, substs) => {
2305-
let substs = tcx.lift_to_global(&substs).unwrap();
2306-
let instance = ty::Instance::resolve(tcx, ty.param_env, did, substs)?;
2306+
// if `substs` has no unresolved components, use and empty param_env
2307+
let pem_and_substs = param_env.with_reveal_all().and(substs);
2308+
let (param_env, substs) = tcx.lift_to_global(&pem_and_substs).unwrap().into_parts();
2309+
// try to resolve e.g. associated constants to their definition on an impl
2310+
let instance = ty::Instance::resolve(tcx, param_env, did, substs)?;
23072311
let gid = GlobalId {
23082312
instance,
23092313
promoted: None,
23102314
};
2311-
let evaluated = tcx.const_eval(ty.param_env.and(gid)).ok()?;
2315+
let evaluated = tcx.const_eval(param_env.and(gid)).ok()?;
23122316
evaluated.val.try_to_bits(size)
23132317
},
23142318
// FIXME(const_generics): try to evaluate generic consts with a given param env?
@@ -2319,7 +2323,7 @@ impl<'tcx> Const<'tcx> {
23192323

23202324
#[inline]
23212325
pub fn try_eval_bool(&self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> Option<bool> {
2322-
self.try_eval_bits(tcx, param_env.and(tcx.types.bool)).and_then(|v| match v {
2326+
self.try_eval_bits(tcx, param_env, tcx.types.bool).and_then(|v| match v {
23232327
0 => Some(false),
23242328
1 => Some(true),
23252329
_ => None,
@@ -2328,18 +2332,18 @@ impl<'tcx> Const<'tcx> {
23282332

23292333
#[inline]
23302334
pub fn try_eval_usize(&self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> Option<u64> {
2331-
self.try_eval_bits(tcx, param_env.and(tcx.types.usize)).map(|v| v as u64)
2335+
self.try_eval_bits(tcx, param_env, tcx.types.usize).map(|v| v as u64)
23322336
}
23332337

23342338
#[inline]
2335-
pub fn eval_bits(&self, tcx: TyCtxt<'tcx>, ty: ParamEnvAnd<'tcx, Ty<'tcx>>) -> u128 {
2336-
self.try_eval_bits(tcx, ty).unwrap_or_else(||
2339+
pub fn eval_bits(&self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>, ty: Ty<'tcx>) -> u128 {
2340+
self.try_eval_bits(tcx, param_env, ty).unwrap_or_else(||
23372341
bug!("expected bits of {:#?}, got {:#?}", ty, self))
23382342
}
23392343

23402344
#[inline]
23412345
pub fn eval_usize(&self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> u64 {
2342-
self.eval_bits(tcx, param_env.and(tcx.types.usize)) as u64
2346+
self.eval_bits(tcx, param_env, tcx.types.usize) as u64
23432347
}
23442348
}
23452349

src/librustc_codegen_utils/symbol_names/v0.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> {
512512
}
513513
self = ct.ty.print(self)?;
514514

515-
if let Some(bits) = ct.try_eval_bits(self.tcx, ty::ParamEnv::reveal_all().and(ct.ty)) {
515+
if let Some(bits) = ct.try_eval_bits(self.tcx, ty::ParamEnv::reveal_all(), ct.ty) {
516516
let _ = write!(self.out, "{:x}_", bits);
517517
} else {
518518
// NOTE(eddyb) despite having the path, we need to

src/librustc_mir/build/matches/test.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
112112
indices.entry(value)
113113
.or_insert_with(|| {
114114
options.push(value.eval_bits(
115-
self.hir.tcx(), self.hir.param_env.and(switch_ty),
115+
self.hir.tcx(), self.hir.param_env, switch_ty,
116116
));
117117
options.len() - 1
118118
});
@@ -655,10 +655,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
655655
use rustc::hir::RangeEnd::*;
656656

657657
let tcx = self.hir.tcx();
658-
let test_ty = self.hir.param_env.and(test.ty);
659658

660-
let lo = compare_const_vals(tcx, test.lo, pat.hi, test_ty)?;
661-
let hi = compare_const_vals(tcx, test.hi, pat.lo, test_ty)?;
659+
let lo = compare_const_vals(tcx, test.lo, pat.hi, self.hir.param_env, test.ty)?;
660+
let hi = compare_const_vals(tcx, test.hi, pat.lo, self.hir.param_env, test.ty)?;
662661

663662
match (test.end, pat.end, lo, hi) {
664663
// pat < test
@@ -775,8 +774,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
775774

776775
let tcx = self.hir.tcx();
777776

778-
let a = compare_const_vals(tcx, range.lo, value, self.hir.param_env.and(range.ty))?;
779-
let b = compare_const_vals(tcx, value, range.hi, self.hir.param_env.and(range.ty))?;
777+
let a = compare_const_vals(tcx, range.lo, value, self.hir.param_env, range.ty)?;
778+
let b = compare_const_vals(tcx, value, range.hi, self.hir.param_env, range.ty)?;
780779

781780
match (b, range.end) {
782781
(Less, _) |

src/librustc_mir/hair/pattern/_match.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@ impl<'tcx> IntRange<'tcx> {
860860
}
861861
ConstantValue(val) if is_integral(val.ty) => {
862862
let ty = val.ty;
863-
if let Some(val) = val.try_eval_bits(tcx, param_env.and(ty)) {
863+
if let Some(val) = val.try_eval_bits(tcx, param_env, ty) {
864864
let bias = IntRange::signed_bias(tcx, ty);
865865
let val = val ^ bias;
866866
Some(IntRange { range: val..=val, ty })
@@ -881,8 +881,8 @@ impl<'tcx> IntRange<'tcx> {
881881
match pat.kind {
882882
box PatternKind::Constant { value } => break ConstantValue(value),
883883
box PatternKind::Range(PatternRange { lo, hi, ty, end }) => break ConstantRange(
884-
lo.eval_bits(tcx, param_env.and(ty)),
885-
hi.eval_bits(tcx, param_env.and(ty)),
884+
lo.eval_bits(tcx, param_env, ty),
885+
hi.eval_bits(tcx, param_env, ty),
886886
ty,
887887
end,
888888
),
@@ -1341,8 +1341,8 @@ fn pat_constructors<'tcx>(cx: &mut MatchCheckCtxt<'_, 'tcx>,
13411341
PatternKind::Constant { value } => Some(vec![ConstantValue(value)]),
13421342
PatternKind::Range(PatternRange { lo, hi, ty, end }) =>
13431343
Some(vec![ConstantRange(
1344-
lo.eval_bits(cx.tcx, cx.param_env.and(ty)),
1345-
hi.eval_bits(cx.tcx, cx.param_env.and(ty)),
1344+
lo.eval_bits(cx.tcx, cx.param_env, ty),
1345+
hi.eval_bits(cx.tcx, cx.param_env, ty),
13461346
ty,
13471347
end,
13481348
)]),
@@ -1480,7 +1480,7 @@ fn slice_pat_covered_by_const<'tcx>(
14801480
{
14811481
match pat.kind {
14821482
box PatternKind::Constant { value } => {
1483-
let b = value.eval_bits(tcx, param_env.and(pat.ty));
1483+
let b = value.eval_bits(tcx, param_env, pat.ty);
14841484
assert_eq!(b as u8 as u128, b);
14851485
if b as u8 != *ch {
14861486
return Ok(false);
@@ -1660,9 +1660,9 @@ fn constructor_covered_by_range<'tcx>(
16601660
_ => bug!("`constructor_covered_by_range` called with {:?}", pat),
16611661
};
16621662
trace!("constructor_covered_by_range {:#?}, {:#?}, {:#?}, {}", ctor, from, to, ty);
1663-
let cmp_from = |c_from| compare_const_vals(tcx, c_from, from, param_env.and(ty))
1663+
let cmp_from = |c_from| compare_const_vals(tcx, c_from, from, param_env, ty)
16641664
.map(|res| res != Ordering::Less);
1665-
let cmp_to = |c_to| compare_const_vals(tcx, c_to, to, param_env.and(ty));
1665+
let cmp_to = |c_to| compare_const_vals(tcx, c_to, to, param_env, ty);
16661666
macro_rules! some_or_ok {
16671667
($e:expr) => {
16681668
match $e {

src/librustc_mir/hair/pattern/mod.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,8 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
446446
self.tcx,
447447
lo,
448448
hi,
449-
self.param_env.and(ty),
449+
self.param_env,
450+
ty,
450451
);
451452
match (end, cmp) {
452453
(RangeEnd::Excluded, Some(Ordering::Less)) =>
@@ -1452,7 +1453,8 @@ pub fn compare_const_vals<'tcx>(
14521453
tcx: TyCtxt<'tcx>,
14531454
a: &'tcx ty::Const<'tcx>,
14541455
b: &'tcx ty::Const<'tcx>,
1455-
ty: ty::ParamEnvAnd<'tcx, Ty<'tcx>>,
1456+
param_env: ty::ParamEnv<'tcx>,
1457+
ty: Ty<'tcx>,
14561458
) -> Option<Ordering> {
14571459
trace!("compare_const_vals: {:?}, {:?}", a, b);
14581460

@@ -1467,13 +1469,16 @@ pub fn compare_const_vals<'tcx>(
14671469
let fallback = || from_bool(a == b);
14681470

14691471
// Use the fallback if any type differs
1470-
if a.ty != b.ty || a.ty != ty.value {
1472+
if a.ty != b.ty || a.ty != ty {
14711473
return fallback();
14721474
}
14731475

1474-
if let (Some(a), Some(b)) = (a.try_eval_bits(tcx, ty), b.try_eval_bits(tcx, ty)) {
1476+
let a_bits = a.try_eval_bits(tcx, param_env, ty);
1477+
let b_bits = b.try_eval_bits(tcx, param_env, ty);
1478+
1479+
if let (Some(a), Some(b)) = (a_bits, b_bits) {
14751480
use ::rustc_apfloat::Float;
1476-
return match ty.value.sty {
1481+
return match ty.sty {
14771482
ty::Float(ast::FloatTy::F32) => {
14781483
let l = ::rustc_apfloat::ieee::Single::from_bits(a);
14791484
let r = ::rustc_apfloat::ieee::Single::from_bits(b);
@@ -1496,7 +1501,7 @@ pub fn compare_const_vals<'tcx>(
14961501
}
14971502
}
14981503

1499-
if let ty::Str = ty.value.sty {
1504+
if let ty::Str = ty.sty {
15001505
match (a.val, b.val) {
15011506
(
15021507
ConstValue::Slice { data: alloc_a, start: offset_a, end: end_a },

src/librustc_mir/transform/simplify_branches.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl MirPass for SimplifyBranches {
2727
TerminatorKind::SwitchInt {
2828
discr: Operand::Constant(ref c), switch_ty, ref values, ref targets, ..
2929
} => {
30-
let constant = c.literal.try_eval_bits(tcx, param_env.and(switch_ty));
30+
let constant = c.literal.try_eval_bits(tcx, param_env, switch_ty);
3131
if let Some(constant) = constant {
3232
let (otherwise, targets) = targets.split_last().unwrap();
3333
let mut ret = TerminatorKind::Goto { target: *otherwise };

0 commit comments

Comments
 (0)