Skip to content

Commit aa884da

Browse files
Delay bug when method confirmation cannot upcast object pick of self
1 parent f2c4ccd commit aa884da

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
@@ -673,17 +673,23 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
673673
traits::upcast_choices(self.tcx, source_trait_ref, target_trait_def_id);
674674

675675
// must be exactly one trait ref or we'd get an ambig error etc
676-
let [upcast_trait_ref] = upcast_trait_refs.as_slice() else {
677-
span_bug!(
676+
if let &[upcast_trait_ref] = upcast_trait_refs.as_slice() {
677+
upcast_trait_ref
678+
} else {
679+
self.dcx().span_delayed_bug(
678680
self.span,
679-
"cannot uniquely upcast `{:?}` to `{:?}`: `{:?}`",
680-
source_trait_ref,
681-
target_trait_def_id,
682-
upcast_trait_refs
683-
)
684-
};
681+
format!(
682+
"cannot uniquely upcast `{:?}` to `{:?}`: `{:?}`",
683+
source_trait_ref, target_trait_def_id, upcast_trait_refs
684+
),
685+
);
685686

686-
*upcast_trait_ref
687+
ty::Binder::dummy(ty::TraitRef::new_from_args(
688+
self.tcx,
689+
target_trait_def_id,
690+
ty::GenericArgs::extend_with_error(self.tcx, target_trait_def_id, &[]),
691+
))
692+
}
687693
}
688694

689695
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)