diff options
| author | Michael Goulet <michael@errs.io> | 2023-07-06 04:48:33 +0000 |
|---|---|---|
| committer | Michael Goulet <michael@errs.io> | 2023-07-06 04:57:17 +0000 |
| commit | 3acaa568c25b7ab8cfb08d5b85f638f6baefae14 (patch) | |
| tree | b3e616b07baa0916196cb1a90f91ad9c4a788127 | |
| parent | bd8aabef316bf8779193798eaf35b8749221a9b4 (diff) | |
| download | rust-3acaa568c25b7ab8cfb08d5b85f638f6baefae14.tar.gz rust-3acaa568c25b7ab8cfb08d5b85f638f6baefae14.zip | |
Prefer object candidates over impl candidates in new selection
| -rw-r--r-- | compiler/rustc_trait_selection/src/solve/eval_ctxt/select.rs | 8 | ||||
| -rw-r--r-- | tests/ui/traits/new-solver/dyn-any-dont-prefer-impl.rs | 13 |
2 files changed, 21 insertions, 0 deletions
diff --git a/compiler/rustc_trait_selection/src/solve/eval_ctxt/select.rs b/compiler/rustc_trait_selection/src/solve/eval_ctxt/select.rs index 18332d6811d..ed238b36ee5 100644 --- a/compiler/rustc_trait_selection/src/solve/eval_ctxt/select.rs +++ b/compiler/rustc_trait_selection/src/solve/eval_ctxt/select.rs @@ -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, } } diff --git a/tests/ui/traits/new-solver/dyn-any-dont-prefer-impl.rs b/tests/ui/traits/new-solver/dyn-any-dont-prefer-impl.rs new file mode 100644 index 00000000000..7d15b8c6392 --- /dev/null +++ b/tests/ui/traits/new-solver/dyn-any-dont-prefer-impl.rs @@ -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); + } +} |
