Skip to content

Confusing error message "this doc comment doesn't document anything" when there is a syntax error right after a comment #71982

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

Closed
ipkiss42 opened this issue May 7, 2020 · 3 comments · Fixed by #137281
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The lexing & parsing of Rust source code to an AST C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ipkiss42
Copy link

ipkiss42 commented May 7, 2020

I tried this code:

/// Some documentation. Below "async" is before "pub" by mistake.
async pub fn hello() {}

fn main() {
}

I expected to see this happen: a clear error message telling me about the mistake (async before pub).

Instead, this happened:

error: expected item after doc comment
 --> src/main.rs:1:1
  |
1 | /// Some documentation. Below "async" is before "pub" by mistake.
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this doc comment doesn't document anything

This is a confusing error message. Without the comment, the error is clearer (though arguably still not completely obvious):

error: expected item, found keyword `async`
 --> src/main.rs:1:1
  |
1 | async pub fn hello() {}
  | ^^^^^ expected item

Meta

rustc --version --verbose:

rustc 1.43.0
binary: rustc
commit-hash: unknown
commit-date: unknown
host: x86_64-unknown-linux-gnu
release: 1.43.0
LLVM version: 10.0
Backtrace

No backtrace.

@ipkiss42 ipkiss42 added the C-bug Category: This is a bug. label May 7, 2020
@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The lexing & parsing of Rust source code to an AST C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed C-bug Category: This is a bug. labels May 7, 2020
@edward-shen
Copy link
Contributor

Agreed, I just hit this as well. It also seems to be inconsistent based on the syntax error. For example, a missing identifier syntax error outputs a helpful error

/// hello world
async fn {}
error: expected identifier, found `{`
 --> src/lib.rs:2:10
  |
2 | async fn {}
  |          ^ expected identifier

while a different syntax error prints out a different error than the first one:

/// hello world
<>
error[E0585]: found a documentation comment that doesn't document anything
 --> src/lib.rs:1:1
  |
1 | /// hello world
  | ^^^^^^^^^^^^^^^
  |
  = help: doc comments must come before what they document, maybe a comment was intended with `//`?

@estebank
Copy link
Contributor

Current output:

error: expected one of `extern`, `fn`, or `unsafe`, found keyword `pub`
 --> src/main.rs:2:7
  |
2 | async pub fn hello() {}
  | ------^^^
  | |     |
  | |     expected one of `extern`, `fn`, or `unsafe`
  | help: visibility `pub` must come before `async`: `pub async`

The following case is still subpar

/// hello world
<>

@RedL0tus
Copy link

The following code snippet can trigger this bug as well:

enum TestEnum {
    Works,
    /// Some documentation
    Self,
}

Current output:

error[E0585]: found a documentation comment that doesn't document anything
 --> test_enum.rs:3:5
  |
1 | enum TestEnum {
  |      -------- while parsing this enum
2 |     Works,
3 |     /// Some documentation
  |     ^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: doc comments must come before what they document, if a comment was intended use `//`
  = help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0585`.

Expected output:

error: expected identifier, found keyword `Self`
 --> test_enum.rs:4:5
  |
1 | enum TestEnum {
  |      -------- while parsing this enum
...
4 |     Self,
  |     ^^^^ expected identifier, found keyword
  |
  = help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`

error: aborting due to 1 previous error

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 19, 2025
… r=compiler-errors

Tweak "expected ident" parse error to avoid talking about doc comments

When encountering a doc comment without an identifier after, we'd unconditionally state "this doc comment doesn't document anything", swallowing the *actual* error which is that the thing *after* the doc comment wasn't expected. Added a check that the found token is something that "conceptually" closes the previous item before emitting that error, otherwise just complain about the missing identifier.

In both of the following cases, the syntax error follows a doc comment:
```
error: expected identifier, found keyword `Self`
  --> $DIR/doc-before-bad-variant.rs:4:5
   |
LL | enum TestEnum {
   |      -------- while parsing this enum
...
LL |     Self,
   |     ^^^^ expected identifier, found keyword
   |
   = help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
```
```
error: expected identifier, found `<`
  --> $DIR/doc-before-syntax-error.rs:2:1
   |
LL | <>
   | ^ expected identifier
```

Fix rust-lang#71982.
@bors bors closed this as completed in a090e76 Feb 20, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Feb 20, 2025
Rollup merge of rust-lang#137281 - estebank:doc-comment-syntax-error, r=compiler-errors

Tweak "expected ident" parse error to avoid talking about doc comments

When encountering a doc comment without an identifier after, we'd unconditionally state "this doc comment doesn't document anything", swallowing the *actual* error which is that the thing *after* the doc comment wasn't expected. Added a check that the found token is something that "conceptually" closes the previous item before emitting that error, otherwise just complain about the missing identifier.

In both of the following cases, the syntax error follows a doc comment:
```
error: expected identifier, found keyword `Self`
  --> $DIR/doc-before-bad-variant.rs:4:5
   |
LL | enum TestEnum {
   |      -------- while parsing this enum
...
LL |     Self,
   |     ^^^^ expected identifier, found keyword
   |
   = help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
```
```
error: expected identifier, found `<`
  --> $DIR/doc-before-syntax-error.rs:2:1
   |
LL | <>
   | ^ expected identifier
```

Fix rust-lang#71982.
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 lexing & parsing of Rust source code to an AST C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants