Skip to content

Commit 2ce9b57

Browse files
authored
Rollup merge of rust-lang#110714 - cjgillot:reveal-consts, r=oli-obk
Normalize types and consts in MIR opts. Some passes were using a non-RevealAll param_env, which is needlessly restrictive in mir-opts. As a drive-by, we normalize all constants, since just normalizing their types is not enough.
2 parents 3ecae29 + 4fe5136 commit 2ce9b57

8 files changed

+23
-9
lines changed

compiler/rustc_mir_transform/src/dataflow_const_prop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ impl<'tcx> std::fmt::Debug for ScalarTy<'tcx> {
323323

324324
impl<'a, 'tcx> ConstAnalysis<'a, 'tcx> {
325325
pub fn new(tcx: TyCtxt<'tcx>, body: &'a Body<'tcx>, map: Map) -> Self {
326-
let param_env = tcx.param_env(body.source.def_id());
326+
let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id());
327327
Self {
328328
map,
329329
tcx,

compiler/rustc_mir_transform/src/large_enums.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ impl EnumSizeOpt {
120120
fn optim<'tcx>(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
121121
let mut alloc_cache = FxHashMap::default();
122122
let body_did = body.source.def_id();
123-
let param_env = tcx.param_env(body_did);
123+
let param_env = tcx.param_env_reveal_all_normalized(body_did);
124124

125125
let blocks = body.basic_blocks.as_mut();
126126
let local_decls = &mut body.local_decls;

compiler/rustc_mir_transform/src/match_branches.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<'tcx> MirPass<'tcx> for MatchBranchSimplification {
4646

4747
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
4848
let def_id = body.source.def_id();
49-
let param_env = tcx.param_env(def_id);
49+
let param_env = tcx.param_env_reveal_all_normalized(def_id);
5050

5151
let bbs = body.basic_blocks.as_mut();
5252
let mut should_cleanup = false;

compiler/rustc_mir_transform/src/reveal_all.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,23 @@ impl<'tcx> MutVisitor<'tcx> for RevealAllVisitor<'tcx> {
3434
self.tcx
3535
}
3636

37+
#[inline]
38+
fn visit_constant(&mut self, constant: &mut Constant<'tcx>, _: Location) {
39+
// We have to use `try_normalize_erasing_regions` here, since it's
40+
// possible that we visit impossible-to-satisfy where clauses here,
41+
// see #91745
42+
if let Ok(c) = self.tcx.try_normalize_erasing_regions(self.param_env, constant.literal) {
43+
constant.literal = c;
44+
}
45+
}
46+
3747
#[inline]
3848
fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _: TyContext) {
3949
// We have to use `try_normalize_erasing_regions` here, since it's
4050
// possible that we visit impossible-to-satisfy where clauses here,
4151
// see #91745
42-
*ty = self.tcx.try_normalize_erasing_regions(self.param_env, *ty).unwrap_or(*ty);
52+
if let Ok(t) = self.tcx.try_normalize_erasing_regions(self.param_env, *ty) {
53+
*ty = t;
54+
}
4355
}
4456
}

compiler/rustc_mir_transform/src/shim.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ fn build_thread_local_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'t
355355
fn build_clone_shim<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, self_ty: Ty<'tcx>) -> Body<'tcx> {
356356
debug!("build_clone_shim(def_id={:?})", def_id);
357357

358-
let param_env = tcx.param_env(def_id);
358+
let param_env = tcx.param_env_reveal_all_normalized(def_id);
359359

360360
let mut builder = CloneShimBuilder::new(tcx, def_id, self_ty);
361361
let is_copy = self_ty.is_copy_modulo_regions(tcx, param_env);
@@ -836,7 +836,7 @@ fn build_call_shim<'tcx>(
836836
pub fn build_adt_ctor(tcx: TyCtxt<'_>, ctor_id: DefId) -> Body<'_> {
837837
debug_assert!(tcx.is_constructor(ctor_id));
838838

839-
let param_env = tcx.param_env(ctor_id);
839+
let param_env = tcx.param_env_reveal_all_normalized(ctor_id);
840840

841841
// Normalize the sig.
842842
let sig = tcx

compiler/rustc_mir_transform/src/simplify_branches.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl<'tcx> MirPass<'tcx> for SimplifyConstCondition {
1616
}
1717

1818
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
19-
let param_env = tcx.param_env(body.source.def_id());
19+
let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id());
2020
for block in body.basic_blocks_mut() {
2121
let terminator = block.terminator_mut();
2222
terminator.kind = match terminator.kind {

compiler/rustc_mir_transform/src/simplify_comparison_integral.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl<'tcx> MirPass<'tcx> for SimplifyComparisonIntegral {
3737
let opts = helper.find_optimizations();
3838
let mut storage_deads_to_insert = vec![];
3939
let mut storage_deads_to_remove: Vec<(usize, BasicBlock)> = vec![];
40-
let param_env = tcx.param_env(body.source.def_id());
40+
let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id());
4141
for opt in opts {
4242
trace!("SUCCESS: Applying {:?}", opt);
4343
// replace terminator with a switchInt that switches on the integer directly

compiler/rustc_mir_transform/src/uninhabited_enum_branching.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ impl<'tcx> MirPass<'tcx> for UninhabitedEnumBranching {
109109
continue;
110110
};
111111

112-
let layout = tcx.layout_of(tcx.param_env(body.source.def_id()).and(discriminant_ty));
112+
let layout = tcx.layout_of(
113+
tcx.param_env_reveal_all_normalized(body.source.def_id()).and(discriminant_ty),
114+
);
113115

114116
let allowed_variants = if let Ok(layout) = layout {
115117
variant_discriminants(&layout, discriminant_ty, tcx)

0 commit comments

Comments
 (0)