Don't ICE if method receiver fails to unify with arbitrary_self_types

This commit is contained in:
Michael Goulet 2023-05-23 03:23:52 +00:00
parent 1b67f8b013
commit 05c5caa500
3 changed files with 42 additions and 8 deletions

View File

@ -471,7 +471,7 @@ fn unify_receivers(
self_ty, method_self_ty, self.span, pick self_ty, method_self_ty, self.span, pick
); );
let cause = self.cause( let cause = self.cause(
self.span, self.self_expr.span,
ObligationCauseCode::UnifyReceiver(Box::new(UnifyReceiverContext { ObligationCauseCode::UnifyReceiver(Box::new(UnifyReceiverContext {
assoc_item: pick.item, assoc_item: pick.item,
param_env: self.param_env, param_env: self.param_env,
@ -482,13 +482,22 @@ fn unify_receivers(
Ok(InferOk { obligations, value: () }) => { Ok(InferOk { obligations, value: () }) => {
self.register_predicates(obligations); self.register_predicates(obligations);
} }
Err(_) => { Err(terr) => {
span_bug!( // FIXME(arbitrary_self_types): We probably should limit the
self.span, // situations where this can occur by adding additional restrictions
"{} was a subtype of {} but now is not?", // to the feature, like the self type can't reference method substs.
self_ty, if self.tcx.features().arbitrary_self_types {
method_self_ty self.err_ctxt()
); .report_mismatched_types(&cause, method_self_ty, self_ty, terr)
.emit();
} else {
span_bug!(
self.span,
"{} was a subtype of {} but now is not?",
self_ty,
method_self_ty
);
}
} }
} }
} }

View File

@ -0,0 +1,16 @@
#![feature(arbitrary_self_types)]
use std::ops::Deref;
struct Foo(u32);
impl Foo {
fn get<R: Deref<Target=Self>>(self: R) -> u32 {
self.0
}
}
fn main() {
let mut foo = Foo(1);
foo.get::<&Foo>();
//~^ ERROR mismatched types
}

View File

@ -0,0 +1,9 @@
error[E0308]: mismatched types
--> $DIR/arbitrary-self-from-method-substs.rs:14:5
|
LL | foo.get::<&Foo>();
| ^^^ expected `&Foo`, found `Foo`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.