Skip to content

Commit 3a5a1df

Browse files
Uplift canonicalizer into rustc_type_ir
1 parent 73bc121 commit 3a5a1df

File tree

13 files changed

+335
-219
lines changed

13 files changed

+335
-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
@@ -130,6 +130,42 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
130130
) -> (Self::Ty, ty::Mutability) {
131131
(ty, mutbl)
132132
}
133+
134+
fn mk_canonical_var_infos(
135+
&self,
136+
infos: &[rustc_type_ir::CanonicalVarInfo<Self>],
137+
) -> Self::CanonicalVars {
138+
(*self).mk_canonical_var_infos(infos)
139+
}
140+
141+
fn mk_bound_ty(
142+
&self,
143+
debruijn: rustc_type_ir::DebruijnIndex,
144+
var: rustc_type_ir::BoundVar,
145+
) -> Self::Ty {
146+
Ty::new_bound(*self, debruijn, ty::BoundTy { var, kind: ty::BoundTyKind::Anon })
147+
}
148+
149+
fn mk_bound_region(
150+
&self,
151+
debruijn: rustc_type_ir::DebruijnIndex,
152+
var: rustc_type_ir::BoundVar,
153+
) -> Self::Region {
154+
Region::new_late_bound(
155+
*self,
156+
debruijn,
157+
ty::BoundRegion { var, kind: ty::BoundRegionKind::BrAnon },
158+
)
159+
}
160+
161+
fn mk_bound_const(
162+
&self,
163+
debruijn: rustc_type_ir::DebruijnIndex,
164+
var: rustc_type_ir::BoundVar,
165+
ty: Self::Ty,
166+
) -> Self::Const {
167+
Const::new_bound(*self, debruijn, var, ty)
168+
}
133169
}
134170

135171
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
@@ -462,6 +462,14 @@ pub struct CReaderCacheKey {
462462
#[rustc_pass_by_value]
463463
pub struct Ty<'tcx>(Interned<'tcx, WithCachedTypeInfo<TyKind<'tcx>>>);
464464

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

15181526
pub type PlaceholderRegion = Placeholder<BoundRegion>;
15191527

1520-
impl rustc_type_ir::Placeholder for PlaceholderRegion {
1528+
impl<'tcx> rustc_type_ir::Placeholder<TyCtxt<'tcx>> for PlaceholderRegion {
15211529
fn universe(&self) -> UniverseIndex {
15221530
self.universe
15231531
}
@@ -1526,14 +1534,18 @@ impl rustc_type_ir::Placeholder for PlaceholderRegion {
15261534
self.bound.var
15271535
}
15281536

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

15341546
pub type PlaceholderType = Placeholder<BoundTy>;
15351547

1536-
impl rustc_type_ir::Placeholder for PlaceholderType {
1548+
impl<'tcx> rustc_type_ir::Placeholder<TyCtxt<'tcx>> for PlaceholderType {
15371549
fn universe(&self) -> UniverseIndex {
15381550
self.universe
15391551
}
@@ -1542,8 +1554,12 @@ impl rustc_type_ir::Placeholder for PlaceholderType {
15421554
self.bound.var
15431555
}
15441556

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

@@ -1556,7 +1572,7 @@ pub struct BoundConst<'tcx> {
15561572

15571573
pub type PlaceholderConst = Placeholder<BoundVar>;
15581574

1559-
impl rustc_type_ir::Placeholder for PlaceholderConst {
1575+
impl<'tcx> rustc_type_ir::Placeholder<TyCtxt<'tcx>> for PlaceholderConst {
15601576
fn universe(&self) -> UniverseIndex {
15611577
self.universe
15621578
}
@@ -1565,8 +1581,12 @@ impl rustc_type_ir::Placeholder for PlaceholderConst {
15651581
self.bound
15661582
}
15671583

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

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};
@@ -1489,6 +1489,14 @@ impl ParamConst {
14891489
#[rustc_pass_by_value]
14901490
pub struct Region<'tcx>(pub Interned<'tcx, RegionKind<'tcx>>);
14911491

1492+
impl<'tcx> IntoKind for Region<'tcx> {
1493+
type Kind = RegionKind<'tcx>;
1494+
1495+
fn kind(&self) -> RegionKind<'tcx> {
1496+
**self
1497+
}
1498+
}
1499+
14921500
impl<'tcx> Region<'tcx> {
14931501
#[inline]
14941502
pub fn new_early_param(

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
@@ -30,7 +30,6 @@ use rustc_middle::ty::{
3030

3131
mod alias_relate;
3232
mod assembly;
33-
mod canonicalize;
3433
mod eval_ctxt;
3534
mod fulfill;
3635
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)