Skip to content

Commit

Permalink
Prefer lower vtable candidates in select in new solver
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed May 4, 2024
1 parent d568423 commit 024a9b8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
19 changes: 13 additions & 6 deletions compiler/rustc_trait_selection/src/solve/eval_ctxt/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ impl<'tcx> inspect::ProofTreeVisitor<'tcx> for Select {
)));
}

// Don't winnow until `Certainty::Yes` -- we don't need to winnow until
// codegen, technically, and all goals should hold in codegen.
if matches!(goal.result().unwrap(), Certainty::Maybe(..)) {
return ControlFlow::Break(Ok(None));
}

// We need to winnow. See comments on `candidate_should_be_dropped_in_favor_of`.
let mut i = 0;
while i < candidates.len() {
Expand Down Expand Up @@ -85,8 +91,7 @@ fn candidate_should_be_dropped_in_favor_of<'tcx>(
victim: &inspect::InspectCandidate<'_, 'tcx>,
other: &inspect::InspectCandidate<'_, 'tcx>,
) -> bool {
// Don't winnow until `Certainty::Yes` -- we don't need to winnow until
// codegen, technically.
// codegen, technically, and all goals should hold in codegen.
if matches!(other.result().unwrap(), Certainty::Maybe(..)) {
return false;
}
Expand All @@ -105,12 +110,14 @@ fn candidate_should_be_dropped_in_favor_of<'tcx>(
bug!("should not have assembled a CoherenceUnknowable candidate")
}

// In the old trait solver, we arbitrarily choose lower vtable candidates
// over higher ones.
(
CandidateSource::BuiltinImpl(BuiltinImplSource::Object { vtable_base: a }),
CandidateSource::BuiltinImpl(BuiltinImplSource::Object { vtable_base: b }),
) => a >= b,
// Prefer dyn candidates over non-dyn candidates. This is necessary to
// handle the unsoundness between `impl<T: ?Sized> Any for T` and `dyn Any: Any`.
(
CandidateSource::BuiltinImpl(BuiltinImplSource::Object { .. }),
CandidateSource::BuiltinImpl(BuiltinImplSource::Object { .. }),
) => false,
(
CandidateSource::Impl(_) | CandidateSource::ParamEnv(_) | CandidateSource::AliasBound,
CandidateSource::BuiltinImpl(BuiltinImplSource::Object { .. }),
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/traits/normalize-supertrait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
// comparing the supertrait `Derived<()>` to the expected trait.

//@ build-pass
//@ revisions: current next
//@ ignore-compare-mode-next-solver (explicit revisions)
//@[next] compile-flags: -Znext-solver

trait Proj {
type S;
Expand Down

0 comments on commit 024a9b8

Please sign in to comment.