Skip to content

Commit a79bcfc

Browse files
Uplift canonicalizer into new trait solver crate
1 parent fe3038f commit a79bcfc

File tree

20 files changed

+509
-272
lines changed

20 files changed

+509
-272
lines changed

Cargo.lock

+8
Original file line numberDiff line numberDiff line change
@@ -4288,6 +4288,13 @@ dependencies = [
42884288
"tracing",
42894289
]
42904290

4291+
[[package]]
4292+
name = "rustc_next_trait_solver"
4293+
version = "0.0.0"
4294+
dependencies = [
4295+
"rustc_type_ir",
4296+
]
4297+
42914298
[[package]]
42924299
name = "rustc_parse"
42934300
version = "0.0.0"
@@ -4556,6 +4563,7 @@ dependencies = [
45564563
"rustc_infer",
45574564
"rustc_macros",
45584565
"rustc_middle",
4566+
"rustc_next_trait_solver",
45594567
"rustc_parse_format",
45604568
"rustc_query_system",
45614569
"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.
@@ -1348,6 +1372,10 @@ impl<'tcx> InferCtxt<'tcx> {
13481372
self.inner.borrow_mut().type_variables().root_var(var)
13491373
}
13501374

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

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

+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;
@@ -26,6 +28,20 @@ use super::sty::ConstKind;
2628
#[rustc_pass_by_value]
2729
pub struct Const<'tcx>(pub(super) Interned<'tcx, WithCachedTypeInfo<ConstData<'tcx>>>);
2830

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

+26-6
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 (`'_`).
@@ -1522,8 +1530,12 @@ impl rustc_type_ir::Placeholder for PlaceholderRegion {
15221530
self.bound.var
15231531
}
15241532

1525-
fn with_updated_universe(self, ui: UniverseIndex) -> Self {
1526-
Placeholder { universe: ui, ..self }
1533+
fn with_updated_universe(&self, ui: UniverseIndex) -> Self {
1534+
Placeholder { universe: ui, ..*self }
1535+
}
1536+
1537+
fn new(ui: UniverseIndex, var: BoundVar) -> Self {
1538+
Placeholder { universe: ui, bound: BoundRegion { var, kind: BoundRegionKind::BrAnon } }
15271539
}
15281540
}
15291541

@@ -1538,8 +1550,12 @@ impl rustc_type_ir::Placeholder for PlaceholderType {
15381550
self.bound.var
15391551
}
15401552

1541-
fn with_updated_universe(self, ui: UniverseIndex) -> Self {
1542-
Placeholder { universe: ui, ..self }
1553+
fn with_updated_universe(&self, ui: UniverseIndex) -> Self {
1554+
Placeholder { universe: ui, ..*self }
1555+
}
1556+
1557+
fn new(ui: UniverseIndex, var: BoundVar) -> Self {
1558+
Placeholder { universe: ui, bound: BoundTy { var, kind: BoundTyKind::Anon } }
15431559
}
15441560
}
15451561

@@ -1561,8 +1577,12 @@ impl rustc_type_ir::Placeholder for PlaceholderConst {
15611577
self.bound
15621578
}
15631579

1564-
fn with_updated_universe(self, ui: UniverseIndex) -> Self {
1565-
Placeholder { universe: ui, ..self }
1580+
fn with_updated_universe(&self, ui: UniverseIndex) -> Self {
1581+
Placeholder { universe: ui, ..*self }
1582+
}
1583+
1584+
fn new(ui: UniverseIndex, var: BoundVar) -> Self {
1585+
Placeholder { universe: ui, bound: var }
15661586
}
15671587
}
15681588

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(
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)