Skip to content

Commit a54ce9a

Browse files
trait_goals: Implement support for trait aliases
1 parent 7105d39 commit a54ce9a

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

compiler/rustc_middle/src/ty/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,10 @@ impl<'tcx> InstantiatedPredicates<'tcx> {
12521252
pub fn is_empty(&self) -> bool {
12531253
self.predicates.is_empty()
12541254
}
1255+
1256+
pub fn into_iter(self) -> impl Iterator<Item = (ty::Predicate<'tcx>, Span)> {
1257+
std::iter::zip(self.predicates, self.spans)
1258+
}
12551259
}
12561260

12571261
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable, TyEncodable, TyDecodable, Lift)]

compiler/rustc_trait_selection/src/solve/assembly.rs

+13
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ pub(super) trait GoalKind<'tcx>: TypeFoldable<'tcx> + Copy {
4747
impl_def_id: DefId,
4848
);
4949

50+
fn consider_trait_alias_candidate(
51+
acx: &mut AssemblyCtxt<'_, 'tcx, Self>,
52+
goal: Goal<'tcx, Self>,
53+
);
54+
5055
fn consider_alias_bound_candidates(
5156
acx: &mut AssemblyCtxt<'_, 'tcx, Self>,
5257
goal: Goal<'tcx, Self>,
@@ -106,6 +111,8 @@ impl<'a, 'tcx, G: GoalKind<'tcx>> AssemblyCtxt<'a, 'tcx, G> {
106111

107112
acx.assemble_auto_trait_candidates(goal);
108113

114+
acx.assemble_trait_alias_candidates(goal);
115+
109116
acx.assemble_fn_like_candidates(goal);
110117

111118
acx.candidates
@@ -207,6 +214,12 @@ impl<'a, 'tcx, G: GoalKind<'tcx>> AssemblyCtxt<'a, 'tcx, G> {
207214
}
208215
}
209216

217+
fn assemble_trait_alias_candidates(&mut self, goal: Goal<'tcx, G>) {
218+
if self.cx.tcx.is_trait_alias(goal.predicate.trait_def_id(self.cx.tcx)) {
219+
G::consider_trait_alias_candidate(self, goal);
220+
}
221+
}
222+
210223
fn assemble_fn_like_candidates(&mut self, goal: Goal<'tcx, G>) {
211224
let tcx = self.cx.tcx;
212225
let trait_def_id = goal.predicate.trait_def_id(tcx);

compiler/rustc_trait_selection/src/solve/project_goals.rs

+7
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,13 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
212212
})
213213
}
214214

215+
fn consider_trait_alias_candidate(
216+
_acx: &mut AssemblyCtxt<'_, 'tcx, Self>,
217+
_goal: Goal<'tcx, Self>,
218+
) {
219+
// Trait aliases never have (their own) associated types
220+
}
221+
215222
fn consider_alias_bound_candidates(
216223
_acx: &mut AssemblyCtxt<'_, 'tcx, Self>,
217224
_goal: Goal<'tcx, ProjectionPredicate<'tcx>>,

compiler/rustc_trait_selection/src/solve/trait_goals.rs

+21
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ use rustc_target::spec::abi::Abi;
2323
pub(super) enum CandidateSource {
2424
/// Some user-defined impl with the given `DefId`.
2525
Impl(DefId),
26+
/// The automatic implementation of a trait alias.
27+
TraitAlias,
2628
/// The n-th caller bound in the `param_env` of our goal.
2729
///
2830
/// This is pretty much always a bound from the `where`-clauses of the
@@ -122,6 +124,24 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
122124
})
123125
}
124126

127+
fn consider_trait_alias_candidate(
128+
acx: &mut AssemblyCtxt<'_, 'tcx, Self>,
129+
goal: Goal<'tcx, Self>,
130+
) {
131+
let tcx = acx.cx.tcx;
132+
acx.infcx.probe(|_| {
133+
let nested_goals = tcx
134+
.predicates_of(goal.predicate.def_id())
135+
.instantiate_own(tcx, goal.predicate.trait_ref.substs)
136+
.predicates
137+
.into_iter()
138+
.map(|pred| goal.with(tcx, pred))
139+
.collect();
140+
let Ok(certainty) = acx.cx.evaluate_all(acx.infcx, nested_goals) else { return };
141+
acx.try_insert_candidate(CandidateSource::TraitAlias, certainty);
142+
})
143+
}
144+
125145
fn consider_alias_bound_candidates(
126146
acx: &mut AssemblyCtxt<'_, 'tcx, Self>,
127147
goal: Goal<'tcx, Self>,
@@ -414,6 +434,7 @@ impl<'tcx> EvalCtxt<'tcx> {
414434
// FIXME: implement this
415435
match (candidate.source, other.source) {
416436
(CandidateSource::Impl(_), _)
437+
| (CandidateSource::TraitAlias, _)
417438
| (CandidateSource::ParamEnv(_), _)
418439
| (CandidateSource::AliasBound(_), _)
419440
| (CandidateSource::ObjectBound(_), _)

0 commit comments

Comments
 (0)