Skip to content

Error with autoderef on newtype wrapping FnMut #26186

Closed
@eefriedman

Description

@eefriedman

Testcase:

use std::ops::*;
struct S<'a, T:FnMut() + 'static + ?Sized>(&'a mut T);
impl<'a, T:?Sized + FnMut() + 'static> DerefMut for S<'a, T> {
    fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 }
}
impl<'a, T:?Sized + FnMut() + 'static> Deref for S<'a, T> {
    type Target = FnMut() + 'a;
    fn deref(&self) -> &Self::Target { &self.0 }
}
fn main() {
    let mut f = ||{};
    let mut s = S(&mut f);
    s();
}

Yields:

<anon>:13:5: 13:6 error: cannot borrow immutable borrowed content as mutable
<anon>:13     s();
              ^

Auto-deref should be picking DerefMut here, not Deref.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-type-systemArea: Type systemC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions