Skip to content

Commit 56f9aff

Browse files
authored
Unrolled build for rust-lang#117586
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 2d2f1b2 + 80f240a commit 56f9aff

File tree

20 files changed

+477
-261
lines changed

20 files changed

+477
-261
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

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

138163
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 (`'_`).
@@ -1545,34 +1548,42 @@ pub struct Placeholder<T> {
15451548

15461549
pub type PlaceholderRegion = Placeholder<BoundRegion>;
15471550

1548-
impl rustc_type_ir::Placeholder for PlaceholderRegion {
1549-
fn universe(&self) -> UniverseIndex {
1551+
impl PlaceholderLike for PlaceholderRegion {
1552+
fn universe(self) -> UniverseIndex {
15501553
self.universe
15511554
}
15521555

1553-
fn var(&self) -> BoundVar {
1556+
fn var(self) -> BoundVar {
15541557
self.bound.var
15551558
}
15561559

15571560
fn with_updated_universe(self, ui: UniverseIndex) -> Self {
15581561
Placeholder { universe: ui, ..self }
15591562
}
1563+
1564+
fn new(ui: UniverseIndex, var: BoundVar) -> Self {
1565+
Placeholder { universe: ui, bound: BoundRegion { var, kind: BoundRegionKind::BrAnon } }
1566+
}
15601567
}
15611568

15621569
pub type PlaceholderType = Placeholder<BoundTy>;
15631570

1564-
impl rustc_type_ir::Placeholder for PlaceholderType {
1565-
fn universe(&self) -> UniverseIndex {
1571+
impl PlaceholderLike for PlaceholderType {
1572+
fn universe(self) -> UniverseIndex {
15661573
self.universe
15671574
}
15681575

1569-
fn var(&self) -> BoundVar {
1576+
fn var(self) -> BoundVar {
15701577
self.bound.var
15711578
}
15721579

15731580
fn with_updated_universe(self, ui: UniverseIndex) -> Self {
15741581
Placeholder { universe: ui, ..self }
15751582
}
1583+
1584+
fn new(ui: UniverseIndex, var: BoundVar) -> Self {
1585+
Placeholder { universe: ui, bound: BoundTy { var, kind: BoundTyKind::Anon } }
1586+
}
15761587
}
15771588

15781589
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable)]
@@ -1584,18 +1595,22 @@ pub struct BoundConst<'tcx> {
15841595

15851596
pub type PlaceholderConst = Placeholder<BoundVar>;
15861597

1587-
impl rustc_type_ir::Placeholder for PlaceholderConst {
1588-
fn universe(&self) -> UniverseIndex {
1598+
impl PlaceholderLike for PlaceholderConst {
1599+
fn universe(self) -> UniverseIndex {
15891600
self.universe
15901601
}
15911602

1592-
fn var(&self) -> BoundVar {
1603+
fn var(self) -> BoundVar {
15931604
self.bound
15941605
}
15951606

15961607
fn with_updated_universe(self, ui: UniverseIndex) -> Self {
15971608
Placeholder { universe: ui, ..self }
15981609
}
1610+
1611+
fn new(ui: UniverseIndex, var: BoundVar) -> Self {
1612+
Placeholder { universe: ui, bound: var }
1613+
}
15991614
}
16001615

16011616
/// 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)