Skip to content

Delay bug when method confirmation cannot upcast object pick of self #136524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions compiler/rustc_hir_typeck/src/method/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,17 +673,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
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/self/invalid-self-dyn-receiver.rs
Original file line number Diff line number Diff line change
@@ -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() {}
12 changes: 12 additions & 0 deletions tests/ui/self/invalid-self-dyn-receiver.stderr
Original file line number Diff line number Diff line change
@@ -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`.
Loading