Skip to content

Commit 298c0d1

Browse files
Implement selection in new trait solver
1 parent f798ada commit 298c0d1

File tree

7 files changed

+356
-21
lines changed

7 files changed

+356
-21
lines changed

compiler/rustc_trait_selection/src/solve/assembly/mod.rs

+36-15
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub(super) enum CandidateSource {
4949
/// Notable examples are auto traits, `Sized`, and `DiscriminantKind`.
5050
/// For a list of all traits with builtin impls, check out the
5151
/// [`EvalCtxt::assemble_builtin_impl_candidates`] method. Not
52-
BuiltinImpl,
52+
BuiltinImpl(BuiltinImplSource),
5353
/// An assumption from the environment.
5454
///
5555
/// More precisely we've used the `n-th` assumption in the `param_env`.
@@ -87,6 +87,16 @@ pub(super) enum CandidateSource {
8787
AliasBound,
8888
}
8989

90+
/// Records additional information about what kind of built-in impl this is.
91+
/// This should only be used by selection.
92+
#[derive(Debug, Clone, Copy)]
93+
pub(super) enum BuiltinImplSource {
94+
TraitUpcasting,
95+
Object,
96+
Misc,
97+
Ambiguity,
98+
}
99+
90100
/// Methods used to assemble candidates for either trait or projection goals.
91101
pub(super) trait GoalKind<'tcx>:
92102
TypeFoldable<TyCtxt<'tcx>> + Copy + Eq + std::fmt::Display
@@ -295,7 +305,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
295305
// least structurally resolve the type one layer.
296306
if goal.predicate.self_ty().is_ty_var() {
297307
return vec![Candidate {
298-
source: CandidateSource::BuiltinImpl,
308+
source: CandidateSource::BuiltinImpl(BuiltinImplSource::Ambiguity),
299309
result: self
300310
.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
301311
.unwrap(),
@@ -344,7 +354,10 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
344354
let result = ecx.evaluate_added_goals_and_make_canonical_response(
345355
Certainty::Maybe(MaybeCause::Overflow),
346356
)?;
347-
Ok(vec![Candidate { source: CandidateSource::BuiltinImpl, result }])
357+
Ok(vec![Candidate {
358+
source: CandidateSource::BuiltinImpl(BuiltinImplSource::Ambiguity),
359+
result,
360+
}])
348361
},
349362
|ecx| {
350363
let normalized_ty = ecx.next_ty_infer();
@@ -447,17 +460,21 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
447460
};
448461

449462
match result {
450-
Ok(result) => {
451-
candidates.push(Candidate { source: CandidateSource::BuiltinImpl, result })
452-
}
463+
Ok(result) => candidates.push(Candidate {
464+
source: CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
465+
result,
466+
}),
453467
Err(NoSolution) => (),
454468
}
455469

456470
// There may be multiple unsize candidates for a trait with several supertraits:
457471
// `trait Foo: Bar<A> + Bar<B>` and `dyn Foo: Unsize<dyn Bar<_>>`
458472
if lang_items.unsize_trait() == Some(trait_def_id) {
459473
for result in G::consider_builtin_dyn_upcast_candidates(self, goal) {
460-
candidates.push(Candidate { source: CandidateSource::BuiltinImpl, result });
474+
candidates.push(Candidate {
475+
source: CandidateSource::BuiltinImpl(BuiltinImplSource::TraitUpcasting),
476+
result,
477+
});
461478
}
462479
}
463480
}
@@ -621,9 +638,10 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
621638
};
622639

623640
match result {
624-
Ok(result) => {
625-
candidates.push(Candidate { source: CandidateSource::BuiltinImpl, result })
626-
}
641+
Ok(result) => candidates.push(Candidate {
642+
source: CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
643+
result,
644+
}),
627645
Err(NoSolution) => (),
628646
}
629647
}
@@ -688,9 +706,10 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
688706
}
689707

690708
match G::consider_object_bound_candidate(self, goal, assumption) {
691-
Ok(result) => {
692-
candidates.push(Candidate { source: CandidateSource::BuiltinImpl, result })
693-
}
709+
Ok(result) => candidates.push(Candidate {
710+
source: CandidateSource::BuiltinImpl(BuiltinImplSource::Object),
711+
result,
712+
}),
694713
Err(NoSolution) => (),
695714
}
696715
}
@@ -711,8 +730,10 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
711730
Err(_) => match self
712731
.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
713732
{
714-
Ok(result) => candidates
715-
.push(Candidate { source: CandidateSource::BuiltinImpl, result }),
733+
Ok(result) => candidates.push(Candidate {
734+
source: CandidateSource::BuiltinImpl(BuiltinImplSource::Ambiguity),
735+
result,
736+
}),
716737
// FIXME: This will be reachable at some point if we're in
717738
// `assemble_candidates_after_normalizing_self_ty` and we get a
718739
// universe error. We'll deal with it at this point.

compiler/rustc_trait_selection/src/solve/eval_ctxt.rs

+2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ use super::inspect::ProofTreeBuilder;
2828
use super::search_graph::{self, OverflowHandler};
2929
use super::SolverMode;
3030
use super::{search_graph::SearchGraph, Goal};
31+
pub use select::InferCtxtSelectExt;
3132

3233
mod canonical;
3334
mod probe;
35+
mod select;
3436

3537
pub struct EvalCtxt<'a, 'tcx> {
3638
/// The inference context that backs (mostly) inference and placeholder terms

compiler/rustc_trait_selection/src/solve/eval_ctxt/canonical.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ use rustc_middle::traits::query::NoSolution;
2020
use rustc_middle::traits::solve::{
2121
ExternalConstraints, ExternalConstraintsData, MaybeCause, PredefinedOpaquesData, QueryInput,
2222
};
23-
use rustc_middle::ty::{self, BoundVar, GenericArgKind, Ty};
23+
use rustc_middle::ty::{self, BoundVar, GenericArgKind, Ty, TyCtxt, TypeFoldable};
2424
use rustc_span::DUMMY_SP;
2525
use std::iter;
2626
use std::ops::Deref;
2727

2828
impl<'tcx> EvalCtxt<'_, 'tcx> {
2929
/// Canonicalizes the goal remembering the original values
3030
/// for each bound variable.
31-
pub(super) fn canonicalize_goal(
31+
pub(super) fn canonicalize_goal<T: TypeFoldable<TyCtxt<'tcx>>>(
3232
&self,
33-
goal: Goal<'tcx, ty::Predicate<'tcx>>,
34-
) -> (Vec<ty::GenericArg<'tcx>>, CanonicalInput<'tcx>) {
33+
goal: Goal<'tcx, T>,
34+
) -> (Vec<ty::GenericArg<'tcx>>, CanonicalInput<'tcx, T>) {
3535
let mut orig_values = Default::default();
3636
let canonical_goal = Canonicalizer::canonicalize(
3737
self.infcx,

0 commit comments

Comments
 (0)