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

Confusing error message: "can't qualify macro invocation with pub" when a inherent method lacks "fn" #38911

Closed
ExpHP opened this issue Jan 7, 2017 · 4 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. WG-diagnostics Working group: Diagnostics

Comments

@ExpHP
Copy link
Contributor

ExpHP commented Jan 7, 2017

Both stable 1.14 and nightly 2017-01-06.
playpen

struct A {}

impl A {
    pub get(self) {}
}

(Edit: Above example is fixed as of 1.20.0, but free functions are affected.)

rustc 1.14.0 (e8a012324 2016-12-16)
error: can't qualify macro invocation with `pub`
 --> <anon>:4:5
  |
4 |     pub get(self) {}
  |     ^^^
  |
  = help: try adjusting the macro to put `pub` inside the invocation

error: expected one of `!` or `::`, found `(`
 --> <anon>:4:12
  |
4 |     pub get(self) {}
  |            ^

error: aborting due to previous error

while the actual issue is that the definition of get is missing the fn keyword.

@Mark-Simulacrum Mark-Simulacrum added the A-parser Area: The parsing of Rust source code to an AST label May 19, 2017
@Mark-Simulacrum Mark-Simulacrum added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-diagnostics Area: Messages for errors, warnings, and lints labels Jul 26, 2017
@estebank
Copy link
Contributor

If parse_impl_method doesn't see fn it tries to parse as macro, always and it uses complain_if_pub_macro to output the "can't qualify macro invocation with 'pub'" error. We can use the same approach as #42578 if we fail to find the !, rewind the parser and try to parse the rest as a method. If it succeeds in doing so, expand the diagnostic with a suggestion to write pub fn get, otherwise rewind the parser to the previous error and emit the original diagnostic without modification.

@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
@ExpHP
Copy link
Contributor Author

ExpHP commented Sep 22, 2017

On the contrary, it seems to me this has been fixed: (current playpen, presumably 1.20.0)

error: missing `fn`, `type`, or `const` for impl-item declaration
 --> src/main.rs:4:8
  |
4 |     pub get(self) {}
  |        ^ missing `fn`, `type`, or `const`

error: aborting due to previous error

@ExpHP
Copy link
Contributor Author

ExpHP commented Sep 22, 2017

Ahah, it still occurs for functions that are not part of an impl.

pub get(x: u8) {}

fn main() {}
   Compiling playground v0.0.1 (file:///playground)
error: can't qualify macro invocation with `pub`
 --> src/main.rs:1:1
  |
1 | pub get(x: u8) {}
  | ^^^
  |
  = help: try adjusting the macro to put `pub` inside the invocation

error: expected one of `!` or `::`, found `(`
 --> src/main.rs:1:8
  |
1 | pub get(x: u8) {}
  |        ^ expected one of `!` or `::` here

error: aborting due to 2 previous errors

error: Could not compile `playground`.

@estebank 's analysis seems to suggest that impl methods should still have issues though. I wonder if there is another example where an impl method still produces the old error?

@fschutt
Copy link
Contributor

fschutt commented Nov 7, 2017

Just wanted to say, this happens when you create a struct but forget the struct keyword:

pub MyStruct { }; // keyword struct missing

fn main() { }
   Compiling playground v0.0.1 (file:///playground)
error: can't qualify macro invocation with `pub`
 --> src/main.rs:1:1
  |
1 | pub MyStruct { }; // keyword struct missing
  | ^^^
  |
  = help: try adjusting the macro to put `pub` inside the invocation

error: expected one of `!` or `::`, found `{`
 --> src/main.rs:1:14
  |
1 | pub MyStruct { }; // keyword struct missing
  |             -^ unexpected token
  |             |
  |             expected one of `!` or `::` here

error: aborting due to 2 previous errors

A better error message would be helpful.

@estebank estebank added WG-diagnostics Working group: Diagnostics and removed E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. labels Nov 15, 2017
bors added a commit that referenced this issue Dec 1, 2017
Account for missing keyword in fn/struct definition

Fix #38911.
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. WG-diagnostics Working group: Diagnostics
Projects
None yet
Development

No branches or pull requests

4 participants