Skip to content

Commit 5fd4f5b

Browse files
committed
Add candidates for DiscriminantKind builtin
1 parent 11d96b5 commit 5fd4f5b

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

compiler/rustc_trait_selection/src/solve/assembly.rs

+8
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ pub(super) enum CandidateSource {
8181
AliasBound,
8282
}
8383

84+
/// Methods used to assemble candidates for either trait or projection goals.
8485
pub(super) trait GoalKind<'tcx>: TypeFoldable<'tcx> + Copy + Eq {
8586
fn self_ty(self) -> Ty<'tcx>;
8687

@@ -188,6 +189,11 @@ pub(super) trait GoalKind<'tcx>: TypeFoldable<'tcx> + Copy + Eq {
188189
ecx: &mut EvalCtxt<'_, 'tcx>,
189190
goal: Goal<'tcx, Self>,
190191
) -> Vec<CanonicalResponse<'tcx>>;
192+
193+
fn consider_builtin_discriminant_kind_candidate(
194+
ecx: &mut EvalCtxt<'_, 'tcx>,
195+
goal: Goal<'tcx, Self>,
196+
) -> QueryResult<'tcx>;
191197
}
192198

193199
impl<'tcx> EvalCtxt<'_, 'tcx> {
@@ -320,6 +326,8 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
320326
G::consider_builtin_generator_candidate(self, goal)
321327
} else if lang_items.unsize_trait() == Some(trait_def_id) {
322328
G::consider_builtin_unsize_candidate(self, goal)
329+
} else if lang_items.discriminant_kind_trait() == Some(trait_def_id) {
330+
G::consider_builtin_discriminant_kind_candidate(self, goal)
323331
} else {
324332
Err(NoSolution)
325333
};

compiler/rustc_trait_selection/src/solve/project_goals.rs

+20
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,26 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
581581
) -> Vec<super::CanonicalResponse<'tcx>> {
582582
bug!("`Unsize` does not have an associated type: {:?}", goal);
583583
}
584+
585+
fn consider_builtin_discriminant_kind_candidate(
586+
ecx: &mut EvalCtxt<'_, 'tcx>,
587+
goal: Goal<'tcx, Self>,
588+
) -> QueryResult<'tcx> {
589+
let self_ty = goal.predicate.self_ty();
590+
591+
let tcx = ecx.tcx();
592+
let term = self_ty.discriminant_ty(tcx).into();
593+
594+
Self::consider_assumption(
595+
ecx,
596+
goal,
597+
ty::Binder::dummy(ty::ProjectionPredicate {
598+
projection_ty: tcx.mk_alias_ty(goal.predicate.def_id(), [self_ty]),
599+
term,
600+
})
601+
.to_predicate(tcx),
602+
)
603+
}
584604
}
585605

586606
/// This behavior is also implemented in `rustc_ty_utils` and in the old `project` code.

compiler/rustc_trait_selection/src/solve/trait_goals.rs

+8
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,14 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
439439

440440
responses
441441
}
442+
443+
fn consider_builtin_discriminant_kind_candidate(
444+
ecx: &mut EvalCtxt<'_, 'tcx>,
445+
_goal: Goal<'tcx, Self>,
446+
) -> QueryResult<'tcx> {
447+
// `DiscriminantKind` is automatically implemented for every type.
448+
ecx.make_canonical_response(Certainty::Yes)
449+
}
442450
}
443451

444452
impl<'tcx> EvalCtxt<'_, 'tcx> {

0 commit comments

Comments
 (0)