Skip to content

Commit 9a9f4e7

Browse files
authored
Merge pull request #19624 from jackh726/chalk-update
Update chalk
2 parents 31dbec7 + 6daa791 commit 9a9f4e7

16 files changed

+166
-173
lines changed

Cargo.lock

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ arrayvec = "0.7.6"
104104
bitflags = "2.9.0"
105105
cargo_metadata = "0.19.2"
106106
camino = "1.1.9"
107-
chalk-solve = { version = "0.100.0", default-features = false }
108-
chalk-ir = "0.100.0"
109-
chalk-recursive = { version = "0.100.0", default-features = false }
110-
chalk-derive = "0.100.0"
107+
chalk-solve = { version = "0.102.0", default-features = false }
108+
chalk-ir = "0.102.0"
109+
chalk-recursive = { version = "0.102.0", default-features = false }
110+
chalk-derive = "0.102.0"
111111
crossbeam-channel = "0.5.15"
112112
dissimilar = "1.0.10"
113113
dot = "0.1.4"

crates/hir-ty/src/builder.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl<D> TyBuilder<D> {
7676
}
7777
let subst = Substitution::from_iter(
7878
Interner,
79-
self.vec.into_iter().chain(self.parent_subst.iter(Interner).cloned()),
79+
self.parent_subst.iter(Interner).cloned().chain(self.vec),
8080
);
8181
(self.data, subst)
8282
}
@@ -278,8 +278,10 @@ impl TyBuilder<()> {
278278
};
279279
Substitution::from_iter(
280280
Interner,
281-
self_subst
282-
.chain(generics(db, parent).placeholder_subst(db).iter(Interner))
281+
generics(db, parent)
282+
.placeholder_subst(db)
283+
.iter(Interner)
284+
.chain(self_subst)
283285
.cloned()
284286
.collect::<Vec<_>>(),
285287
)

crates/hir-ty/src/chalk_db.rs

+25-7
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ impl chalk_solve::RustIrDatabase<Interner> for ChalkContext<'_> {
5555
fn associated_ty_data(&self, id: AssocTypeId) -> Arc<AssociatedTyDatum> {
5656
self.db.associated_ty_data(from_assoc_type_id(id))
5757
}
58+
fn associated_ty_from_impl(
59+
&self,
60+
impl_id: chalk_ir::ImplId<Interner>,
61+
assoc_type_id: chalk_ir::AssocTypeId<Interner>,
62+
) -> Option<rust_ir::AssociatedTyValueId<Interner>> {
63+
let alias_id = from_assoc_type_id(assoc_type_id);
64+
let trait_sig = self.db.type_alias_signature(alias_id);
65+
self.db.impl_items(hir_def::ImplId::from_chalk(self.db, impl_id)).items.iter().find_map(
66+
|(name, item)| match item {
67+
AssocItemId::TypeAliasId(alias) if &trait_sig.name == name => {
68+
Some(TypeAliasAsValue(*alias).to_chalk(self.db))
69+
}
70+
_ => None,
71+
},
72+
)
73+
}
5874
fn trait_datum(&self, trait_id: TraitId) -> Arc<TraitDatum> {
5975
self.db.trait_datum(self.krate, trait_id)
6076
}
@@ -468,12 +484,13 @@ impl chalk_solve::RustIrDatabase<Interner> for ChalkContext<'_> {
468484
// `resume_type`, `yield_type`, and `return_type` of the coroutine in question.
469485
let subst = TyBuilder::subst_for_coroutine(self.db, parent).fill_with_unknown().build();
470486

487+
let len = subst.len(Interner);
471488
let input_output = rust_ir::CoroutineInputOutputDatum {
472-
resume_type: TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0))
489+
resume_type: TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, len - 3))
473490
.intern(Interner),
474-
yield_type: TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 1))
491+
yield_type: TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, len - 2))
475492
.intern(Interner),
476-
return_type: TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 2))
493+
return_type: TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, len - 1))
477494
.intern(Interner),
478495
// FIXME: calculate upvars
479496
upvars: vec![],
@@ -620,10 +637,10 @@ pub(crate) fn associated_ty_data_query(
620637
.with_type_param_mode(crate::lower::ParamLoweringMode::Variable);
621638

622639
let trait_subst = TyBuilder::subst_for_def(db, trait_, None)
623-
.fill_with_bound_vars(crate::DebruijnIndex::INNERMOST, generic_params.len_self())
640+
.fill_with_bound_vars(crate::DebruijnIndex::INNERMOST, 0)
624641
.build();
625642
let pro_ty = TyBuilder::assoc_type_projection(db, type_alias, Some(trait_subst))
626-
.fill_with_bound_vars(crate::DebruijnIndex::INNERMOST, 0)
643+
.fill_with_bound_vars(crate::DebruijnIndex::INNERMOST, generic_params.len_self())
627644
.build();
628645
let self_ty = TyKind::Alias(AliasTy::Projection(pro_ty)).intern(Interner);
629646

@@ -1022,8 +1039,9 @@ pub(super) fn generic_predicate_to_inline_bound(
10221039
}
10231040
WhereClause::AliasEq(AliasEq { alias: AliasTy::Projection(projection_ty), ty }) => {
10241041
let generics = generics(db, from_assoc_type_id(projection_ty.associated_ty_id).into());
1025-
let (assoc_args, trait_args) =
1026-
projection_ty.substitution.as_slice(Interner).split_at(generics.len_self());
1042+
let parent_len = generics.parent_generics().map_or(0, |g| g.len_self());
1043+
let (trait_args, assoc_args) =
1044+
projection_ty.substitution.as_slice(Interner).split_at(parent_len);
10271045
let (self_ty, args_no_self) =
10281046
trait_args.split_first().expect("projection without trait self type");
10291047
if self_ty.assert_ty_ref(Interner) != &self_ty_shifted_in {

crates/hir-ty/src/chalk_ext.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,9 @@ impl ProjectionTyExt for ProjectionTy {
418418
fn trait_ref(&self, db: &dyn HirDatabase) -> TraitRef {
419419
// FIXME: something like `Split` trait from chalk-solve might be nice.
420420
let generics = generics(db, from_assoc_type_id(self.associated_ty_id).into());
421-
let substitution = Substitution::from_iter(
422-
Interner,
423-
self.substitution.iter(Interner).skip(generics.len_self()),
424-
);
421+
let parent_len = generics.parent_generics().map_or(0, |g| g.len_self());
422+
let substitution =
423+
Substitution::from_iter(Interner, self.substitution.iter(Interner).take(parent_len));
425424
TraitRef { trait_id: to_chalk_trait_id(self.trait_(db)), substitution }
426425
}
427426

crates/hir-ty/src/display.rs

+21-17
Original file line numberDiff line numberDiff line change
@@ -621,9 +621,8 @@ impl HirDisplay for ProjectionTy {
621621
.name
622622
.display(f.db, f.edition())
623623
)?;
624-
let proj_params_count =
625-
self.substitution.len(Interner) - trait_ref.substitution.len(Interner);
626-
let proj_params = &self.substitution.as_slice(Interner)[..proj_params_count];
624+
let proj_params =
625+
&self.substitution.as_slice(Interner)[trait_ref.substitution.len(Interner)..];
627626
hir_fmt_generics(f, proj_params, None, None)
628627
}
629628
}
@@ -1196,27 +1195,31 @@ impl HirDisplay for Ty {
11961195

11971196
// Normally, functions cannot have default parameters, but they can,
11981197
// for function-like things such as struct names or enum variants.
1199-
// The former cannot have defaults but parents, and the later cannot have
1200-
// parents but defaults.
1201-
// So, if `parent_len` > 0, it have a parent and thus it doesn't have any
1202-
// default. Therefore, we shouldn't subtract defaults because those defaults
1203-
// are from their parents.
1204-
// And if `parent_len` == 0, either parents don't exists or they don't have
1205-
// any defaults. Thus, we can - and should - subtract defaults.
1206-
let without_impl = if parent_len > 0 {
1207-
params_len - parent_len - impl_
1198+
// The former cannot have defaults but does have parents,
1199+
// but the latter cannot have parents but can have defaults.
1200+
//
1201+
// However, it's also true that *traits* can have defaults too.
1202+
// In this case, there can be no function params.
1203+
let parent_end = if parent_len > 0 {
1204+
// If `parent_len` > 0, then there cannot be defaults on the function
1205+
// and all defaults must come from the parent.
1206+
parent_len - defaults
12081207
} else {
1209-
params_len - parent_len - impl_ - defaults
1208+
parent_len
12101209
};
1211-
// parent's params (those from enclosing impl or trait, if any).
1212-
let (fn_params, parent_params) = parameters.split_at(without_impl + impl_);
1210+
let fn_params_no_impl_or_defaults = parameters.len() - parent_end - impl_;
1211+
let (parent_params, fn_params) = parameters.split_at(parent_end);
12131212

12141213
write!(f, "<")?;
12151214
hir_fmt_generic_arguments(f, parent_params, None)?;
12161215
if !parent_params.is_empty() && !fn_params.is_empty() {
12171216
write!(f, ", ")?;
12181217
}
1219-
hir_fmt_generic_arguments(f, &fn_params[0..without_impl], None)?;
1218+
hir_fmt_generic_arguments(
1219+
f,
1220+
&fn_params[..fn_params_no_impl_or_defaults],
1221+
None,
1222+
)?;
12201223
write!(f, ">")?;
12211224
}
12221225
}
@@ -1872,11 +1875,12 @@ fn write_bounds_like_dyn_trait(
18721875
f.end_location_link();
18731876

18741877
let proj_arg_count = generics(f.db, assoc_ty_id.into()).len_self();
1878+
let parent_len = proj.substitution.len(Interner) - proj_arg_count;
18751879
if proj_arg_count > 0 {
18761880
write!(f, "<")?;
18771881
hir_fmt_generic_arguments(
18781882
f,
1879-
&proj.substitution.as_slice(Interner)[..proj_arg_count],
1883+
&proj.substitution.as_slice(Interner)[parent_len..],
18801884
None,
18811885
)?;
18821886
write!(f, ">")?;

crates/hir-ty/src/generics.rs

+12-14
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl Generics {
6565
}
6666

6767
pub(crate) fn iter_id(&self) -> impl Iterator<Item = GenericParamId> + '_ {
68-
self.iter_self_id().chain(self.iter_parent_id())
68+
self.iter_parent_id().chain(self.iter_self_id())
6969
}
7070

7171
pub(crate) fn iter_self_id(&self) -> impl Iterator<Item = GenericParamId> + '_ {
@@ -82,11 +82,11 @@ impl Generics {
8282
self.params.iter_type_or_consts().map(from_toc_id(self)).map(TupleExt::head)
8383
}
8484

85-
/// Iterate over the params followed by the parent params.
85+
/// Iterate over the parent params followed by self params.
8686
pub(crate) fn iter(
8787
&self,
8888
) -> impl DoubleEndedIterator<Item = (GenericParamId, GenericParamDataRef<'_>)> + '_ {
89-
self.iter_self().chain(self.iter_parent())
89+
self.iter_parent().chain(self.iter_self())
9090
}
9191

9292
pub(crate) fn iter_parents_with_store(
@@ -166,13 +166,10 @@ impl Generics {
166166
if self.params.trait_self_param() == Some(param.local_id) {
167167
return Some(idx);
168168
}
169-
Some(self.params.len_lifetimes() + idx)
169+
Some(self.parent_generics().map_or(0, |g| g.len()) + self.params.len_lifetimes() + idx)
170170
} else {
171171
debug_assert_eq!(self.parent_generics().map(|it| it.def), Some(param.parent));
172-
self.parent_generics()
173-
.and_then(|g| g.find_type_or_const_param(param))
174-
// Remember that parent parameters come after parameters for self.
175-
.map(|idx| self.len_self() + idx)
172+
self.parent_generics().and_then(|g| g.find_type_or_const_param(param))
176173
}
177174
}
178175

@@ -184,12 +181,14 @@ impl Generics {
184181
if lifetime.parent == self.def {
185182
let idx = lifetime.local_id.into_raw().into_u32() as usize;
186183
debug_assert!(idx <= self.params.len_lifetimes());
187-
Some(self.params.trait_self_param().is_some() as usize + idx)
184+
Some(
185+
self.parent_generics().map_or(0, |g| g.len())
186+
+ self.params.trait_self_param().is_some() as usize
187+
+ idx,
188+
)
188189
} else {
189190
debug_assert_eq!(self.parent_generics().map(|it| it.def), Some(lifetime.parent));
190-
self.parent_generics()
191-
.and_then(|g| g.find_lifetime(lifetime))
192-
.map(|idx| self.len_self() + idx)
191+
self.parent_generics().and_then(|g| g.find_lifetime(lifetime))
193192
}
194193
}
195194

@@ -253,8 +252,7 @@ pub(crate) fn trait_self_param_idx(db: &dyn DefDatabase, def: GenericDefId) -> O
253252
let parent_def = parent_generic_def(db, def)?;
254253
let parent_params = db.generic_params(parent_def);
255254
let parent_self_idx = parent_params.trait_self_param()?.into_raw().into_u32() as usize;
256-
let self_params = db.generic_params(def);
257-
Some(self_params.len() + parent_self_idx)
255+
Some(parent_self_idx)
258256
}
259257
}
260258
}

crates/hir-ty/src/infer/expr.rs

+21-16
Original file line numberDiff line numberDiff line change
@@ -2065,7 +2065,16 @@ impl InferenceContext<'_> {
20652065
assert!(!has_self_param); // method shouldn't have another Self param
20662066
let total_len =
20672067
parent_params + type_params + const_params + impl_trait_params + lifetime_params;
2068-
let mut substs = Vec::with_capacity(total_len);
2068+
2069+
let param_to_var = |id| match id {
2070+
GenericParamId::TypeParamId(_) => self.table.new_type_var().cast(Interner),
2071+
GenericParamId::ConstParamId(id) => {
2072+
self.table.new_const_var(self.db.const_param_ty(id)).cast(Interner)
2073+
}
2074+
GenericParamId::LifetimeParamId(_) => self.table.new_lifetime_var().cast(Interner),
2075+
};
2076+
2077+
let mut substs: Vec<_> = def_generics.iter_parent_id().map(param_to_var).collect();
20692078

20702079
// handle provided arguments
20712080
if let Some(generic_args) = generic_args {
@@ -2105,20 +2114,17 @@ impl InferenceContext<'_> {
21052114
}
21062115
};
21072116

2108-
// Handle everything else as unknown. This also handles generic arguments for the method's
2109-
// parent (impl or trait), which should come after those for the method.
2110-
for (id, _data) in def_generics.iter().skip(substs.len()) {
2111-
match id {
2112-
GenericParamId::TypeParamId(_) => {
2113-
substs.push(self.table.new_type_var().cast(Interner))
2114-
}
2115-
GenericParamId::ConstParamId(id) => {
2116-
substs.push(self.table.new_const_var(self.db.const_param_ty(id)).cast(Interner))
2117-
}
2118-
GenericParamId::LifetimeParamId(_) => {
2119-
substs.push(self.table.new_lifetime_var().cast(Interner))
2120-
}
2117+
let mut param_to_var = |id| match id {
2118+
GenericParamId::TypeParamId(_) => self.table.new_type_var().cast(Interner),
2119+
GenericParamId::ConstParamId(id) => {
2120+
self.table.new_const_var(self.db.const_param_ty(id)).cast(Interner)
21212121
}
2122+
GenericParamId::LifetimeParamId(_) => self.table.new_lifetime_var().cast(Interner),
2123+
};
2124+
2125+
// Handle everything else as unknown.
2126+
for (id, _data) in def_generics.iter().skip(substs.len()) {
2127+
substs.push(param_to_var(id));
21222128
}
21232129
assert_eq!(substs.len(), total_len);
21242130
Substitution::from_iter(Interner, substs)
@@ -2143,13 +2149,12 @@ impl InferenceContext<'_> {
21432149
CallableDefId::FunctionId(f) => {
21442150
if let ItemContainerId::TraitId(trait_) = f.lookup(self.db).container {
21452151
// construct a TraitRef
2146-
let params_len = parameters.len(Interner);
21472152
let trait_params_len = generics(self.db, trait_.into()).len();
21482153
let substs = Substitution::from_iter(
21492154
Interner,
21502155
// The generic parameters for the trait come after those for the
21512156
// function.
2152-
&parameters.as_slice(Interner)[params_len - trait_params_len..],
2157+
&parameters.as_slice(Interner)[..trait_params_len],
21532158
);
21542159
self.push_obligation(
21552160
TraitRef { trait_id: to_chalk_trait_id(trait_), substitution: substs }

0 commit comments

Comments
 (0)