rustc_mir_transform/
post_analysis_normalize.rs1use 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 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 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 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 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}