Skip to content

Commit 773e972

Browse files
Uplift canonicalizer into rustc_type_ir
1 parent ea26331 commit 773e972

File tree

13 files changed

+330
-219
lines changed

13 files changed

+330
-219
lines changed

compiler/rustc_infer/src/infer/mod.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,10 @@ pub struct InferCtxt<'tcx> {
345345
impl<'tcx> ty::InferCtxtLike for InferCtxt<'tcx> {
346346
type Interner = TyCtxt<'tcx>;
347347

348+
fn interner(&self) -> TyCtxt<'tcx> {
349+
self.tcx
350+
}
351+
348352
fn universe_of_ty(&self, ty: ty::InferTy) -> Option<ty::UniverseIndex> {
349353
use InferTy::*;
350354
match ty {
@@ -356,7 +360,8 @@ impl<'tcx> ty::InferCtxtLike for InferCtxt<'tcx> {
356360
Err(universe) => Some(universe),
357361
Ok(_) => None,
358362
},
359-
IntVar(_) | FloatVar(_) | FreshTy(_) | FreshIntTy(_) | FreshFloatTy(_) => None,
363+
IntVar(_) | FloatVar(_) => Some(ty::UniverseIndex::ROOT),
364+
FreshTy(_) | FreshIntTy(_) | FreshFloatTy(_) => None,
360365
}
361366
}
362367

@@ -368,7 +373,7 @@ impl<'tcx> ty::InferCtxtLike for InferCtxt<'tcx> {
368373
Err(universe) => Some(universe),
369374
Ok(_) => None,
370375
},
371-
EffectVar(_) => None,
376+
EffectVar(_) => Some(ty::UniverseIndex::ROOT),
372377
Fresh(_) => None,
373378
}
374379
}

compiler/rustc_middle/src/ty/consts.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::middle::resolve_bound_vars as rbv;
22
use crate::mir::interpret::{AllocId, ErrorHandled, LitToConstInput, Scalar};
3-
use crate::ty::{self, GenericArgs, ParamEnv, ParamEnvAnd, Ty, TyCtxt, TypeVisitableExt};
3+
use crate::ty::{
4+
self, ConstTy, GenericArgs, IntoKind, ParamEnv, ParamEnvAnd, Ty, TyCtxt, TypeVisitableExt,
5+
};
46
use rustc_data_structures::intern::Interned;
57
use rustc_error_messages::MultiSpan;
68
use rustc_hir as hir;
@@ -25,6 +27,20 @@ use super::sty::ConstKind;
2527
#[rustc_pass_by_value]
2628
pub struct Const<'tcx>(pub(super) Interned<'tcx, ConstData<'tcx>>);
2729

30+
impl<'tcx> IntoKind for Const<'tcx> {
31+
type Kind = ConstKind<'tcx>;
32+
33+
fn kind(&self) -> ConstKind<'tcx> {
34+
(*self).kind().clone()
35+
}
36+
}
37+
38+
impl<'tcx> ConstTy<TyCtxt<'tcx>> for Const<'tcx> {
39+
fn ty(&self) -> Ty<'tcx> {
40+
(*self).ty()
41+
}
42+
}
43+
2844
/// Typed constant value.
2945
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, HashStable, TyEncodable, TyDecodable)]
3046
pub struct ConstData<'tcx> {

compiler/rustc_middle/src/ty/context.rs

+36
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,42 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
133133
) -> (Self::Ty, ty::Mutability) {
134134
(ty, mutbl)
135135
}
136+
137+
fn mk_canonical_var_infos(
138+
&self,
139+
infos: &[rustc_type_ir::CanonicalVarInfo<Self>],
140+
) -> Self::CanonicalVars {
141+
(*self).mk_canonical_var_infos(infos)
142+
}
143+
144+
fn mk_bound_ty(
145+
&self,
146+
debruijn: rustc_type_ir::DebruijnIndex,
147+
var: rustc_type_ir::BoundVar,
148+
) -> Self::Ty {
149+
Ty::new_bound(*self, debruijn, ty::BoundTy { var, kind: ty::BoundTyKind::Anon })
150+
}
151+
152+
fn mk_bound_region(
153+
&self,
154+
debruijn: rustc_type_ir::DebruijnIndex,
155+
var: rustc_type_ir::BoundVar,
156+
) -> Self::Region {
157+
Region::new_late_bound(
158+
*self,
159+
debruijn,
160+
ty::BoundRegion { var, kind: ty::BoundRegionKind::BrAnon },
161+
)
162+
}
163+
164+
fn mk_bound_const(
165+
&self,
166+
debruijn: rustc_type_ir::DebruijnIndex,
167+
var: rustc_type_ir::BoundVar,
168+
ty: Self::Ty,
169+
) -> Self::Const {
170+
Const::new_bound(*self, debruijn, var, ty)
171+
}
136172
}
137173

138174
type InternedSet<'tcx, T> = ShardedHashMap<InternedInSet<'tcx, T>, ()>;

compiler/rustc_middle/src/ty/mod.rs

+29-9
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,14 @@ pub struct CReaderCacheKey {
463463
#[rustc_pass_by_value]
464464
pub struct Ty<'tcx>(Interned<'tcx, WithCachedTypeInfo<TyKind<'tcx>>>);
465465

466+
impl<'tcx> IntoKind for Ty<'tcx> {
467+
type Kind = TyKind<'tcx>;
468+
469+
fn kind(&self) -> TyKind<'tcx> {
470+
(*self).kind().clone()
471+
}
472+
}
473+
466474
impl ty::EarlyBoundRegion {
467475
/// Does this early bound region have a name? Early bound regions normally
468476
/// always have names except when using anonymous lifetimes (`'_`).
@@ -1518,7 +1526,7 @@ pub struct Placeholder<T> {
15181526

15191527
pub type PlaceholderRegion = Placeholder<BoundRegion>;
15201528

1521-
impl rustc_type_ir::Placeholder for PlaceholderRegion {
1529+
impl<'tcx> rustc_type_ir::Placeholder<TyCtxt<'tcx>> for PlaceholderRegion {
15221530
fn universe(&self) -> UniverseIndex {
15231531
self.universe
15241532
}
@@ -1527,14 +1535,18 @@ impl rustc_type_ir::Placeholder for PlaceholderRegion {
15271535
self.bound.var
15281536
}
15291537

1530-
fn with_updated_universe(self, ui: UniverseIndex) -> Self {
1531-
Placeholder { universe: ui, ..self }
1538+
fn with_updated_universe(&self, ui: UniverseIndex) -> Self {
1539+
Placeholder { universe: ui, ..*self }
1540+
}
1541+
1542+
fn new(ui: UniverseIndex, var: BoundVar) -> Self {
1543+
Placeholder { universe: ui, bound: BoundRegion { var, kind: BoundRegionKind::BrAnon } }
15321544
}
15331545
}
15341546

15351547
pub type PlaceholderType = Placeholder<BoundTy>;
15361548

1537-
impl rustc_type_ir::Placeholder for PlaceholderType {
1549+
impl<'tcx> rustc_type_ir::Placeholder<TyCtxt<'tcx>> for PlaceholderType {
15381550
fn universe(&self) -> UniverseIndex {
15391551
self.universe
15401552
}
@@ -1543,8 +1555,12 @@ impl rustc_type_ir::Placeholder for PlaceholderType {
15431555
self.bound.var
15441556
}
15451557

1546-
fn with_updated_universe(self, ui: UniverseIndex) -> Self {
1547-
Placeholder { universe: ui, ..self }
1558+
fn with_updated_universe(&self, ui: UniverseIndex) -> Self {
1559+
Placeholder { universe: ui, ..*self }
1560+
}
1561+
1562+
fn new(ui: UniverseIndex, var: BoundVar) -> Self {
1563+
Placeholder { universe: ui, bound: BoundTy { var, kind: BoundTyKind::Anon } }
15481564
}
15491565
}
15501566

@@ -1557,7 +1573,7 @@ pub struct BoundConst<'tcx> {
15571573

15581574
pub type PlaceholderConst = Placeholder<BoundVar>;
15591575

1560-
impl rustc_type_ir::Placeholder for PlaceholderConst {
1576+
impl<'tcx> rustc_type_ir::Placeholder<TyCtxt<'tcx>> for PlaceholderConst {
15611577
fn universe(&self) -> UniverseIndex {
15621578
self.universe
15631579
}
@@ -1566,8 +1582,12 @@ impl rustc_type_ir::Placeholder for PlaceholderConst {
15661582
self.bound
15671583
}
15681584

1569-
fn with_updated_universe(self, ui: UniverseIndex) -> Self {
1570-
Placeholder { universe: ui, ..self }
1585+
fn with_updated_universe(&self, ui: UniverseIndex) -> Self {
1586+
Placeholder { universe: ui, ..*self }
1587+
}
1588+
1589+
fn new(ui: UniverseIndex, var: BoundVar) -> Self {
1590+
Placeholder { universe: ui, bound: var }
15711591
}
15721592
}
15731593

compiler/rustc_middle/src/ty/sty.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::infer::canonical::Canonical;
66
use crate::ty::visit::ValidateBoundVars;
77
use crate::ty::InferTy::*;
88
use crate::ty::{
9-
self, AdtDef, Discr, Term, Ty, TyCtxt, TypeFlags, TypeSuperVisitable, TypeVisitable,
9+
self, AdtDef, Discr, IntoKind, Term, Ty, TyCtxt, TypeFlags, TypeSuperVisitable, TypeVisitable,
1010
TypeVisitableExt, TypeVisitor,
1111
};
1212
use crate::ty::{GenericArg, GenericArgs, GenericArgsRef};
@@ -1467,6 +1467,14 @@ impl ParamConst {
14671467
#[rustc_pass_by_value]
14681468
pub struct Region<'tcx>(pub Interned<'tcx, RegionKind<'tcx>>);
14691469

1470+
impl<'tcx> IntoKind for Region<'tcx> {
1471+
type Kind = RegionKind<'tcx>;
1472+
1473+
fn kind(&self) -> RegionKind<'tcx> {
1474+
**self
1475+
}
1476+
}
1477+
14701478
impl<'tcx> Region<'tcx> {
14711479
#[inline]
14721480
pub fn new_early_bound(

compiler/rustc_trait_selection/src/solve/eval_ctxt/canonical.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
//!
1010
//! [c]: https://rustc-dev-guide.rust-lang.org/solve/canonicalization.html
1111
use super::{CanonicalInput, Certainty, EvalCtxt, Goal};
12-
use crate::solve::canonicalize::{CanonicalizeMode, Canonicalizer};
1312
use crate::solve::{
1413
inspect, response_no_constraints_raw, CanonicalResponse, QueryResult, Response,
1514
};
@@ -25,6 +24,7 @@ use rustc_middle::traits::solve::{
2524
ExternalConstraintsData, MaybeCause, PredefinedOpaquesData, QueryInput,
2625
};
2726
use rustc_middle::traits::ObligationCause;
27+
use rustc_middle::ty::canonicalizer::{CanonicalizeMode, Canonicalizer};
2828
use rustc_middle::ty::{
2929
self, BoundVar, GenericArgKind, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable,
3030
TypeVisitableExt,

compiler/rustc_trait_selection/src/solve/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use rustc_middle::ty::{
2929

3030
mod alias_relate;
3131
mod assembly;
32-
mod canonicalize;
3332
mod eval_ctxt;
3433
mod fulfill;
3534
pub mod inspect;

compiler/rustc_type_ir/src/canonical.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl<I: Interner> CanonicalVarInfo<I> {
162162
}
163163

164164
#[must_use]
165-
pub fn with_updated_universe(self, ui: UniverseIndex) -> CanonicalVarInfo<I> {
165+
pub fn with_updated_universe(&self, ui: UniverseIndex) -> CanonicalVarInfo<I> {
166166
CanonicalVarInfo { kind: self.kind.with_updated_universe(ui) }
167167
}
168168

@@ -324,13 +324,13 @@ impl<I: Interner> CanonicalVarKind<I> {
324324
///
325325
/// In case this is a float or int variable, this causes an ICE if
326326
/// the updated universe is not the root.
327-
pub fn with_updated_universe(self, ui: UniverseIndex) -> CanonicalVarKind<I> {
327+
pub fn with_updated_universe(&self, ui: UniverseIndex) -> CanonicalVarKind<I> {
328328
match self {
329329
CanonicalVarKind::Ty(CanonicalTyVarKind::General(_)) => {
330330
CanonicalVarKind::Ty(CanonicalTyVarKind::General(ui))
331331
}
332332
CanonicalVarKind::Region(_) => CanonicalVarKind::Region(ui),
333-
CanonicalVarKind::Const(_, ty) => CanonicalVarKind::Const(ui, ty),
333+
CanonicalVarKind::Const(_, ty) => CanonicalVarKind::Const(ui, ty.clone()),
334334

335335
CanonicalVarKind::PlaceholderTy(placeholder) => {
336336
CanonicalVarKind::PlaceholderTy(placeholder.with_updated_universe(ui))
@@ -339,12 +339,15 @@ impl<I: Interner> CanonicalVarKind<I> {
339339
CanonicalVarKind::PlaceholderRegion(placeholder.with_updated_universe(ui))
340340
}
341341
CanonicalVarKind::PlaceholderConst(placeholder, ty) => {
342-
CanonicalVarKind::PlaceholderConst(placeholder.with_updated_universe(ui), ty)
342+
CanonicalVarKind::PlaceholderConst(
343+
placeholder.with_updated_universe(ui),
344+
ty.clone(),
345+
)
343346
}
344347
CanonicalVarKind::Ty(CanonicalTyVarKind::Int | CanonicalTyVarKind::Float)
345348
| CanonicalVarKind::Effect => {
346349
assert_eq!(ui, UniverseIndex::ROOT);
347-
self
350+
self.clone()
348351
}
349352
}
350353
}

0 commit comments

Comments
 (0)