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

entertaining error message on bad macro syntax #15980

Closed
dhardy opened this issue Jul 25, 2014 · 5 comments
Closed

entertaining error message on bad macro syntax #15980

dhardy opened this issue Jul 25, 2014 · 5 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST C-enhancement Category: An issue proposing an enhancement or a PR with one. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-diagnostics Working group: Diagnostics

Comments

@dhardy
Copy link
Contributor

dhardy commented Jul 25, 2014

UPDATE: Mentoring instructions below.


Try compiling this code:

use std::io;

fn main(){
    let x: io::IoResult<()> = Ok(());
    match x {
        Err(ref e) if e.kind == io::EndOfFile {
            return
        }
        _ => {}
    }
}

I (using Rust 0.11.0) get the very misleading message:

$ rustc match_syntax_err.rs
match_syntax_err.rs:7:13: 7:19 error: found `return` in ident position
match_syntax_err.rs:7             return
                                  ^~~~~~
match_syntax_err.rs:8:9: 8:10 error: expected `:` but found `}`
match_syntax_err.rs:8         }

when actually all that's wrong is a missing =>. Not really a good brain teaser on a Friday evening ;)

@steveklabnik steveklabnik added the A-parser Area: The parsing of Rust source code to an AST label Jan 24, 2015
@steveklabnik
Copy link
Member

This error message is the same today. How awkward.

@bombless
Copy link
Contributor

I guess we can summary a pattern here (missing => between pattern and block) to the parsing rule that allow us to add something similar as check_no_chained_comparison.

I suspect another valid approach would be, don't mention the ident thing, just focus on the fact that a value is needed here (a struct literal, in fact), so we can just say "invalid struct literal, unexpected return here" or even simpler, "return not allowed inside pattern matching". At least this will warn the user that the return is not really inside a block.

@gchp
Copy link
Contributor

gchp commented May 30, 2016

On 1.10.0-nightly this gives:

test.rs:5:30: 5:31 error: expected one of `.`, `=>`, `?`, or an operator, found `{`
test.rs:5         Err(ref e) if 1 == 2 {
                                       ^
error: aborting due to previous error

This seems accurate enough to me, the compiler is pointing out where it is expecting to find the =>, and mentioning it in the error message. Do we need something more specific than this?

@dhardy
Copy link
Contributor Author

dhardy commented May 30, 2016

The error I see is still pretty misleading

@Mark-Simulacrum Mark-Simulacrum added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Jul 21, 2017
@estebank
Copy link
Contributor

Current output:

error: expected identifier, found keyword `return`
 --> src/main.rs:7:13
  |
7 |             return
  |             ^^^^^^

parse_pat can parse a struct and recover from an error. It should attach a secondary label pointing at the start of the struct constructor being parsed (I believe it should be lo.to(self.prev_span)) with a label along the lines of "while parsing the fields for this struct". This would end up looking close to this:

error: expected identifier, found keyword `return`
 --> src/main.rs:7:13
  |
6 |         Err(ref e) if e.kind == io::EndOfFile {
  |                                 ------------- while parsing the fields for this struct
7 |             return
  |             ^^^^^^ expected identifier, found keyword

@estebank estebank added the E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. label Sep 21, 2017
@nikomatsakis nikomatsakis added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-diagnostics Working group: Diagnostics labels Sep 25, 2017
bors added a commit that referenced this issue Jan 13, 2018
`struct` pattern parsing and diagnostic tweaks

 - Recover from struct parse error on match and point out missing match
   body.
 - Point at struct when finding non-identifier while parsing its fields.
 - Add label to "expected identifier, found {}" error.

Fix #15980.
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 A-parser Area: The parsing of Rust source code to an AST C-enhancement Category: An issue proposing an enhancement or a PR with one. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-diagnostics Working group: Diagnostics
Projects
None yet
Development

No branches or pull requests

7 participants