Skip to content

Cannot index and immediately call FnMut #65682

Closed
@timotree3

Description

@timotree3

Running this code on the latest stable (1.38.0) as well as nightly (2019-10-20)

fn foo<F, I>(i: &mut I)
where
    F: FnMut(),
    I: std::ops::IndexMut<(), Output = F>,
{
    i[()](); // fails
    (&mut i[()])(); // works
}

gives the error

error[E0596]: cannot borrow data in an index of `I` as mutable
 --> src/lib.rs:6:5
  |
6 |     i[()](); // fails
  |     ^^^^^ cannot borrow as mutable
  |
  = help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `I`

which is clearly wrong.

Notably, the same error does not occur if we switch everything to Fn:

fn foo<F, I>(i: &I)
where
    F: Fn(),
    I: std::ops::Index<(), Output = F>,
{
    i[()](); // works
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemC-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