At its core we use fudging for the following pattern
fn foo<T>(x: T) -> Box<T> { Box::new(x) }
fn main() {
let _: Box<dyn Fn(&str) -> usize> = foo(|s| s.len());
}
We end up with the expectation that foos argument is dyn Fn(&str) -> usize even though that type is not Sized. This is why fudge_inference_if_ok exists and why it's used for function calls when checkig their arguments.
We try to deal with this by weakening the expectation in case it's not sized
|
pub(super) fn rvalue_hint(fcx: &FnCtxt<'a, 'tcx>, ty: Ty<'tcx>) -> Expectation<'tcx> { |
|
let span = match ty.kind() { |
|
ty::Adt(adt_def, _) => fcx.tcx.def_span(adt_def.did()), |
|
_ => fcx.tcx.def_span(fcx.body_id), |
|
}; |
|
let cause = ObligationCause::misc(span, fcx.body_id); |
|
|
|
// FIXME: This is not right, even in the old solver... |
|
match fcx.tcx.struct_tail_raw(ty, &cause, |ty| ty, || {}).kind() { |
|
ty::Slice(_) | ty::Str | ty::Dynamic(..) => ExpectRvalueLikeUnsized(ty), |
|
_ => ExpectHasType(ty), |
|
} |
|
} |
This is widely insufficient for multiple reasons
At its core we use fudging for the following pattern
We end up with the expectation that
foos argument isdyn Fn(&str) -> usizeeven though that type is notSized. This is whyfudge_inference_if_okexists and why it's used for function calls when checkig their arguments.We try to deal with this by weakening the expectation in case it's not sized
rust/compiler/rustc_hir_typeck/src/expectation.rs
Lines 77 to 89 in 7b9905e
This is widely insufficient for multiple reasons