Skip to content

Commit bb8c2f4

Browse files
committed
Auto merge of rust-lang#98247 - jackh726:regionkind-rustc-type-ir, r=compiler-errors
Move RegionKind to rustc_type_ir (Also UniverseIndex) r? rust-lang/types
2 parents 2b646bd + 1e9f8df commit bb8c2f4

File tree

12 files changed

+597
-344
lines changed

12 files changed

+597
-344
lines changed

compiler/rustc_borrowck/src/region_infer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
500500
}
501501

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

compiler/rustc_middle/src/ty/context.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
114114
type DelaySpanBugEmitted = DelaySpanBugEmitted;
115115
type PredicateKind = ty::PredicateKind<'tcx>;
116116
type AllocId = crate::mir::interpret::AllocId;
117+
118+
type EarlyBoundRegion = ty::EarlyBoundRegion;
119+
type BoundRegion = ty::BoundRegion;
120+
type FreeRegion = ty::FreeRegion;
121+
type RegionVid = ty::RegionVid;
122+
type PlaceholderRegion = ty::PlaceholderRegion;
117123
}
118124

119125
/// A type that is not publicly constructable. This prevents people from making [`TyKind::Error`]s
@@ -136,7 +142,7 @@ pub struct CtxtInterners<'tcx> {
136142
type_: InternedSet<'tcx, WithStableHash<TyS<'tcx>>>,
137143
substs: InternedSet<'tcx, InternalSubsts<'tcx>>,
138144
canonical_var_infos: InternedSet<'tcx, List<CanonicalVarInfo<'tcx>>>,
139-
region: InternedSet<'tcx, RegionKind>,
145+
region: InternedSet<'tcx, RegionKind<'tcx>>,
140146
poly_existential_predicates:
141147
InternedSet<'tcx, List<ty::Binder<'tcx, ExistentialPredicate<'tcx>>>>,
142148
predicate: InternedSet<'tcx, PredicateS<'tcx>>,
@@ -2175,7 +2181,7 @@ macro_rules! direct_interners {
21752181
}
21762182

21772183
direct_interners! {
2178-
region: mk_region(RegionKind): Region -> Region<'tcx>,
2184+
region: mk_region(RegionKind<'tcx>): Region -> Region<'tcx>,
21792185
const_: mk_const(ConstS<'tcx>): Const -> Const<'tcx>,
21802186
const_allocation: intern_const_alloc(Allocation): ConstAllocation -> ConstAllocation<'tcx>,
21812187
layout: intern_layout(LayoutS<'tcx>): Layout -> Layout<'tcx>,
@@ -2274,7 +2280,7 @@ impl<'tcx> TyCtxt<'tcx> {
22742280
/// Same a `self.mk_region(kind)`, but avoids accessing the interners if
22752281
/// `*r == kind`.
22762282
#[inline]
2277-
pub fn reuse_or_mk_region(self, r: Region<'tcx>, kind: RegionKind) -> Region<'tcx> {
2283+
pub fn reuse_or_mk_region(self, r: Region<'tcx>, kind: RegionKind<'tcx>) -> Region<'tcx> {
22782284
if *r == kind { r } else { self.mk_region(kind) }
22792285
}
22802286

compiler/rustc_middle/src/ty/impls_ty.rs

+5-37
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use rustc_data_structures::stable_hasher::HashingControls;
1111
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
1212
use rustc_query_system::ich::StableHashingContext;
1313
use std::cell::RefCell;
14-
use std::mem;
1514

1615
impl<'a, 'tcx, T> HashStable<StableHashingContext<'a>> for &'tcx ty::List<T>
1716
where
@@ -102,43 +101,12 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for ty::subst::GenericArgKin
102101
}
103102
}
104103

105-
impl<'a> HashStable<StableHashingContext<'a>> for ty::RegionKind {
104+
impl<'a> HashStable<StableHashingContext<'a>> for ty::EarlyBoundRegion {
105+
#[inline]
106106
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
107-
mem::discriminant(self).hash_stable(hcx, hasher);
108-
match *self {
109-
ty::ReErased | ty::ReStatic => {
110-
// No variant fields to hash for these ...
111-
}
112-
ty::ReEmpty(universe) => {
113-
universe.hash_stable(hcx, hasher);
114-
}
115-
ty::ReLateBound(db, ty::BoundRegion { kind: ty::BrAnon(i), .. }) => {
116-
db.hash_stable(hcx, hasher);
117-
i.hash_stable(hcx, hasher);
118-
}
119-
ty::ReLateBound(db, ty::BoundRegion { kind: ty::BrNamed(def_id, name), .. }) => {
120-
db.hash_stable(hcx, hasher);
121-
def_id.hash_stable(hcx, hasher);
122-
name.hash_stable(hcx, hasher);
123-
}
124-
ty::ReLateBound(db, ty::BoundRegion { kind: ty::BrEnv, .. }) => {
125-
db.hash_stable(hcx, hasher);
126-
}
127-
ty::ReEarlyBound(ty::EarlyBoundRegion { def_id, index, name }) => {
128-
def_id.hash_stable(hcx, hasher);
129-
index.hash_stable(hcx, hasher);
130-
name.hash_stable(hcx, hasher);
131-
}
132-
ty::ReFree(ref free_region) => {
133-
free_region.hash_stable(hcx, hasher);
134-
}
135-
ty::RePlaceholder(p) => {
136-
p.hash_stable(hcx, hasher);
137-
}
138-
ty::ReVar(reg) => {
139-
reg.hash_stable(hcx, hasher);
140-
}
141-
}
107+
self.def_id.hash_stable(hcx, hasher);
108+
self.index.hash_stable(hcx, hasher);
109+
self.name.hash_stable(hcx, hasher);
142110
}
143111
}
144112

compiler/rustc_middle/src/ty/mod.rs

+1-78
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ use std::{fmt, str};
5555

5656
pub use crate::ty::diagnostics::*;
5757
pub use rustc_type_ir::InferTy::*;
58+
pub use rustc_type_ir::RegionKind::*;
5859
pub use rustc_type_ir::TyKind::*;
5960
pub use rustc_type_ir::*;
6061

@@ -80,7 +81,6 @@ pub use self::list::List;
8081
pub use self::parameterized::ParameterizedOverTcx;
8182
pub use self::rvalue_scopes::RvalueScopes;
8283
pub use self::sty::BoundRegionKind::*;
83-
pub use self::sty::RegionKind::*;
8484
pub use self::sty::{
8585
Article, Binder, BoundRegion, BoundRegionKind, BoundTy, BoundTyKind, BoundVar,
8686
BoundVariableKind, CanonicalPolyFnSig, ClosureSubsts, ClosureSubstsParts, ConstVid,
@@ -1161,83 +1161,6 @@ impl<'tcx> OpaqueHiddenType<'tcx> {
11611161
}
11621162
}
11631163

1164-
rustc_index::newtype_index! {
1165-
/// "Universes" are used during type- and trait-checking in the
1166-
/// presence of `for<..>` binders to control what sets of names are
1167-
/// visible. Universes are arranged into a tree: the root universe
1168-
/// contains names that are always visible. Each child then adds a new
1169-
/// set of names that are visible, in addition to those of its parent.
1170-
/// We say that the child universe "extends" the parent universe with
1171-
/// new names.
1172-
///
1173-
/// To make this more concrete, consider this program:
1174-
///
1175-
/// ```ignore (illustrative)
1176-
/// struct Foo { }
1177-
/// fn bar<T>(x: T) {
1178-
/// let y: for<'a> fn(&'a u8, Foo) = ...;
1179-
/// }
1180-
/// ```
1181-
///
1182-
/// The struct name `Foo` is in the root universe U0. But the type
1183-
/// parameter `T`, introduced on `bar`, is in an extended universe U1
1184-
/// -- i.e., within `bar`, we can name both `T` and `Foo`, but outside
1185-
/// of `bar`, we cannot name `T`. Then, within the type of `y`, the
1186-
/// region `'a` is in a universe U2 that extends U1, because we can
1187-
/// name it inside the fn type but not outside.
1188-
///
1189-
/// Universes are used to do type- and trait-checking around these
1190-
/// "forall" binders (also called **universal quantification**). The
1191-
/// idea is that when, in the body of `bar`, we refer to `T` as a
1192-
/// type, we aren't referring to any type in particular, but rather a
1193-
/// kind of "fresh" type that is distinct from all other types we have
1194-
/// actually declared. This is called a **placeholder** type, and we
1195-
/// use universes to talk about this. In other words, a type name in
1196-
/// universe 0 always corresponds to some "ground" type that the user
1197-
/// declared, but a type name in a non-zero universe is a placeholder
1198-
/// type -- an idealized representative of "types in general" that we
1199-
/// use for checking generic functions.
1200-
pub struct UniverseIndex {
1201-
derive [HashStable]
1202-
DEBUG_FORMAT = "U{}",
1203-
}
1204-
}
1205-
1206-
impl UniverseIndex {
1207-
pub const ROOT: UniverseIndex = UniverseIndex::from_u32(0);
1208-
1209-
/// Returns the "next" universe index in order -- this new index
1210-
/// is considered to extend all previous universes. This
1211-
/// corresponds to entering a `forall` quantifier. So, for
1212-
/// example, suppose we have this type in universe `U`:
1213-
///
1214-
/// ```ignore (illustrative)
1215-
/// for<'a> fn(&'a u32)
1216-
/// ```
1217-
///
1218-
/// Once we "enter" into this `for<'a>` quantifier, we are in a
1219-
/// new universe that extends `U` -- in this new universe, we can
1220-
/// name the region `'a`, but that region was not nameable from
1221-
/// `U` because it was not in scope there.
1222-
pub fn next_universe(self) -> UniverseIndex {
1223-
UniverseIndex::from_u32(self.private.checked_add(1).unwrap())
1224-
}
1225-
1226-
/// Returns `true` if `self` can name a name from `other` -- in other words,
1227-
/// if the set of names in `self` is a superset of those in
1228-
/// `other` (`self >= other`).
1229-
pub fn can_name(self, other: UniverseIndex) -> bool {
1230-
self.private >= other.private
1231-
}
1232-
1233-
/// Returns `true` if `self` cannot name some names from `other` -- in other
1234-
/// words, if the set of names in `self` is a strict subset of
1235-
/// those in `other` (`self < other`).
1236-
pub fn cannot_name(self, other: UniverseIndex) -> bool {
1237-
self.private < other.private
1238-
}
1239-
}
1240-
12411164
/// The "placeholder index" fully defines a placeholder region, type, or const. Placeholders are
12421165
/// identified by both a universe, as well as a name residing within that universe. Distinct bound
12431166
/// regions/types/consts within the same universe simply have an unknown relationship to one

compiler/rustc_middle/src/ty/print/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub trait Printer<'tcx>: Sized {
5757
self.default_print_impl_path(impl_def_id, substs, self_ty, trait_ref)
5858
}
5959

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

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

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

294-
impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for ty::Region<'_> {
294+
impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for ty::Region<'tcx> {
295295
type Output = P::Region;
296296
type Error = P::Error;
297297
fn print(&self, cx: P) -> Result<Self::Output, Self::Error> {

compiler/rustc_middle/src/ty/print/pretty.rs

+7-15
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ impl<'tcx> RegionHighlightMode<'tcx> {
182182
}
183183

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

281281
// Defaults (should not be overridden):
282282

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

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

@@ -1911,7 +1911,7 @@ impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> {
19111911
Ok(inner)
19121912
}
19131913

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

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

19851985
// Watch out for region highlights.
@@ -2383,15 +2383,6 @@ macro_rules! define_print_and_forward_display {
23832383
};
23842384
}
23852385

2386-
// HACK(eddyb) this is separate because `ty::RegionKind` doesn't need lifting.
2387-
impl<'tcx> fmt::Display for ty::Region<'tcx> {
2388-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2389-
ty::tls::with(|tcx| {
2390-
f.write_str(&self.print(FmtPrinter::new(tcx, Namespace::TypeNS))?.into_buffer())
2391-
})
2392-
}
2393-
}
2394-
23952386
/// Wrapper type for `ty::TraitRef` which opts-in to pretty printing only
23962387
/// the trait path. That is, it will print `Trait<U>` instead of
23972388
/// `<T as Trait<U>>`.
@@ -2456,6 +2447,7 @@ impl<'tcx> ty::PolyTraitPredicate<'tcx> {
24562447
}
24572448

24582449
forward_display_to_print! {
2450+
ty::Region<'tcx>,
24592451
Ty<'tcx>,
24602452
&'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>>,
24612453
ty::Const<'tcx>,

compiler/rustc_middle/src/ty/structural_impls.rs

-24
Original file line numberDiff line numberDiff line change
@@ -81,30 +81,6 @@ impl fmt::Debug for ty::BoundRegionKind {
8181
}
8282
}
8383

84-
impl fmt::Debug for ty::RegionKind {
85-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86-
match *self {
87-
ty::ReEarlyBound(ref data) => write!(f, "ReEarlyBound({}, {})", data.index, data.name),
88-
89-
ty::ReLateBound(binder_id, ref bound_region) => {
90-
write!(f, "ReLateBound({:?}, {:?})", binder_id, bound_region)
91-
}
92-
93-
ty::ReFree(ref fr) => fr.fmt(f),
94-
95-
ty::ReStatic => write!(f, "ReStatic"),
96-
97-
ty::ReVar(ref vid) => vid.fmt(f),
98-
99-
ty::RePlaceholder(placeholder) => write!(f, "RePlaceholder({:?})", placeholder),
100-
101-
ty::ReEmpty(ui) => write!(f, "ReEmpty({:?})", ui),
102-
103-
ty::ReErased => write!(f, "ReErased"),
104-
}
105-
}
106-
}
107-
10884
impl fmt::Debug for ty::FreeRegion {
10985
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11086
write!(f, "ReFree({:?}, {:?})", self.scope, self.bound_region)

0 commit comments

Comments
 (0)