Skip to content

Commit fcb22a6

Browse files
Merge #8387
8387: Remove `SolutionVariables`, add ConstrainedSubst analogous to Chalk r=flodiebold a=flodiebold ... just missing the constraints. Co-authored-by: Florian Diebold <flodiebold@gmail.com>
2 parents 31d2b3b + b03969c commit fcb22a6

File tree

7 files changed

+53
-33
lines changed

7 files changed

+53
-33
lines changed

crates/hir/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,8 @@ use hir_ty::{
5858
subst_prefix,
5959
traits::FnTrait,
6060
AliasEq, AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, CanonicalVarKinds, Cast,
61-
DebruijnIndex, InEnvironment, Interner, QuantifiedWhereClause, Scalar, Solution,
62-
SolutionVariables, Substitution, TraitEnvironment, Ty, TyBuilder, TyDefId, TyExt, TyKind,
63-
TyVariableKind, WhereClause,
61+
DebruijnIndex, InEnvironment, Interner, QuantifiedWhereClause, Scalar, Solution, Substitution,
62+
TraitEnvironment, Ty, TyBuilder, TyDefId, TyExt, TyKind, TyVariableKind, WhereClause,
6463
};
6564
use itertools::Itertools;
6665
use rustc_hash::FxHashSet;
@@ -1822,8 +1821,9 @@ impl Type {
18221821
);
18231822

18241823
match db.trait_solve(self.krate, goal)? {
1825-
Solution::Unique(SolutionVariables(subst)) => subst
1824+
Solution::Unique(s) => s
18261825
.value
1826+
.subst
18271827
.interned()
18281828
.first()
18291829
.map(|ty| self.derived(ty.assert_ty_ref(&Interner).clone())),

crates/hir_ty/src/autoderef.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ fn deref_by_trait(
120120
// assumptions will be broken. We would need to properly introduce
121121
// new variables in that case
122122

123-
for i in 1..vars.0.binders.len(&Interner) {
124-
if vars.0.value.at(&Interner, i - 1).assert_ty_ref(&Interner).kind(&Interner)
123+
for i in 1..vars.binders.len(&Interner) {
124+
if vars.value.subst.at(&Interner, i - 1).assert_ty_ref(&Interner).kind(&Interner)
125125
!= &TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, i - 1))
126126
{
127127
warn!("complex solution for derefing {:?}: {:?}, ignoring", ty.goal, solution);
@@ -130,12 +130,12 @@ fn deref_by_trait(
130130
}
131131
Some(Canonical {
132132
value: vars
133-
.0
134133
.value
135-
.at(&Interner, vars.0.value.len(&Interner) - 1)
134+
.subst
135+
.at(&Interner, vars.value.subst.len(&Interner) - 1)
136136
.assert_ty_ref(&Interner)
137137
.clone(),
138-
binders: vars.0.binders.clone(),
138+
binders: vars.binders.clone(),
139139
})
140140
}
141141
Solution::Ambig(_) => {

crates/hir_ty/src/infer.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use super::{
4242
};
4343
use crate::{
4444
db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode,
45-
to_assoc_type_id, AliasEq, AliasTy, Interner, TyBuilder, TyExt, TyKind,
45+
to_assoc_type_id, AliasEq, AliasTy, Canonical, Interner, TyBuilder, TyExt, TyKind,
4646
};
4747

4848
// This lint has a false positive here. See the link below for details.
@@ -342,11 +342,18 @@ impl<'a> InferenceContext<'a> {
342342
self.db.trait_solve(self.resolver.krate().unwrap(), canonicalized.value.clone());
343343

344344
match solution {
345-
Some(Solution::Unique(substs)) => {
346-
canonicalized.apply_solution(self, substs.0);
345+
Some(Solution::Unique(canonical_subst)) => {
346+
canonicalized.apply_solution(
347+
self,
348+
Canonical {
349+
binders: canonical_subst.binders,
350+
// FIXME: handle constraints
351+
value: canonical_subst.value.subst,
352+
},
353+
);
347354
}
348355
Some(Solution::Ambig(Guidance::Definite(substs))) => {
349-
canonicalized.apply_solution(self, substs.0);
356+
canonicalized.apply_solution(self, substs);
350357
self.obligations.push(obligation);
351358
}
352359
Some(_) => {

crates/hir_ty/src/infer/coerce.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use chalk_ir::{cast::Cast, Mutability, TyVariableKind};
88
use hir_def::lang_item::LangItemTarget;
99

10-
use crate::{autoderef, Interner, Solution, Ty, TyBuilder, TyExt, TyKind};
10+
use crate::{autoderef, Canonical, Interner, Solution, Ty, TyBuilder, TyExt, TyKind};
1111

1212
use super::{InEnvironment, InferenceContext};
1313

@@ -148,7 +148,14 @@ impl<'a> InferenceContext<'a> {
148148

149149
match solution {
150150
Solution::Unique(v) => {
151-
canonicalized.apply_solution(self, v.0);
151+
canonicalized.apply_solution(
152+
self,
153+
Canonical {
154+
binders: v.binders,
155+
// FIXME handle constraints
156+
value: v.value.subst,
157+
},
158+
);
152159
}
153160
_ => return None,
154161
};

crates/hir_ty/src/traits.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use stdx::panic_context;
99

1010
use crate::{
1111
db::HirDatabase, AliasEq, AliasTy, Canonical, DomainGoal, Guidance, HirDisplay, InEnvironment,
12-
Solution, SolutionVariables, Ty, TyKind, WhereClause,
12+
Solution, Ty, TyKind, WhereClause,
1313
};
1414

1515
use self::chalk::{from_chalk, Interner, ToChalk};
@@ -173,23 +173,15 @@ fn solution_from_chalk(
173173
db: &dyn HirDatabase,
174174
solution: chalk_solve::Solution<Interner>,
175175
) -> Solution {
176-
let convert_subst = |subst: chalk_ir::Canonical<chalk_ir::Substitution<Interner>>| {
177-
let result = from_chalk(db, subst);
178-
SolutionVariables(result)
179-
};
180176
match solution {
181177
chalk_solve::Solution::Unique(constr_subst) => {
182-
let subst = chalk_ir::Canonical {
183-
value: constr_subst.value.subst,
184-
binders: constr_subst.binders,
185-
};
186-
Solution::Unique(convert_subst(subst))
178+
Solution::Unique(from_chalk(db, constr_subst))
187179
}
188180
chalk_solve::Solution::Ambig(chalk_solve::Guidance::Definite(subst)) => {
189-
Solution::Ambig(Guidance::Definite(convert_subst(subst)))
181+
Solution::Ambig(Guidance::Definite(from_chalk(db, subst)))
190182
}
191183
chalk_solve::Solution::Ambig(chalk_solve::Guidance::Suggested(subst)) => {
192-
Solution::Ambig(Guidance::Suggested(convert_subst(subst)))
184+
Solution::Ambig(Guidance::Suggested(from_chalk(db, subst)))
193185
}
194186
chalk_solve::Solution::Ambig(chalk_solve::Guidance::Unknown) => {
195187
Solution::Ambig(Guidance::Unknown)

crates/hir_ty/src/traits/chalk/mapping.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use hir_def::{GenericDefId, TypeAliasId};
1111

1212
use crate::{
1313
chalk_ext::ProjectionTyExt, db::HirDatabase, static_lifetime, AliasTy, CallableDefId,
14-
Canonical, DomainGoal, FnPointer, GenericArg, InEnvironment, OpaqueTy, ProjectionTy,
15-
QuantifiedWhereClause, Substitution, TraitRef, Ty, TypeWalk, WhereClause,
14+
Canonical, ConstrainedSubst, DomainGoal, FnPointer, GenericArg, InEnvironment, OpaqueTy,
15+
ProjectionTy, QuantifiedWhereClause, Substitution, TraitRef, Ty, TypeWalk, WhereClause,
1616
};
1717

1818
use super::interner::*;
@@ -459,6 +459,18 @@ where
459459
}
460460
}
461461

462+
impl ToChalk for crate::ConstrainedSubst {
463+
type Chalk = chalk_ir::ConstrainedSubst<Interner>;
464+
465+
fn to_chalk(self, _db: &dyn HirDatabase) -> Self::Chalk {
466+
unimplemented!()
467+
}
468+
469+
fn from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self {
470+
ConstrainedSubst { subst: from_chalk(db, chalk.subst) }
471+
}
472+
}
473+
462474
pub(super) fn make_binders<T>(value: T, num_vars: usize) -> chalk_ir::Binders<T>
463475
where
464476
T: HasInterner<Interner = Interner>,

crates/hir_ty/src/types.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -490,14 +490,16 @@ pub struct AliasEq {
490490
}
491491

492492
#[derive(Clone, Debug, PartialEq, Eq)]
493-
pub struct SolutionVariables(pub Canonical<Substitution>);
493+
pub struct ConstrainedSubst {
494+
pub subst: Substitution,
495+
}
494496

495497
#[derive(Clone, Debug, PartialEq, Eq)]
496498
/// A (possible) solution for a proposed goal.
497499
pub enum Solution {
498500
/// The goal indeed holds, and there is a unique value for all existential
499501
/// variables.
500-
Unique(SolutionVariables),
502+
Unique(Canonical<ConstrainedSubst>),
501503

502504
/// The goal may be provable in multiple ways, but regardless we may have some guidance
503505
/// for type inference. In this case, we don't return any lifetime
@@ -513,12 +515,12 @@ pub enum Guidance {
513515
/// The existential variables *must* have the given values if the goal is
514516
/// ever to hold, but that alone isn't enough to guarantee the goal will
515517
/// actually hold.
516-
Definite(SolutionVariables),
518+
Definite(Canonical<Substitution>),
517519

518520
/// There are multiple plausible values for the existentials, but the ones
519521
/// here are suggested as the preferred choice heuristically. These should
520522
/// be used for inference fallback only.
521-
Suggested(SolutionVariables),
523+
Suggested(Canonical<Substitution>),
522524

523525
/// There's no useful information to feed back to type inference
524526
Unknown,

0 commit comments

Comments
 (0)