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

Multiline module level docs cannot handle /* in a string #129685

Closed
nathanosdev opened this issue Aug 28, 2024 · 4 comments · Fixed by rust-lang/reference#1602
Closed

Multiline module level docs cannot handle /* in a string #129685

nathanosdev opened this issue Aug 28, 2024 · 4 comments · Fixed by rust-lang/reference#1602
Labels
A-parser Area: The parsing of Rust source code to an AST C-discussion Category: Discussion or questions that doesn't represent real issues. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@nathanosdev
Copy link

I tried this code:

/*!
 * ```
 * let thing = "**/*.css";
 * ```
 */

/// ```
/// let thing = "**/*.css";
/// ```
pub fn get_thing() -> &'static str {
    let thing = "**/*.css";

    thing
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        let result = get_thing();
        assert_eq!(result, "**/*.css");
    }
}

I expected to see this happen: The docs to compile.

Instead, this happened:

The docs fail to compile with this message:

error: prefix css is unknown
--> src/lib.rs:3:22
|
3 | * let thing = "/*.css";
| ^^^ unknown prefix
|
= note: prefixed identifiers and literals are reserved since Rust 2021
help: consider inserting whitespace here
|
3 | * let thing = "
/*.css ";
| +

error: prefix css is unknown
--> src/lib.rs:11:23
|
11 | let thing = "/*.css";
| ^^^ unknown prefix
|
= note: prefixed identifiers and literals are reserved since Rust 2021
help: consider inserting whitespace here
|
11 | let thing = "
/*.css ";
| +

error[E0758]: unterminated block comment
--> src/lib.rs:23:31
|
23 | assert_eq!(result, "**/*.css");
| _______________________________^
24 | | }
25 | | }
| |__^

For more information about this error, try rustc --explain E0758.
error: could not document rustdoc_issue

Meta

rustc --version --verbose:

rustc 1.80.0 (051478957 2024-07-21)
binary: rustc
commit-hash: 051478957371ee0084a7c0913941d2a8c4757bb9
commit-date: 2024-07-21
host: x86_64-unknown-linux-gnu
release: 1.80.0
LLVM version: 18.1.7

I've also reproduced on nightly.

@nathanosdev nathanosdev added the C-bug Category: This is a bug. label Aug 28, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 28, 2024
@GKFX
Copy link
Contributor

GKFX commented Aug 28, 2024

In your example, the */ on line three, inside the string, terminates the doc comment. This is something that can be depended on in working code (example), so changing it would be a breaking change. The next few characters are invalid Rust so the syntax error is expected behaviour.

Your second comment can be used OK as is - example.

The reference (https://doc.rust-lang.org/reference/comments.html) makes no mention of markdown syntax when defining comments, so I would not expect presence of a string within backticks to make any difference to parsing.

@rustbot label -C-bug

@rustbot rustbot removed the C-bug Category: This is a bug. label Aug 28, 2024
@nathanosdev
Copy link
Author

I would not expect presence of a string within backticks to make any difference to parsing

That's fair, so then would it be possible to add a way to escape the characters in a way that does not affect the final output?

I can use \*\*\/\* instead of **/* but then it's still present in the output.

@saethlin saethlin added T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. A-parser Area: The parsing of Rust source code to an AST C-discussion Category: Discussion or questions that doesn't represent real issues. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Sep 1, 2024
@GKFX
Copy link
Contributor

GKFX commented Sep 7, 2024

would it be possible to add a way to escape the characters in a way that does not affect the final output

This can technically be done as below, although it does look rather odd. Considering that the convention is to use /// doc comments, and those don't have an issue with nested comment characters, I think a special syntax just for /** */ doc comments is unlikely to happen. I don't believe Markdown offers a way around this.

/*!
 * ```
 * # // /*
 * let thing = "**/*.css";
 * ```
 */

@nathanosdev
Copy link
Author

I see. Okay well for now I've switched to /// doc comments anyway. Thanks for the update to the docs too 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-parser Area: The parsing of Rust source code to an AST C-discussion Category: Discussion or questions that doesn't represent real issues. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants