2022-09-14 15:07:19 +00:00
|
|
|
#![deny(rustc::untranslatable_diagnostic)]
|
|
|
|
#![deny(rustc::diagnostic_outside_of_impl)]
|
2020-03-29 15:19:48 +00:00
|
|
|
use rustc_index::vec::IndexVec;
|
2021-01-28 07:18:25 +00:00
|
|
|
use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin};
|
2020-03-29 14:41:09 +00:00
|
|
|
use rustc_middle::mir::visit::{MutVisitor, TyContext};
|
2022-09-27 09:59:25 +00:00
|
|
|
use rustc_middle::mir::Constant;
|
2021-10-16 12:03:30 +00:00
|
|
|
use rustc_middle::mir::{Body, Location, Promoted};
|
2020-03-29 14:41:09 +00:00
|
|
|
use rustc_middle::ty::subst::SubstsRef;
|
|
|
|
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable};
|
2017-10-24 18:20:47 +00: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 12:08:17 +00:00
|
|
|
pub fn renumber_mir<'tcx>(
|
2022-09-09 18:01:06 +00:00
|
|
|
infcx: &InferCtxt<'tcx>,
|
2020-04-12 17:31:00 +00:00
|
|
|
body: &mut Body<'tcx>,
|
|
|
|
promoted: &mut IndexVec<Promoted, Body<'tcx>>,
|
2019-08-14 12:08:17 +00:00
|
|
|
) {
|
2021-08-20 13:36:04 +00:00
|
|
|
debug!(?body.arg_count);
|
2017-11-07 09:30:43 +00:00
|
|
|
|
2021-01-28 07:18:25 +00:00
|
|
|
let mut visitor = NllVisitor { infcx };
|
2019-08-15 10:39:31 +00:00
|
|
|
|
|
|
|
for body in promoted.iter_mut() {
|
|
|
|
visitor.visit_body(body);
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:00:46 +00:00
|
|
|
visitor.visit_body(body);
|
2017-10-24 18:20:47 +00:00
|
|
|
}
|
|
|
|
|
2017-12-10 15:23:45 +00:00
|
|
|
/// Replaces all regions appearing in `value` with fresh inference
|
|
|
|
/// variables.
|
2021-08-20 13:36:04 +00:00
|
|
|
#[instrument(skip(infcx), level = "debug")]
|
2022-09-09 18:01:06 +00:00
|
|
|
pub fn renumber_regions<'tcx, T>(infcx: &InferCtxt<'tcx>, value: T) -> T
|
2017-12-10 15:23:45 +00:00
|
|
|
where
|
|
|
|
T: TypeFoldable<'tcx>,
|
|
|
|
{
|
2022-06-27 13:55:03 +00:00
|
|
|
infcx.tcx.fold_regions(value, |_region, _depth| {
|
2021-01-28 07:18:25 +00:00
|
|
|
let origin = NllRegionVariableOrigin::Existential { from_forall: false };
|
2017-12-10 15:23:45 +00:00
|
|
|
infcx.next_nll_region_var(origin)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-28 07:18:25 +00:00
|
|
|
struct NllVisitor<'a, 'tcx> {
|
2022-09-09 18:01:06 +00:00
|
|
|
infcx: &'a InferCtxt<'tcx>,
|
2017-10-24 18:20:47 +00:00
|
|
|
}
|
|
|
|
|
2021-01-28 07:18:25 +00:00
|
|
|
impl<'a, 'tcx> NllVisitor<'a, 'tcx> {
|
2020-10-24 00:21:18 +00:00
|
|
|
fn renumber_regions<T>(&mut self, value: T) -> T
|
2017-10-30 08:51:10 +00:00
|
|
|
where
|
|
|
|
T: TypeFoldable<'tcx>,
|
|
|
|
{
|
2018-07-22 16:42:40 +00:00
|
|
|
renumber_regions(self.infcx, value)
|
2017-10-24 18:20:47 +00:00
|
|
|
}
|
2019-08-04 20:20:21 +00:00
|
|
|
}
|
2019-01-17 20:03:58 +00:00
|
|
|
|
2021-01-28 07:18:25 +00:00
|
|
|
impl<'a, 'tcx> MutVisitor<'tcx> for NllVisitor<'a, 'tcx> {
|
2019-10-20 20:11:04 +00: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 23:53:31 +00: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 03:13:38 +00:00
|
|
|
*ty = self.renumber_regions(*ty);
|
2017-11-22 22:38:51 +00:00
|
|
|
|
2021-08-20 13:36:04 +00:00
|
|
|
debug!(?ty);
|
2017-10-24 18:20:47 +00:00
|
|
|
}
|
|
|
|
|
2021-08-20 13:36:04 +00:00
|
|
|
#[instrument(skip(self), level = "debug")]
|
2019-02-09 14:11:53 +00:00
|
|
|
fn visit_substs(&mut self, substs: &mut SubstsRef<'tcx>, location: Location) {
|
2020-10-24 00:21:18 +00:00
|
|
|
*substs = self.renumber_regions(*substs);
|
2017-11-07 09:30:43 +00:00
|
|
|
|
2021-08-20 13:36:04 +00:00
|
|
|
debug!(?substs);
|
2017-10-24 18:20:47 +00:00
|
|
|
}
|
|
|
|
|
2021-08-20 13:36:04 +00:00
|
|
|
#[instrument(skip(self), level = "debug")]
|
2017-11-07 09:30:43 +00:00
|
|
|
fn visit_region(&mut self, region: &mut ty::Region<'tcx>, location: Location) {
|
|
|
|
let old_region = *region;
|
2022-01-28 00:25:15 +00:00
|
|
|
*region = self.renumber_regions(old_region);
|
2017-11-07 09:30:43 +00:00
|
|
|
|
2021-08-20 13:36:04 +00:00
|
|
|
debug!(?region);
|
2017-10-24 18:20:47 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 15:38:01 +00:00
|
|
|
#[instrument(skip(self), level = "debug")]
|
|
|
|
fn visit_constant(&mut self, constant: &mut Constant<'tcx>, _location: Location) {
|
|
|
|
let literal = constant.literal;
|
2022-09-27 09:59:25 +00:00
|
|
|
constant.literal = self.renumber_regions(literal);
|
2022-06-28 15:38:01 +00:00
|
|
|
debug!("constant: {:#?}", constant);
|
|
|
|
}
|
2017-10-24 18:20:47 +00:00
|
|
|
}
|