Closed
Description
I tried this code:
pub pub fn toto() {}
I expected to see this happen: Rust tells me to fix the issue by removing a pub
.
Instead, this happened: Rust tells me to add a pub
before the existing ones: pub pub pub
:
3 | pub pub fn toto() {}
| ^^^
| |
| expected one of 9 possible tokens
| help: visibility `pub` must come before `pub pub`: `pub pub pub`
This will also be an issue with const async const
(and similar errors with other keywords) after #87235 is merged.
Meta
rustc --version --verbose
:
rustc 1.56.0-nightly (2827db2b1 2021-08-01)
binary: rustc
commit-hash: 2827db2b137e899242e81f1beea39ae26e245153
commit-date: 2021-08-01
host: x86_64-apple-darwin
release: 1.56.0-nightly
LLVM version: 12.0.1
Backtrace
Compiling playground v0.1.0 (/Users/alexis/Projects/rust/playground)
error: expected one of `(`, `async`, `const`, `default`, `extern`, `fn`, `pub`, `unsafe`, or `use`, found keyword `pub`
--> src/main.rs:3:5
|
3 | pub pub fn toto() {}
| ^^^
| |
| expected one of 9 possible tokens
| help: visibility `pub` must come before `pub pub`: `pub pub pub`
error: could not compile `playground` due to previous error
@rustbot label A-diagnostics T-compiler
Activity
Nicholas-Baron commentedon Aug 4, 2021
I have been looking at this problem and I would like to share my findings so far.
It seems that the second
pub
is being caught too late.The function
parse_fn_front_matter
generates this error.However, the visibility modifier (e.g.
pub
,crate
) should have been taken byparse_visibility
.I am new to this area of the code. I have been trying to create a
Err(DiagnosticBuilder)
to return fromparse_visibility
, but I cannot figure out how to create or even get one. Any help is appreciated.poliorcetics commentedon Aug 4, 2021
It will probably be very hard to handle this from
parse_visibility
because a similar error occurs when usingpub const pub ...
and soparse_visibility
will not detect this case either.My first intuition would be to pass a parameter to
parse_fn_front_matter
that says whether a visibility was parsed earlier or not (something likehas_vis: bool
, or maybe anOption<Span>
, depends on the error message you want to expose) and act accordingly. I'm waiting for my PR #87235 to be merged and I'll fix that behaviour forconst
,async
and other keywords that can be found in front of a function.Edit: actual error of
pub const pub
:Nicholas-Baron commentedon Aug 5, 2021
I started investigating the call tree between
parse_visibility
andparse_fn_front_matter
.The root is at
parse_item_common_
. First isparse_visibility
, then isparse_item_kind
.parse_item_kind
callsparse_fn
, which callsparse_fn_front_matter
.&vis
is already passed toparse_item_kind
, soparse_fn
will need to take it. However,parse_fn
is called inparse_field_ident
to help beginners, soOption<&Visibility>
is the parameter type.Nicholas-Baron commentedon Aug 5, 2021
I have got the error message changed! How should I word the message, and how should the spans be shown? The warning for
pub
macros seems like a decent place to start.Link to my fork, where the work is being done.
Nicholas-Baron commentedon Aug 5, 2021
@rustbot claim.
Nicholas-Baron commentedon Aug 5, 2021
@rustbot release-assignment.
poliorcetics commentedon Aug 9, 2021
@rustbot claim
pub
when usingpub pub fn ...
#87901Rollup merge of rust-lang#87901 - poliorcetics:pub-pub-pub, r=jackh726