2017-11-21 13:12:24 -05:00
|
|
|
//! Code to extract the universally quantified regions declared on a
|
|
|
|
//! function and the relationships between them. For example:
|
2017-10-30 04:51:10 -04:00
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! fn foo<'a, 'b, 'c: 'b>() { }
|
|
|
|
//! ```
|
|
|
|
//!
|
2019-12-28 07:05:44 -08:00
|
|
|
//! here we would return a map assigning each of `{'a, 'b, 'c}`
|
2017-10-30 04:51:10 -04:00
|
|
|
//! to an index, as well as the `FreeRegionMap` which can compute
|
|
|
|
//! relationships between them.
|
|
|
|
//!
|
|
|
|
//! The code in this file doesn't *do anything* with those results; it
|
|
|
|
//! just returns them for other code to use.
|
|
|
|
|
2018-07-03 11:38:09 -04:00
|
|
|
use either::Either;
|
2019-12-24 05:02:53 +01:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2018-08-27 15:10:05 -04:00
|
|
|
use rustc_errors::DiagnosticBuilder;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir as hir;
|
2020-04-12 13:45:41 +01:00
|
|
|
use rustc_hir::def_id::{DefId, LocalDefId};
|
2020-08-18 11:47:27 +01:00
|
|
|
use rustc_hir::lang_items::LangItem;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir::{BodyOwnerKind, HirId};
|
2019-12-22 17:42:04 -05:00
|
|
|
use rustc_index::vec::{Idx, IndexVec};
|
2021-01-28 16:18:25 +09:00
|
|
|
use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin};
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::ty::fold::TypeFoldable;
|
|
|
|
use rustc_middle::ty::subst::{InternalSubsts, Subst, SubstsRef};
|
2021-10-02 13:12:33 +01:00
|
|
|
use rustc_middle::ty::{self, InlineConstSubsts, InlineConstSubstsParts, RegionVid, Ty, TyCtxt};
|
2017-11-22 17:38:51 -05:00
|
|
|
use std::iter;
|
|
|
|
|
2020-12-30 18:48:40 +01:00
|
|
|
use crate::nll::ToRegionVid;
|
2017-11-06 04:04:45 -05:00
|
|
|
|
2017-10-30 04:51:10 -04:00
|
|
|
#[derive(Debug)]
|
2017-11-21 13:12:24 -05:00
|
|
|
pub struct UniversalRegions<'tcx> {
|
2017-11-22 17:38:51 -05:00
|
|
|
indices: UniversalRegionIndices<'tcx>,
|
|
|
|
|
|
|
|
/// The vid assigned to `'static`
|
|
|
|
pub fr_static: RegionVid,
|
|
|
|
|
2017-12-04 05:40:43 -05:00
|
|
|
/// A special region vid created to represent the current MIR fn
|
2019-02-08 14:53:55 +01:00
|
|
|
/// body. It will outlive the entire CFG but it will not outlive
|
2017-12-04 05:40:43 -05:00
|
|
|
/// any other universal regions.
|
|
|
|
pub fr_fn_body: RegionVid,
|
|
|
|
|
2017-11-22 17:38:51 -05:00
|
|
|
/// We create region variables such that they are ordered by their
|
|
|
|
/// `RegionClassification`. The first block are globals, then
|
2019-02-08 14:53:55 +01:00
|
|
|
/// externals, then locals. So, things from:
|
|
|
|
/// - `FIRST_GLOBAL_INDEX..first_extern_index` are global,
|
|
|
|
/// - `first_extern_index..first_local_index` are external,
|
2018-07-21 22:23:50 +08:00
|
|
|
/// - `first_local_index..num_universals` are local.
|
2017-11-22 17:38:51 -05:00
|
|
|
first_extern_index: usize,
|
|
|
|
|
|
|
|
/// See `first_extern_index`.
|
|
|
|
first_local_index: usize,
|
|
|
|
|
|
|
|
/// The total number of universal region variables instantiated.
|
|
|
|
num_universals: usize,
|
|
|
|
|
2020-04-09 10:55:27 +00:00
|
|
|
/// A special region variable created for the `'empty(U0)` region.
|
|
|
|
/// Note that this is **not** a "universal" region, as it doesn't
|
|
|
|
/// represent a universally bound placeholder or any such thing.
|
|
|
|
/// But we do create it here in this type because it's a useful region
|
|
|
|
/// to have around in a few limited cases.
|
|
|
|
pub root_empty: RegionVid,
|
|
|
|
|
2017-11-22 17:38:51 -05:00
|
|
|
/// The "defining" type for this function, with all universal
|
2019-02-08 14:53:55 +01:00
|
|
|
/// regions instantiated. For a closure or generator, this is the
|
2018-08-22 01:35:02 +01:00
|
|
|
/// closure type, but for a top-level function it's the `FnDef`.
|
2017-12-08 13:07:23 -05:00
|
|
|
pub defining_ty: DefiningTy<'tcx>,
|
2017-11-22 17:38:51 -05:00
|
|
|
|
2017-12-06 04:30:58 -05:00
|
|
|
/// The return type of this function, with all regions replaced by
|
2017-12-10 09:55:43 -05:00
|
|
|
/// their universal `RegionVid` equivalents.
|
|
|
|
///
|
2019-02-08 14:53:55 +01:00
|
|
|
/// N.B., associated types in this type have not been normalized,
|
2017-12-10 09:55:43 -05:00
|
|
|
/// as the name suggests. =)
|
|
|
|
pub unnormalized_output_ty: Ty<'tcx>,
|
2017-11-22 17:38:51 -05:00
|
|
|
|
|
|
|
/// The fully liberated input types of this function, with all
|
|
|
|
/// regions replaced by their universal `RegionVid` equivalents.
|
2017-12-10 09:55:43 -05:00
|
|
|
///
|
2019-02-08 14:53:55 +01:00
|
|
|
/// N.B., associated types in these types have not been normalized,
|
2017-12-10 09:55:43 -05:00
|
|
|
/// as the name suggests. =)
|
|
|
|
pub unnormalized_input_tys: &'tcx [Ty<'tcx>],
|
2017-11-22 17:38:51 -05:00
|
|
|
|
2018-01-19 19:18:02 -03:00
|
|
|
pub yield_ty: Option<Ty<'tcx>>,
|
2017-11-22 17:38:51 -05:00
|
|
|
}
|
|
|
|
|
2017-12-08 13:07:23 -05:00
|
|
|
/// The "defining type" for this MIR. The key feature of the "defining
|
|
|
|
/// type" is that it contains the information needed to derive all the
|
|
|
|
/// universal regions that are in scope as well as the types of the
|
|
|
|
/// inputs/output from the MIR. In general, early-bound universal
|
|
|
|
/// regions appear free in the defining type and late-bound regions
|
|
|
|
/// appear bound in the signature.
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub enum DefiningTy<'tcx> {
|
|
|
|
/// The MIR is a closure. The signature is found via
|
|
|
|
/// `ClosureSubsts::closure_sig_ty`.
|
2019-09-26 17:30:44 +00:00
|
|
|
Closure(DefId, SubstsRef<'tcx>),
|
2017-12-08 13:07:23 -05:00
|
|
|
|
|
|
|
/// The MIR is a generator. The signature is that generators take
|
|
|
|
/// no parameters and return the result of
|
|
|
|
/// `ClosureSubsts::generator_return_ty`.
|
2019-11-09 18:06:57 +01:00
|
|
|
Generator(DefId, SubstsRef<'tcx>, hir::Movability),
|
2017-12-08 13:07:23 -05:00
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// The MIR is a fn item with the given `DefId` and substs. The signature
|
2017-12-08 13:07:23 -05:00
|
|
|
/// of the function can be bound then with the `fn_sig` query.
|
2019-02-09 22:11:53 +08:00
|
|
|
FnDef(DefId, SubstsRef<'tcx>),
|
2017-12-08 13:07:23 -05:00
|
|
|
|
|
|
|
/// The MIR represents some form of constant. The signature then
|
|
|
|
/// is that it has no inputs and a single return value, which is
|
|
|
|
/// the value of the constant.
|
2019-02-09 22:11:53 +08:00
|
|
|
Const(DefId, SubstsRef<'tcx>),
|
2021-10-02 13:12:33 +01:00
|
|
|
|
|
|
|
/// The MIR represents an inline const. The signature has no inputs and a
|
|
|
|
/// single return value found via `InlineConstSubsts::ty`.
|
|
|
|
InlineConst(DefId, SubstsRef<'tcx>),
|
2017-12-08 13:07:23 -05:00
|
|
|
}
|
|
|
|
|
2018-07-03 11:38:09 -04:00
|
|
|
impl<'tcx> DefiningTy<'tcx> {
|
|
|
|
/// Returns a list of all the upvar types for this MIR. If this is
|
|
|
|
/// not a closure or generator, there are no upvars, and hence it
|
|
|
|
/// will be an empty list. The order of types in this list will
|
2018-05-16 15:38:32 +03:00
|
|
|
/// match up with the upvar order in the HIR, typesystem, and MIR.
|
2020-03-13 03:23:38 +02:00
|
|
|
pub fn upvar_tys(self) -> impl Iterator<Item = Ty<'tcx>> + 'tcx {
|
2018-07-03 11:38:09 -04:00
|
|
|
match self {
|
2020-03-13 03:23:38 +02:00
|
|
|
DefiningTy::Closure(_, substs) => Either::Left(substs.as_closure().upvar_tys()),
|
|
|
|
DefiningTy::Generator(_, substs, _) => {
|
|
|
|
Either::Right(Either::Left(substs.as_generator().upvar_tys()))
|
2018-07-03 11:38:09 -04:00
|
|
|
}
|
2021-10-02 13:12:33 +01:00
|
|
|
DefiningTy::FnDef(..) | DefiningTy::Const(..) | DefiningTy::InlineConst(..) => {
|
2018-07-03 11:38:09 -04:00
|
|
|
Either::Right(Either::Right(iter::empty()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Number of implicit inputs -- notably the "environment"
|
|
|
|
/// parameter for closures -- that appear in MIR but not in the
|
|
|
|
/// user's code.
|
|
|
|
pub fn implicit_inputs(self) -> usize {
|
|
|
|
match self {
|
|
|
|
DefiningTy::Closure(..) | DefiningTy::Generator(..) => 1,
|
2021-10-02 13:12:33 +01:00
|
|
|
DefiningTy::FnDef(..) | DefiningTy::Const(..) | DefiningTy::InlineConst(..) => 0,
|
2018-07-03 11:38:09 -04:00
|
|
|
}
|
|
|
|
}
|
2019-12-30 19:46:30 -06:00
|
|
|
|
2020-01-25 19:09:23 -06:00
|
|
|
pub fn is_fn_def(&self) -> bool {
|
2021-11-06 01:31:32 +01:00
|
|
|
matches!(*self, DefiningTy::FnDef(..))
|
2019-12-30 19:46:30 -06:00
|
|
|
}
|
|
|
|
|
2020-01-25 19:09:23 -06:00
|
|
|
pub fn is_const(&self) -> bool {
|
2021-10-02 13:12:33 +01:00
|
|
|
matches!(*self, DefiningTy::Const(..) | DefiningTy::InlineConst(..))
|
2019-12-30 19:46:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn def_id(&self) -> DefId {
|
|
|
|
match *self {
|
2020-01-24 12:02:33 -06:00
|
|
|
DefiningTy::Closure(def_id, ..)
|
|
|
|
| DefiningTy::Generator(def_id, ..)
|
|
|
|
| DefiningTy::FnDef(def_id, ..)
|
2021-10-02 13:12:33 +01:00
|
|
|
| DefiningTy::Const(def_id, ..)
|
|
|
|
| DefiningTy::InlineConst(def_id, ..) => def_id,
|
2019-12-30 19:46:30 -06:00
|
|
|
}
|
|
|
|
}
|
2018-07-03 11:38:09 -04:00
|
|
|
}
|
|
|
|
|
2017-11-22 17:38:51 -05:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct UniversalRegionIndices<'tcx> {
|
|
|
|
/// For those regions that may appear in the parameter environment
|
|
|
|
/// ('static and early-bound regions), we maintain a map from the
|
|
|
|
/// `ty::Region` to the internal `RegionVid` we are using. This is
|
|
|
|
/// used because trait matching and type-checking will feed us
|
|
|
|
/// region constraints that reference those regions and we need to
|
|
|
|
/// be able to map them our internal `RegionVid`. This is
|
2021-08-22 14:46:15 +02:00
|
|
|
/// basically equivalent to an `InternalSubsts`, except that it also
|
2017-11-22 17:38:51 -05:00
|
|
|
/// contains an entry for `ReStatic` -- it might be nice to just
|
|
|
|
/// use a substs, and then handle `ReStatic` another way.
|
|
|
|
indices: FxHashMap<ty::Region<'tcx>, RegionVid>,
|
|
|
|
}
|
|
|
|
|
2019-10-20 15:54:53 +11:00
|
|
|
#[derive(Debug, PartialEq)]
|
2017-11-22 17:38:51 -05:00
|
|
|
pub enum RegionClassification {
|
|
|
|
/// A **global** region is one that can be named from
|
|
|
|
/// anywhere. There is only one, `'static`.
|
|
|
|
Global,
|
|
|
|
|
2021-12-31 16:55:34 -05:00
|
|
|
/// An **external** region is only relevant for
|
|
|
|
/// closures, generators, and inline consts. In that
|
|
|
|
/// case, it refers to regions that are free in the type
|
2017-11-22 17:38:51 -05:00
|
|
|
/// -- basically, something bound in the surrounding context.
|
|
|
|
///
|
|
|
|
/// Consider this example:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// fn foo<'a, 'b>(a: &'a u32, b: &'b u32, c: &'static u32) {
|
|
|
|
/// let closure = for<'x> |x: &'x u32| { .. };
|
|
|
|
/// ^^^^^^^ pretend this were legal syntax
|
|
|
|
/// for declaring a late-bound region in
|
|
|
|
/// a closure signature
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Here, the lifetimes `'a` and `'b` would be **external** to the
|
|
|
|
/// closure.
|
|
|
|
///
|
2021-12-31 16:55:34 -05:00
|
|
|
/// If we are not analyzing a closure/generator/inline-const,
|
|
|
|
/// there are no external lifetimes.
|
2017-11-22 17:38:51 -05:00
|
|
|
External,
|
|
|
|
|
|
|
|
/// A **local** lifetime is one about which we know the full set
|
|
|
|
/// of relevant constraints (that is, relationships to other named
|
2019-02-08 14:53:55 +01:00
|
|
|
/// regions). For a closure, this includes any region bound in
|
|
|
|
/// the closure's signature. For a fn item, this includes all
|
2017-11-22 17:38:51 -05:00
|
|
|
/// regions other than global ones.
|
|
|
|
///
|
|
|
|
/// Continuing with the example from `External`, if we were
|
|
|
|
/// analyzing the closure, then `'x` would be local (and `'a` and
|
2019-02-08 14:53:55 +01:00
|
|
|
/// `'b` are external). If we are analyzing the function item
|
2017-11-22 17:38:51 -05:00
|
|
|
/// `foo`, then `'a` and `'b` are local (and `'x` is not in
|
|
|
|
/// scope).
|
|
|
|
Local,
|
2017-10-30 04:51:10 -04:00
|
|
|
}
|
|
|
|
|
2017-11-22 17:38:51 -05:00
|
|
|
const FIRST_GLOBAL_INDEX: usize = 0;
|
|
|
|
|
|
|
|
impl<'tcx> UniversalRegions<'tcx> {
|
|
|
|
/// Creates a new and fully initialized `UniversalRegions` that
|
|
|
|
/// contains indices for all the free regions found in the given
|
|
|
|
/// MIR -- that is, all the regions that appear in the function's
|
|
|
|
/// signature. This will also compute the relationships that are
|
|
|
|
/// known between those regions.
|
|
|
|
pub fn new(
|
2019-06-14 00:48:52 +03:00
|
|
|
infcx: &InferCtxt<'_, 'tcx>,
|
2020-07-15 10:50:54 +02:00
|
|
|
mir_def: ty::WithOptConstParam<LocalDefId>,
|
2017-11-22 17:38:51 -05:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
) -> Self {
|
|
|
|
let tcx = infcx.tcx;
|
2020-08-12 12:22:56 +02:00
|
|
|
let mir_hir_id = tcx.hir().local_def_id_to_hir_id(mir_def.did);
|
2020-07-06 23:49:53 +02:00
|
|
|
UniversalRegionsBuilder { infcx, mir_def, mir_hir_id, param_env }.build()
|
2017-11-22 17:38:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Given a reference to a closure type, extracts all the values
|
|
|
|
/// from its free regions and returns a vector with them. This is
|
|
|
|
/// used when the closure's creator checks that the
|
|
|
|
/// `ClosureRegionRequirements` are met. The requirements from
|
|
|
|
/// `ClosureRegionRequirements` are expressed in terms of
|
|
|
|
/// `RegionVid` entries that map into the returned vector `V`: so
|
|
|
|
/// if the `ClosureRegionRequirements` contains something like
|
|
|
|
/// `'1: '2`, then the caller would impose the constraint that
|
|
|
|
/// `V[1]: V[2]`.
|
|
|
|
pub fn closure_mapping(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-02-09 22:11:53 +08:00
|
|
|
closure_substs: SubstsRef<'tcx>,
|
2017-11-22 17:38:51 -05:00
|
|
|
expected_num_vars: usize,
|
2021-11-06 20:01:35 +00:00
|
|
|
typeck_root_def_id: DefId,
|
2017-11-22 17:38:51 -05:00
|
|
|
) -> IndexVec<RegionVid, ty::Region<'tcx>> {
|
|
|
|
let mut region_mapping = IndexVec::with_capacity(expected_num_vars);
|
2019-04-25 22:05:04 +01:00
|
|
|
region_mapping.push(tcx.lifetimes.re_static);
|
2018-10-12 15:16:29 +01:00
|
|
|
tcx.for_each_free_region(&closure_substs, |fr| {
|
2017-11-22 17:38:51 -05:00
|
|
|
region_mapping.push(fr);
|
|
|
|
});
|
|
|
|
|
2021-11-06 20:01:35 +00:00
|
|
|
for_each_late_bound_region_defined_on(tcx, typeck_root_def_id, |r| {
|
2018-09-06 12:39:38 -04:00
|
|
|
region_mapping.push(r);
|
|
|
|
});
|
2018-07-27 22:46:16 +01:00
|
|
|
|
2017-11-22 17:38:51 -05:00
|
|
|
assert_eq!(
|
|
|
|
region_mapping.len(),
|
|
|
|
expected_num_vars,
|
|
|
|
"index vec had unexpected number of variables"
|
|
|
|
);
|
2017-10-30 04:51:10 -04:00
|
|
|
|
2017-11-22 17:38:51 -05:00
|
|
|
region_mapping
|
|
|
|
}
|
2017-10-30 04:51:10 -04:00
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Returns `true` if `r` is a member of this set of universal regions.
|
2017-11-22 17:38:51 -05:00
|
|
|
pub fn is_universal_region(&self, r: RegionVid) -> bool {
|
2018-04-07 15:47:18 -07:00
|
|
|
(FIRST_GLOBAL_INDEX..self.num_universals).contains(&r.index())
|
2017-11-22 17:38:51 -05:00
|
|
|
}
|
2017-11-06 07:26:34 -05:00
|
|
|
|
2017-11-22 17:38:51 -05:00
|
|
|
/// Classifies `r` as a universal region, returning `None` if this
|
|
|
|
/// is not a member of this set of universal regions.
|
|
|
|
pub fn region_classification(&self, r: RegionVid) -> Option<RegionClassification> {
|
|
|
|
let index = r.index();
|
2018-04-07 15:47:18 -07:00
|
|
|
if (FIRST_GLOBAL_INDEX..self.first_extern_index).contains(&index) {
|
2017-11-22 17:38:51 -05:00
|
|
|
Some(RegionClassification::Global)
|
2018-04-07 15:47:18 -07:00
|
|
|
} else if (self.first_extern_index..self.first_local_index).contains(&index) {
|
2017-11-22 17:38:51 -05:00
|
|
|
Some(RegionClassification::External)
|
2018-04-07 15:47:18 -07:00
|
|
|
} else if (self.first_local_index..self.num_universals).contains(&index) {
|
2017-11-22 17:38:51 -05:00
|
|
|
Some(RegionClassification::Local)
|
|
|
|
} else {
|
|
|
|
None
|
2017-10-30 04:51:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-22 17:38:51 -05:00
|
|
|
/// Returns an iterator over all the RegionVids corresponding to
|
|
|
|
/// universally quantified free regions.
|
|
|
|
pub fn universal_regions(&self) -> impl Iterator<Item = RegionVid> {
|
|
|
|
(FIRST_GLOBAL_INDEX..self.num_universals).map(RegionVid::new)
|
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Returns `true` if `r` is classified as an local region.
|
2017-11-22 17:38:51 -05:00
|
|
|
pub fn is_local_free_region(&self, r: RegionVid) -> bool {
|
|
|
|
self.region_classification(r) == Some(RegionClassification::Local)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the number of universal regions created in any category.
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.num_universals
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the number of global plus external universal regions.
|
|
|
|
/// For closures, these are the regions that appear free in the
|
|
|
|
/// closure type (versus those bound in the closure
|
|
|
|
/// signature). They are therefore the regions between which the
|
|
|
|
/// closure may impose constraints that its creator must verify.
|
|
|
|
pub fn num_global_and_external_regions(&self) -> usize {
|
|
|
|
self.first_local_index
|
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Gets an iterator over all the early-bound regions that have names.
|
2017-11-22 17:38:51 -05:00
|
|
|
pub fn named_universal_regions<'s>(
|
|
|
|
&'s self,
|
|
|
|
) -> impl Iterator<Item = (ty::Region<'tcx>, ty::RegionVid)> + 's {
|
|
|
|
self.indices.indices.iter().map(|(&r, &v)| (r, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// See `UniversalRegionIndices::to_region_vid`.
|
|
|
|
pub fn to_region_vid(&self, r: ty::Region<'tcx>) -> RegionVid {
|
2022-01-28 11:25:15 +11:00
|
|
|
if let ty::ReEmpty(ty::UniverseIndex::ROOT) = *r {
|
2020-04-09 10:55:27 +00:00
|
|
|
self.root_empty
|
|
|
|
} else {
|
|
|
|
self.indices.to_region_vid(r)
|
|
|
|
}
|
2017-11-22 17:38:51 -05:00
|
|
|
}
|
2018-08-27 15:10:05 -04:00
|
|
|
|
|
|
|
/// As part of the NLL unit tests, you can annotate a function with
|
|
|
|
/// `#[rustc_regions]`, and we will emit information about the region
|
|
|
|
/// inference context and -- in particular -- the external constraints
|
|
|
|
/// that this region imposes on others. The methods in this file
|
|
|
|
/// handle the part about dumping the inference context internal
|
|
|
|
/// state.
|
2019-06-14 00:48:52 +03:00
|
|
|
crate fn annotate(&self, tcx: TyCtxt<'tcx>, err: &mut DiagnosticBuilder<'_>) {
|
2018-08-27 15:10:05 -04:00
|
|
|
match self.defining_ty {
|
|
|
|
DefiningTy::Closure(def_id, substs) => {
|
|
|
|
err.note(&format!(
|
2019-11-30 18:47:21 +02:00
|
|
|
"defining type: {} with closure substs {:#?}",
|
|
|
|
tcx.def_path_str_with_substs(def_id, substs),
|
|
|
|
&substs[tcx.generics_of(def_id).parent_count..],
|
2018-08-27 15:10:05 -04:00
|
|
|
));
|
|
|
|
|
2018-09-07 13:35:16 -04:00
|
|
|
// FIXME: It'd be nice to print the late-bound regions
|
|
|
|
// here, but unfortunately these wind up stored into
|
|
|
|
// tests, and the resulting print-outs include def-ids
|
|
|
|
// and other things that are not stable across tests!
|
|
|
|
// So we just include the region-vid. Annoying.
|
2021-11-06 20:01:35 +00:00
|
|
|
let typeck_root_def_id = tcx.typeck_root_def_id(def_id);
|
|
|
|
for_each_late_bound_region_defined_on(tcx, typeck_root_def_id, |r| {
|
2019-12-22 17:42:04 -05:00
|
|
|
err.note(&format!("late-bound region is {:?}", self.to_region_vid(r),));
|
2018-08-27 15:10:05 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
DefiningTy::Generator(def_id, substs, _) => {
|
|
|
|
err.note(&format!(
|
2019-11-30 18:47:21 +02:00
|
|
|
"defining type: {} with generator substs {:#?}",
|
|
|
|
tcx.def_path_str_with_substs(def_id, substs),
|
|
|
|
&substs[tcx.generics_of(def_id).parent_count..],
|
2018-08-27 15:10:05 -04:00
|
|
|
));
|
|
|
|
|
2018-09-07 13:35:16 -04:00
|
|
|
// FIXME: As above, we'd like to print out the region
|
|
|
|
// `r` but doing so is not stable across architectures
|
|
|
|
// and so forth.
|
2021-11-06 20:01:35 +00:00
|
|
|
let typeck_root_def_id = tcx.typeck_root_def_id(def_id);
|
|
|
|
for_each_late_bound_region_defined_on(tcx, typeck_root_def_id, |r| {
|
2019-12-22 17:42:04 -05:00
|
|
|
err.note(&format!("late-bound region is {:?}", self.to_region_vid(r),));
|
2018-08-27 15:10:05 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
DefiningTy::FnDef(def_id, substs) => {
|
|
|
|
err.note(&format!(
|
2019-11-30 18:47:21 +02:00
|
|
|
"defining type: {}",
|
|
|
|
tcx.def_path_str_with_substs(def_id, substs),
|
2018-08-27 15:10:05 -04:00
|
|
|
));
|
|
|
|
}
|
|
|
|
DefiningTy::Const(def_id, substs) => {
|
|
|
|
err.note(&format!(
|
2019-11-30 18:47:21 +02:00
|
|
|
"defining constant type: {}",
|
|
|
|
tcx.def_path_str_with_substs(def_id, substs),
|
2018-08-27 15:10:05 -04:00
|
|
|
));
|
|
|
|
}
|
2021-10-02 13:12:33 +01:00
|
|
|
DefiningTy::InlineConst(def_id, substs) => {
|
|
|
|
err.note(&format!(
|
|
|
|
"defining inline constant type: {}",
|
|
|
|
tcx.def_path_str_with_substs(def_id, substs),
|
|
|
|
));
|
|
|
|
}
|
2018-08-27 15:10:05 -04:00
|
|
|
}
|
|
|
|
}
|
2017-11-22 17:38:51 -05:00
|
|
|
}
|
|
|
|
|
2019-06-14 19:39:39 +03:00
|
|
|
struct UniversalRegionsBuilder<'cx, 'tcx> {
|
2019-06-14 00:48:52 +03:00
|
|
|
infcx: &'cx InferCtxt<'cx, 'tcx>,
|
2020-07-15 10:50:54 +02:00
|
|
|
mir_def: ty::WithOptConstParam<LocalDefId>,
|
2017-11-22 17:38:51 -05:00
|
|
|
mir_hir_id: HirId,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
}
|
|
|
|
|
2021-01-28 16:18:25 +09:00
|
|
|
const FR: NllRegionVariableOrigin = NllRegionVariableOrigin::FreeRegion;
|
2017-11-22 17:38:51 -05:00
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
|
2018-07-26 14:30:22 +03:00
|
|
|
fn build(self) -> UniversalRegions<'tcx> {
|
2020-07-06 23:49:53 +02:00
|
|
|
debug!("build(mir_def={:?})", self.mir_def);
|
2017-12-01 08:51:01 -05:00
|
|
|
|
2017-11-22 17:38:51 -05:00
|
|
|
let param_env = self.param_env;
|
2017-12-01 08:51:01 -05:00
|
|
|
debug!("build: param_env={:?}", param_env);
|
2017-11-22 17:38:51 -05:00
|
|
|
|
|
|
|
assert_eq!(FIRST_GLOBAL_INDEX, self.infcx.num_region_vars());
|
|
|
|
|
|
|
|
// Create the "global" region that is always free in all contexts: 'static.
|
|
|
|
let fr_static = self.infcx.next_nll_region_var(FR).to_region_vid();
|
|
|
|
|
|
|
|
// We've now added all the global regions. The next ones we
|
|
|
|
// add will be external.
|
|
|
|
let first_extern_index = self.infcx.num_region_vars();
|
|
|
|
|
|
|
|
let defining_ty = self.defining_ty();
|
2017-12-01 08:51:01 -05:00
|
|
|
debug!("build: defining_ty={:?}", defining_ty);
|
2017-11-22 17:38:51 -05:00
|
|
|
|
2017-12-12 09:06:35 -05:00
|
|
|
let mut indices = self.compute_indices(fr_static, defining_ty);
|
2017-12-01 08:51:01 -05:00
|
|
|
debug!("build: indices={:?}", indices);
|
2017-11-22 17:38:51 -05:00
|
|
|
|
2021-11-06 20:01:35 +00:00
|
|
|
let typeck_root_def_id = self.infcx.tcx.typeck_root_def_id(self.mir_def.did.to_def_id());
|
2018-07-27 22:46:16 +01:00
|
|
|
|
2021-12-31 16:55:34 -05:00
|
|
|
// If this is is a 'root' body (not a closure/generator/inline const), then
|
|
|
|
// there are no extern regions, so the local regions start at the same
|
|
|
|
// position as the (empty) sub-list of extern regions
|
|
|
|
let first_local_index = if self.mir_def.did.to_def_id() == typeck_root_def_id {
|
|
|
|
first_extern_index
|
|
|
|
} else {
|
|
|
|
// If this is a closure, generator, or inline-const, then the late-bound regions from the enclosing
|
|
|
|
// function are actually external regions to us. For example, here, 'a is not local
|
|
|
|
// to the closure c (although it is local to the fn foo):
|
|
|
|
// fn foo<'a>() {
|
|
|
|
// let c = || { let x: &'a u32 = ...; }
|
|
|
|
// }
|
2020-07-06 23:49:53 +02:00
|
|
|
self.infcx
|
2021-12-31 16:55:34 -05:00
|
|
|
.replace_late_bound_regions_with_nll_infer_vars(self.mir_def.did, &mut indices);
|
|
|
|
// Any regions created during the execution of `defining_ty` or during the above
|
|
|
|
// late-bound region replacement are all considered 'extern' regions
|
|
|
|
self.infcx.num_region_vars()
|
|
|
|
};
|
2017-11-22 17:38:51 -05:00
|
|
|
|
|
|
|
// "Liberate" the late-bound regions. These correspond to
|
|
|
|
// "local" free regions.
|
2021-12-31 16:55:34 -05:00
|
|
|
|
|
|
|
let bound_inputs_and_output = self.compute_inputs_and_output(&indices, defining_ty);
|
|
|
|
|
2017-12-12 09:06:35 -05:00
|
|
|
let inputs_and_output = self.infcx.replace_bound_regions_with_nll_infer_vars(
|
|
|
|
FR,
|
2020-07-06 23:49:53 +02:00
|
|
|
self.mir_def.did,
|
2020-10-24 02:21:18 +02:00
|
|
|
bound_inputs_and_output,
|
2017-12-12 09:06:35 -05:00
|
|
|
&mut indices,
|
|
|
|
);
|
2018-07-27 22:46:16 +01:00
|
|
|
// Converse of above, if this is a function then the late-bound regions declared on its
|
|
|
|
// signature are local to the fn.
|
2021-11-06 20:01:35 +00:00
|
|
|
if self.mir_def.did.to_def_id() == typeck_root_def_id {
|
2018-09-06 12:39:38 -04:00
|
|
|
self.infcx
|
2020-07-06 23:49:53 +02:00
|
|
|
.replace_late_bound_regions_with_nll_infer_vars(self.mir_def.did, &mut indices);
|
2018-07-27 22:46:16 +01:00
|
|
|
}
|
|
|
|
|
2019-08-10 14:38:17 +03:00
|
|
|
let (unnormalized_output_ty, mut unnormalized_input_tys) =
|
|
|
|
inputs_and_output.split_last().unwrap();
|
|
|
|
|
|
|
|
// C-variadic fns also have a `VaList` input that's not listed in the signature
|
|
|
|
// (as it's created inside the body itself, not passed in from outside).
|
|
|
|
if let DefiningTy::FnDef(def_id, _) = defining_ty {
|
|
|
|
if self.infcx.tcx.fn_sig(def_id).c_variadic() {
|
|
|
|
let va_list_did = self.infcx.tcx.require_lang_item(
|
2020-08-18 11:47:27 +01:00
|
|
|
LangItem::VaList,
|
2020-07-06 23:49:53 +02:00
|
|
|
Some(self.infcx.tcx.def_span(self.mir_def.did)),
|
2019-08-10 14:38:17 +03:00
|
|
|
);
|
2019-12-22 17:42:04 -05:00
|
|
|
let region = self
|
|
|
|
.infcx
|
|
|
|
.tcx
|
|
|
|
.mk_region(ty::ReVar(self.infcx.next_nll_region_var(FR).to_region_vid()));
|
|
|
|
let va_list_ty =
|
|
|
|
self.infcx.tcx.type_of(va_list_did).subst(self.infcx.tcx, &[region.into()]);
|
2019-08-10 14:38:17 +03:00
|
|
|
|
|
|
|
unnormalized_input_tys = self.infcx.tcx.mk_type_list(
|
2019-12-22 17:42:04 -05:00
|
|
|
unnormalized_input_tys.iter().copied().chain(iter::once(va_list_ty)),
|
2019-08-10 14:38:17 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 05:40:43 -05:00
|
|
|
let fr_fn_body = self.infcx.next_nll_region_var(FR).to_region_vid();
|
2017-11-22 17:38:51 -05:00
|
|
|
let num_universals = self.infcx.num_region_vars();
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
debug!("build: global regions = {}..{}", FIRST_GLOBAL_INDEX, first_extern_index);
|
|
|
|
debug!("build: extern regions = {}..{}", first_extern_index, first_local_index);
|
|
|
|
debug!("build: local regions = {}..{}", first_local_index, num_universals);
|
2017-11-22 17:38:51 -05:00
|
|
|
|
2018-01-19 19:18:02 -03:00
|
|
|
let yield_ty = match defining_ty {
|
2020-03-13 03:23:38 +02:00
|
|
|
DefiningTy::Generator(_, substs, _) => Some(substs.as_generator().yield_ty()),
|
2018-01-19 19:18:02 -03:00
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
|
2020-04-09 10:55:27 +00:00
|
|
|
let root_empty = self
|
|
|
|
.infcx
|
2021-01-28 16:18:25 +09:00
|
|
|
.next_nll_region_var(NllRegionVariableOrigin::RootEmptyRegion)
|
2020-04-09 10:55:27 +00:00
|
|
|
.to_region_vid();
|
|
|
|
|
2017-11-22 17:38:51 -05:00
|
|
|
UniversalRegions {
|
|
|
|
indices,
|
|
|
|
fr_static,
|
2017-12-04 05:40:43 -05:00
|
|
|
fr_fn_body,
|
2020-04-09 10:55:27 +00:00
|
|
|
root_empty,
|
2017-11-22 17:38:51 -05:00
|
|
|
first_extern_index,
|
|
|
|
first_local_index,
|
|
|
|
num_universals,
|
|
|
|
defining_ty,
|
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
|
|
|
unnormalized_output_ty: *unnormalized_output_ty,
|
2017-12-10 09:55:43 -05:00
|
|
|
unnormalized_input_tys,
|
2020-03-06 19:28:44 +01:00
|
|
|
yield_ty,
|
2017-11-22 17:38:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-08 13:07:23 -05:00
|
|
|
/// Returns the "defining type" of the current MIR;
|
|
|
|
/// see `DefiningTy` for details.
|
|
|
|
fn defining_ty(&self) -> DefiningTy<'tcx> {
|
2017-11-22 17:38:51 -05:00
|
|
|
let tcx = self.infcx.tcx;
|
2021-11-06 20:01:35 +00:00
|
|
|
let typeck_root_def_id = tcx.typeck_root_def_id(self.mir_def.did.to_def_id());
|
2017-11-22 17:38:51 -05:00
|
|
|
|
2019-06-14 18:58:55 +02:00
|
|
|
match tcx.hir().body_owner_kind(self.mir_hir_id) {
|
2019-12-22 17:42:04 -05:00
|
|
|
BodyOwnerKind::Closure | BodyOwnerKind::Fn => {
|
2021-11-06 20:01:35 +00:00
|
|
|
let defining_ty = if self.mir_def.did.to_def_id() == typeck_root_def_id {
|
|
|
|
tcx.type_of(typeck_root_def_id)
|
2018-02-01 22:26:48 -05:00
|
|
|
} else {
|
2020-07-17 08:47:04 +00:00
|
|
|
let tables = tcx.typeck(self.mir_def.did);
|
2019-02-04 09:38:11 +01:00
|
|
|
tables.node_type(self.mir_hir_id)
|
2018-02-01 22:26:48 -05:00
|
|
|
};
|
|
|
|
|
2018-07-03 11:38:09 -04:00
|
|
|
debug!("defining_ty (pre-replacement): {:?}", defining_ty);
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let defining_ty =
|
2020-10-24 02:21:18 +02:00
|
|
|
self.infcx.replace_free_regions_with_nll_infer_vars(FR, defining_ty);
|
2018-02-01 22:26:48 -05:00
|
|
|
|
2020-08-03 00:49:11 +02:00
|
|
|
match *defining_ty.kind() {
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::Closure(def_id, substs) => DefiningTy::Closure(def_id, substs),
|
|
|
|
ty::Generator(def_id, substs, movability) => {
|
2018-05-02 13:14:30 +02:00
|
|
|
DefiningTy::Generator(def_id, substs, movability)
|
2018-02-01 22:26:48 -05:00
|
|
|
}
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::FnDef(def_id, substs) => DefiningTy::FnDef(def_id, substs),
|
2018-02-01 22:26:48 -05:00
|
|
|
_ => span_bug!(
|
2020-07-06 23:49:53 +02:00
|
|
|
tcx.def_span(self.mir_def.did),
|
2018-02-01 22:26:48 -05:00
|
|
|
"expected defining type for `{:?}`: `{:?}`",
|
2020-07-06 23:49:53 +02:00
|
|
|
self.mir_def.did,
|
2018-02-01 22:26:48 -05:00
|
|
|
defining_ty
|
|
|
|
),
|
2017-12-08 13:07:23 -05:00
|
|
|
}
|
2018-02-01 22:26:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
BodyOwnerKind::Const | BodyOwnerKind::Static(..) => {
|
2021-11-06 20:01:35 +00:00
|
|
|
let identity_substs = InternalSubsts::identity_for_item(tcx, typeck_root_def_id);
|
|
|
|
if self.mir_def.did.to_def_id() == typeck_root_def_id {
|
2021-10-02 13:12:33 +01:00
|
|
|
let substs =
|
|
|
|
self.infcx.replace_free_regions_with_nll_infer_vars(FR, identity_substs);
|
|
|
|
DefiningTy::Const(self.mir_def.did.to_def_id(), substs)
|
|
|
|
} else {
|
|
|
|
let ty = tcx.typeck(self.mir_def.did).node_type(self.mir_hir_id);
|
|
|
|
let substs = InlineConstSubsts::new(
|
|
|
|
tcx,
|
|
|
|
InlineConstSubstsParts { parent_substs: identity_substs, ty },
|
|
|
|
)
|
|
|
|
.substs;
|
|
|
|
let substs = self.infcx.replace_free_regions_with_nll_infer_vars(FR, substs);
|
|
|
|
DefiningTy::InlineConst(self.mir_def.did.to_def_id(), substs)
|
|
|
|
}
|
2018-02-01 22:26:48 -05:00
|
|
|
}
|
2017-12-08 13:07:23 -05:00
|
|
|
}
|
2017-11-22 17:38:51 -05:00
|
|
|
}
|
|
|
|
|
2017-12-12 19:46:36 -05:00
|
|
|
/// Builds a hashmap that maps from the universal regions that are
|
|
|
|
/// in scope (as a `ty::Region<'tcx>`) to their indices (as a
|
|
|
|
/// `RegionVid`). The map returned by this function contains only
|
|
|
|
/// the early-bound regions.
|
2017-11-22 17:38:51 -05:00
|
|
|
fn compute_indices(
|
|
|
|
&self,
|
|
|
|
fr_static: RegionVid,
|
2017-12-08 13:07:23 -05:00
|
|
|
defining_ty: DefiningTy<'tcx>,
|
2017-11-22 17:38:51 -05:00
|
|
|
) -> UniversalRegionIndices<'tcx> {
|
|
|
|
let tcx = self.infcx.tcx;
|
2021-11-06 20:01:35 +00:00
|
|
|
let typeck_root_def_id = tcx.typeck_root_def_id(self.mir_def.did.to_def_id());
|
|
|
|
let identity_substs = InternalSubsts::identity_for_item(tcx, typeck_root_def_id);
|
2017-12-08 13:07:23 -05:00
|
|
|
let fr_substs = match defining_ty {
|
2021-10-02 13:12:33 +01:00
|
|
|
DefiningTy::Closure(_, ref substs)
|
|
|
|
| DefiningTy::Generator(_, ref substs, _)
|
|
|
|
| DefiningTy::InlineConst(_, ref substs) => {
|
2017-11-22 17:38:51 -05:00
|
|
|
// In the case of closures, we rely on the fact that
|
|
|
|
// the first N elements in the ClosureSubsts are
|
2021-11-06 20:01:35 +00:00
|
|
|
// inherited from the `typeck_root_def_id`.
|
2017-11-22 17:38:51 -05:00
|
|
|
// Therefore, when we zip together (below) with
|
|
|
|
// `identity_substs`, we will get only those regions
|
|
|
|
// that correspond to early-bound regions declared on
|
2021-11-06 20:01:35 +00:00
|
|
|
// the `typeck_root_def_id`.
|
2018-05-02 13:14:30 +02:00
|
|
|
assert!(substs.len() >= identity_substs.len());
|
|
|
|
assert_eq!(substs.regions().count(), identity_substs.regions().count());
|
|
|
|
substs
|
2017-11-22 17:38:51 -05:00
|
|
|
}
|
2017-11-26 20:05:18 -05:00
|
|
|
|
2018-02-01 22:26:48 -05:00
|
|
|
DefiningTy::FnDef(_, substs) | DefiningTy::Const(_, substs) => substs,
|
2017-11-22 17:38:51 -05:00
|
|
|
};
|
|
|
|
|
2019-09-25 15:36:14 -04:00
|
|
|
let global_mapping = iter::once((tcx.lifetimes.re_static, fr_static));
|
2019-12-22 17:42:04 -05:00
|
|
|
let subst_mapping =
|
2021-03-08 15:32:41 -08:00
|
|
|
iter::zip(identity_substs.regions(), fr_substs.regions().map(|r| r.to_region_vid()));
|
2017-11-22 17:38:51 -05:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
UniversalRegionIndices { indices: global_mapping.chain(subst_mapping).collect() }
|
2017-11-22 17:38:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn compute_inputs_and_output(
|
|
|
|
&self,
|
|
|
|
indices: &UniversalRegionIndices<'tcx>,
|
2017-12-08 13:07:23 -05:00
|
|
|
defining_ty: DefiningTy<'tcx>,
|
2020-10-05 16:51:33 -04:00
|
|
|
) -> ty::Binder<'tcx, &'tcx ty::List<Ty<'tcx>>> {
|
2017-11-22 17:38:51 -05:00
|
|
|
let tcx = self.infcx.tcx;
|
2017-12-08 13:07:23 -05:00
|
|
|
match defining_ty {
|
|
|
|
DefiningTy::Closure(def_id, substs) => {
|
2020-07-06 23:49:53 +02:00
|
|
|
assert_eq!(self.mir_def.did.to_def_id(), def_id);
|
2020-03-13 03:23:38 +02:00
|
|
|
let closure_sig = substs.as_closure().sig();
|
2017-11-22 17:38:51 -05:00
|
|
|
let inputs_and_output = closure_sig.inputs_and_output();
|
2020-10-26 14:18:31 -04:00
|
|
|
let bound_vars = tcx.mk_bound_variable_kinds(
|
|
|
|
inputs_and_output
|
|
|
|
.bound_vars()
|
|
|
|
.iter()
|
|
|
|
.chain(iter::once(ty::BoundVariableKind::Region(ty::BrEnv))),
|
|
|
|
);
|
|
|
|
let br = ty::BoundRegion {
|
|
|
|
var: ty::BoundVar::from_usize(bound_vars.len() - 1),
|
|
|
|
kind: ty::BrEnv,
|
|
|
|
};
|
|
|
|
let env_region = ty::ReLateBound(ty::INNERMOST, br);
|
|
|
|
let closure_ty = tcx.closure_env_ty(def_id, substs, env_region).unwrap();
|
|
|
|
|
|
|
|
// The "inputs" of the closure in the
|
|
|
|
// signature appear as a tuple. The MIR side
|
|
|
|
// flattens this tuple.
|
|
|
|
let (&output, tuplized_inputs) =
|
|
|
|
inputs_and_output.skip_binder().split_last().unwrap();
|
|
|
|
assert_eq!(tuplized_inputs.len(), 1, "multiple closure inputs");
|
2022-02-07 16:06:31 +01:00
|
|
|
let &ty::Tuple(inputs) = tuplized_inputs[0].kind() else {
|
2022-02-19 00:48:49 +01:00
|
|
|
bug!("closure inputs not a tuple: {:?}", tuplized_inputs[0]);
|
2020-10-26 14:18:31 -04:00
|
|
|
};
|
2019-12-22 17:42:04 -05:00
|
|
|
|
2020-10-26 14:18:31 -04:00
|
|
|
ty::Binder::bind_with_vars(
|
2019-12-22 17:42:04 -05:00
|
|
|
tcx.mk_type_list(
|
2022-02-07 16:06:31 +01:00
|
|
|
iter::once(closure_ty).chain(inputs).chain(iter::once(output)),
|
2020-10-26 14:18:31 -04:00
|
|
|
),
|
|
|
|
bound_vars,
|
|
|
|
)
|
2017-11-22 17:38:51 -05:00
|
|
|
}
|
|
|
|
|
2018-05-02 13:14:30 +02:00
|
|
|
DefiningTy::Generator(def_id, substs, movability) => {
|
2020-07-06 23:49:53 +02:00
|
|
|
assert_eq!(self.mir_def.did.to_def_id(), def_id);
|
2020-03-13 03:23:38 +02:00
|
|
|
let resume_ty = substs.as_generator().resume_ty();
|
|
|
|
let output = substs.as_generator().return_ty();
|
2018-05-02 13:14:30 +02:00
|
|
|
let generator_ty = tcx.mk_generator(def_id, substs, movability);
|
2020-01-25 02:30:46 +01:00
|
|
|
let inputs_and_output =
|
|
|
|
self.infcx.tcx.intern_type_list(&[generator_ty, resume_ty, output]);
|
2017-12-07 05:24:59 -05:00
|
|
|
ty::Binder::dummy(inputs_and_output)
|
2017-11-22 17:38:51 -05:00
|
|
|
}
|
|
|
|
|
2017-12-08 13:07:23 -05:00
|
|
|
DefiningTy::FnDef(def_id, _) => {
|
2017-11-22 17:38:51 -05:00
|
|
|
let sig = tcx.fn_sig(def_id);
|
2020-10-24 02:21:18 +02:00
|
|
|
let sig = indices.fold_to_region_vids(tcx, sig);
|
2017-11-26 20:05:18 -05:00
|
|
|
sig.inputs_and_output()
|
|
|
|
}
|
|
|
|
|
2018-02-01 22:26:48 -05:00
|
|
|
DefiningTy::Const(def_id, _) => {
|
|
|
|
// For a constant body, there are no inputs, and one
|
|
|
|
// "output" (the type of the constant).
|
2020-07-06 23:49:53 +02:00
|
|
|
assert_eq!(self.mir_def.did.to_def_id(), def_id);
|
2020-07-15 10:53:11 +02:00
|
|
|
let ty = tcx.type_of(self.mir_def.def_id_for_type_of());
|
2020-10-24 02:21:18 +02:00
|
|
|
let ty = indices.fold_to_region_vids(tcx, ty);
|
2018-05-16 10:43:24 +03:00
|
|
|
ty::Binder::dummy(tcx.intern_type_list(&[ty]))
|
2018-02-01 22:26:48 -05:00
|
|
|
}
|
2021-10-02 13:12:33 +01:00
|
|
|
|
|
|
|
DefiningTy::InlineConst(def_id, substs) => {
|
|
|
|
assert_eq!(self.mir_def.did.to_def_id(), def_id);
|
|
|
|
let ty = substs.as_inline_const().ty();
|
|
|
|
ty::Binder::dummy(tcx.intern_type_list(&[ty]))
|
|
|
|
}
|
2017-11-22 17:38:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-12 09:06:35 -05:00
|
|
|
trait InferCtxtExt<'tcx> {
|
2017-11-22 17:38:51 -05:00
|
|
|
fn replace_free_regions_with_nll_infer_vars<T>(
|
|
|
|
&self,
|
2021-01-28 16:18:25 +09:00
|
|
|
origin: NllRegionVariableOrigin,
|
2020-10-24 02:21:18 +02:00
|
|
|
value: T,
|
2017-11-22 17:38:51 -05:00
|
|
|
) -> T
|
|
|
|
where
|
|
|
|
T: TypeFoldable<'tcx>;
|
|
|
|
|
|
|
|
fn replace_bound_regions_with_nll_infer_vars<T>(
|
|
|
|
&self,
|
2021-01-28 16:18:25 +09:00
|
|
|
origin: NllRegionVariableOrigin,
|
2020-06-27 13:38:00 +02:00
|
|
|
all_outlive_scope: LocalDefId,
|
2020-10-05 16:51:33 -04:00
|
|
|
value: ty::Binder<'tcx, T>,
|
2017-12-12 09:06:35 -05:00
|
|
|
indices: &mut UniversalRegionIndices<'tcx>,
|
2017-11-22 17:38:51 -05:00
|
|
|
) -> T
|
|
|
|
where
|
|
|
|
T: TypeFoldable<'tcx>;
|
2018-07-27 22:46:16 +01:00
|
|
|
|
|
|
|
fn replace_late_bound_regions_with_nll_infer_vars(
|
|
|
|
&self,
|
2020-06-27 13:38:00 +02:00
|
|
|
mir_def_id: LocalDefId,
|
2018-09-06 12:39:38 -04:00
|
|
|
indices: &mut UniversalRegionIndices<'tcx>,
|
2018-07-27 22:46:16 +01:00
|
|
|
);
|
2017-11-22 17:38:51 -05:00
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'cx, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'cx, 'tcx> {
|
2017-11-22 17:38:51 -05:00
|
|
|
fn replace_free_regions_with_nll_infer_vars<T>(
|
|
|
|
&self,
|
2021-01-28 16:18:25 +09:00
|
|
|
origin: NllRegionVariableOrigin,
|
2020-10-24 02:21:18 +02:00
|
|
|
value: T,
|
2017-11-22 17:38:51 -05:00
|
|
|
) -> T
|
|
|
|
where
|
|
|
|
T: TypeFoldable<'tcx>,
|
|
|
|
{
|
2019-12-22 17:42:04 -05:00
|
|
|
self.tcx.fold_regions(value, &mut false, |_region, _depth| self.next_nll_region_var(origin))
|
2017-11-22 17:38:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn replace_bound_regions_with_nll_infer_vars<T>(
|
|
|
|
&self,
|
2021-01-28 16:18:25 +09:00
|
|
|
origin: NllRegionVariableOrigin,
|
2020-06-27 13:38:00 +02:00
|
|
|
all_outlive_scope: LocalDefId,
|
2020-10-05 16:51:33 -04:00
|
|
|
value: ty::Binder<'tcx, T>,
|
2017-12-12 09:06:35 -05:00
|
|
|
indices: &mut UniversalRegionIndices<'tcx>,
|
2017-11-22 17:38:51 -05:00
|
|
|
) -> T
|
|
|
|
where
|
|
|
|
T: TypeFoldable<'tcx>,
|
|
|
|
{
|
2022-02-11 07:18:06 +00:00
|
|
|
debug!(
|
|
|
|
"replace_bound_regions_with_nll_infer_vars(value={:?}, all_outlive_scope={:?})",
|
|
|
|
value, all_outlive_scope,
|
|
|
|
);
|
2017-12-12 09:06:35 -05:00
|
|
|
let (value, _map) = self.tcx.replace_late_bound_regions(value, |br| {
|
2022-02-11 07:18:06 +00:00
|
|
|
debug!("replace_bound_regions_with_nll_infer_vars: br={:?}", br);
|
2017-12-12 09:06:35 -05:00
|
|
|
let liberated_region = self.tcx.mk_region(ty::ReFree(ty::FreeRegion {
|
2020-06-27 13:38:00 +02:00
|
|
|
scope: all_outlive_scope.to_def_id(),
|
2020-12-18 13:24:55 -05:00
|
|
|
bound_region: br.kind,
|
2017-12-12 09:06:35 -05:00
|
|
|
}));
|
|
|
|
let region_vid = self.next_nll_region_var(origin);
|
|
|
|
indices.insert_late_bound_region(liberated_region, region_vid.to_region_vid());
|
2022-02-11 07:18:06 +00:00
|
|
|
debug!(
|
|
|
|
"replace_bound_regions_with_nll_infer_vars: liberated_region={:?} => {:?}",
|
|
|
|
liberated_region, region_vid
|
|
|
|
);
|
2017-12-12 09:06:35 -05:00
|
|
|
region_vid
|
|
|
|
});
|
2017-11-22 17:38:51 -05:00
|
|
|
value
|
|
|
|
}
|
2018-07-27 22:46:16 +01:00
|
|
|
|
|
|
|
/// Finds late-bound regions that do not appear in the parameter listing and adds them to the
|
|
|
|
/// indices vector. Typically, we identify late-bound regions as we process the inputs and
|
|
|
|
/// outputs of the closure/function. However, sometimes there are late-bound regions which do
|
|
|
|
/// not appear in the fn parameters but which are nonetheless in scope. The simplest case of
|
2019-02-08 14:53:55 +01:00
|
|
|
/// this are unused functions, like fn foo<'a>() { } (see e.g., #51351). Despite not being used,
|
2018-07-27 22:46:16 +01:00
|
|
|
/// users can still reference these regions (e.g., let x: &'a u32 = &22;), so we need to create
|
|
|
|
/// entries for them and store them in the indices map. This code iterates over the complete
|
|
|
|
/// set of late-bound regions and checks for any that we have not yet seen, adding them to the
|
|
|
|
/// inputs vector.
|
|
|
|
fn replace_late_bound_regions_with_nll_infer_vars(
|
|
|
|
&self,
|
2020-06-27 13:38:00 +02:00
|
|
|
mir_def_id: LocalDefId,
|
2018-07-27 22:46:16 +01:00
|
|
|
indices: &mut UniversalRegionIndices<'tcx>,
|
|
|
|
) {
|
2019-12-22 17:42:04 -05:00
|
|
|
debug!("replace_late_bound_regions_with_nll_infer_vars(mir_def_id={:?})", mir_def_id);
|
2021-11-06 20:01:35 +00:00
|
|
|
let typeck_root_def_id = self.tcx.typeck_root_def_id(mir_def_id.to_def_id());
|
|
|
|
for_each_late_bound_region_defined_on(self.tcx, typeck_root_def_id, |r| {
|
2018-09-06 12:39:27 -04:00
|
|
|
debug!("replace_late_bound_regions_with_nll_infer_vars: r={:?}", r);
|
2018-07-27 22:46:16 +01:00
|
|
|
if !indices.indices.contains_key(&r) {
|
|
|
|
let region_vid = self.next_nll_region_var(FR);
|
|
|
|
indices.insert_late_bound_region(r, region_vid.to_region_vid());
|
2018-09-06 12:39:27 -04:00
|
|
|
}
|
|
|
|
});
|
2018-07-27 22:46:16 +01:00
|
|
|
}
|
2017-11-22 17:38:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> UniversalRegionIndices<'tcx> {
|
2017-12-12 09:06:35 -05:00
|
|
|
/// Initially, the `UniversalRegionIndices` map contains only the
|
|
|
|
/// early-bound regions in scope. Once that is all setup, we come
|
|
|
|
/// in later and instantiate the late-bound regions, and then we
|
|
|
|
/// insert the `ReFree` version of those into the map as
|
|
|
|
/// well. These are used for error reporting.
|
2018-07-03 11:45:02 -04:00
|
|
|
fn insert_late_bound_region(&mut self, r: ty::Region<'tcx>, vid: ty::RegionVid) {
|
2018-07-03 11:38:09 -04:00
|
|
|
debug!("insert_late_bound_region({:?}, {:?})", r, vid);
|
2017-12-12 09:06:35 -05:00
|
|
|
self.indices.insert(r, vid);
|
|
|
|
}
|
|
|
|
|
2017-11-22 17:38:51 -05:00
|
|
|
/// Converts `r` into a local inference variable: `r` can either
|
|
|
|
/// by a `ReVar` (i.e., already a reference to an inference
|
|
|
|
/// variable) or it can be `'static` or some early-bound
|
|
|
|
/// region. This is useful when taking the results from
|
|
|
|
/// type-checking and trait-matching, which may sometimes
|
|
|
|
/// reference those regions from the `ParamEnv`. It is also used
|
|
|
|
/// during initialization. Relies on the `indices` map having been
|
|
|
|
/// fully initialized.
|
|
|
|
pub fn to_region_vid(&self, r: ty::Region<'tcx>) -> RegionVid {
|
2022-01-28 11:25:15 +11:00
|
|
|
if let ty::ReVar(..) = *r {
|
2018-01-11 06:03:51 -05:00
|
|
|
r.to_region_vid()
|
|
|
|
} else {
|
2019-12-22 17:42:04 -05:00
|
|
|
*self
|
|
|
|
.indices
|
2018-07-03 11:45:02 -04:00
|
|
|
.get(&r)
|
|
|
|
.unwrap_or_else(|| bug!("cannot convert `{:?}` to a region vid", r))
|
2017-11-22 17:38:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Replaces all free regions in `value` with region vids, as
|
2017-11-22 17:38:51 -05:00
|
|
|
/// returned by `to_region_vid`.
|
2020-10-24 02:21:18 +02:00
|
|
|
pub fn fold_to_region_vids<T>(&self, tcx: TyCtxt<'tcx>, value: T) -> T
|
2017-11-22 17:38:51 -05:00
|
|
|
where
|
|
|
|
T: TypeFoldable<'tcx>,
|
|
|
|
{
|
2017-12-08 13:07:23 -05:00
|
|
|
tcx.fold_regions(value, &mut false, |region, _| {
|
|
|
|
tcx.mk_region(ty::ReVar(self.to_region_vid(region)))
|
|
|
|
})
|
2017-11-22 17:38:51 -05:00
|
|
|
}
|
2017-10-30 04:51:10 -04:00
|
|
|
}
|
2017-12-10 10:23:45 -05:00
|
|
|
|
2018-07-27 22:46:16 +01:00
|
|
|
/// Iterates over the late-bound regions defined on fn_def_id and
|
|
|
|
/// invokes `f` with the liberated form of each one.
|
|
|
|
fn for_each_late_bound_region_defined_on<'tcx>(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2018-07-27 22:46:16 +01:00
|
|
|
fn_def_id: DefId,
|
2018-09-06 12:39:38 -04:00
|
|
|
mut f: impl FnMut(ty::Region<'tcx>),
|
|
|
|
) {
|
2020-11-22 13:57:11 +01:00
|
|
|
if let Some((owner, late_bounds)) = tcx.is_late_bound_map(fn_def_id.expect_local()) {
|
|
|
|
for &late_bound in late_bounds.iter() {
|
|
|
|
let hir_id = HirId { owner, local_id: late_bound };
|
2019-10-18 13:22:50 +11:00
|
|
|
let name = tcx.hir().name(hir_id);
|
2019-06-27 11:28:14 +02:00
|
|
|
let region_def_id = tcx.hir().local_def_id(hir_id);
|
2018-07-27 22:46:16 +01:00
|
|
|
let liberated_region = tcx.mk_region(ty::ReFree(ty::FreeRegion {
|
2020-11-22 13:57:11 +01:00
|
|
|
scope: owner.to_def_id(),
|
2020-12-18 13:24:55 -05:00
|
|
|
bound_region: ty::BoundRegionKind::BrNamed(region_def_id.to_def_id(), name),
|
2018-07-27 22:46:16 +01:00
|
|
|
}));
|
|
|
|
f(liberated_region);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|