forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#85254 - cjgillot:reveal-mir, r=lcnr
Normalize MIR with RevealAll before optimizations. Fixes rust-lang#78442
- Loading branch information
Showing
7 changed files
with
207 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
//! Normalizes MIR in RevealAll mode. | ||
use crate::MirPass; | ||
use rustc_middle::mir::visit::*; | ||
use rustc_middle::mir::*; | ||
use rustc_middle::ty::{self, Ty, TyCtxt}; | ||
|
||
pub struct RevealAll; | ||
|
||
impl<'tcx> MirPass<'tcx> for RevealAll { | ||
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { | ||
// This pass must run before inlining, since we insert callee bodies in RevealAll mode. | ||
// Do not apply this transformation to generators. | ||
if (tcx.sess.mir_opt_level() >= 3 || !super::inline::is_enabled(tcx)) | ||
&& body.generator.is_none() | ||
{ | ||
let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id()); | ||
RevealAllVisitor { tcx, param_env }.visit_body(body); | ||
} | ||
} | ||
} | ||
|
||
struct RevealAllVisitor<'tcx> { | ||
tcx: TyCtxt<'tcx>, | ||
param_env: ty::ParamEnv<'tcx>, | ||
} | ||
|
||
impl<'tcx> MutVisitor<'tcx> for RevealAllVisitor<'tcx> { | ||
#[inline] | ||
fn tcx(&self) -> TyCtxt<'tcx> { | ||
self.tcx | ||
} | ||
|
||
#[inline] | ||
fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _: TyContext) { | ||
*ty = self.tcx.normalize_erasing_regions(self.param_env, ty); | ||
} | ||
|
||
#[inline] | ||
fn process_projection_elem( | ||
&mut self, | ||
elem: PlaceElem<'tcx>, | ||
_: Location, | ||
) -> Option<PlaceElem<'tcx>> { | ||
match elem { | ||
PlaceElem::Field(field, ty) => { | ||
let new_ty = self.tcx.normalize_erasing_regions(self.param_env, ty); | ||
if ty != new_ty { Some(PlaceElem::Field(field, new_ty)) } else { None } | ||
} | ||
// None of those contain a Ty. | ||
PlaceElem::Index(..) | ||
| PlaceElem::Deref | ||
| PlaceElem::ConstantIndex { .. } | ||
| PlaceElem::Subslice { .. } | ||
| PlaceElem::Downcast(..) => None, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// compile-flags: -Z mir-opt-level=3 -Z inline-mir | ||
// ignore-wasm32-bare compiled with panic=abort by default | ||
#![crate_type = "lib"] | ||
|
||
// EMIT_MIR issue_78442.bar.RevealAll.diff | ||
// EMIT_MIR issue_78442.bar.Inline.diff | ||
pub fn bar<P>( | ||
// Error won't happen if "bar" is not generic | ||
_baz: P, | ||
) { | ||
hide_foo()(); | ||
} | ||
|
||
fn hide_foo() -> impl Fn() { | ||
// Error won't happen if "iterate" hasn't impl Trait or has generics | ||
foo | ||
} | ||
|
||
fn foo() { // Error won't happen if "foo" isn't used in "iterate" or has generics | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
- // MIR for `bar` before Inline | ||
+ // MIR for `bar` after Inline | ||
|
||
fn bar(_1: P) -> () { | ||
debug _baz => _1; // in scope 0 at $DIR/issue-78442.rs:9:5: 9:9 | ||
let mut _0: (); // return place in scope 0 at $DIR/issue-78442.rs:10:3: 10:3 | ||
let _2: (); // in scope 0 at $DIR/issue-78442.rs:11:5: 11:17 | ||
let mut _3: &fn() {foo}; // in scope 0 at $DIR/issue-78442.rs:11:5: 11:15 | ||
let _4: fn() {foo}; // in scope 0 at $DIR/issue-78442.rs:11:5: 11:15 | ||
let mut _5: (); // in scope 0 at $DIR/issue-78442.rs:11:5: 11:17 | ||
+ scope 1 (inlined <fn() {foo} as Fn<()>>::call - shim(fn() {foo})) { // at $DIR/issue-78442.rs:11:5: 11:17 | ||
+ } | ||
|
||
bb0: { | ||
StorageLive(_2); // scope 0 at $DIR/issue-78442.rs:11:5: 11:17 | ||
StorageLive(_3); // scope 0 at $DIR/issue-78442.rs:11:5: 11:15 | ||
StorageLive(_4); // scope 0 at $DIR/issue-78442.rs:11:5: 11:15 | ||
- _4 = hide_foo() -> [return: bb1, unwind: bb4]; // scope 0 at $DIR/issue-78442.rs:11:5: 11:15 | ||
+ _4 = hide_foo() -> [return: bb1, unwind: bb3]; // scope 0 at $DIR/issue-78442.rs:11:5: 11:15 | ||
// mir::Constant | ||
// + span: $DIR/issue-78442.rs:11:5: 11:13 | ||
// + literal: Const { ty: fn() -> impl std::ops::Fn<()> {hide_foo}, val: Value(Scalar(<ZST>)) } | ||
} | ||
|
||
bb1: { | ||
_3 = &_4; // scope 0 at $DIR/issue-78442.rs:11:5: 11:15 | ||
StorageLive(_5); // scope 0 at $DIR/issue-78442.rs:11:5: 11:17 | ||
- _2 = <impl Fn<()> as Fn<()>>::call(move _3, move _5) -> [return: bb2, unwind: bb4]; // scope 0 at $DIR/issue-78442.rs:11:5: 11:17 | ||
- // mir::Constant | ||
- // + span: $DIR/issue-78442.rs:11:5: 11:15 | ||
- // + literal: Const { ty: for<'r> extern "rust-call" fn(&'r impl std::ops::Fn<()>, ()) -> <impl std::ops::Fn<()> as std::ops::FnOnce<()>>::Output {<impl std::ops::Fn<()> as std::ops::Fn<()>>::call}, val: Value(Scalar(<ZST>)) } | ||
+ _2 = move (*_3)() -> [return: bb5, unwind: bb3]; // scope 1 at $DIR/issue-78442.rs:11:5: 11:17 | ||
} | ||
|
||
bb2: { | ||
- StorageDead(_5); // scope 0 at $DIR/issue-78442.rs:11:16: 11:17 | ||
- StorageDead(_3); // scope 0 at $DIR/issue-78442.rs:11:16: 11:17 | ||
- StorageDead(_4); // scope 0 at $DIR/issue-78442.rs:11:17: 11:18 | ||
- StorageDead(_2); // scope 0 at $DIR/issue-78442.rs:11:17: 11:18 | ||
- _0 = const (); // scope 0 at $DIR/issue-78442.rs:10:3: 12:2 | ||
- drop(_1) -> [return: bb3, unwind: bb5]; // scope 0 at $DIR/issue-78442.rs:12:1: 12:2 | ||
+ return; // scope 0 at $DIR/issue-78442.rs:12:2: 12:2 | ||
} | ||
|
||
- bb3: { | ||
- return; // scope 0 at $DIR/issue-78442.rs:12:2: 12:2 | ||
+ bb3 (cleanup): { | ||
+ drop(_1) -> bb4; // scope 0 at $DIR/issue-78442.rs:12:1: 12:2 | ||
} | ||
|
||
bb4 (cleanup): { | ||
- drop(_1) -> bb5; // scope 0 at $DIR/issue-78442.rs:12:1: 12:2 | ||
+ resume; // scope 0 at $DIR/issue-78442.rs:7:1: 12:2 | ||
} | ||
|
||
- bb5 (cleanup): { | ||
- resume; // scope 0 at $DIR/issue-78442.rs:7:1: 12:2 | ||
+ bb5: { | ||
+ StorageDead(_5); // scope 0 at $DIR/issue-78442.rs:11:16: 11:17 | ||
+ StorageDead(_3); // scope 0 at $DIR/issue-78442.rs:11:16: 11:17 | ||
+ StorageDead(_4); // scope 0 at $DIR/issue-78442.rs:11:17: 11:18 | ||
+ StorageDead(_2); // scope 0 at $DIR/issue-78442.rs:11:17: 11:18 | ||
+ _0 = const (); // scope 0 at $DIR/issue-78442.rs:10:3: 12:2 | ||
+ drop(_1) -> [return: bb2, unwind: bb4]; // scope 0 at $DIR/issue-78442.rs:12:1: 12:2 | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
- // MIR for `bar` before RevealAll | ||
+ // MIR for `bar` after RevealAll | ||
|
||
fn bar(_1: P) -> () { | ||
debug _baz => _1; // in scope 0 at $DIR/issue-78442.rs:9:5: 9:9 | ||
let mut _0: (); // return place in scope 0 at $DIR/issue-78442.rs:10:3: 10:3 | ||
let _2: (); // in scope 0 at $DIR/issue-78442.rs:11:5: 11:17 | ||
- let mut _3: &impl std::ops::Fn<()>; // in scope 0 at $DIR/issue-78442.rs:11:5: 11:15 | ||
- let _4: impl std::ops::Fn<()>; // in scope 0 at $DIR/issue-78442.rs:11:5: 11:15 | ||
+ let mut _3: &fn() {foo}; // in scope 0 at $DIR/issue-78442.rs:11:5: 11:15 | ||
+ let _4: fn() {foo}; // in scope 0 at $DIR/issue-78442.rs:11:5: 11:15 | ||
let mut _5: (); // in scope 0 at $DIR/issue-78442.rs:11:5: 11:17 | ||
|
||
bb0: { | ||
StorageLive(_2); // scope 0 at $DIR/issue-78442.rs:11:5: 11:17 | ||
StorageLive(_3); // scope 0 at $DIR/issue-78442.rs:11:5: 11:15 | ||
StorageLive(_4); // scope 0 at $DIR/issue-78442.rs:11:5: 11:15 | ||
_4 = hide_foo() -> [return: bb1, unwind: bb4]; // scope 0 at $DIR/issue-78442.rs:11:5: 11:15 | ||
// mir::Constant | ||
// + span: $DIR/issue-78442.rs:11:5: 11:13 | ||
// + literal: Const { ty: fn() -> impl std::ops::Fn<()> {hide_foo}, val: Value(Scalar(<ZST>)) } | ||
} | ||
|
||
bb1: { | ||
_3 = &_4; // scope 0 at $DIR/issue-78442.rs:11:5: 11:15 | ||
StorageLive(_5); // scope 0 at $DIR/issue-78442.rs:11:5: 11:17 | ||
nop; // scope 0 at $DIR/issue-78442.rs:11:5: 11:17 | ||
_2 = <impl Fn<()> as Fn<()>>::call(move _3, move _5) -> [return: bb2, unwind: bb4]; // scope 0 at $DIR/issue-78442.rs:11:5: 11:17 | ||
// mir::Constant | ||
// + span: $DIR/issue-78442.rs:11:5: 11:15 | ||
// + literal: Const { ty: for<'r> extern "rust-call" fn(&'r impl std::ops::Fn<()>, ()) -> <impl std::ops::Fn<()> as std::ops::FnOnce<()>>::Output {<impl std::ops::Fn<()> as std::ops::Fn<()>>::call}, val: Value(Scalar(<ZST>)) } | ||
} | ||
|
||
bb2: { | ||
StorageDead(_5); // scope 0 at $DIR/issue-78442.rs:11:16: 11:17 | ||
StorageDead(_3); // scope 0 at $DIR/issue-78442.rs:11:16: 11:17 | ||
StorageDead(_4); // scope 0 at $DIR/issue-78442.rs:11:17: 11:18 | ||
StorageDead(_2); // scope 0 at $DIR/issue-78442.rs:11:17: 11:18 | ||
_0 = const (); // scope 0 at $DIR/issue-78442.rs:10:3: 12:2 | ||
drop(_1) -> [return: bb3, unwind: bb5]; // scope 0 at $DIR/issue-78442.rs:12:1: 12:2 | ||
} | ||
|
||
bb3: { | ||
return; // scope 0 at $DIR/issue-78442.rs:12:2: 12:2 | ||
} | ||
|
||
bb4 (cleanup): { | ||
drop(_1) -> bb5; // scope 0 at $DIR/issue-78442.rs:12:1: 12:2 | ||
} | ||
|
||
bb5 (cleanup): { | ||
resume; // scope 0 at $DIR/issue-78442.rs:7:1: 12:2 | ||
} | ||
} | ||
|