Skip to content

Commit 7e0441d

Browse files
authored
Rollup merge of #78969 - tmiasko:normalize, r=davidtwco
Normalize function type during validation During inlining, the callee body is normalized and has types revealed, but some of locals corresponding to the arguments might come from the caller body which is not. As a result the caller body does not pass validation without additional normalization. Closes #78442.
2 parents 3fe2aba + 99be78d commit 7e0441d

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

Diff for: compiler/rustc_mir/src/transform/validate.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ pub struct Validator {
3838
impl<'tcx> MirPass<'tcx> for Validator {
3939
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
4040
let def_id = body.source.def_id();
41-
let param_env = tcx.param_env(def_id);
41+
// We need to param_env_reveal_all_normalized, as some optimizations
42+
// change types in ways that require unfolding opaque types.
43+
let param_env = tcx.param_env_reveal_all_normalized(def_id);
4244
let mir_phase = self.mir_phase;
4345

4446
let always_live_locals = AlwaysLiveLocals::new(body);
@@ -79,7 +81,6 @@ pub fn equal_up_to_regions(
7981
}
8082

8183
// Normalize lifetimes away on both sides, then compare.
82-
let param_env = param_env.with_reveal_all_normalized(tcx);
8384
let normalize = |ty: Ty<'tcx>| {
8485
tcx.normalize_erasing_regions(
8586
param_env,
@@ -167,17 +168,14 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
167168
return true;
168169
}
169170
// Normalize projections and things like that.
170-
// FIXME: We need to reveal_all, as some optimizations change types in ways
171-
// that require unfolding opaque types.
172-
let param_env = self.param_env.with_reveal_all_normalized(self.tcx);
173-
let src = self.tcx.normalize_erasing_regions(param_env, src);
174-
let dest = self.tcx.normalize_erasing_regions(param_env, dest);
171+
let src = self.tcx.normalize_erasing_regions(self.param_env, src);
172+
let dest = self.tcx.normalize_erasing_regions(self.param_env, dest);
175173

176174
// Type-changing assignments can happen when subtyping is used. While
177175
// all normal lifetimes are erased, higher-ranked types with their
178176
// late-bound lifetimes are still around and can lead to type
179177
// differences. So we compare ignoring lifetimes.
180-
equal_up_to_regions(self.tcx, param_env, src, dest)
178+
equal_up_to_regions(self.tcx, self.param_env, src, dest)
181179
}
182180
}
183181

@@ -358,6 +356,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
358356
}
359357
TerminatorKind::Call { func, args, destination, cleanup, .. } => {
360358
let func_ty = func.ty(&self.body.local_decls, self.tcx);
359+
let func_ty = self.tcx.normalize_erasing_regions(self.param_env, func_ty);
361360
match func_ty.kind() {
362361
ty::FnPtr(..) | ty::FnDef(..) => {}
363362
_ => self.fail(

Diff for: src/test/ui/issues/issue-50865-private-impl-trait/auxiliary/lib.rs

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
// revisions: default miropt
2-
//[miropt]compile-flags: -Z mir-opt-level=2
3-
// ~^ This flag is for #77668, it used to be ICE.
4-
51
#![crate_type = "lib"]
62

73
pub fn bar<P>( // Error won't happen if "bar" is not generic

Diff for: src/test/ui/mir/mir-inlining/ice-issue-77306-1.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
1-
// run-pass
1+
// Regression test for various issues related to normalization & inlining.
2+
// * #68347, #77306, #77668 - missed normalization during inlining.
3+
// * #78442 - missed normalization in validator after inlining.
4+
//
5+
// build-pass
26
// compile-flags:-Zmir-opt-level=2
37

4-
// Previously ICEd because we did not normalize during inlining,
5-
// see https://github.com/rust-lang/rust/pull/77306 for more discussion.
6-
78
pub fn write() {
89
create()()
910
}
1011

12+
pub fn write_generic<T>(_t: T) {
13+
hide()();
14+
}
15+
1116
pub fn create() -> impl FnOnce() {
1217
|| ()
1318
}
1419

20+
pub fn hide() -> impl Fn() {
21+
write
22+
}
23+
1524
fn main() {
1625
write();
26+
write_generic(());
1727
}

0 commit comments

Comments
 (0)