Skip to content

Commit 085ae9e

Browse files
Add support for inherent projections
1 parent c4083fa commit 085ae9e

File tree

6 files changed

+48
-3
lines changed

6 files changed

+48
-3
lines changed

compiler/rustc_middle/src/ty/sty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1313,7 +1313,7 @@ impl<'tcx> AliasTy<'tcx> {
13131313
/// I_i impl subst
13141314
/// P_j GAT subst
13151315
/// ```
1316-
pub fn rebase_args_onto_impl(
1316+
pub fn rebase_inherent_args_onto_impl(
13171317
self,
13181318
impl_args: ty::GenericArgsRef<'tcx>,
13191319
tcx: TyCtxt<'tcx>,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use rustc_middle::traits::solve::{Certainty, Goal, QueryResult};
2+
use rustc_middle::ty;
3+
4+
use super::EvalCtxt;
5+
6+
impl<'tcx> EvalCtxt<'_, 'tcx> {
7+
pub(super) fn normalize_inherent_associated_type(
8+
&mut self,
9+
goal: Goal<'tcx, ty::ProjectionPredicate<'tcx>>,
10+
) -> QueryResult<'tcx> {
11+
let tcx = self.tcx();
12+
let inherent = goal.predicate.projection_ty;
13+
let expected = goal.predicate.term.ty().expect("inherent consts are treated separately");
14+
15+
let impl_def_id = tcx.parent(inherent.def_id);
16+
let impl_substs = self.fresh_args_for_item(impl_def_id);
17+
18+
// Equate impl header and add impl where clauses
19+
self.eq(
20+
goal.param_env,
21+
inherent.self_ty(),
22+
tcx.type_of(impl_def_id).instantiate(tcx, impl_substs),
23+
)?;
24+
self.add_goals(
25+
tcx.predicates_of(impl_def_id)
26+
.instantiate(tcx, impl_substs)
27+
.into_iter()
28+
.map(|(pred, _)| goal.with(tcx, pred)),
29+
);
30+
31+
// Equate IAT with the RHS of the project goal
32+
let inherent_substs = inherent.rebase_inherent_args_onto_impl(impl_substs, tcx);
33+
self.eq(
34+
goal.param_env,
35+
expected,
36+
tcx.type_of(inherent.def_id).instantiate(tcx, inherent_substs),
37+
)
38+
.expect("expected goal term to be fully unconstrained");
39+
40+
self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
41+
}
42+
}

compiler/rustc_trait_selection/src/solve/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ mod assembly;
2525
mod canonicalize;
2626
mod eval_ctxt;
2727
mod fulfill;
28+
mod inherent_projection;
2829
pub mod inspect;
2930
mod normalize;
3031
mod opaques;

compiler/rustc_trait_selection/src/solve/project_goals.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
4848
self.merge_candidates(candidates)
4949
}
5050
ty::AssocItemContainer::ImplContainer => {
51-
bug!("IATs not supported here yet")
51+
self.normalize_inherent_associated_type(goal)
5252
}
5353
}
5454
} else {

compiler/rustc_trait_selection/src/traits/project.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1421,7 +1421,7 @@ pub fn compute_inherent_assoc_ty_args<'a, 'b, 'tcx>(
14211421
}
14221422
}
14231423

1424-
alias_ty.rebase_args_onto_impl(impl_args, tcx)
1424+
alias_ty.rebase_inherent_args_onto_impl(impl_args, tcx)
14251425
}
14261426

14271427
enum Projected<'tcx> {

tests/ui/associated-inherent-types/inference.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Testing inference capabilities.
22
// check-pass
3+
// revisions: current next
4+
//[next] compile-flags: -Ztrait-solver=next
35

46
#![feature(inherent_associated_types)]
57
#![allow(incomplete_features)]

0 commit comments

Comments
 (0)