Skip to content

rustdoc: Take into account edition information for keyword highlighting - #161944

Open
GuillaumeGomez wants to merge 1 commit into
rust-lang:mainfrom
GuillaumeGomez:keyword-highlighting
Open

rustdoc: Take into account edition information for keyword highlighting#161944
GuillaumeGomez wants to merge 1 commit into
rust-lang:mainfrom
GuillaumeGomez:keyword-highlighting

Conversation

@GuillaumeGomez

Copy link
Copy Markdown
Member

Fixes #148221.

The only thing that was missing was passing down an Edition to the highlighter (and adding tests).

r? @fmease

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. labels Aug 28, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@GuillaumeGomez
GuillaumeGomez force-pushed the keyword-highlighting branch 2 times, most recently from 1e831c0 to 05a128c Compare August 28, 2026 21:04
@GuillaumeGomez

Copy link
Copy Markdown
Member Author

Cleaned up all my mess. Ready for review.

@fmease fmease left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! :)

I've also tested markdown input rustdoc *.md --edition * and that works, too, automatically.

Two nitpicks, otherwise r=me

View changes since this review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't source-code-pages/ for testing the contents of source code pages *.rs.html only? Here however, we're testing how the code blocks are rendered on "API pages", aren't we?

IIRC this belongs into tests/rustdoc-html/doctest/.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough!

&code,
None,
None,
original_span.edition(),

@fmease fmease Aug 29, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a // NOTE: here that states that this is only "an approximation" or "best effort" since the edition of individual tokens contained in the expansion can differ from the "overarching edition" / the edition of the entire expansion (original_span.edition()).

And we can't fix that since code is just a String that was produced by rustc_ast_pretty meaning more precise edition information has been lost.


To give an example:

//@ edition: 2015
#[macro_export]
macro_rules! generate {
    ($kw:ident) => {
        pub fn host() {
            let _ = $kw {};
        }
    };
}
//@ edition: 2024
dependency::generate!(async);

Here, your PR doesn't highlight the async in the rendered expansion … let _ = async {} … since it uses the edition of the entire expansion (which is Rust 2015) but the async in the Rust 2015 expansion does actually refer to Rust 2024 keyword async and thus contains an async block, not a struct expression! That's because the keyword async originates from a Rust 2024 crate (root expansion).1

I think that's perfectly acceptable for now but a NOTE would be nice.

Footnotes

  1. Or vice versa: The dependency is Rust 2024 and the dependent is Rust 2015. In such a case, the keyword currently gets highlighted even though it shouldn't be.

@fmease fmease added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 29, 2026
@GuillaumeGomez

Copy link
Copy Markdown
Member Author

Applied suggestions (and mostly took your comment for the note because I think it's more than enough hehe).

@GuillaumeGomez

Copy link
Copy Markdown
Member Author

@bors r=fmease rollup

@rust-bors

rust-bors Bot commented Aug 29, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 40dc901 has been approved by fmease

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 29, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Aug 29, 2026
…g, r=fmease

rustdoc: Take into account edition information for keyword highlighting

Fixes rust-lang#148221.

The only thing that was missing was passing down an `Edition` to the highlighter (and adding tests).

r? @fmease
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Aug 29, 2026
…g, r=fmease

rustdoc: Take into account edition information for keyword highlighting

Fixes rust-lang#148221.

The only thing that was missing was passing down an `Edition` to the highlighter (and adding tests).

r? @fmease
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Aug 29, 2026
…g, r=fmease

rustdoc: Take into account edition information for keyword highlighting

Fixes rust-lang#148221.

The only thing that was missing was passing down an `Edition` to the highlighter (and adding tests).

r? @fmease
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Aug 29, 2026
… r=fmease

Move rustdoc ui doctests into the right testsuite

When working on rust-lang#161944 (comment), I realized that a lot of doctests in `rustdoc-html` testsuite were not in the correct testsuite, so moving them in the right location.

Because you're the one who made me realize it, here goes @fmease (thanks!) :p

r? @fmease
rust-bors Bot pushed a commit that referenced this pull request Aug 29, 2026
Rollup of 7 pull requests

Successful merges:

 - #160941 (Enforce safety comments on `alloc`)
 - #161975 (Abort instead of unwinding out of an inconsistent BTreeMap::split_off)
 - #157968 (Preserve ambiguous glob lint for subtrait method picks)
 - #161514 (rework handling of doc attributes on macro calls)
 - #161944 (rustdoc: Take into account edition information for keyword highlighting)
 - #161974 (remove rustc_never_type_options attr remnants)
 - #161978 (Move rustdoc ui doctests into the right testsuite)

Failed merges:

 - #161702 (Use `drop_guard` in some places in {core,alloc,std})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rustdoc: Source code pages & code blocks should be highlighted according to the requested / ambient edition

4 participants