From 5fd4f5bceb456bd3681381bb390562466fdc4356 Mon Sep 17 00:00:00 2001 From: Wilco Kusee Date: Fri, 27 Jan 2023 20:45:03 +0100 Subject: [PATCH 1/3] Add candidates for DiscriminantKind builtin --- .../src/solve/assembly.rs | 8 ++++++++ .../src/solve/project_goals.rs | 20 +++++++++++++++++++ .../src/solve/trait_goals.rs | 8 ++++++++ 3 files changed, 36 insertions(+) diff --git a/compiler/rustc_trait_selection/src/solve/assembly.rs b/compiler/rustc_trait_selection/src/solve/assembly.rs index 5690b6536bbc0..ccdf6246083cc 100644 --- a/compiler/rustc_trait_selection/src/solve/assembly.rs +++ b/compiler/rustc_trait_selection/src/solve/assembly.rs @@ -81,6 +81,7 @@ pub(super) enum CandidateSource { AliasBound, } +/// Methods used to assemble candidates for either trait or projection goals. pub(super) trait GoalKind<'tcx>: TypeFoldable<'tcx> + Copy + Eq { fn self_ty(self) -> Ty<'tcx>; @@ -188,6 +189,11 @@ pub(super) trait GoalKind<'tcx>: TypeFoldable<'tcx> + Copy + Eq { ecx: &mut EvalCtxt<'_, 'tcx>, goal: Goal<'tcx, Self>, ) -> Vec>; + + fn consider_builtin_discriminant_kind_candidate( + ecx: &mut EvalCtxt<'_, 'tcx>, + goal: Goal<'tcx, Self>, + ) -> QueryResult<'tcx>; } impl<'tcx> EvalCtxt<'_, 'tcx> { @@ -320,6 +326,8 @@ impl<'tcx> EvalCtxt<'_, 'tcx> { G::consider_builtin_generator_candidate(self, goal) } else if lang_items.unsize_trait() == Some(trait_def_id) { G::consider_builtin_unsize_candidate(self, goal) + } else if lang_items.discriminant_kind_trait() == Some(trait_def_id) { + G::consider_builtin_discriminant_kind_candidate(self, goal) } else { Err(NoSolution) }; diff --git a/compiler/rustc_trait_selection/src/solve/project_goals.rs b/compiler/rustc_trait_selection/src/solve/project_goals.rs index a23fdd24b4e42..48627ee6096f3 100644 --- a/compiler/rustc_trait_selection/src/solve/project_goals.rs +++ b/compiler/rustc_trait_selection/src/solve/project_goals.rs @@ -581,6 +581,26 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> { ) -> Vec> { bug!("`Unsize` does not have an associated type: {:?}", goal); } + + fn consider_builtin_discriminant_kind_candidate( + ecx: &mut EvalCtxt<'_, 'tcx>, + goal: Goal<'tcx, Self>, + ) -> QueryResult<'tcx> { + let self_ty = goal.predicate.self_ty(); + + let tcx = ecx.tcx(); + let term = self_ty.discriminant_ty(tcx).into(); + + Self::consider_assumption( + ecx, + goal, + ty::Binder::dummy(ty::ProjectionPredicate { + projection_ty: tcx.mk_alias_ty(goal.predicate.def_id(), [self_ty]), + term, + }) + .to_predicate(tcx), + ) + } } /// This behavior is also implemented in `rustc_ty_utils` and in the old `project` code. diff --git a/compiler/rustc_trait_selection/src/solve/trait_goals.rs b/compiler/rustc_trait_selection/src/solve/trait_goals.rs index 29ee9da38e08b..0003dfeaee781 100644 --- a/compiler/rustc_trait_selection/src/solve/trait_goals.rs +++ b/compiler/rustc_trait_selection/src/solve/trait_goals.rs @@ -439,6 +439,14 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> { responses } + + fn consider_builtin_discriminant_kind_candidate( + ecx: &mut EvalCtxt<'_, 'tcx>, + _goal: Goal<'tcx, Self>, + ) -> QueryResult<'tcx> { + // `DiscriminantKind` is automatically implemented for every type. + ecx.make_canonical_response(Certainty::Yes) + } } impl<'tcx> EvalCtxt<'_, 'tcx> { From de50a86a12d6db76e7fec4c8f15e17ae199acb7e Mon Sep 17 00:00:00 2001 From: Wilco Kusee Date: Wed, 1 Feb 2023 17:13:57 +0100 Subject: [PATCH 2/3] Simplify discriminant_kind goal using new helper function --- .../src/solve/project_goals.rs | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_trait_selection/src/solve/project_goals.rs b/compiler/rustc_trait_selection/src/solve/project_goals.rs index 48627ee6096f3..170b560d7b6a1 100644 --- a/compiler/rustc_trait_selection/src/solve/project_goals.rs +++ b/compiler/rustc_trait_selection/src/solve/project_goals.rs @@ -586,20 +586,13 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> { ecx: &mut EvalCtxt<'_, 'tcx>, goal: Goal<'tcx, Self>, ) -> QueryResult<'tcx> { - let self_ty = goal.predicate.self_ty(); - - let tcx = ecx.tcx(); - let term = self_ty.discriminant_ty(tcx).into(); - - Self::consider_assumption( - ecx, - goal, - ty::Binder::dummy(ty::ProjectionPredicate { - projection_ty: tcx.mk_alias_ty(goal.predicate.def_id(), [self_ty]), - term, - }) - .to_predicate(tcx), - ) + let discriminant = goal.predicate.self_ty().discriminant_ty(ecx.tcx()); + let nested_goals = ecx.infcx.eq( + goal.param_env, + goal.predicate.term.ty().expect("expected ty goal"), + discriminant, + )?; + ecx.evaluate_all_and_make_canonical_response(nested_goals) } } From f29000eba9ebd43dca5ae97bab39148cce9319bc Mon Sep 17 00:00:00 2001 From: Wilco Kusee Date: Fri, 3 Feb 2023 10:04:15 +0100 Subject: [PATCH 3/3] Use new helper inside probe --- compiler/rustc_trait_selection/src/solve/project_goals.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_trait_selection/src/solve/project_goals.rs b/compiler/rustc_trait_selection/src/solve/project_goals.rs index 170b560d7b6a1..9f62f686af647 100644 --- a/compiler/rustc_trait_selection/src/solve/project_goals.rs +++ b/compiler/rustc_trait_selection/src/solve/project_goals.rs @@ -587,12 +587,8 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> { goal: Goal<'tcx, Self>, ) -> QueryResult<'tcx> { let discriminant = goal.predicate.self_ty().discriminant_ty(ecx.tcx()); - let nested_goals = ecx.infcx.eq( - goal.param_env, - goal.predicate.term.ty().expect("expected ty goal"), - discriminant, - )?; - ecx.evaluate_all_and_make_canonical_response(nested_goals) + ecx.infcx + .probe(|_| ecx.eq_term_and_make_canonical_response(goal, Certainty::Yes, discriminant)) } }