Skip to content

Commit 376416e

Browse files
authored
Rollup merge of rust-lang#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.
2 parents 0951f38 + aa884da commit 376416e

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

compiler/rustc_hir_typeck/src/method/confirm.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -681,17 +681,23 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
681681
traits::upcast_choices(self.tcx, source_trait_ref, target_trait_def_id);
682682

683683
// must be exactly one trait ref or we'd get an ambig error etc
684-
let [upcast_trait_ref] = upcast_trait_refs.as_slice() else {
685-
span_bug!(
684+
if let &[upcast_trait_ref] = upcast_trait_refs.as_slice() {
685+
upcast_trait_ref
686+
} else {
687+
self.dcx().span_delayed_bug(
686688
self.span,
687-
"cannot uniquely upcast `{:?}` to `{:?}`: `{:?}`",
688-
source_trait_ref,
689-
target_trait_def_id,
690-
upcast_trait_refs
691-
)
692-
};
689+
format!(
690+
"cannot uniquely upcast `{:?}` to `{:?}`: `{:?}`",
691+
source_trait_ref, target_trait_def_id, upcast_trait_refs
692+
),
693+
);
693694

694-
*upcast_trait_ref
695+
ty::Binder::dummy(ty::TraitRef::new_from_args(
696+
self.tcx,
697+
target_trait_def_id,
698+
ty::GenericArgs::extend_with_error(self.tcx, target_trait_def_id, &[]),
699+
))
700+
}
695701
}
696702

697703
fn instantiate_binder_with_fresh_vars<T>(&self, value: ty::Binder<'tcx, T>) -> T
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Makes sure we don't ICE when encountering a receiver that is *ostensibly* dyn safe,
2+
// because it satisfies `&dyn Bar: DispatchFromDyn<&dyn Bar>`, but is not a valid receiver
3+
// in wfcheck.
4+
5+
#![feature(arbitrary_self_types)]
6+
7+
use std::ops::Deref;
8+
9+
trait Foo: Deref<Target = dyn Bar> {
10+
fn method(self: &dyn Bar) {}
11+
//~^ ERROR invalid `self` parameter type: `&dyn Bar`
12+
}
13+
14+
trait Bar {}
15+
16+
fn test(x: &dyn Foo) {
17+
x.method();
18+
}
19+
20+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0307]: invalid `self` parameter type: `&dyn Bar`
2+
--> $DIR/invalid-self-dyn-receiver.rs:10:22
3+
|
4+
LL | fn method(self: &dyn Bar) {}
5+
| ^^^^^^^^
6+
|
7+
= note: type of `self` must be `Self` or some type implementing `Receiver`
8+
= 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>`
9+
10+
error: aborting due to 1 previous error
11+
12+
For more information about this error, try `rustc --explain E0307`.

0 commit comments

Comments
 (0)