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

Formatting raw string literals in doc comments is broken in nightly #6142

Closed
wiktor-k opened this issue Apr 9, 2024 · 5 comments
Closed
Labels
a-comments good first issue Issues up for grabs, also good candidates for new rustfmt contributors only-with-option requires a non-default option value to reproduce

Comments

@wiktor-k
Copy link

wiktor-k commented Apr 9, 2024

I found an issue with cargo 1.79.0-nightly (d438c80c4 2024-03-19) and format_code_in_doc_comments = true with raw string literals:

/// # Examples
///
/// ```
/// use std::io::Write;
///
/// # fn main() -> testresult::TestResult {
/// let source = r#"```sh
/// echo x
/// ```"#;
/// let mut sink = Vec::new();
///
/// tangler::extract(source.as_bytes(), &mut sink, &["sh"])?;
///
/// assert_eq!(&b"echo x\n\n"[..], &sink[..]);
/// # Ok(()) }
/// ```

(Original source at https://github.com/wiktor-k/tangler/blob/main/src/lib.rs#L24-L26 . The tool works with markdown documents, that's why the doc comment uses raw strings).

It shows:

$ cargo +nightly fmt --all -- --check
error[E0748]: unterminated raw string
 --> <stdin>:5:18
  |
5 |     let source = r#"```sh
  |                  ^ unterminated raw string
  |
  = note: this raw string should be terminated with `"#`

but cargo test (nightly and stable) and cargo doc are OK with this fragment of code.

I'm not sure if I should report it as a separate ticket or just appending it here is OK...?

Originally posted by @wiktor-k in #3348 (comment)

@ytmimi
Copy link
Contributor

ytmimi commented Apr 10, 2024

Thank you for the report. Linking the tracking issue for format_code_in_doc_comments #3348

@ytmimi ytmimi added a-comments only-with-option requires a non-default option value to reproduce labels Apr 10, 2024
@ytmimi
Copy link
Contributor

ytmimi commented Apr 12, 2024

I think the fix for this would involve updating this logic. We might also want check that the line ends with "```" to make sure that we're actually looking at a closing code fence.

@ytmimi ytmimi added the good first issue Issues up for grabs, also good candidates for new rustfmt contributors label Apr 12, 2024
@ytmimi
Copy link
Contributor

ytmimi commented Apr 30, 2024

After looking into this one a little more I think the error message that's popping up is related to #6109, which was a result of nightly changes in the compiler. The issue has been fixed on nightly for a while now.

@wiktor-k when you have a moment can you confirm if a more recent nightly version still causes the error message to pop up.

That all said, I still think rustfmt can do better here and not treat the end of the string literal (/// ```"#;) as the end of the code block, and I think my suggestion in #6142 (comment) is still where we need to make the change.

@wiktor-k
Copy link
Author

wiktor-k commented May 6, 2024

@wiktor-k when you have a moment can you confirm if a more recent nightly version still causes the error message to pop up.

Indeed I can't reproduce it now.

That all said, I still think rustfmt can do better here and not treat the end of the string literal (/// ```"#;) as the end of the code block, and I think my suggestion in #6142 (comment) is still where we need to make the change.

Just for the record I'm okay with closing this issue and if it pops up again I can create a new one.

Thanks for your help and time! 🙇

@ytmimi
Copy link
Contributor

ytmimi commented May 6, 2024

Going to close this since the original issue is resolved.

@ytmimi ytmimi closed this as not planned Won't fix, can't repro, duplicate, stale May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-comments good first issue Issues up for grabs, also good candidates for new rustfmt contributors only-with-option requires a non-default option value to reproduce
Projects
None yet
Development

No branches or pull requests

2 participants