Skip to content

Cannot call by reference to &mut F: FnMut #112808

Open
@AngelicosPhosphoros

Description

@AngelicosPhosphoros

I tried this code:

fn call_by_ref<F, A0, A1>(mut f: F, arg0: A0, arg1: A1)
where
    A0: Sized,
    A1: Sized,
    for<'w> &'w mut F: FnMut(A0, A1),
{
    // This doesn't work.
    (&mut f)(arg0, arg1);
}

// Workaround
fn call_by_ref_and_hack<F, A0, A1>(mut f: F, arg0: A0, arg1: A1)
where
    A0: Sized,
    A1: Sized,
    for<'w> &'w mut F: FnMut(A0, A1),
{
    fn call_once<B0, B1>(f: impl FnOnce(B0, B1), arg0: B0, arg1: B1){
        f(arg0, arg1)
    }
    // This works
    call_once(&mut f, arg0, arg1);
}

I expected to see this happen: Both functions should compile and work.

Instead, this happened: Only version with inner function hack works.

Meta

rustc --version --verbose:

rustc 1.70.0 (90c541806 2023-05-31)

AFAIK, it never worked.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-HIRArea: The high-level intermediate representation (HIR)C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions