Skip to content

expectations from fudging are a mess #149379

@lcnr

Description

@lcnr

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-technical-debtArea: Internal cleanup workC-cleanupCategory: PRs that clean code up or issues documenting cleanup.T-typesRelevant to the types team, which will review and decide on the PR/issue.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions