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

rustdoc fails on ignore'd code #30032

Closed
steveklabnik opened this issue Nov 24, 2015 · 8 comments
Closed

rustdoc fails on ignore'd code #30032

steveklabnik opened this issue Nov 24, 2015 · 8 comments
Labels
C-bug Category: This is a bug. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@steveklabnik
Copy link
Member

This doc example:

/// ```ignore
/// let heart = '❤️';
/// ```

compiles and passes with rustdoc --test, but not just generating documentation.

@steveklabnik steveklabnik added the T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. label Nov 24, 2015
@fhahn
Copy link
Contributor

fhahn commented Dec 1, 2015

I took a look at this and it seems like your char literal contains another (apparently invisible) code point, which means it is no valid Rust code.

The problem seems to be that rustdoc tries to highlight the code regardless of the ignore annotation, as it only ignores the snippet when running tests. And AFAIK rustdoc uses libsyntax's parser to do that, which will complain about the additional codepoint.

While the error is confusing (because there is no visible additional code point), I think the behavior of rustdoc is OK. Otherwise we would also have to exclude ingored code from the documentation, which means ignored code in docs would be rather pointless.

Finally this code should work with rustdoc, because both hearts are single codepoints

/// ```ignore
/// let foo = '♥'; // U+2665
/// let foo = '❤'; // U+2764
/// ```
pub fn this_is_a_function() {}

@steveklabnik
Copy link
Member Author

I took a look at this and it seems like your char literal contains another (apparently invisible) code point, which means it is no valid Rust code.

Yes. That's why it's in ignore.

Otherwise we would also have to exclude ingored code from the documentation, which means ignored code in docs would be rather pointless.

Or maybe not syntax highlight code that's ignored?

@fhahn
Copy link
Contributor

fhahn commented Dec 1, 2015

I think it would still make sense to have an annotation which excludes the code from the tests but highlights it in the docs, e.g. for nice looking example code, which does not have to include every little detail to make it compile.

If one would like to include code that should not be highlighted and not be used when testing, wouldn't it be possible to just add it as normal text, not in a code block?

birkenfeld added a commit to birkenfeld/rust that referenced this issue May 9, 2016
Currently, the libsyntax lexer panics on invalid source, which
makes rustdoc panic when trying to highlight it.

I assume there are efforts underway to make the lexer panic-free,
but until this is done this should be an acceptable workaround.

Note that the panic is still printed like normal as "thread X panicked,
run with RUST_BACKTRACE=1 ...", so I added the printout below to
make it seem less like a fatal error.

I didn't touch `render_inner_with_highlighting` below, as it is
currently unused.  It returns a Result whose Err type would have to
be changed if it was to support lexer errors.

Fixes: rust-lang#30032
birkenfeld added a commit to birkenfeld/rust that referenced this issue Aug 7, 2016
Currently, the libsyntax lexer panics on invalid source, which
makes rustdoc panic when trying to highlight it.

I assume there are efforts underway to make the lexer panic-free,
but until this is done this should be an acceptable workaround.

Note that the panic is still printed like normal as "thread X panicked,
run with RUST_BACKTRACE=1 ...", so I added the printout below to
make it seem less like a fatal error.

I didn't touch `render_inner_with_highlighting` below, as it is
currently unused.  It returns a Result whose Err type would have to
be changed if it was to support lexer errors.

Fixes: rust-lang#30032
@steveklabnik
Copy link
Member Author

Triage: looks like #33510 didn't happen

@steveklabnik steveklabnik added T-dev-tools Relevant to the dev-tools subteam, which will review and decide on the PR/issue. and removed T-tools labels May 18, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 24, 2017
@JeanMertz
Copy link

I ran into this today. I use ```ignore to add compiler error descriptions to my crate docs, which is documented in the book, but as soon as I tried to run $ rust doc, it failed because it tried compile the non-Rust characters in that code block as Rust code.

I'm surprised this issue hasn't had any responses since 2016, so either this is already solved and I'm hitting some other issue, or people don't really put non-Rust code blocks in their documentation.

Here's part of the documentation that currently fails.

@JeanMertz
Copy link

Here's part of the documentation that currently fails.

FYI: changing this from using ignore tags to using text made things work as expected.

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Jan 3, 2021
…deblock-warn, r=jyn514

Add suggestion for "ignore" doc code block

Part of rust-lang#30032.

This PR adds a suggestion to help users when they have a "ignore" doc code block which is invalid rust code.

r? `@jyn514`
@ehuss ehuss removed the T-dev-tools Relevant to the dev-tools subteam, which will review and decide on the PR/issue. label Jan 18, 2022
@kadiwa4
Copy link
Contributor

kadiwa4 commented Aug 22, 2023

Triage: rustdoc outputs this:

warning: could not parse code block as Rust code
 --> src/lib.rs:1:5
  |
1 |   /// ```ignore
  |  _____^
2 | | /// let heart = '❤️';
3 | | /// ```
  | |_______^
  |
help: `ignore` code blocks require valid Rust code for syntax highlighting; mark blocks that do not contain Rust code as text: ```text
 --> src/lib.rs:1:5
  |
1 | /// ```ignore
  |     ^^^
  = note: error from rustc: character literal may only contain one codepoint
  = note: `#[warn(rustdoc::invalid_rust_codeblocks)]` on by default

but in the generated docs, it uses correct syntax highlighting on the broken code.

I think that's pretty optimal. If I understand correctly, this issue was filed because rustdoc used to abort doc-generation on encountering something like this?

@steveklabnik
Copy link
Member Author

steveklabnik commented Aug 22, 2023

this issue was filed because rustdoc used to abort doc-generation on encountering something like this?

I believe so, but to be honest, that diagnostic output looks great to me, and there's a path to having incorrect things be documented, so as far as I'm concerned, I don't personally feel this is an issue anymore, so happy to close it, regardless of whatever historical behavior may or may not have been happening way back then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants