rustfmt icon indicating copy to clipboard operation
rustfmt copied to clipboard

Confusing error message on syntax error in code in doc comments

Open feds01 opened this issue 2 years ago • 10 comments

When enabling format_code_in_doc_comments, the formatter reports that there is a syntax issue in a code block. However the syntax issue source location seems to be confusing/ambigious:

Running rustfmt on the following code snippet produces a confusing error message:

/// Some doc comment with code snippet:
///```
/// '\u{1F}
/// ```
pub struct Code {}

fn main() {}

The error that is produced:

error[E0762]: unterminated character literal
 --> <stdin>:2:5
  |
2 |     '\u{1F}
  |     ^^^^^^^

This is not explicitly a bug, but nonetheless is confusing as it does not indicate the true origin of the issue.

feds01 avatar Mar 10 '24 16:03 feds01

Thanks for the report. Just want to understand how you're calling rustfmt. Are you running rustfmt directly on a file or is it your editor that's invoking rustfmt?

ytmimi avatar Mar 11 '24 16:03 ytmimi

Linking the tracking issue format_code_in_doc_comments https://github.com/rust-lang/rustfmt/issues/3348

ytmimi avatar Mar 11 '24 16:03 ytmimi

Calling through cargo fmt in the project workspace - hence why the error pops up and its unclear where the source of the error is.

feds01 avatar Mar 14 '24 00:03 feds01

@feds01 by the way, what version of rustfmt are you using?

ytmimi avatar Mar 19 '24 02:03 ytmimi

@davidtwco I'm assuming this might have been caused by sharing the SilentEmitter between rustc and rustfmt in https://github.com/rust-lang/rust/pull/121301. I believe this is coming up because rustc's SilentEmitter is defined to still emit Fatal diagnostics, while rustfmt's SilentEmitter emits nothing. I'm able to reproduce the issue using nightly rustfmt, but not when building rustfmt from source, presumably because we haven't pulled these changes back into rustfmt yet.

Do you think it's possible to modify rustc's SilentEmitter to conditionally ignore Fatal diagnostics as well? For rustfmt I think that's the behavior that we'd want.

ytmimi avatar Mar 19 '24 04:03 ytmimi

@feds01 by the way, what version of rustfmt are you using?

$ cargo --version --verbose

cargo 1.78.0-nightly (a4c63fe53 2024-03-06)
release: 1.78.0-nightly
commit-hash: a4c63fe5388beaa09e5f91196c86addab0a03580
commit-date: 2024-03-06
host: aarch64-apple-darwin
libgit2: 1.7.2 (sys:0.18.2 vendored)
libcurl: 8.1.2 (sys:0.4.72+curl-8.6.0 system ssl:(SecureTransport) LibreSSL/3.3.6)
ssl: OpenSSL 1.1.1w  11 Sep 2023
os: Mac OS 14.1.1 [64-bit]
cargo fmt --version
rustfmt 1.7.0-nightly (2d24fe59 2024-03-09)

feds01 avatar Mar 19 '24 10:03 feds01

I just tested with the nightly-2024-03-25 toolchain (rustfmt 1.7.0-nightly (0824b300 2024-03-24)) and this issue should be resolved.

@feds01 when you have a moment could you also confirm this:

Steps to confirm:

  • rustup install nightly-2024-03-25
  • rustfmt +nightly-2024-03-25 --config=format_code_in_doc_comments=true /path/to/test/file.rs

ytmimi avatar Mar 25 '24 14:03 ytmimi

Before closing this issue I think it would be great if we could add a test case to prevent a regression

ytmimi avatar Mar 25 '24 14:03 ytmimi

It is now not reporting any issues with the file, is the desired behaviour?

feds01 avatar Mar 25 '24 14:03 feds01

@feds01 In this case yes. The previous behavior was for rustfmt to ignore the diagnostic message produced by the rustc parser, however there were some internal changes made in rust-lang/rust that changed that behavior. https://github.com/rust-lang/rust/pull/122737 restored rustfmts previous behavior of ignoring parser errors when formatting code in doc comments.

ytmimi avatar Mar 25 '24 14:03 ytmimi

Hi, I'm planning to help with this as my first contribution. From looking at the contributing.md doc I understand that I would need to create single target file with the code mentioned at the top of this issue?

giordan12 avatar Jul 04 '24 23:07 giordan12

@giordan12 that's great. I appreciate you wanting to take this one one. Yes, you can add a test case in the target directory, and in addition to that I think a new standalone test case should be added in https://github.com/rust-lang/rustfmt/blob/master/tests/rustfmt/main.rs to validate that no errors are emitted to stdout or stderr.

ytmimi avatar Jul 05 '24 19:07 ytmimi

@ytmimi appreciate the guidance, just created the PR. Let me know if there is something to add (I'm expecting there will be :) )

giordan12 avatar Jul 07 '24 04:07 giordan12

closing now that #6232 is merged

ytmimi avatar Jul 08 '24 00:07 ytmimi

Thanks for this guys ♥️ Let me know if I can also help with anything, I'd love to contribute!

feds01 avatar Jul 08 '24 01:07 feds01

@ytmimi I was wondering if it's worth making it clear in Contributing.md that besides creating source and target files an actual test is needed? The current text makes it look like it's enough to make the test files IMO

giordan12 avatar Jul 10 '24 02:07 giordan12

I don't think it's necessary. It's mostly this specific case since I wanted to add a test to ensure nothing was being output to stdout and stderr. Typically a source and target file are all that's required

ytmimi avatar Jul 10 '24 03:07 ytmimi