about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2025-02-07 21:30:59 +0100
committerGitHub <noreply@github.com>2025-02-07 21:30:59 +0100
commitc49fc911f451b6e27dcd7fbfac77ab2902fd3ae5 (patch)
treeeb6bbc0989ed43a212b2a8af8fb189ad6858db8f
parent64e06c0f5578829373743884b708d494136c3e8f (diff)
parent999695bab9779db8b9c6dc5fc2135777562eae47 (diff)
downloadrust-c49fc911f451b6e27dcd7fbfac77ab2902fd3ae5.tar.gz
rust-c49fc911f451b6e27dcd7fbfac77ab2902fd3ae5.zip
Rollup merge of #135179 - compiler-errors:arbitrary-self-types-object, r=BoxyUwU
Make sure to use `Receiver` trait when extracting object method candidate

In method confirmation, the `extract_existential_trait_ref` function re-extracts the object type by derefing until it reaches an object. If we're assembling methods via the `Receiver` trait, make sure we re-do our work also using the receiver trait.

Fixes #135155

cc ``@adetaylor``
-rw-r--r--compiler/rustc_hir_typeck/src/method/confirm.rs14
-rw-r--r--tests/ui/self/arbitrary_self_types_dispatch_to_vtable.rs33
2 files changed, 44 insertions, 3 deletions
diff --git a/compiler/rustc_hir_typeck/src/method/confirm.rs b/compiler/rustc_hir_typeck/src/method/confirm.rs
index 5c1c38aeb95..3e48e8d15c3 100644
--- a/compiler/rustc_hir_typeck/src/method/confirm.rs
+++ b/compiler/rustc_hir_typeck/src/method/confirm.rs
@@ -347,9 +347,17 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
         // yield an object-type (e.g., `&Object` or `Box<Object>`
         // etc).
 
-        // FIXME: this feels, like, super dubious
-        self.fcx
-            .autoderef(self.span, self_ty)
+        let mut autoderef = self.fcx.autoderef(self.span, self_ty);
+
+        // We don't need to gate this behind arbitrary self types
+        // per se, but it does make things a bit more gated.
+        if self.tcx.features().arbitrary_self_types()
+            || self.tcx.features().arbitrary_self_types_pointers()
+        {
+            autoderef = autoderef.use_receiver_trait();
+        }
+
+        autoderef
             .include_raw_pointers()
             .find_map(|(ty, _)| match ty.kind() {
                 ty::Dynamic(data, ..) => Some(closure(
diff --git a/tests/ui/self/arbitrary_self_types_dispatch_to_vtable.rs b/tests/ui/self/arbitrary_self_types_dispatch_to_vtable.rs
new file mode 100644
index 00000000000..f9e346ea11e
--- /dev/null
+++ b/tests/ui/self/arbitrary_self_types_dispatch_to_vtable.rs
@@ -0,0 +1,33 @@
+//@ check-pass
+
+#![feature(derive_coerce_pointee)]
+#![feature(arbitrary_self_types)]
+
+use std::marker::CoercePointee;
+use std::ops::Receiver;
+
+// `CoercePointee` isn't needed here, it's just a simpler
+// (and more conceptual) way of deriving `DispatchFromDyn`.
+// You could think of `MyDispatcher` as a smart pointer
+// that just doesn't deref to its target type.
+#[derive(CoercePointee)]
+#[repr(transparent)]
+struct MyDispatcher<T: ?Sized>(*const T);
+
+impl<T: ?Sized> Receiver for MyDispatcher<T> {
+    type Target = T;
+}
+struct Test;
+
+trait Trait {
+    fn test(self: MyDispatcher<Self>);
+}
+
+impl Trait for Test {
+    fn test(self: MyDispatcher<Self>) {
+        todo!()
+    }
+}
+fn main() {
+    MyDispatcher::<dyn Trait>(core::ptr::null_mut::<Test>()).test();
+}