Skip to content

Commit cb41509

Browse files
Uplift canonicalizer into new trait solver crate
1 parent ae612be commit cb41509

File tree

20 files changed

+506
-272
lines changed

20 files changed

+506
-272
lines changed

Cargo.lock

+8
Original file line numberDiff line numberDiff line change
@@ -4303,6 +4303,13 @@ dependencies = [
43034303
"tracing",
43044304
]
43054305

4306+
[[package]]
4307+
name = "rustc_next_trait_solver"
4308+
version = "0.0.0"
4309+
dependencies = [
4310+
"rustc_type_ir",
4311+
]
4312+
43064313
[[package]]
43074314
name = "rustc_parse"
43084315
version = "0.0.0"
@@ -4571,6 +4578,7 @@ dependencies = [
45714578
"rustc_infer",
45724579
"rustc_macros",
45734580
"rustc_middle",
4581+
"rustc_next_trait_solver",
45744582
"rustc_parse_format",
45754583
"rustc_query_system",
45764584
"rustc_session",

compiler/rustc_infer/src/infer/mod.rs

+50-22
Original file line numberDiff line numberDiff line change
@@ -345,37 +345,61 @@ pub struct InferCtxt<'tcx> {
345345
impl<'tcx> ty::InferCtxtLike for InferCtxt<'tcx> {
346346
type Interner = TyCtxt<'tcx>;
347347

348-
fn universe_of_ty(&self, ty: ty::InferTy) -> Option<ty::UniverseIndex> {
349-
use InferTy::*;
350-
match ty {
351-
// FIXME(BoxyUwU): this is kind of jank and means that printing unresolved
352-
// ty infers will give you the universe of the var it resolved to not the universe
353-
// it actually had. It also means that if you have a `?0.1` and infer it to `u8` then
354-
// try to print out `?0.1` it will just print `?0`.
355-
TyVar(ty_vid) => match self.probe_ty_var(ty_vid) {
356-
Err(universe) => Some(universe),
357-
Ok(_) => None,
358-
},
359-
IntVar(_) | FloatVar(_) | FreshTy(_) | FreshIntTy(_) | FreshFloatTy(_) => None,
348+
fn interner(&self) -> TyCtxt<'tcx> {
349+
self.tcx
350+
}
351+
352+
fn universe_of_ty(&self, vid: TyVid) -> Option<ty::UniverseIndex> {
353+
// FIXME(BoxyUwU): this is kind of jank and means that printing unresolved
354+
// ty infers will give you the universe of the var it resolved to not the universe
355+
// it actually had. It also means that if you have a `?0.1` and infer it to `u8` then
356+
// try to print out `?0.1` it will just print `?0`.
357+
match self.probe_ty_var(vid) {
358+
Err(universe) => Some(universe),
359+
Ok(_) => None,
360360
}
361361
}
362362

363-
fn universe_of_ct(&self, ct: ty::InferConst) -> Option<ty::UniverseIndex> {
364-
use ty::InferConst::*;
365-
match ct {
366-
// Same issue as with `universe_of_ty`
367-
Var(ct_vid) => match self.probe_const_var(ct_vid) {
368-
Err(universe) => Some(universe),
369-
Ok(_) => None,
370-
},
371-
EffectVar(_) => None,
372-
Fresh(_) => None,
363+
fn universe_of_ct(&self, ct: ConstVid) -> Option<ty::UniverseIndex> {
364+
// Same issue as with `universe_of_ty`
365+
match self.probe_const_var(ct) {
366+
Err(universe) => Some(universe),
367+
Ok(_) => None,
373368
}
374369
}
375370

376371
fn universe_of_lt(&self, lt: ty::RegionVid) -> Option<ty::UniverseIndex> {
377372
Some(self.universe_of_region_vid(lt))
378373
}
374+
375+
fn root_ty_var(&self, vid: TyVid) -> TyVid {
376+
self.root_var(vid)
377+
}
378+
379+
fn probe_ty_var(&self, vid: TyVid) -> Option<Ty<'tcx>> {
380+
self.probe_ty_var(vid).ok()
381+
}
382+
383+
fn root_lt_var(&self, vid: ty::RegionVid) -> ty::RegionVid {
384+
self.root_region_var(vid)
385+
}
386+
387+
fn probe_lt_var(&self, vid: ty::RegionVid) -> Option<ty::Region<'tcx>> {
388+
let re = self
389+
.inner
390+
.borrow_mut()
391+
.unwrap_region_constraints()
392+
.opportunistic_resolve_var(self.tcx, vid);
393+
if re.is_var() { None } else { Some(re) }
394+
}
395+
396+
fn root_ct_var(&self, vid: ConstVid) -> ConstVid {
397+
self.root_const_var(vid)
398+
}
399+
400+
fn probe_ct_var(&self, vid: ConstVid) -> Option<ty::Const<'tcx>> {
401+
self.probe_const_var(vid).ok()
402+
}
379403
}
380404

381405
/// See the `error_reporting` module for more details.
@@ -1347,6 +1371,10 @@ impl<'tcx> InferCtxt<'tcx> {
13471371
self.inner.borrow_mut().type_variables().root_var(var)
13481372
}
13491373

1374+
pub fn root_region_var(&self, var: ty::RegionVid) -> ty::RegionVid {
1375+
self.inner.borrow_mut().unwrap_region_constraints().root_var(var)
1376+
}
1377+
13501378
pub fn root_const_var(&self, var: ty::ConstVid) -> ty::ConstVid {
13511379
self.inner.borrow_mut().const_unification_table().find(var).vid
13521380
}

compiler/rustc_infer/src/infer/region_constraints/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,11 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
623623
}
624624
}
625625

626+
pub fn root_var(&mut self, vid: ty::RegionVid) -> ty::RegionVid {
627+
let mut ut = self.unification_table_mut(); // FIXME(rust-lang/ena#42): unnecessary mut
628+
ut.find(vid).vid
629+
}
630+
626631
fn combine_map(&mut self, t: CombineMapType) -> &mut CombineMap<'tcx> {
627632
match t {
628633
Glb => &mut self.glbs,

compiler/rustc_middle/src/ty/consts.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_hir as hir;
77
use rustc_hir::def::{DefKind, Res};
88
use rustc_hir::def_id::LocalDefId;
99
use rustc_macros::HashStable;
10-
use rustc_type_ir::{TypeFlags, WithCachedTypeInfo};
10+
use rustc_type_ir::{ConstTy, IntoKind, TypeFlags, WithCachedTypeInfo};
1111

1212
mod int;
1313
mod kind;
@@ -26,6 +26,20 @@ use super::sty::ConstKind;
2626
#[rustc_pass_by_value]
2727
pub struct Const<'tcx>(pub(super) Interned<'tcx, WithCachedTypeInfo<ConstData<'tcx>>>);
2828

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

compiler/rustc_middle/src/ty/context.rs

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

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

compiler/rustc_middle/src/ty/mod.rs

+26-6
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,14 @@ pub struct CReaderCacheKey {
474474
#[rustc_pass_by_value]
475475
pub struct Ty<'tcx>(Interned<'tcx, WithCachedTypeInfo<TyKind<'tcx>>>);
476476

477+
impl<'tcx> IntoKind for Ty<'tcx> {
478+
type Kind = TyKind<'tcx>;
479+
480+
fn kind(&self) -> TyKind<'tcx> {
481+
(*self).kind().clone()
482+
}
483+
}
484+
477485
impl EarlyParamRegion {
478486
/// Does this early bound region have a name? Early bound regions normally
479487
/// always have names except when using anonymous lifetimes (`'_`).
@@ -1554,8 +1562,12 @@ impl rustc_type_ir::Placeholder for PlaceholderRegion {
15541562
self.bound.var
15551563
}
15561564

1557-
fn with_updated_universe(self, ui: UniverseIndex) -> Self {
1558-
Placeholder { universe: ui, ..self }
1565+
fn with_updated_universe(&self, ui: UniverseIndex) -> Self {
1566+
Placeholder { universe: ui, ..*self }
1567+
}
1568+
1569+
fn new(ui: UniverseIndex, var: BoundVar) -> Self {
1570+
Placeholder { universe: ui, bound: BoundRegion { var, kind: BoundRegionKind::BrAnon } }
15591571
}
15601572
}
15611573

@@ -1570,8 +1582,12 @@ impl rustc_type_ir::Placeholder for PlaceholderType {
15701582
self.bound.var
15711583
}
15721584

1573-
fn with_updated_universe(self, ui: UniverseIndex) -> Self {
1574-
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: BoundTy { var, kind: BoundTyKind::Anon } }
15751591
}
15761592
}
15771593

@@ -1593,8 +1609,12 @@ impl rustc_type_ir::Placeholder for PlaceholderConst {
15931609
self.bound
15941610
}
15951611

1596-
fn with_updated_universe(self, ui: UniverseIndex) -> Self {
1597-
Placeholder { universe: ui, ..self }
1612+
fn with_updated_universe(&self, ui: UniverseIndex) -> Self {
1613+
Placeholder { universe: ui, ..*self }
1614+
}
1615+
1616+
fn new(ui: UniverseIndex, var: BoundVar) -> Self {
1617+
Placeholder { universe: ui, bound: var }
15981618
}
15991619
}
16001620

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};
@@ -1477,6 +1477,14 @@ impl ParamConst {
14771477
#[rustc_pass_by_value]
14781478
pub struct Region<'tcx>(pub Interned<'tcx, RegionKind<'tcx>>);
14791479

1480+
impl<'tcx> IntoKind for Region<'tcx> {
1481+
type Kind = RegionKind<'tcx>;
1482+
1483+
fn kind(&self) -> RegionKind<'tcx> {
1484+
**self
1485+
}
1486+
}
1487+
14801488
impl<'tcx> Region<'tcx> {
14811489
#[inline]
14821490
pub fn new_early_param(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "rustc_next_trait_solver"
3+
version = "0.0.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
rustc_type_ir = { path = "../rustc_type_ir" }

0 commit comments

Comments
 (0)