Skip to main content

rustc_mir_transform/
post_analysis_normalize.rs

1//! Normalizes MIR in `TypingMode::PostAnalysis` mode, most notably revealing
2//! its opaques. We also only normalize specializable associated items once in
3//! `PostAnalysis` mode.
4
5use rustc_middle::mir::visit::*;
6use rustc_middle::mir::*;
7use rustc_middle::ty::{self, Ty, TyCtxt};
8
9use crate::PassPolicy;
10
11pub(super) struct PostAnalysisNormalize;
12
13impl<'tcx> crate::MirPass<'tcx> for PostAnalysisNormalize {
14    fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
15        let typing_env = body.typing_env(tcx);
16        PostAnalysisNormalizeVisitor { tcx, typing_env }.visit_body_preserves_cfg(body);
17    }
18
19    fn policy(&self, _ctx: &crate::PassCtx<'_>) -> PassPolicy {
20        // Reveals opaque types and normalizes MIR while transitioning to the runtime dialect.
21        PassPolicy::Required
22    }
23}
24
25struct PostAnalysisNormalizeVisitor<'tcx> {
26    tcx: TyCtxt<'tcx>,
27    typing_env: ty::TypingEnv<'tcx>,
28}
29
30impl<'tcx> MutVisitor<'tcx> for PostAnalysisNormalizeVisitor<'tcx> {
31    #[inline]
32    fn tcx(&self) -> TyCtxt<'tcx> {
33        self.tcx
34    }
35
36    #[inline]
37    fn visit_place(
38        &mut self,
39        place: &mut Place<'tcx>,
40        _context: PlaceContext,
41        _location: Location,
42    ) {
43        if !self.tcx.next_trait_solver_globally() {
44            // `OpaqueCast` projections are only needed if there are opaque types on which projections
45            // are performed. After the `PostAnalysisNormalize` pass, all opaque types are replaced with their
46            // hidden types, so we don't need these projections anymore.
47            //
48            // Performance optimization: don't reintern if there is no `OpaqueCast` to remove.
49            if place.projection.iter().any(|elem| matches!(elem, ProjectionElem::OpaqueCast(_))) {
50                place.projection = self.tcx.mk_place_elems(
51                    &place
52                        .projection
53                        .into_iter()
54                        .filter(|elem| !matches!(elem, ProjectionElem::OpaqueCast(_)))
55                        .collect::<Vec<_>>(),
56                );
57            };
58        }
59        self.super_place(place, _context, _location);
60    }
61
62    #[inline]
63    fn visit_const_operand(&mut self, constant: &mut ConstOperand<'tcx>, location: Location) {
64        // We have to use `try_normalize_erasing_regions` here, since it's
65        // possible that we visit impossible-to-satisfy where clauses here,
66        // see #91745
67        if let Ok(c) = self.tcx.try_normalize_erasing_regions(
68            self.typing_env,
69            ty::set_aliases_to_non_rigid(self.tcx, constant.const_),
70        ) {
71            constant.const_ = c;
72        }
73        self.super_const_operand(constant, location);
74    }
75
76    #[inline]
77    fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _: TyContext) {
78        // We have to use `try_normalize_erasing_regions` here, since it's
79        // possible that we visit impossible-to-satisfy where clauses here,
80        // see #91745
81        if let Ok(t) = self.tcx.try_normalize_erasing_regions(
82            self.typing_env,
83            ty::set_aliases_to_non_rigid(self.tcx, *ty),
84        ) {
85            *ty = t;
86        }
87    }
88
89    #[inline]
90    fn visit_args(&mut self, args: &mut ty::GenericArgs<'tcx>, _: Location) {
91        if let Ok(a) = self.tcx.try_normalize_erasing_regions(
92            self.typing_env,
93            ty::set_aliases_to_non_rigid(self.tcx, *args),
94        ) {
95            *args = a;
96        }
97    }
98}