about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Goulet <michael@errs.io>2023-07-06 20:11:40 -0700
committerGitHub <noreply@github.com>2023-07-06 20:11:40 -0700
commitf1c90985e87f2b144b065b4383ab56657749c372 (patch)
tree65e59fc7a1dd0c9144dc9d126554d13787d45135
parent7913d76cb9be2c8a61cbda3833373b81f0a7d736 (diff)
parent3acaa568c25b7ab8cfb08d5b85f638f6baefae14 (diff)
downloadrust-f1c90985e87f2b144b065b4383ab56657749c372.tar.gz
rust-f1c90985e87f2b144b065b4383ab56657749c372.zip
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.
-rw-r--r--compiler/rustc_trait_selection/src/solve/eval_ctxt/select.rs8
-rw-r--r--tests/ui/traits/new-solver/dyn-any-dont-prefer-impl.rs13
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 366e9aa793d..0800738a3f2 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);
+    }
+}