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

Better error message for incorrect struct construction syntax. #12425

Closed
dylanbraithwaite opened this issue Feb 20, 2014 · 3 comments
Closed
Labels
A-parser Area: The parsing of Rust source code to an AST

Comments

@dylanbraithwaite
Copy link
Contributor

If a type parametrized struct is constructed with the parameter in the type name, the error produced is not relevant to the actual problem:

struct MyStruct<T> { my_field : T}

followed by:

let struct_instance = MyStruct<int> { my_field : 42}

Will produce the error:

error: expected one of `;`, `}` but found `:`
    let struct_instance = MyStruct<int>{my_field : 42};
                                                 ^

Which does not indicate that the error is due to the inclusion of '<int>' after the struct's name.

@jdm
Copy link
Contributor

jdm commented Feb 20, 2014

The problem is that the code is being interpreted like this:
let struct_instance = (MyStruct < int) > { my_field, ie. parsing MyStruct, int, and my_field as expression names, and the : is a parse error while try to parse a { expr } expression.

@bombless
Copy link
Contributor

struct MyStruct<T> { my_field : T}
fn main() {
    let x = MyStruct<i32> { my_field: 42 };
}
<anon>:3:21: 3:26 error: chained comparison operators require parentheses
<anon>:3     let x = MyStruct<i32> { my_field: 42 };
                             ^~~~~
<anon>:3:26: 3:26 help: use `::<...>` instead of `<...>` if you meant to specify type arguments
<anon>:3:37: 3:38 error: expected one of `!`, `.`, `::`, `;`, `{`, `}`, or an operator, found `:`
<anon>:3     let x = MyStruct<i32> { my_field: 42 };
                                             ^

I believe current error message for this is quite good-looking, so I think it's safe to say this issue has been fixed.

@dylanbraithwaite
Copy link
Contributor Author

Yes, that certainly is a lot better. I guess I can close this now.

bors added a commit to rust-lang-ci/rust that referenced this issue Jul 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-parser Area: The parsing of Rust source code to an AST
Projects
None yet
Development

No branches or pull requests

4 participants