Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustc loosing track of DerefMut impl on nested type #68590

Closed
programmerjake opened this issue Jan 28, 2020 · 3 comments · Fixed by #72280
Closed

rustc loosing track of DerefMut impl on nested type #68590

programmerjake opened this issue Jan 28, 2020 · 3 comments · Fixed by #72280
Labels
A-traits Area: Trait system C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@programmerjake
Copy link
Member

The following code should compile but doesn't. With only 9 nesting levels, I wouldn't expect to be running into deref-limits yet.

use std::ops::{Deref, DerefMut};

trait DynTrait {
    fn f(&mut self);
}

struct Base<'a> {
    dyn_trait: &'a mut dyn DynTrait,
}

macro_rules! impl_type {
    ($derived:ident, $base:ident) => {
        struct $derived<'a> {
            base: $base<'a>,
        }
        
        impl<'a> Deref for $derived<'a> {
            type Target = $base<'a>;
            fn deref(&self) -> &$base<'a> {
                &self.base
            }
        }
        
        impl<'a> DerefMut for $derived<'a> {
            fn deref_mut(&mut self) -> &mut $base<'a> {
                &mut self.base
            }
        }
    };
}

impl_type!(Level1, Base);
impl_type!(Level2, Level1);
impl_type!(Level3, Level2);
impl_type!(Level4, Level3);
impl_type!(Level5, Level4);
impl_type!(Level6, Level5);
impl_type!(Level7, Level6);
impl_type!(Level8, Level7);
impl_type!(Level9, Level8);

fn take_dyn_trait(dyn_trait: &mut dyn DynTrait) {}

fn f<'a>(value: &mut Level9<'a>) {
    // uncomment following line to make compile
    //let value: &mut Base = value;
    take_dyn_trait(value.dyn_trait);
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
warning: unused variable: `dyn_trait`
  --> src/lib.rs:42:19
   |
42 | fn take_dyn_trait(dyn_trait: &mut dyn DynTrait) {}
   |                   ^^^^^^^^^ help: consider prefixing with an underscore: `_dyn_trait`
   |
   = note: `#[warn(unused_variables)]` on by default

error[E0596]: cannot borrow data in a dereference of `Level1<'_>` as mutable
  --> src/lib.rs:47:20
   |
47 |     take_dyn_trait(value.dyn_trait);
   |                    ^^^^^^^^^^^^^^^ cannot borrow as mutable
   |
   = help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `Level1<'_>`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0596`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

@jonas-schievink jonas-schievink added C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. and removed C-bug Category: This is a bug. labels Jan 28, 2020
@jonas-schievink
Copy link
Contributor

This does not hit any recursion limit, it also happens with only one level. I suspect the issue is that you're taking a &'a mut reference from a mutable reference that has a shorter lifetime.

@spunit262
Copy link
Contributor

This isn't a lifetime issue as an explicit mutable reborrow fixes it.

    take_dyn_trait(&mut *value.dyn_trait);

I believe the cause is the compiler can't see that it's a mutable use so it just uses Deref instead of DerefMut. There's an another similar issue (#64261).

@nbdd0121
Copy link
Contributor

I managed to create a minimal example

use std::cell::RefCell;
use std::ops::DerefMut;

struct S<'a> {
    f: &'a mut dyn FnMut(),
}

fn take_f(_: &mut dyn FnMut()) {}

fn test<'a>(s: &RefCell<S<'a>>) {
    let mut guard = s.borrow_mut();

    // In function call context
    take_f(DerefMut::deref_mut(&mut guard).f); // Works
    take_f(guard.f); // Doesn't work
    
    // In plain expression context
    let s2 = S { f: DerefMut::deref_mut(&mut guard).f }; // Works
    let s2 = S { f: guard.f }; // Doesn't work
}

fn main() {
    let f = &mut || ();
    let s = RefCell::new(S { f });
    test(&s);
}

It actually has nothing to do with nested Deref, and is not an diagnostics bug. Basically Rust defaults to use Deref unless it's in a context of mutable place. This is similar to #72225 but it way more generic... I'll look into this if I have time but it does not seem to be a straightforward fix.

@jonas-schievink jonas-schievink added A-traits Area: Trait system and removed A-diagnostics Area: Messages for errors, warnings, and lints labels May 16, 2020
@bors bors closed this as completed in 70622db Jun 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-traits Area: Trait system C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants