Skip to content

Commit 1ef3021

Browse files
jackh726compiler-errors
authored andcommitted
Put assoc type params after trait params (like rustc)
1 parent 97eb261 commit 1ef3021

File tree

7 files changed

+26
-29
lines changed

7 files changed

+26
-29
lines changed

chalk-integration/src/lowering.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,10 @@ impl LowerWithEnv for ProjectionTy {
633633
substitution: trait_substitution,
634634
} = trait_ref.lower(env)?;
635635
let lookup = env.lookup_associated_ty(trait_id, name)?;
636-
let mut args: Vec<_> = args
636+
637+
let mut all_args: Vec<_> = trait_substitution.iter(interner).cloned().collect();
638+
639+
let args: Vec<_> = args
637640
.iter()
638641
.map(|a| a.lower(env))
639642
.collect::<LowerResult<_>>()?;
@@ -656,11 +659,11 @@ impl LowerWithEnv for ProjectionTy {
656659
}
657660
}
658661

659-
args.extend(trait_substitution.iter(interner).cloned());
662+
all_args.extend(args.into_iter());
660663

661664
Ok(chalk_ir::ProjectionTy {
662665
associated_ty_id: lookup.id,
663-
substitution: chalk_ir::Substitution::from_iter(interner, args),
666+
substitution: chalk_ir::Substitution::from_iter(interner, all_args),
664667
})
665668
}
666669
}
@@ -1024,9 +1027,8 @@ pub fn lower_goal(goal: &Goal, program: &LoweredProgram) -> LowerResult<chalk_ir
10241027
.map(|(&associated_ty_id, datum)| {
10251028
let trait_datum = &program.trait_data[&datum.trait_id];
10261029
let num_trait_params = trait_datum.binders.len(interner);
1027-
let num_addl_params = datum.binders.len(interner) - num_trait_params;
10281030
let addl_variable_kinds =
1029-
datum.binders.binders.as_slice(interner)[..num_addl_params].to_owned();
1031+
datum.binders.binders.as_slice(interner)[num_trait_params..].to_owned();
10301032
let lookup = AssociatedTyLookup {
10311033
id: associated_ty_id,
10321034
addl_variable_kinds,

chalk-integration/src/lowering/program_lowerer.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ impl ProgramLowerer {
292292
// type Bar<A0, A1>;
293293
// }
294294
// ```
295-
let mut variable_kinds = assoc_ty_defn.all_parameters();
296-
variable_kinds.extend(trait_defn.all_parameters());
295+
let mut variable_kinds = trait_defn.all_parameters();
296+
variable_kinds.extend(assoc_ty_defn.all_parameters());
297297

298298
let binders = empty_env.in_binders(variable_kinds, |env| {
299299
Ok(rust_ir::AssociatedTyDatumBound {
@@ -330,8 +330,8 @@ impl ProgramLowerer {
330330
// impl *and* those from the associated type
331331
// itself. As in the "trait" case above, we begin
332332
// with the parameters from the impl.
333-
let mut variable_kinds = atv.all_parameters();
334-
variable_kinds.extend(impl_defn.all_parameters());
333+
let mut variable_kinds = impl_defn.all_parameters();
334+
variable_kinds.extend(atv.all_parameters());
335335

336336
let value = empty_env.in_binders(variable_kinds, |env| {
337337
Ok(rust_ir::AssociatedTyValueBound {

chalk-solve/src/clauses.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -866,11 +866,9 @@ fn push_alias_alias_eq_clause<I: Interner>(
866866
let (_, trait_args, assoc_args) = builder.db.split_projection(&projection_ty);
867867
let fresh_self_subst = Substitution::from_iter(
868868
interner,
869-
assoc_args
870-
.iter()
871-
.cloned()
872-
.chain(std::iter::once(intermediate_eq_ty.clone().cast(interner)))
873-
.chain(trait_args[1..].iter().cloned()),
869+
std::iter::once(intermediate_eq_ty.clone().cast(interner))
870+
.chain(trait_args[1..].iter().cloned())
871+
.chain(assoc_args.iter().cloned()),
874872
);
875873
let fresh_alias = AliasTy::Projection(ProjectionTy {
876874
associated_ty_id: projection_ty.associated_ty_id,

chalk-solve/src/split.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,8 @@ pub trait Split<I: Interner>: RustIrDatabase<I> {
9393
// the impl parameters are a suffix
9494
//
9595
// [ P0..Pn, Pn...Pm ]
96-
// ^^^^^^^ impl parameters
97-
let split_point = parameters.len() - impl_params_len;
98-
let (other_params, impl_params) = parameters.split_at(split_point);
96+
// ^^^^^^ impl parameters
97+
let (impl_params, other_params) = parameters.split_at(impl_params_len);
9998
(impl_params, other_params)
10099
}
101100

@@ -144,9 +143,10 @@ pub trait Split<I: Interner>: RustIrDatabase<I> {
144143
// `<Box<!T> as Foo>::Item<'!a>`
145144
let projection_substitution = Substitution::from_iter(
146145
interner,
147-
atv_parameters
148-
.iter()
149-
.chain(trait_ref.substitution.iter(interner))
146+
trait_ref
147+
.substitution
148+
.iter(interner)
149+
.chain(atv_parameters.iter())
150150
.cloned(),
151151
);
152152

@@ -190,8 +190,8 @@ pub trait Split<I: Interner>: RustIrDatabase<I> {
190190
) -> (&'p [P], &'p [P]) {
191191
let trait_datum = &self.trait_datum(associated_ty_datum.trait_id);
192192
let trait_num_params = trait_datum.binders.len(self.interner());
193-
let split_point = parameters.len() - trait_num_params;
194-
let (other_params, trait_params) = parameters.split_at(split_point);
193+
let split_point = trait_num_params;
194+
let (trait_params, other_params) = parameters.split_at(split_point);
195195
(trait_params, other_params)
196196
}
197197
}

tests/lowering/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ fn atc_accounting() {
192192
&r#"AssociatedTyValue {
193193
impl_id: ImplId(#2),
194194
associated_ty_id: (Iterable::Iter),
195-
value: for<lifetime, type> AssociatedTyValueBound {
196-
ty: Iter<'^0.0, ^0.1>
195+
value: for<type, lifetime> AssociatedTyValueBound {
196+
ty: Iter<'^0.1, ^0.0>
197197
},
198198
}"#
199199
.replace(",\n", "\n"),

tests/test/impls.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,7 @@ fn normalize_rev_infer_gat() {
240240
T: Combine<Item<U> = Either<A, B>>
241241
}
242242
} yields {
243-
// T is ?1 and U is ?0, so this is surprising, but correct! (See #126.)
244-
expect![["Unique; substitution [?0 := B, ?1 := A]"]]
243+
expect![["Unique; substitution [?0 := A, ?1 := B]"]]
245244
}
246245
}
247246
}

tests/test/unify.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,7 @@ fn mixed_indices_normalize_gat_application() {
367367
Normalize(<Ref<'a, T> as Foo>::T<X> -> Either<U, Y>)
368368
}
369369
} yields {
370-
// Our GAT parameter <X> is mapped to ?0; all others appear left to right
371-
// in our Normalize(...) goal.
372-
expect![["Unique; for<?U0,?U0,?U0> { substitution [?0 := ^0.0, ?1 := '^0.1, ?2 := ^0.2, ?3 := ^0.0, ?4 := ^0.2] }"]]
370+
expect![["Unique; for<?U0,?U0,?U0> { substitution [?0 := '^0.0, ?1 := ^0.1, ?2 := ^0.2, ?3 := ^0.2, ?4 := ^0.1] }"]]
373371
}
374372
}
375373
}

0 commit comments

Comments
 (0)