Skip to content

Bad error message for missing DerefMut #21536

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
kmcallister opened this issue Jan 23, 2015 · 4 comments
Closed

Bad error message for missing DerefMut #21536

kmcallister opened this issue Jan 23, 2015 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@kmcallister
Copy link
Contributor

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

struct Foo(u32);

impl Deref for Foo {
    type Target = u32;
    fn deref<'a>(&'a self) -> &'a u32 { &self.0 }
}

/*
impl DerefMut for Foo {
    fn deref_mut<'a>(&'a mut self) -> &'a mut u32 { &mut self.0 }
}
*/

fn main() {
    let mut foo = Foo(3);
    *foo = 4;
}

gives

foo.rs:18:5: 18:13 error: cannot assign to immutable borrowed content
foo.rs:18     *foo = 4;
              ^~~~~~~~

which is not too helpful. We shouldn't even try to borrow immutably in this case. Or at least tag the borrow with some metadata saying DerefMut wasn't available, so we can print a help: message here.

rustc 1.0.0-dev (ed530d7 2015-01-16 22:41:16 +0000)

Not sure this is actually easy to fix, but I think it'd be a good beginner project to spend a little time looking into it.

@kmcallister kmcallister added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. A-diagnostics Area: Messages for errors, warnings, and lints labels Jan 23, 2015
@ghost
Copy link

ghost commented Jan 23, 2015

I'd like to try tackling this issue.

@ghost
Copy link

ghost commented Jan 29, 2015

So after a lot of reading, I was wondering if it would just be better to present a warning in situations where the compiler prefers to use DerefMut but cannot. However, I am curious as to how one might add the metadata as @kmcallister suggested. I haven't yet understood a clear mechanism by which this could be done.

@steveklabnik
Copy link
Member

Triage: no changes

@nikomatsakis
Copy link
Contributor

Dup of #28419, closing in favor of that one as it has a bit more discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
Development

No branches or pull requests

3 participants