2020-03-29 17:19:48 +02:00
|
|
|
use rustc_index::vec::IndexVec;
|
2021-01-28 16:18:25 +09:00
|
|
|
use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin};
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::visit::{MutVisitor, TyContext};
|
2021-10-16 14:03:30 +02:00
|
|
|
use rustc_middle::mir::{Body, Location, Promoted};
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::ty::subst::SubstsRef;
|
|
|
|
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable};
|
2017-10-24 14:20:47 -04:00
|
|
|
|
|
|
|
/// Replaces all free regions appearing in the MIR with fresh
|
|
|
|
/// inference variables, returning the number of variables created.
|
2021-08-20 13:36:04 +00:00
|
|
|
#[instrument(skip(infcx, body, promoted), level = "debug")]
|
2019-08-14 08:08:17 -04:00
|
|
|
pub fn renumber_mir<'tcx>(
|
|
|
|
infcx: &InferCtxt<'_, 'tcx>,
|
2020-04-12 10:31:00 -07:00
|
|
|
body: &mut Body<'tcx>,
|
|
|
|
promoted: &mut IndexVec<Promoted, Body<'tcx>>,
|
2019-08-14 08:08:17 -04:00
|
|
|
) {
|
2021-08-20 13:36:04 +00:00
|
|
|
debug!(?body.arg_count);
|
2017-11-07 04:30:43 -05:00
|
|
|
|
2021-01-28 16:18:25 +09:00
|
|
|
let mut visitor = NllVisitor { infcx };
|
2019-08-15 06:39:31 -04:00
|
|
|
|
|
|
|
for body in promoted.iter_mut() {
|
|
|
|
visitor.visit_body(body);
|
|
|
|
}
|
|
|
|
|
2019-11-06 12:00:46 -05:00
|
|
|
visitor.visit_body(body);
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2017-12-10 10:23:45 -05:00
|
|
|
/// Replaces all regions appearing in `value` with fresh inference
|
|
|
|
/// variables.
|
2021-08-20 13:36:04 +00:00
|
|
|
#[instrument(skip(infcx), level = "debug")]
|
2020-10-24 02:21:18 +02:00
|
|
|
pub fn renumber_regions<'tcx, T>(infcx: &InferCtxt<'_, 'tcx>, value: T) -> T
|
2017-12-10 10:23:45 -05:00
|
|
|
where
|
|
|
|
T: TypeFoldable<'tcx>,
|
|
|
|
{
|
2022-06-27 15:55:03 +02:00
|
|
|
infcx.tcx.fold_regions(value, |_region, _depth| {
|
2021-01-28 16:18:25 +09:00
|
|
|
let origin = NllRegionVariableOrigin::Existential { from_forall: false };
|
2017-12-10 10:23:45 -05:00
|
|
|
infcx.next_nll_region_var(origin)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-28 16:18:25 +09:00
|
|
|
struct NllVisitor<'a, 'tcx> {
|
2019-06-14 00:48:52 +03:00
|
|
|
infcx: &'a InferCtxt<'a, 'tcx>,
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2021-01-28 16:18:25 +09:00
|
|
|
impl<'a, 'tcx> NllVisitor<'a, 'tcx> {
|
2020-10-24 02:21:18 +02:00
|
|
|
fn renumber_regions<T>(&mut self, value: T) -> T
|
2017-10-30 04:51:10 -04:00
|
|
|
where
|
|
|
|
T: TypeFoldable<'tcx>,
|
|
|
|
{
|
2018-07-22 19:42:40 +03:00
|
|
|
renumber_regions(self.infcx, value)
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
2019-08-04 16:20:21 -04:00
|
|
|
}
|
2019-01-17 20:03:58 +00:00
|
|
|
|
2021-01-28 16:18:25 +09:00
|
|
|
impl<'a, 'tcx> MutVisitor<'tcx> for NllVisitor<'a, 'tcx> {
|
2019-10-20 16:11:04 -04:00
|
|
|
fn tcx(&self) -> TyCtxt<'tcx> {
|
|
|
|
self.infcx.tcx
|
|
|
|
}
|
|
|
|
|
2021-08-20 13:36:04 +00:00
|
|
|
#[instrument(skip(self), level = "debug")]
|
2017-10-26 19:53:31 -04:00
|
|
|
fn visit_ty(&mut self, ty: &mut Ty<'tcx>, ty_context: TyContext) {
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 14:13:38 +11:00
|
|
|
*ty = self.renumber_regions(*ty);
|
2017-11-22 17:38:51 -05:00
|
|
|
|
2021-08-20 13:36:04 +00:00
|
|
|
debug!(?ty);
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2021-08-20 13:36:04 +00:00
|
|
|
#[instrument(skip(self), level = "debug")]
|
2019-02-09 22:11:53 +08:00
|
|
|
fn visit_substs(&mut self, substs: &mut SubstsRef<'tcx>, location: Location) {
|
2020-10-24 02:21:18 +02:00
|
|
|
*substs = self.renumber_regions(*substs);
|
2017-11-07 04:30:43 -05:00
|
|
|
|
2021-08-20 13:36:04 +00:00
|
|
|
debug!(?substs);
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2021-08-20 13:36:04 +00:00
|
|
|
#[instrument(skip(self), level = "debug")]
|
2017-11-07 04:30:43 -05:00
|
|
|
fn visit_region(&mut self, region: &mut ty::Region<'tcx>, location: Location) {
|
|
|
|
let old_region = *region;
|
2022-01-28 11:25:15 +11:00
|
|
|
*region = self.renumber_regions(old_region);
|
2017-11-07 04:30:43 -05:00
|
|
|
|
2021-08-20 13:36:04 +00:00
|
|
|
debug!(?region);
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2022-02-02 14:24:45 +11:00
|
|
|
fn visit_const(&mut self, constant: &mut ty::Const<'tcx>, _location: Location) {
|
|
|
|
*constant = self.renumber_regions(*constant);
|
2017-11-22 17:29:55 -05:00
|
|
|
}
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|