Rollup merge of #113397 - compiler-errors:new-select-prefer-obj, r=lcnr

Prefer object candidates in new selection

`dyn Any` shouldn't be using [this implementation](https://doc.rust-lang.org/std/any/trait.Any.html#impl-Any-for-T) during codegen.

Prefer object candidates over other candidates, except for other object candidates.
This commit is contained in:
Michael Goulet 2023-07-06 20:11:40 -07:00 committed by GitHub
commit f1c90985e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 0 deletions

View File

@ -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,
}
}

View File

@ -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);
}
}