Skip to content

Commit d1d761f

Browse files
Rollup merge of rust-lang#117586 - compiler-errors:the-canonicalizer, r=lcnr
Uplift the (new solver) canonicalizer into `rustc_next_trait_solver` Uplifts the new trait solver's canonicalizer into a new crate called `rustc_next_trait_solver`. The crate name is literally a bikeshed-avoidance name, so let's not block this PR on that -- renames are welcome later. There are a host of other changes that were required to make this possible: * Expose a `ConstTy` trait to get the `Interner::Ty` from a `Interner::Const`. * Expose some constructor methods to construct `Bound` variants. These are currently methods defined on the interner themselves, but they could be pulled into traits later. * Expose a `IntoKind` trait to turn a `Ty`/`Const`/`Region` into their corresponding `*Kind`s. * Some minor tweaks to other APIs in `rustc_type_ir`. The canonicalizer code itself is best reviewed **with whitespace ignored.** r? `@lcnr`
2 parents 95aa0b4 + 1c1df45 commit d1d761f

File tree

20 files changed

+478
-260
lines changed

20 files changed

+478
-260
lines changed

Cargo.lock

+8
Original file line numberDiff line numberDiff line change
@@ -4308,6 +4308,13 @@ dependencies = [
43084308
"tracing",
43094309
]
43104310

4311+
[[package]]
4312+
name = "rustc_next_trait_solver"
4313+
version = "0.0.0"
4314+
dependencies = [
4315+
"rustc_type_ir",
4316+
]
4317+
43114318
[[package]]
43124319
name = "rustc_parse"
43134320
version = "0.0.0"
@@ -4576,6 +4583,7 @@ dependencies = [
45764583
"rustc_infer",
45774584
"rustc_macros",
45784585
"rustc_middle",
4586+
"rustc_next_trait_solver",
45794587
"rustc_parse_format",
45804588
"rustc_query_system",
45814589
"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

+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

+25
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,31 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
130130
) -> (Self::Ty, ty::Mutability) {
131131
(ty, mutbl)
132132
}
133+
134+
fn mk_canonical_var_infos(self, infos: &[ty::CanonicalVarInfo<Self>]) -> Self::CanonicalVars {
135+
self.mk_canonical_var_infos(infos)
136+
}
137+
138+
fn mk_bound_ty(self, debruijn: ty::DebruijnIndex, var: ty::BoundVar) -> Self::Ty {
139+
Ty::new_bound(self, debruijn, ty::BoundTy { var, kind: ty::BoundTyKind::Anon })
140+
}
141+
142+
fn mk_bound_region(self, debruijn: ty::DebruijnIndex, var: ty::BoundVar) -> Self::Region {
143+
Region::new_bound(
144+
self,
145+
debruijn,
146+
ty::BoundRegion { var, kind: ty::BoundRegionKind::BrAnon },
147+
)
148+
}
149+
150+
fn mk_bound_const(
151+
self,
152+
debruijn: ty::DebruijnIndex,
153+
var: ty::BoundVar,
154+
ty: Self::Ty,
155+
) -> Self::Const {
156+
Const::new_bound(self, debruijn, var, ty)
157+
}
133158
}
134159

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

compiler/rustc_middle/src/ty/mod.rs

+29-14
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,10 @@ use std::ops::ControlFlow;
6565
use std::{fmt, str};
6666

6767
pub use crate::ty::diagnostics::*;
68-
pub use rustc_type_ir::AliasKind::*;
6968
pub use rustc_type_ir::ConstKind::{
7069
Bound as BoundCt, Error as ErrorCt, Expr as ExprCt, Infer as InferCt, Param as ParamCt,
7170
Placeholder as PlaceholderCt, Unevaluated, Value,
7271
};
73-
pub use rustc_type_ir::DynKind::*;
74-
pub use rustc_type_ir::InferTy::*;
75-
pub use rustc_type_ir::RegionKind::*;
76-
pub use rustc_type_ir::TyKind::*;
7772
pub use rustc_type_ir::*;
7873

7974
pub use self::binding::BindingMode;
@@ -474,6 +469,14 @@ pub struct CReaderCacheKey {
474469
#[rustc_pass_by_value]
475470
pub struct Ty<'tcx>(Interned<'tcx, WithCachedTypeInfo<TyKind<'tcx>>>);
476471

472+
impl<'tcx> IntoKind for Ty<'tcx> {
473+
type Kind = TyKind<'tcx>;
474+
475+
fn kind(self) -> TyKind<'tcx> {
476+
self.kind().clone()
477+
}
478+
}
479+
477480
impl EarlyParamRegion {
478481
/// Does this early bound region have a name? Early bound regions normally
479482
/// always have names except when using anonymous lifetimes (`'_`).
@@ -1506,34 +1509,42 @@ pub struct Placeholder<T> {
15061509

15071510
pub type PlaceholderRegion = Placeholder<BoundRegion>;
15081511

1509-
impl rustc_type_ir::Placeholder for PlaceholderRegion {
1510-
fn universe(&self) -> UniverseIndex {
1512+
impl PlaceholderLike for PlaceholderRegion {
1513+
fn universe(self) -> UniverseIndex {
15111514
self.universe
15121515
}
15131516

1514-
fn var(&self) -> BoundVar {
1517+
fn var(self) -> BoundVar {
15151518
self.bound.var
15161519
}
15171520

15181521
fn with_updated_universe(self, ui: UniverseIndex) -> Self {
15191522
Placeholder { universe: ui, ..self }
15201523
}
1524+
1525+
fn new(ui: UniverseIndex, var: BoundVar) -> Self {
1526+
Placeholder { universe: ui, bound: BoundRegion { var, kind: BoundRegionKind::BrAnon } }
1527+
}
15211528
}
15221529

15231530
pub type PlaceholderType = Placeholder<BoundTy>;
15241531

1525-
impl rustc_type_ir::Placeholder for PlaceholderType {
1526-
fn universe(&self) -> UniverseIndex {
1532+
impl PlaceholderLike for PlaceholderType {
1533+
fn universe(self) -> UniverseIndex {
15271534
self.universe
15281535
}
15291536

1530-
fn var(&self) -> BoundVar {
1537+
fn var(self) -> BoundVar {
15311538
self.bound.var
15321539
}
15331540

15341541
fn with_updated_universe(self, ui: UniverseIndex) -> Self {
15351542
Placeholder { universe: ui, ..self }
15361543
}
1544+
1545+
fn new(ui: UniverseIndex, var: BoundVar) -> Self {
1546+
Placeholder { universe: ui, bound: BoundTy { var, kind: BoundTyKind::Anon } }
1547+
}
15371548
}
15381549

15391550
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable)]
@@ -1545,18 +1556,22 @@ pub struct BoundConst<'tcx> {
15451556

15461557
pub type PlaceholderConst = Placeholder<BoundVar>;
15471558

1548-
impl rustc_type_ir::Placeholder for PlaceholderConst {
1549-
fn universe(&self) -> UniverseIndex {
1559+
impl PlaceholderLike for PlaceholderConst {
1560+
fn universe(self) -> UniverseIndex {
15501561
self.universe
15511562
}
15521563

1553-
fn var(&self) -> BoundVar {
1564+
fn var(self) -> BoundVar {
15541565
self.bound
15551566
}
15561567

15571568
fn with_updated_universe(self, ui: UniverseIndex) -> Self {
15581569
Placeholder { universe: ui, ..self }
15591570
}
1571+
1572+
fn new(ui: UniverseIndex, var: BoundVar) -> Self {
1573+
Placeholder { universe: ui, bound: var }
1574+
}
15601575
}
15611576

15621577
/// When type checking, we use the `ParamEnv` to track

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,13 @@
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", default-features = false }
8+
9+
[features]
10+
default = ["nightly"]
11+
nightly = [
12+
"rustc_type_ir/nightly",
13+
]

0 commit comments

Comments
 (0)