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

Error E0053 (values differ in mutability) has a too general span #18760

Closed
mcpherrinm opened this issue Nov 8, 2014 · 4 comments
Closed

Error E0053 (values differ in mutability) has a too general span #18760

mcpherrinm opened this issue Nov 8, 2014 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@mcpherrinm
Copy link
Contributor

An example: http://is.gd/GuGCDC

:4:5: 7:4 error: method `add` has an incompatible type for trait: values differ in mutability [E0053]
:4     fn add(&self, _rhs: &mut Foo) -> Foo {
:5       println!("Adding!");
:6       *self
:7   }

A better error might have a span showing just the mut (or the parameter) and a suggested correct signature. Right now I have to go look the trait up, either in the code or the docs.

 fn add(&self, _rhs: &mut Foo) -> Foo {
                      ^~~
 Note: The correct signature would be
 fn add(&self, _rhs: &Foo) -> Foo {
@huonw huonw added the A-diagnostics Area: Messages for errors, warnings, and lints label Nov 8, 2014
@nham
Copy link
Contributor

nham commented May 6, 2015

This appears to still be a problem. The Add trait has changed (so the example above no longer applies), but an updated example is:

use std::ops::Deref;

struct Foo(u8);

impl Deref for  Foo {
    type Target = u8;

    fn deref<'a>(&'a mut self) -> &'a u8 {
        &self.0
    }
}

fn main() {}

The result:

:8:5: 10:6 error: method `deref` has an incompatible type for trait: values differ in mutability [E0053]
:8     fn deref<'a>(&'a mut self) -> &'a u8 {
:9         &self.0
:10     }
error: aborting due to previous error

@oli-obk
Copy link
Contributor

oli-obk commented Feb 4, 2016

simplified version:

trait A {
    fn b(c: i32);
}

impl A for () {
    fn b(c: ()) {  }
}

This occurs because the check re-uses the type inference/relation code for creating the error message, but the type inference/relation code does not use sub-spans for checking inner types and thus doesn't/can't attach spans to the error object.

@steveklabnik steveklabnik added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@estebank
Copy link
Contributor

Current output:

error[E0053]: method `b` has an incompatible type for trait
 --> <anon>:6:13
  |
2 |     fn b(c: i32);
  |             --- type in trait
...
6 |     fn b(c: ()) {  }
  |             ^^ expected i32, found ()
  |
  = note: expected type `fn(i32)`
             found type `fn(())`

@Mark-Simulacrum
Copy link
Member

Closing as fixed, tests already exist -- ui/mismatched_types/trait-impl-fn-incompatibility.rs.

trait A {
    fn b(c: &i32);
}

impl A for () {
    fn b(c: &mut i32) {  }
}
error[E0053]: method `b` has an incompatible type for trait
 --> test.rs:6:13
  |
2 |     fn b(c: &i32);
  |             ---- type in trait
...
6 |     fn b(c: &mut i32) {  }
  |             ^^^^^^^^ types differ in mutability
  |
  = note: expected type `fn(&i32)`
             found type `fn(&mut i32)`

error: aborting due to previous error(s)

lnicola pushed a commit to lnicola/rust that referenced this issue Jan 7, 2025
internal: Workaround salsa cycles leaking
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 T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants