-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Misleading error message on typo "Type ascription" #34255
Comments
Another from Stack Overflow (missing struct name in constructor): impl Reactor {
pub fn new() -> Self {
input_cells: Vec::new()
}
} |
Same issue here (accidentally typed
|
From the two issues I just closed: fn foo() {
let two = 2:
let two_squared = two * two;
}
fn bar() {
x : i32 = 0;
} |
I was doing some refactoring, going from a tuple struct to a struct. I forgot to replace the parenthesis in the main with curly brackets, and got that confusing error:
enum Test {
Drill {
field: i32,
}
}
fn main() {
Test::Drill(field: 42);
} |
I was also confused by that strange error message. Here is my example: Compiling fn main() {
let s = String:from("Just a string.");
println!("{}", s);
} using rustc 1.29.1 leads to
A little line could really help:
Would that be feasible to implement? |
The case in the first comment is not handled correctly at all (related #73941):
Update: the output for the last case has changed slightly, but it is still not great:
|
Expand suggestions for type ascription parse errors Fix rust-lang#51222. CC rust-lang#48016, rust-lang#47666, rust-lang#54516, rust-lang#34255.
Expand suggestions for type ascription parse errors Fix rust-lang#51222. CC rust-lang#48016, rust-lang#47666, rust-lang#54516, rust-lang#34255.
Expand suggestions for type ascription parse errors Fix rust-lang#51222. CC rust-lang#48016, rust-lang#47666, rust-lang#54516, rust-lang#34255.
…enkov Handle more cases of typos misinterpreted as type ascription Fix rust-lang#60933, rust-lang#54516. CC rust-lang#47666, rust-lang#34255, rust-lang#48016.
…enkov Handle more cases of typos misinterpreted as type ascription Fix rust-lang#60933, rust-lang#54516. CC rust-lang#47666, rust-lang#34255, rust-lang#48016.
…rochenkov Clean up errors in typeck and resolve * Tweak ordering of suggestions * Do not suggest similarly named enclosing item * Point at item definition in foreign crates * Add missing primary label CC rust-lang#34255.
This approach lives exclusively in the parser, so struct expr bodies that are syntactically correct on their own but are otherwise incorrect will still emit confusing errors, like in the following case: ```rust fn foo() -> Foo { bar: Vec::new() } ``` ``` error[E0425]: cannot find value `bar` in this scope --> src/file.rs:5:5 | 5 | bar: Vec::new() | ^^^ expecting a type here because of type ascription error[E0214]: parenthesized type parameters may only be used with a `Fn` trait --> src/file.rs:5:15 | 5 | bar: Vec::new() | ^^^^^ only `Fn` traits may use parentheses error[E0107]: wrong number of type arguments: expected 1, found 0 --> src/file.rs:5:10 | 5 | bar: Vec::new() | ^^^^^^^^^^ expected 1 type argument ``` If that field had a trailing comma, that would be a parse error and it would trigger the new, more targetted, error: ``` error: struct literal body without path --> file.rs:4:17 | 4 | fn foo() -> Foo { | _________________^ 5 | | bar: Vec::new(), 6 | | } | |_^ | help: you might have forgotten to add the struct literal inside the block | 4 | fn foo() -> Foo { Path { 5 | bar: Vec::new(), 6 | } } | ``` Partially address last part of rust-lang#34255.
Detect blocks that could be struct expr bodies This approach lives exclusively in the parser, so struct expr bodies that are syntactically correct on their own but are otherwise incorrect will still emit confusing errors, like in the following case: ```rust fn foo() -> Foo { bar: Vec::new() } ``` ``` error[E0425]: cannot find value `bar` in this scope --> src/file.rs:5:5 | 5 | bar: Vec::new() | ^^^ expecting a type here because of type ascription error[E0214]: parenthesized type parameters may only be used with a `Fn` trait --> src/file.rs:5:15 | 5 | bar: Vec::new() | ^^^^^ only `Fn` traits may use parentheses error[E0107]: wrong number of type arguments: expected 1, found 0 --> src/file.rs:5:10 | 5 | bar: Vec::new() | ^^^^^^^^^^ expected 1 type argument ``` If that field had a trailing comma, that would be a parse error and it would trigger the new, more targetted, error: ``` error: struct literal body without path --> file.rs:4:17 | 4 | fn foo() -> Foo { | _________________^ 5 | | bar: Vec::new(), 6 | | } | |_^ | help: you might have forgotten to add the struct literal inside the block | 4 | fn foo() -> Foo { Path { 5 | bar: Vec::new(), 6 | } } | ``` Partially address last remaining part of rust-lang#34255.
…ct` literal Address part of rust-lang#34255. Potential improvement: silence the other knock down errors in `issue-34255-1.rs`.
…-fire, r=wesleywiser Detect bare blocks with type ascription that were meant to be a `struct` literal Address part of rust-lang#34255. Potential improvement: silence the other knock down errors in `issue-34255-1.rs`.
There are two outstanding issues:
As such, I consider this ticket addressed 😄 |
…ct` literal Address part of rust-lang#34255. Potential improvement: silence the other knock down errors in `issue-34255-1.rs`.
This had me googling for answers. But I had entered a colon instead of a semi-colon at the end of a line. Nothing in my search results pointed that out.
The text was updated successfully, but these errors were encountered: