rustc_borrowck/
renumber.rs

1use rustc_index::IndexSlice;
2use rustc_infer::infer::NllRegionVariableOrigin;
3use rustc_middle::mir::visit::{MutVisitor, TyContext};
4use rustc_middle::mir::{Body, ConstOperand, Location, Promoted};
5use rustc_middle::ty::{self, GenericArgsRef, Ty, TyCtxt, TypeFoldable, fold_regions};
6use rustc_span::Symbol;
7use tracing::{debug, instrument};
8
9use crate::BorrowckInferCtxt;
10
11/// Replaces all free regions appearing in the MIR with fresh
12/// inference variables, returning the number of variables created.
13#[instrument(skip(infcx, body, promoted), level = "debug")]
14pub(crate) fn renumber_mir<'tcx>(
15    infcx: &BorrowckInferCtxt<'tcx>,
16    body: &mut Body<'tcx>,
17    promoted: &mut IndexSlice<Promoted, Body<'tcx>>,
18) {
19    debug!(?body.arg_count);
20
21    let mut renumberer = RegionRenumberer { infcx };
22
23    for body in promoted.iter_mut() {
24        renumberer.visit_body(body);
25    }
26
27    renumberer.visit_body(body);
28}
29
30// The fields are used only for debugging output in `sccs_info`.
31#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
32pub(crate) enum RegionCtxt {
33    Location(Location),
34    TyContext(TyContext),
35    Free(Symbol),
36    LateBound(Symbol),
37    Existential(Option<Symbol>),
38    Placeholder(Symbol),
39    Unknown,
40}
41
42impl RegionCtxt {
43    /// Used to determine the representative of a component in the strongly connected
44    /// constraint graph
45    pub(crate) fn preference_value(self) -> usize {
46        match self {
47            RegionCtxt::Unknown => 1,
48            RegionCtxt::Existential(None) => 2,
49            RegionCtxt::Existential(Some(_)) | RegionCtxt::Free(_) => 2,
50            RegionCtxt::Location(_) => 3,
51            RegionCtxt::TyContext(_) => 4,
52            _ => 5,
53        }
54    }
55}
56
57struct RegionRenumberer<'a, 'tcx> {
58    infcx: &'a BorrowckInferCtxt<'tcx>,
59}
60
61impl<'a, 'tcx> RegionRenumberer<'a, 'tcx> {
62    /// Replaces all regions appearing in `value` with fresh inference
63    /// variables.
64    fn renumber_regions<T, F>(&mut self, value: T, region_ctxt_fn: F) -> T
65    where
66        T: TypeFoldable<TyCtxt<'tcx>>,
67        F: Fn() -> RegionCtxt,
68    {
69        let origin = NllRegionVariableOrigin::Existential { from_forall: false };
70        fold_regions(self.infcx.tcx, value, |_region, _depth| {
71            self.infcx.next_nll_region_var(origin, || region_ctxt_fn())
72        })
73    }
74}
75
76impl<'a, 'tcx> MutVisitor<'tcx> for RegionRenumberer<'a, 'tcx> {
77    fn tcx(&self) -> TyCtxt<'tcx> {
78        self.infcx.tcx
79    }
80
81    #[instrument(skip(self), level = "debug")]
82    fn visit_ty(&mut self, ty: &mut Ty<'tcx>, ty_context: TyContext) {
83        if matches!(ty_context, TyContext::ReturnTy(_)) {
84            // We will renumber the return ty when called again with `TyContext::LocalDecl`
85            return;
86        }
87        *ty = self.renumber_regions(*ty, || RegionCtxt::TyContext(ty_context));
88
89        debug!(?ty);
90    }
91
92    #[instrument(skip(self), level = "debug")]
93    fn visit_args(&mut self, args: &mut GenericArgsRef<'tcx>, location: Location) {
94        *args = self.renumber_regions(*args, || RegionCtxt::Location(location));
95
96        debug!(?args);
97    }
98
99    #[instrument(skip(self), level = "debug")]
100    fn visit_region(&mut self, region: &mut ty::Region<'tcx>, location: Location) {
101        let old_region = *region;
102        *region = self.renumber_regions(old_region, || RegionCtxt::Location(location));
103
104        debug!(?region);
105    }
106
107    #[instrument(skip(self), level = "debug")]
108    fn visit_ty_const(&mut self, ct: &mut ty::Const<'tcx>, location: Location) {
109        let old_ct = *ct;
110        *ct = self.renumber_regions(old_ct, || RegionCtxt::Location(location));
111
112        debug!(?ct);
113    }
114
115    #[instrument(skip(self), level = "debug")]
116    fn visit_const_operand(&mut self, constant: &mut ConstOperand<'tcx>, location: Location) {
117        let const_ = constant.const_;
118        constant.const_ = self.renumber_regions(const_, || RegionCtxt::Location(location));
119        debug!("constant: {:#?}", constant);
120    }
121}