-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Helpful error message when confusing a field with a method #2392
Comments
Test:
The error message is better now than it was:
in that it suggests there is a method named |
My previous comment still holds. It would be great if you would see the "try writing an anonymous function" hint for something like |
I'd also like to see one when trying to call a function-typed struct field:
emits It'd be nice if it tried to see if it could auto-deref to a struct field, and if so, say |
A nice idea. Assigning P-low. |
Currently struct Cat {
x: int
}
trait Meow {
fn mew(&self) -> int;
}
impl Meow for Cat {
fn mew(&self) -> int {
self.x
}
}
fn main() {
let kitty = Cat { x:5 };
assert!(kitty.mew == 5);
} gives
which is pretty close to the proposed behavior. Replacing the fn main() {
let kitty = Cat { x:5 };
assert!(kitty.x() == 5);
} yields the same as before (i.e. does not suggest removing the
|
Updated the example @vks gives: struct Cat {
x: i32
}
trait Meow {
fn mew(&self) -> i32;
}
impl Meow for Cat {
fn mew(&self) -> i32 {
self.x
}
}
fn main() {
let kitty = Cat { x:5 };
assert!(kitty.x() == 5);
} which now prints
It's nice that the note is there, but the text is suboptimal: |
I'm working on a fix for this, but I'm struggling with the final portion. With this code: struct Cat<F> where F: FnMut() -> u32 {
func: F,
x: i32
}
trait Meow {
fn mew(&self) -> i32;
}
impl Meow for Cat {
fn mew(&self) -> i32 {
self.x
}
}
fn main() {
let kitty = Cat { func: || 5, x: 5 };
assert!(kitty.x() == 5);
let x = kitty.func();
assert_eq!(x, 5);
} I get the output:
My current changes are here. I'm not sure if it's possible to have the output say "kitty.func" and "kitty.x" with the input to the Edit: Ideally, I would like to output:
I'm wondering if that's at all possible, especially because instead of |
I got around that error message. Now I'm making sure the previous |
I forget if there's already a bug on this, but it would be nice if, when you wrote
A.B
andA
doesn't have a field namedB
, but does have a nullary method namedB
, the compiler gave a hint like "did you mean to writeA.B()
?" It could also do vice versa (if you write A.B()and
Ais a class with field
B, it could ask "did you mean
A.B```?)I ran into this in trans, where I have frequently written
bcx.ccx
instead ofbcx.ccx()
.The text was updated successfully, but these errors were encountered: