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

Poor parser error message when item category missing (assumes input is a macro) #40006

Closed
pnkfelix opened this issue Feb 21, 2017 · 3 comments
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@pnkfelix
Copy link
Member

Consider this code (playpen):

struct S;
impl S { pub hello_method(&self) { println!("Hello"); } }
fn main() { S.hello_method(); }

It yields this error today:

rustc 1.17.0-nightly (a17e5e294 2017-02-20)
error: can't qualify macro invocation with `pub`
 --> <anon>:2:10
  |
2 | impl S { pub hello_method(&self) { println!("Hello"); } }
  |          ^^^
  |
  = help: try adjusting the macro to put `pub` inside the invocation

error: expected one of `!` or `::`, found `(`
 --> <anon>:2:26
  |
2 | impl S { pub hello_method(&self) { println!("Hello"); } }
  |                          ^

error: aborting due to previous error

In this case, the actual error is that the user left out the keyword after the visibility to indicate which kind of item they are trying to define here.

Our parser should be able to do a better job here. At the very least, if there is no trailing !, then instead of assuming that the input was a macro invocation, it should instead assume it was some item definition that is missing its keyword.

@pnkfelix pnkfelix added A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST labels Feb 21, 2017
@steveklabnik steveklabnik added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@timthelion
Copy link

I ran into what seems to be a very similar case. If you don't put a ; at the end of a match block, then the compiler tells you very unrelated things: https://play.rust-lang.org/?gist=ab74d4c0f3dce8c095f79469bc69b072&version=nightly&backtrace=0

@sanxiyn
Copy link
Member

sanxiyn commented Mar 24, 2017

@timthelion Can you file that as a separate bug? Thanks!

@timthelion
Copy link

@sanxiyn as you wish.

frewsxcv added a commit to frewsxcv/rust that referenced this issue Apr 5, 2017
Identify missing item category in `impl`s

```rust
struct S;
impl S {
    pub hello_method(&self) {
        println!("Hello");
    }
}
fn main() { S.hello_method(); }
```

```rust
error: missing `fn` for method declaration
 --> file.rs:3:4
  |
3 |     pub hello_method(&self) {
  |        ^ missing `fn`
```

Fix rust-lang#40006. r? @pnkfelix CC @jonathandturner @GuillaumeGomez
arielb1 pushed a commit to arielb1/rust that referenced this issue Apr 5, 2017
Identify missing item category in `impl`s

```rust
struct S;
impl S {
    pub hello_method(&self) {
        println!("Hello");
    }
}
fn main() { S.hello_method(); }
```

```rust
error: missing `fn` for method declaration
 --> file.rs:3:4
  |
3 |     pub hello_method(&self) {
  |        ^ missing `fn`
```

Fix rust-lang#40006. r? @pnkfelix CC @jonathandturner @GuillaumeGomez
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 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

4 participants