Skip to content

Commit

Permalink
Auto merge of rust-lang#98247 - jackh726:regionkind-rustc-type-ir, r=…
Browse files Browse the repository at this point in the history
…compiler-errors

Move RegionKind to rustc_type_ir

(Also UniverseIndex)

r? rust-lang/types
  • Loading branch information
bors authored and jsha committed Jun 19, 2022
2 parents 2b646bd + 1e9f8df commit 408cd93
Show file tree
Hide file tree
Showing 13 changed files with 599 additions and 346 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/region_infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
}

/// Returns an iterator over all the region indices.
pub fn regions(&self) -> impl Iterator<Item = RegionVid> + '_ {
pub fn regions(&self) -> impl Iterator<Item = RegionVid> + 'tcx {
self.definitions.indices()
}

Expand Down
12 changes: 9 additions & 3 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
type DelaySpanBugEmitted = DelaySpanBugEmitted;
type PredicateKind = ty::PredicateKind<'tcx>;
type AllocId = crate::mir::interpret::AllocId;

type EarlyBoundRegion = ty::EarlyBoundRegion;
type BoundRegion = ty::BoundRegion;
type FreeRegion = ty::FreeRegion;
type RegionVid = ty::RegionVid;
type PlaceholderRegion = ty::PlaceholderRegion;
}

/// A type that is not publicly constructable. This prevents people from making [`TyKind::Error`]s
Expand All @@ -136,7 +142,7 @@ pub struct CtxtInterners<'tcx> {
type_: InternedSet<'tcx, WithStableHash<TyS<'tcx>>>,
substs: InternedSet<'tcx, InternalSubsts<'tcx>>,
canonical_var_infos: InternedSet<'tcx, List<CanonicalVarInfo<'tcx>>>,
region: InternedSet<'tcx, RegionKind>,
region: InternedSet<'tcx, RegionKind<'tcx>>,
poly_existential_predicates:
InternedSet<'tcx, List<ty::Binder<'tcx, ExistentialPredicate<'tcx>>>>,
predicate: InternedSet<'tcx, PredicateS<'tcx>>,
Expand Down Expand Up @@ -2175,7 +2181,7 @@ macro_rules! direct_interners {
}

direct_interners! {
region: mk_region(RegionKind): Region -> Region<'tcx>,
region: mk_region(RegionKind<'tcx>): Region -> Region<'tcx>,
const_: mk_const(ConstS<'tcx>): Const -> Const<'tcx>,
const_allocation: intern_const_alloc(Allocation): ConstAllocation -> ConstAllocation<'tcx>,
layout: intern_layout(LayoutS<'tcx>): Layout -> Layout<'tcx>,
Expand Down Expand Up @@ -2274,7 +2280,7 @@ impl<'tcx> TyCtxt<'tcx> {
/// Same a `self.mk_region(kind)`, but avoids accessing the interners if
/// `*r == kind`.
#[inline]
pub fn reuse_or_mk_region(self, r: Region<'tcx>, kind: RegionKind) -> Region<'tcx> {
pub fn reuse_or_mk_region(self, r: Region<'tcx>, kind: RegionKind<'tcx>) -> Region<'tcx> {
if *r == kind { r } else { self.mk_region(kind) }
}

Expand Down
42 changes: 5 additions & 37 deletions compiler/rustc_middle/src/ty/impls_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use rustc_data_structures::stable_hasher::HashingControls;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
use rustc_query_system::ich::StableHashingContext;
use std::cell::RefCell;
use std::mem;

impl<'a, 'tcx, T> HashStable<StableHashingContext<'a>> for &'tcx ty::List<T>
where
Expand Down Expand Up @@ -102,43 +101,12 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for ty::subst::GenericArgKin
}
}

impl<'a> HashStable<StableHashingContext<'a>> for ty::RegionKind {
impl<'a> HashStable<StableHashingContext<'a>> for ty::EarlyBoundRegion {
#[inline]
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
mem::discriminant(self).hash_stable(hcx, hasher);
match *self {
ty::ReErased | ty::ReStatic => {
// No variant fields to hash for these ...
}
ty::ReEmpty(universe) => {
universe.hash_stable(hcx, hasher);
}
ty::ReLateBound(db, ty::BoundRegion { kind: ty::BrAnon(i), .. }) => {
db.hash_stable(hcx, hasher);
i.hash_stable(hcx, hasher);
}
ty::ReLateBound(db, ty::BoundRegion { kind: ty::BrNamed(def_id, name), .. }) => {
db.hash_stable(hcx, hasher);
def_id.hash_stable(hcx, hasher);
name.hash_stable(hcx, hasher);
}
ty::ReLateBound(db, ty::BoundRegion { kind: ty::BrEnv, .. }) => {
db.hash_stable(hcx, hasher);
}
ty::ReEarlyBound(ty::EarlyBoundRegion { def_id, index, name }) => {
def_id.hash_stable(hcx, hasher);
index.hash_stable(hcx, hasher);
name.hash_stable(hcx, hasher);
}
ty::ReFree(ref free_region) => {
free_region.hash_stable(hcx, hasher);
}
ty::RePlaceholder(p) => {
p.hash_stable(hcx, hasher);
}
ty::ReVar(reg) => {
reg.hash_stable(hcx, hasher);
}
}
self.def_id.hash_stable(hcx, hasher);
self.index.hash_stable(hcx, hasher);
self.name.hash_stable(hcx, hasher);
}
}

Expand Down
79 changes: 1 addition & 78 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ use std::{fmt, str};

pub use crate::ty::diagnostics::*;
pub use rustc_type_ir::InferTy::*;
pub use rustc_type_ir::RegionKind::*;
pub use rustc_type_ir::TyKind::*;
pub use rustc_type_ir::*;

Expand All @@ -80,7 +81,6 @@ pub use self::list::List;
pub use self::parameterized::ParameterizedOverTcx;
pub use self::rvalue_scopes::RvalueScopes;
pub use self::sty::BoundRegionKind::*;
pub use self::sty::RegionKind::*;
pub use self::sty::{
Article, Binder, BoundRegion, BoundRegionKind, BoundTy, BoundTyKind, BoundVar,
BoundVariableKind, CanonicalPolyFnSig, ClosureSubsts, ClosureSubstsParts, ConstVid,
Expand Down Expand Up @@ -1161,83 +1161,6 @@ impl<'tcx> OpaqueHiddenType<'tcx> {
}
}

rustc_index::newtype_index! {
/// "Universes" are used during type- and trait-checking in the
/// presence of `for<..>` binders to control what sets of names are
/// visible. Universes are arranged into a tree: the root universe
/// contains names that are always visible. Each child then adds a new
/// set of names that are visible, in addition to those of its parent.
/// We say that the child universe "extends" the parent universe with
/// new names.
///
/// To make this more concrete, consider this program:
///
/// ```ignore (illustrative)
/// struct Foo { }
/// fn bar<T>(x: T) {
/// let y: for<'a> fn(&'a u8, Foo) = ...;
/// }
/// ```
///
/// The struct name `Foo` is in the root universe U0. But the type
/// parameter `T`, introduced on `bar`, is in an extended universe U1
/// -- i.e., within `bar`, we can name both `T` and `Foo`, but outside
/// of `bar`, we cannot name `T`. Then, within the type of `y`, the
/// region `'a` is in a universe U2 that extends U1, because we can
/// name it inside the fn type but not outside.
///
/// Universes are used to do type- and trait-checking around these
/// "forall" binders (also called **universal quantification**). The
/// idea is that when, in the body of `bar`, we refer to `T` as a
/// type, we aren't referring to any type in particular, but rather a
/// kind of "fresh" type that is distinct from all other types we have
/// actually declared. This is called a **placeholder** type, and we
/// use universes to talk about this. In other words, a type name in
/// universe 0 always corresponds to some "ground" type that the user
/// declared, but a type name in a non-zero universe is a placeholder
/// type -- an idealized representative of "types in general" that we
/// use for checking generic functions.
pub struct UniverseIndex {
derive [HashStable]
DEBUG_FORMAT = "U{}",
}
}

impl UniverseIndex {
pub const ROOT: UniverseIndex = UniverseIndex::from_u32(0);

/// Returns the "next" universe index in order -- this new index
/// is considered to extend all previous universes. This
/// corresponds to entering a `forall` quantifier. So, for
/// example, suppose we have this type in universe `U`:
///
/// ```ignore (illustrative)
/// for<'a> fn(&'a u32)
/// ```
///
/// Once we "enter" into this `for<'a>` quantifier, we are in a
/// new universe that extends `U` -- in this new universe, we can
/// name the region `'a`, but that region was not nameable from
/// `U` because it was not in scope there.
pub fn next_universe(self) -> UniverseIndex {
UniverseIndex::from_u32(self.private.checked_add(1).unwrap())
}

/// Returns `true` if `self` can name a name from `other` -- in other words,
/// if the set of names in `self` is a superset of those in
/// `other` (`self >= other`).
pub fn can_name(self, other: UniverseIndex) -> bool {
self.private >= other.private
}

/// Returns `true` if `self` cannot name some names from `other` -- in other
/// words, if the set of names in `self` is a strict subset of
/// those in `other` (`self < other`).
pub fn cannot_name(self, other: UniverseIndex) -> bool {
self.private < other.private
}
}

/// The "placeholder index" fully defines a placeholder region, type, or const. Placeholders are
/// identified by both a universe, as well as a name residing within that universe. Distinct bound
/// regions/types/consts within the same universe simply have an unknown relationship to one
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/ty/print/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub trait Printer<'tcx>: Sized {
self.default_print_impl_path(impl_def_id, substs, self_ty, trait_ref)
}

fn print_region(self, region: ty::Region<'_>) -> Result<Self::Region, Self::Error>;
fn print_region(self, region: ty::Region<'tcx>) -> Result<Self::Region, Self::Error>;

fn print_type(self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error>;

Expand Down Expand Up @@ -291,7 +291,7 @@ pub fn characteristic_def_id_of_type(ty: Ty<'_>) -> Option<DefId> {
characteristic_def_id_of_type_cached(ty, &mut SsoHashSet::new())
}

impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for ty::Region<'_> {
impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for ty::Region<'tcx> {
type Output = P::Region;
type Error = P::Error;
fn print(&self, cx: P) -> Result<Self::Output, Self::Error> {
Expand Down
22 changes: 7 additions & 15 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl<'tcx> RegionHighlightMode<'tcx> {
}

/// Returns `Some(n)` with the number to use for the given region, if any.
fn region_highlighted(&self, region: ty::Region<'_>) -> Option<usize> {
fn region_highlighted(&self, region: ty::Region<'tcx>) -> Option<usize> {
self.highlight_regions.iter().find_map(|h| match h {
Some((r, n)) if *r == region => Some(*n),
_ => None,
Expand Down Expand Up @@ -276,7 +276,7 @@ pub trait PrettyPrinter<'tcx>:
/// Returns `true` if the region should be printed in
/// optional positions, e.g., `&'a T` or `dyn Tr + 'b`.
/// This is typically the case for all non-`'_` regions.
fn should_print_region(&self, region: ty::Region<'_>) -> bool;
fn should_print_region(&self, region: ty::Region<'tcx>) -> bool;

// Defaults (should not be overridden):

Expand Down Expand Up @@ -1706,7 +1706,7 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> {
self.default_print_def_path(def_id, substs)
}

fn print_region(self, region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
fn print_region(self, region: ty::Region<'tcx>) -> Result<Self::Region, Self::Error> {
self.pretty_print_region(region)
}

Expand Down Expand Up @@ -1911,7 +1911,7 @@ impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> {
Ok(inner)
}

fn should_print_region(&self, region: ty::Region<'_>) -> bool {
fn should_print_region(&self, region: ty::Region<'tcx>) -> bool {
let highlight = self.region_highlight_mode;
if highlight.region_highlighted(region).is_some() {
return true;
Expand Down Expand Up @@ -1978,8 +1978,8 @@ impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> {
}

// HACK(eddyb) limited to `FmtPrinter` because of `region_highlight_mode`.
impl FmtPrinter<'_, '_> {
pub fn pretty_print_region(mut self, region: ty::Region<'_>) -> Result<Self, fmt::Error> {
impl<'tcx> FmtPrinter<'_, 'tcx> {
pub fn pretty_print_region(mut self, region: ty::Region<'tcx>) -> Result<Self, fmt::Error> {
define_scoped_cx!(self);

// Watch out for region highlights.
Expand Down Expand Up @@ -2383,15 +2383,6 @@ macro_rules! define_print_and_forward_display {
};
}

// HACK(eddyb) this is separate because `ty::RegionKind` doesn't need lifting.
impl<'tcx> fmt::Display for ty::Region<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
ty::tls::with(|tcx| {
f.write_str(&self.print(FmtPrinter::new(tcx, Namespace::TypeNS))?.into_buffer())
})
}
}

/// Wrapper type for `ty::TraitRef` which opts-in to pretty printing only
/// the trait path. That is, it will print `Trait<U>` instead of
/// `<T as Trait<U>>`.
Expand Down Expand Up @@ -2456,6 +2447,7 @@ impl<'tcx> ty::PolyTraitPredicate<'tcx> {
}

forward_display_to_print! {
ty::Region<'tcx>,
Ty<'tcx>,
&'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>>,
ty::Const<'tcx>,
Expand Down
24 changes: 0 additions & 24 deletions compiler/rustc_middle/src/ty/structural_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,30 +81,6 @@ impl fmt::Debug for ty::BoundRegionKind {
}
}

impl fmt::Debug for ty::RegionKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
ty::ReEarlyBound(ref data) => write!(f, "ReEarlyBound({}, {})", data.index, data.name),

ty::ReLateBound(binder_id, ref bound_region) => {
write!(f, "ReLateBound({:?}, {:?})", binder_id, bound_region)
}

ty::ReFree(ref fr) => fr.fmt(f),

ty::ReStatic => write!(f, "ReStatic"),

ty::ReVar(ref vid) => vid.fmt(f),

ty::RePlaceholder(placeholder) => write!(f, "RePlaceholder({:?})", placeholder),

ty::ReEmpty(ui) => write!(f, "ReEmpty({:?})", ui),

ty::ReErased => write!(f, "ReErased"),
}
}
}

impl fmt::Debug for ty::FreeRegion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ReFree({:?}, {:?})", self.scope, self.bound_region)
Expand Down
Loading

0 comments on commit 408cd93

Please sign in to comment.