Skip to content

Commit

Permalink
Prefer object candidates over impl candidates in new selection
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Jul 6, 2023
1 parent bd8aabe commit 3acaa56
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions compiler/rustc_trait_selection/src/solve/eval_ctxt/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,18 @@ fn candidate_should_be_dropped_in_favor_of<'tcx>(
victim_idx >= other_idx
}
(_, CandidateSource::ParamEnv(_)) => true,

(
CandidateSource::BuiltinImpl(BuiltinImplSource::Object),
CandidateSource::BuiltinImpl(BuiltinImplSource::Object),
) => false,
(_, CandidateSource::BuiltinImpl(BuiltinImplSource::Object)) => true,

(CandidateSource::Impl(victim_def_id), CandidateSource::Impl(other_def_id)) => {
tcx.specializes((other_def_id, victim_def_id))
&& other.result.value.certainty == Certainty::Yes
}

_ => false,
}
}
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/traits/new-solver/dyn-any-dont-prefer-impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// compile-flags: -Ztrait-solver=next
// check-pass

use std::any::Any;

fn needs_usize(_: &usize) {}

fn main() {
let x: &dyn Any = &1usize;
if let Some(x) = x.downcast_ref::<usize>() {
needs_usize(x);
}
}

0 comments on commit 3acaa56

Please sign in to comment.