summary refs log tree commit diff
path: root/compiler/rustc_mir_transform/src/reveal_all.rs
blob: 6c423a2bb5756c79476adf6a8186781b61c29914 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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,
        }
    }
}