]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_transform/src/reveal_all.rs
Rollup merge of #89876 - AlexApps99:const_ops, r=oli-obk
[rust.git] / compiler / rustc_mir_transform / src / reveal_all.rs
1 //! Normalizes MIR in RevealAll mode.
2
3 use crate::MirPass;
4 use rustc_middle::mir::visit::*;
5 use rustc_middle::mir::*;
6 use rustc_middle::ty::{self, Ty, TyCtxt};
7
8 pub struct RevealAll;
9
10 impl<'tcx> MirPass<'tcx> for RevealAll {
11     fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
12         // This pass must run before inlining, since we insert callee bodies in RevealAll mode.
13         // Do not apply this transformation to generators.
14         if (tcx.sess.mir_opt_level() >= 3 || !super::inline::is_enabled(tcx))
15             && body.generator.is_none()
16         {
17             let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id());
18             RevealAllVisitor { tcx, param_env }.visit_body(body);
19         }
20     }
21 }
22
23 struct RevealAllVisitor<'tcx> {
24     tcx: TyCtxt<'tcx>,
25     param_env: ty::ParamEnv<'tcx>,
26 }
27
28 impl<'tcx> MutVisitor<'tcx> for RevealAllVisitor<'tcx> {
29     #[inline]
30     fn tcx(&self) -> TyCtxt<'tcx> {
31         self.tcx
32     }
33
34     #[inline]
35     fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _: TyContext) {
36         *ty = self.tcx.normalize_erasing_regions(self.param_env, ty);
37     }
38
39     #[inline]
40     fn process_projection_elem(
41         &mut self,
42         elem: PlaceElem<'tcx>,
43         _: Location,
44     ) -> Option<PlaceElem<'tcx>> {
45         match elem {
46             PlaceElem::Field(field, ty) => {
47                 let new_ty = self.tcx.normalize_erasing_regions(self.param_env, ty);
48                 if ty != new_ty { Some(PlaceElem::Field(field, new_ty)) } else { None }
49             }
50             // None of those contain a Ty.
51             PlaceElem::Index(..)
52             | PlaceElem::Deref
53             | PlaceElem::ConstantIndex { .. }
54             | PlaceElem::Subslice { .. }
55             | PlaceElem::Downcast(..) => None,
56         }
57     }
58 }