Skip to content

Implementing Deref and DerefMut for the same type makes it impossible to call &self methods through immutable refs of that type #16099

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

Closed
reem opened this issue Jul 30, 2014 · 4 comments

Comments

@reem
Copy link
Contributor

reem commented Jul 30, 2014

Code:

struct Ref<T> {
    inner: T
}

impl<T> Deref<T> for Ref<T> {
    fn deref(&self) -> &T {
        &self.inner
    }
}

impl<T> DerefMut<T> for Ref<T> {
    fn deref_mut(&mut self) -> &mut T {
        &mut self.inner
    }
}

trait ImmutableRefTrait { fn immutable_borrow_method(&self) { () } }
trait MutableRefTrait { fn mutable_borrow_method(&mut self) { () } }

impl ImmutableRefTrait for uint {}
impl MutableRefTrait for uint {}

fn main() {
    let a = Ref { inner: 7u };
    a.immutable_borrow_method();
}

Error:

test.rs:28:5: 28:6 error: cannot borrow immutable local variable `a` as mutable
test.rs:28     a.immutable_borrow_method();
@jdm
Copy link
Contributor

jdm commented Jul 30, 2014

Probably related to #15609.

@huonw
Copy link
Member

huonw commented Jul 30, 2014

I think this may be a dupe of #12825.

@eddyb
Copy link
Member

eddyb commented Jul 30, 2014

Confirmed as unrelated to #15609, dupe of #12825, can close.

@alexcrichton
Copy link
Member

Thanks @eddyb!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants