about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2025-02-11 02:53:43 +0100
committerGitHub <noreply@github.com>2025-02-11 02:53:43 +0100
commit66573926f28be9116de08f5b707fd62bb6ef94df (patch)
treee7d59aa206127fc1ca0d83951ac381fb2f2426aa
parent7b490494f2d466076951a472e09fe75478f6e762 (diff)
parentaa884da1e7e1f7d08a3c66ca25a824a907f43258 (diff)
downloadrust-66573926f28be9116de08f5b707fd62bb6ef94df.tar.gz
rust-66573926f28be9116de08f5b707fd62bb6ef94df.zip
Rollup merge of #136524 - compiler-errors:bad-pick, r=BoxyUwU
Delay bug when method confirmation cannot upcast object pick of self

Justification is on the test comment. Simply delays a bug that we were previously ICEing on.

cc ``@adetaylor`` since this is a `arbitrary_self_types` ICE.
-rw-r--r--compiler/rustc_hir_typeck/src/method/confirm.rs24
-rw-r--r--tests/ui/self/invalid-self-dyn-receiver.rs20
-rw-r--r--tests/ui/self/invalid-self-dyn-receiver.stderr12
3 files changed, 47 insertions, 9 deletions
diff --git a/compiler/rustc_hir_typeck/src/method/confirm.rs b/compiler/rustc_hir_typeck/src/method/confirm.rs
index 3e48e8d15c3..36cc9b40d00 100644
--- a/compiler/rustc_hir_typeck/src/method/confirm.rs
+++ b/compiler/rustc_hir_typeck/src/method/confirm.rs
@@ -681,17 +681,23 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
             traits::upcast_choices(self.tcx, source_trait_ref, target_trait_def_id);
 
         // must be exactly one trait ref or we'd get an ambig error etc
-        let [upcast_trait_ref] = upcast_trait_refs.as_slice() else {
-            span_bug!(
+        if let &[upcast_trait_ref] = upcast_trait_refs.as_slice() {
+            upcast_trait_ref
+        } else {
+            self.dcx().span_delayed_bug(
                 self.span,
-                "cannot uniquely upcast `{:?}` to `{:?}`: `{:?}`",
-                source_trait_ref,
-                target_trait_def_id,
-                upcast_trait_refs
-            )
-        };
+                format!(
+                    "cannot uniquely upcast `{:?}` to `{:?}`: `{:?}`",
+                    source_trait_ref, target_trait_def_id, upcast_trait_refs
+                ),
+            );
 
-        *upcast_trait_ref
+            ty::Binder::dummy(ty::TraitRef::new_from_args(
+                self.tcx,
+                target_trait_def_id,
+                ty::GenericArgs::extend_with_error(self.tcx, target_trait_def_id, &[]),
+            ))
+        }
     }
 
     fn instantiate_binder_with_fresh_vars<T>(&self, value: ty::Binder<'tcx, T>) -> T
diff --git a/tests/ui/self/invalid-self-dyn-receiver.rs b/tests/ui/self/invalid-self-dyn-receiver.rs
new file mode 100644
index 00000000000..a989b331b5e
--- /dev/null
+++ b/tests/ui/self/invalid-self-dyn-receiver.rs
@@ -0,0 +1,20 @@
+// Makes sure we don't ICE when encountering a receiver that is *ostensibly* dyn safe,
+// because it satisfies `&dyn Bar: DispatchFromDyn<&dyn Bar>`, but is not a valid receiver
+// in wfcheck.
+
+#![feature(arbitrary_self_types)]
+
+use std::ops::Deref;
+
+trait Foo: Deref<Target = dyn Bar> {
+     fn method(self: &dyn Bar) {}
+     //~^ ERROR invalid `self` parameter type: `&dyn Bar`
+}
+
+trait Bar {}
+
+fn test(x: &dyn Foo) {
+     x.method();
+}
+
+fn main() {}
diff --git a/tests/ui/self/invalid-self-dyn-receiver.stderr b/tests/ui/self/invalid-self-dyn-receiver.stderr
new file mode 100644
index 00000000000..f77f5686ad2
--- /dev/null
+++ b/tests/ui/self/invalid-self-dyn-receiver.stderr
@@ -0,0 +1,12 @@
+error[E0307]: invalid `self` parameter type: `&dyn Bar`
+  --> $DIR/invalid-self-dyn-receiver.rs:10:22
+   |
+LL |      fn method(self: &dyn Bar) {}
+   |                      ^^^^^^^^
+   |
+   = note: type of `self` must be `Self` or some type implementing `Receiver`
+   = help: consider changing to `self`, `&self`, `&mut self`, or a type implementing `Receiver` such as `self: Box<Self>`, `self: Rc<Self>`, or `self: Arc<Self>`
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0307`.