Skip to content

Commit cf86088

Browse files
Uplift GenericArgKind, CanonicalVarValues, QueryInput, and NestedGoals
1 parent 1a73979 commit cf86088

File tree

26 files changed

+398
-289
lines changed

26 files changed

+398
-289
lines changed

Cargo.lock

+7
Original file line numberDiff line numberDiff line change
@@ -4343,6 +4343,7 @@ dependencies = [
43434343
"rustc_hir_pretty",
43444344
"rustc_index",
43454345
"rustc_macros",
4346+
"rustc_next_trait_solver",
43464347
"rustc_query_system",
43474348
"rustc_serialize",
43484349
"rustc_session",
@@ -4451,7 +4452,13 @@ dependencies = [
44514452
name = "rustc_next_trait_solver"
44524453
version = "0.0.0"
44534454
dependencies = [
4455+
"derivative",
4456+
"rustc_ast_ir",
4457+
"rustc_data_structures",
4458+
"rustc_macros",
4459+
"rustc_serialize",
44544460
"rustc_type_ir",
4461+
"rustc_type_ir_macros",
44554462
]
44564463

44574464
[[package]]

compiler/rustc_infer/src/infer/at.rs

+19-7
Original file line numberDiff line numberDiff line change
@@ -385,19 +385,31 @@ impl<'tcx> ToTrace<'tcx> for ty::GenericArg<'tcx> {
385385
a: Self,
386386
b: Self,
387387
) -> TypeTrace<'tcx> {
388-
use GenericArgKind::*;
389388
TypeTrace {
390389
cause: cause.clone(),
391390
values: match (a.unpack(), b.unpack()) {
392-
(Lifetime(a), Lifetime(b)) => Regions(ExpectedFound::new(a_is_expected, a, b)),
393-
(Type(a), Type(b)) => Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())),
394-
(Const(a), Const(b)) => {
391+
(GenericArgKind::Lifetime(a), GenericArgKind::Lifetime(b)) => {
392+
Regions(ExpectedFound::new(a_is_expected, a, b))
393+
}
394+
(GenericArgKind::Type(a), GenericArgKind::Type(b)) => {
395+
Terms(ExpectedFound::new(a_is_expected, a.into(), b.into()))
396+
}
397+
(GenericArgKind::Const(a), GenericArgKind::Const(b)) => {
395398
Terms(ExpectedFound::new(a_is_expected, a.into(), b.into()))
396399
}
397400

398-
(Lifetime(_), Type(_) | Const(_))
399-
| (Type(_), Lifetime(_) | Const(_))
400-
| (Const(_), Lifetime(_) | Type(_)) => {
401+
(
402+
GenericArgKind::Lifetime(_),
403+
GenericArgKind::Type(_) | GenericArgKind::Const(_),
404+
)
405+
| (
406+
GenericArgKind::Type(_),
407+
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_),
408+
)
409+
| (
410+
GenericArgKind::Const(_),
411+
GenericArgKind::Lifetime(_) | GenericArgKind::Type(_),
412+
) => {
401413
bug!("relating different kinds: {a:?} {b:?}")
402414
}
403415
},

compiler/rustc_infer/src/traits/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ impl<T: Hash> Hash for Obligation<'_, T> {
7878
}
7979
}
8080

81-
impl<'tcx, P> From<Obligation<'tcx, P>> for ty::Goal<'tcx, P> {
81+
impl<'tcx, P> From<Obligation<'tcx, P>> for solve::Goal<'tcx, P> {
8282
fn from(value: Obligation<'tcx, P>) -> Self {
83-
ty::Goal { param_env: value.param_env, predicate: value.predicate }
83+
solve::Goal { param_env: value.param_env, predicate: value.predicate }
8484
}
8585
}
8686

compiler/rustc_middle/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ rustc_hir = { path = "../rustc_hir" }
2828
rustc_hir_pretty = { path = "../rustc_hir_pretty" }
2929
rustc_index = { path = "../rustc_index" }
3030
rustc_macros = { path = "../rustc_macros" }
31+
rustc_next_trait_solver = { path = "../rustc_next_trait_solver" }
3132
rustc_query_system = { path = "../rustc_query_system" }
3233
rustc_serialize = { path = "../rustc_serialize" }
3334
rustc_session = { path = "../rustc_session" }

compiler/rustc_middle/src/infer/canonical.rs

+6-149
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,20 @@
2323
2424
use rustc_data_structures::fx::FxHashMap;
2525
use rustc_data_structures::sync::Lock;
26-
use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
27-
use rustc_type_ir::Canonical as IrCanonical;
28-
use rustc_type_ir::CanonicalVarInfo as IrCanonicalVarInfo;
26+
use rustc_macros::{HashStable, TypeFoldable, TypeVisitable};
27+
pub use rustc_type_ir as ir;
2928
pub use rustc_type_ir::{CanonicalTyVarKind, CanonicalVarKind};
3029
use smallvec::SmallVec;
3130
use std::collections::hash_map::Entry;
32-
use std::ops::Index;
3331

3432
use crate::infer::MemberConstraint;
3533
use crate::mir::ConstraintCategory;
3634
use crate::ty::GenericArg;
37-
use crate::ty::{self, BoundVar, List, Region, Ty, TyCtxt, TypeFlags, TypeVisitableExt};
38-
39-
pub type Canonical<'tcx, V> = IrCanonical<TyCtxt<'tcx>, V>;
40-
41-
pub type CanonicalVarInfo<'tcx> = IrCanonicalVarInfo<TyCtxt<'tcx>>;
35+
use crate::ty::{self, List, Region, Ty, TyCtxt, TypeFlags, TypeVisitableExt};
4236

37+
pub type Canonical<'tcx, V> = ir::Canonical<TyCtxt<'tcx>, V>;
38+
pub type CanonicalVarInfo<'tcx> = ir::CanonicalVarInfo<TyCtxt<'tcx>>;
39+
pub type CanonicalVarValues<'tcx> = ir::CanonicalVarValues<TyCtxt<'tcx>>;
4340
pub type CanonicalVarInfos<'tcx> = &'tcx List<CanonicalVarInfo<'tcx>>;
4441

4542
impl<'tcx> ty::TypeFoldable<TyCtxt<'tcx>> for CanonicalVarInfos<'tcx> {
@@ -51,74 +48,6 @@ impl<'tcx> ty::TypeFoldable<TyCtxt<'tcx>> for CanonicalVarInfos<'tcx> {
5148
}
5249
}
5350

54-
/// A set of values corresponding to the canonical variables from some
55-
/// `Canonical`. You can give these values to
56-
/// `canonical_value.instantiate` to instantiate them into the canonical
57-
/// value at the right places.
58-
///
59-
/// When you canonicalize a value `V`, you get back one of these
60-
/// vectors with the original values that were replaced by canonical
61-
/// variables. You will need to supply it later to instantiate the
62-
/// canonicalized query response.
63-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, TyDecodable, TyEncodable)]
64-
#[derive(HashStable, TypeFoldable, TypeVisitable)]
65-
pub struct CanonicalVarValues<'tcx> {
66-
pub var_values: ty::GenericArgsRef<'tcx>,
67-
}
68-
69-
impl CanonicalVarValues<'_> {
70-
pub fn is_identity(&self) -> bool {
71-
self.var_values.iter().enumerate().all(|(bv, arg)| match arg.unpack() {
72-
ty::GenericArgKind::Lifetime(r) => {
73-
matches!(*r, ty::ReBound(ty::INNERMOST, br) if br.var.as_usize() == bv)
74-
}
75-
ty::GenericArgKind::Type(ty) => {
76-
matches!(*ty.kind(), ty::Bound(ty::INNERMOST, bt) if bt.var.as_usize() == bv)
77-
}
78-
ty::GenericArgKind::Const(ct) => {
79-
matches!(ct.kind(), ty::ConstKind::Bound(ty::INNERMOST, bc) if bc.as_usize() == bv)
80-
}
81-
})
82-
}
83-
84-
pub fn is_identity_modulo_regions(&self) -> bool {
85-
let mut var = ty::BoundVar::ZERO;
86-
for arg in self.var_values {
87-
match arg.unpack() {
88-
ty::GenericArgKind::Lifetime(r) => {
89-
if let ty::ReBound(ty::INNERMOST, br) = *r
90-
&& var == br.var
91-
{
92-
var = var + 1;
93-
} else {
94-
// It's ok if this region var isn't unique
95-
}
96-
}
97-
ty::GenericArgKind::Type(ty) => {
98-
if let ty::Bound(ty::INNERMOST, bt) = *ty.kind()
99-
&& var == bt.var
100-
{
101-
var = var + 1;
102-
} else {
103-
return false;
104-
}
105-
}
106-
ty::GenericArgKind::Const(ct) => {
107-
if let ty::ConstKind::Bound(ty::INNERMOST, bc) = ct.kind()
108-
&& var == bc
109-
{
110-
var = var + 1;
111-
} else {
112-
return false;
113-
}
114-
}
115-
}
116-
}
117-
118-
true
119-
}
120-
}
121-
12251
/// When we canonicalize a value to form a query, we wind up replacing
12352
/// various parts of it with canonical variables. This struct stores
12453
/// those replaced bits to remember for when we process the query
@@ -218,78 +147,6 @@ TrivialTypeTraversalImpls! {
218147
crate::infer::canonical::Certainty,
219148
}
220149

221-
impl<'tcx> CanonicalVarValues<'tcx> {
222-
// Given a list of canonical variables, construct a set of values which are
223-
// the identity response.
224-
pub fn make_identity(
225-
tcx: TyCtxt<'tcx>,
226-
infos: CanonicalVarInfos<'tcx>,
227-
) -> CanonicalVarValues<'tcx> {
228-
CanonicalVarValues {
229-
var_values: tcx.mk_args_from_iter(infos.iter().enumerate().map(
230-
|(i, info)| -> ty::GenericArg<'tcx> {
231-
match info.kind {
232-
CanonicalVarKind::Ty(_) | CanonicalVarKind::PlaceholderTy(_) => {
233-
Ty::new_bound(tcx, ty::INNERMOST, ty::BoundVar::from_usize(i).into())
234-
.into()
235-
}
236-
CanonicalVarKind::Region(_) | CanonicalVarKind::PlaceholderRegion(_) => {
237-
let br = ty::BoundRegion {
238-
var: ty::BoundVar::from_usize(i),
239-
kind: ty::BrAnon,
240-
};
241-
ty::Region::new_bound(tcx, ty::INNERMOST, br).into()
242-
}
243-
CanonicalVarKind::Effect => ty::Const::new_bound(
244-
tcx,
245-
ty::INNERMOST,
246-
ty::BoundVar::from_usize(i),
247-
tcx.types.bool,
248-
)
249-
.into(),
250-
CanonicalVarKind::Const(_, ty)
251-
| CanonicalVarKind::PlaceholderConst(_, ty) => ty::Const::new_bound(
252-
tcx,
253-
ty::INNERMOST,
254-
ty::BoundVar::from_usize(i),
255-
ty,
256-
)
257-
.into(),
258-
}
259-
},
260-
)),
261-
}
262-
}
263-
264-
/// Creates dummy var values which should not be used in a
265-
/// canonical response.
266-
pub fn dummy() -> CanonicalVarValues<'tcx> {
267-
CanonicalVarValues { var_values: ty::List::empty() }
268-
}
269-
270-
#[inline]
271-
pub fn len(&self) -> usize {
272-
self.var_values.len()
273-
}
274-
}
275-
276-
impl<'a, 'tcx> IntoIterator for &'a CanonicalVarValues<'tcx> {
277-
type Item = GenericArg<'tcx>;
278-
type IntoIter = ::std::iter::Copied<::std::slice::Iter<'a, GenericArg<'tcx>>>;
279-
280-
fn into_iter(self) -> Self::IntoIter {
281-
self.var_values.iter()
282-
}
283-
}
284-
285-
impl<'tcx> Index<BoundVar> for CanonicalVarValues<'tcx> {
286-
type Output = GenericArg<'tcx>;
287-
288-
fn index(&self, value: BoundVar) -> &GenericArg<'tcx> {
289-
&self.var_values[value.as_usize()]
290-
}
291-
}
292-
293150
#[derive(Default)]
294151
pub struct CanonicalParamEnvCache<'tcx> {
295152
map: Lock<

compiler/rustc_middle/src/traits/solve.rs

+5-31
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use rustc_ast_ir::try_visit;
22
use rustc_data_structures::intern::Interned;
33
use rustc_macros::{HashStable, TypeFoldable, TypeVisitable};
4+
use rustc_next_trait_solver::solve as ir_solve;
5+
pub use rustc_next_trait_solver::solve::*;
46
use rustc_span::def_id::DefId;
57

68
use crate::infer::canonical::{CanonicalVarValues, QueryRegionConstraints};
@@ -9,8 +11,6 @@ use crate::traits::Canonical;
911
use crate::ty::{
1012
self, FallibleTypeFolder, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeVisitable, TypeVisitor,
1113
};
12-
// FIXME(compiler-errors): remove this import in favor of `use rustc_middle::ty::Goal`.
13-
pub use crate::ty::Goal;
1414

1515
use super::BuiltinImplSource;
1616

@@ -19,6 +19,9 @@ pub mod inspect;
1919

2020
pub use cache::{CacheData, EvaluationCache};
2121

22+
pub type Goal<'tcx, P> = ir_solve::Goal<TyCtxt<'tcx>, P>;
23+
pub type QueryInput<'tcx, P> = ir_solve::QueryInput<TyCtxt<'tcx>, P>;
24+
2225
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, HashStable, TypeFoldable, TypeVisitable)]
2326
pub struct Response<'tcx> {
2427
pub certainty: Certainty,
@@ -87,12 +90,6 @@ impl MaybeCause {
8790
}
8891
}
8992

90-
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, HashStable, TypeFoldable, TypeVisitable)]
91-
pub struct QueryInput<'tcx, T> {
92-
pub goal: Goal<'tcx, T>,
93-
pub predefined_opaques_in_body: PredefinedOpaques<'tcx>,
94-
}
95-
9693
/// Additional constraints returned on success.
9794
#[derive(Debug, PartialEq, Eq, Clone, Hash, HashStable, Default)]
9895
pub struct PredefinedOpaquesData<'tcx> {
@@ -229,29 +226,6 @@ impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for PredefinedOpaques<'tcx> {
229226
}
230227
}
231228

232-
/// Why a specific goal has to be proven.
233-
///
234-
/// This is necessary as we treat nested goals different depending on
235-
/// their source.
236-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable, TypeVisitable, TypeFoldable)]
237-
pub enum GoalSource {
238-
Misc,
239-
/// We're proving a where-bound of an impl.
240-
///
241-
/// FIXME(-Znext-solver=coinductive): Explain how and why this
242-
/// changes whether cycles are coinductive.
243-
///
244-
/// This also impacts whether we erase constraints on overflow.
245-
/// Erasing constraints is generally very useful for perf and also
246-
/// results in better error messages by avoiding spurious errors.
247-
/// We do not erase overflow constraints in `normalizes-to` goals unless
248-
/// they are from an impl where-clause. This is necessary due to
249-
/// backwards compatability, cc trait-system-refactor-initiatitive#70.
250-
ImplWhereBound,
251-
/// Instantiating a higher-ranked goal and re-proving it.
252-
InstantiateHigherRanked,
253-
}
254-
255229
/// Possible ways the given goal can be proven.
256230
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
257231
pub enum CandidateSource {

compiler/rustc_middle/src/ty/context.rs

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ use std::ops::{Bound, Deref};
9090
impl<'tcx> Interner for TyCtxt<'tcx> {
9191
type DefId = DefId;
9292
type DefiningOpaqueTypes = &'tcx ty::List<LocalDefId>;
93+
type PredefinedOpaques = solve::PredefinedOpaques<'tcx>;
9394
type AdtDef = ty::AdtDef<'tcx>;
9495
type GenericArgs = ty::GenericArgsRef<'tcx>;
9596
type OwnItemArgs = &'tcx [ty::GenericArg<'tcx>];

compiler/rustc_middle/src/ty/generic_args.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_ast_ir::walk_visitable_list;
1111
use rustc_data_structures::intern::Interned;
1212
use rustc_errors::{DiagArgValue, IntoDiagArg};
1313
use rustc_hir::def_id::DefId;
14+
use rustc_macros::extension;
1415
use rustc_macros::{
1516
Decodable, Encodable, HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable,
1617
};
@@ -25,6 +26,8 @@ use std::num::NonZero;
2526
use std::ops::Deref;
2627
use std::ptr::NonNull;
2728

29+
pub type GenericArgKind<'tcx> = rustc_type_ir::GenericArgKind<TyCtxt<'tcx>>;
30+
2831
/// An entity in the Rust type system, which can be one of
2932
/// several kinds (types, lifetimes, and consts).
3033
/// To reduce memory usage, a `GenericArg` is an interned pointer,
@@ -49,6 +52,14 @@ impl<'tcx> rustc_type_ir::inherent::GenericArgs<TyCtxt<'tcx>> for ty::GenericArg
4952
}
5053
}
5154

55+
impl<'tcx> rustc_type_ir::inherent::IntoKind for GenericArg<'tcx> {
56+
type Kind = GenericArgKind<'tcx>;
57+
58+
fn kind(self) -> Self::Kind {
59+
self.unpack()
60+
}
61+
}
62+
5263
#[cfg(parallel_compiler)]
5364
unsafe impl<'tcx> rustc_data_structures::sync::DynSend for GenericArg<'tcx> where
5465
&'tcx (Ty<'tcx>, ty::Region<'tcx>, ty::Const<'tcx>): rustc_data_structures::sync::DynSend
@@ -79,13 +90,7 @@ const TYPE_TAG: usize = 0b00;
7990
const REGION_TAG: usize = 0b01;
8091
const CONST_TAG: usize = 0b10;
8192

82-
#[derive(Debug, TyEncodable, TyDecodable, PartialEq, Eq, HashStable)]
83-
pub enum GenericArgKind<'tcx> {
84-
Lifetime(ty::Region<'tcx>),
85-
Type(Ty<'tcx>),
86-
Const(ty::Const<'tcx>),
87-
}
88-
93+
#[extension(trait GenericArgPackExt<'tcx>)]
8994
impl<'tcx> GenericArgKind<'tcx> {
9095
#[inline]
9196
fn pack(self) -> GenericArg<'tcx> {

0 commit comments

Comments
 (0)