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

union and other weak keywords are not highlighted correctly in rustdoc code blocks #85016

Closed
ThePuzzlemaker opened this issue May 7, 2021 · 2 comments
Labels
A-rustdoc-ui Area: Rustdoc UI (generated HTML) C-bug Category: This is a bug. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@ThePuzzlemaker
Copy link
Contributor

ThePuzzlemaker commented May 7, 2021

This issue is quite similar to #80004, which I also worked on, so I have a general idea of how it might be able to be fixed.

I tried this code:

/// ```rust ignore
/// async fn quux() {} // rust 2018+ keyword, fixed in #80226
///
/// #[repr(C)]
/// union ExampleUnion { // weak keyword, not working
///     foo: u8,
///     bar: u16
/// }
///
/// fn bar () {
///     let mut a = 0;
///     abstract blah; // reserved keyword, working
/// }
pub fn foo() {}

I expected to see this happen: union is highlighted, even though it is a weak keyword, as it is acting as a keyword in that location in the code

Instead, this happened: They were not highlighted, but regular keywords including reserved ones and edition 2018+ ones (fixed in #80266) were.
image

The issue is here, because Ident::is_reserved does not check for weak keyword status:

fn get_real_ident_class(text: &str, edition: Edition) -> Class {
match text {
"ref" | "mut" => Class::RefKeyWord,
"self" | "Self" => Class::Self_,
"false" | "true" => Class::Bool,
_ if Symbol::intern(text).is_reserved(|| edition) => Class::KeyWord,
_ => Class::Ident,
}
}

Meta

Tested

rustc --version --verbose:
stable:

rustc 1.52.0 (88f19c6da 2021-05-03)
binary: rustc
commit-hash: 88f19c6dab716c6281af7602e30f413e809c5974
commit-date: 2021-05-03
host: x86_64-unknown-linux-gnu
release: 1.52.0
LLVM version: 12.0.0

nightly:

rustc 1.54.0-nightly (bacf770f2 2021-05-05)
binary: rustc
commit-hash: bacf770f2983a52f31e3537db5f0fe1ef2eaa874
commit-date: 2021-05-05
host: x86_64-unknown-linux-gnu
release: 1.54.0-nightly
LLVM version: 12.0.0

Backtrace is not applicable.

@rustbot claim
@rustbot modify labels: +A-rustdoc +A-rustdoc-ui +C-bug

@rustbot rustbot added T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. A-rustdoc-ui Area: Rustdoc UI (generated HTML) C-bug Category: This is a bug. labels May 7, 2021
@ThePuzzlemaker
Copy link
Contributor Author

Hm, now that I think about it, this might be a bit harder to solve than I initially thought, as weak keywords require a bit of syntactic context to find out whether they're being used as keywords or not. Thus, the logic for weak keywords will probably have to be further up the chain than get_real_ident_class. I'll try what I can to get this to work, but if I can't get a working solution I'll either put something on Zulip or release assignment of this issue.

@ThePuzzlemaker ThePuzzlemaker removed their assignment Sep 6, 2021
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Sep 29, 2021
…r=notriddle

Fix union keyword highlighting in rustdoc HTML sources

I followed this logic: if I find an ident "union", I check if it followed by another ident or not. If it's the case, then I consider this is a keyword because it's declaring a union type.

To do so I created a new Iterator which allows to peek the next items without moving the current iterator position.

This is part of rust-lang#85016. If the fix makes sense, I'll extend it to other weak keywords (the issue only mentions they exist but https://doc.rust-lang.org/nightly/reference/keywords.html#weak-keywords only talks about `dyn` and `'static` so not sure if there is anything else to be done?).

cc `@notriddle` (you're one of the last ones who worked on this part of rustdoc so here you go 😉 )
r? `@jyn514`
ehuss added a commit to ehuss/rust that referenced this issue Sep 30, 2021
…r=notriddle

Fix union keyword highlighting in rustdoc HTML sources

I followed this logic: if I find an ident "union", I check if it followed by another ident or not. If it's the case, then I consider this is a keyword because it's declaring a union type.

To do so I created a new Iterator which allows to peek the next items without moving the current iterator position.

This is part of rust-lang#85016. If the fix makes sense, I'll extend it to other weak keywords (the issue only mentions they exist but https://doc.rust-lang.org/nightly/reference/keywords.html#weak-keywords only talks about `dyn` and `'static` so not sure if there is anything else to be done?).

cc `@notriddle` (you're one of the last ones who worked on this part of rustdoc so here you go 😉 )
r? `@jyn514`
@GuillaumeGomez
Copy link
Member

The union keyword highlighting has been fixed in #87428. Please open another issue for the other "weak" keywords so we can fix them as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-rustdoc-ui Area: Rustdoc UI (generated HTML) 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

3 participants