Skip to content

Commit ab10778

Browse files
committed
Auto merge of #80226 - ThePuzzlemaker:issue-80004-fix, r=jyn514,petrochenkov
Highlight edition-specific keywords correctly in code blocks, accounting for code block edition modifiers Previously, edition-specific keywords (such as `async` and `await`) were not highlighted in code blocks, regardless of what edition was set. With this PR, this issue is fixed. Now, the following behavior happens: - When a code block is explicitly set to edition X, keywords from edition X are highlighted - When a code block is explicitly set to a version that does not contain those keywords from edition X (e.g. edition Y), keywords from edition X are **not** highlighted - When a code block has no explicit edition, keywords from the edition passed via `--edition` to rustdoc are highlighted For example, a project set with `edition = "2015"` in its `Cargo.toml` would not highlight `async`/`await` unless the code block was set to `edition2018`. Additionally, a project set with `edition = "2018"` in its `Cargo.toml` *would* highlight `async`/`await` unless the code block was set to a version that did not contain those keywords (e.g. `edition2015`). This PR fixes #80004. r? `@jyn514`
2 parents 198ec34 + db1451c commit ab10778

File tree

5 files changed

+24
-12
lines changed

5 files changed

+24
-12
lines changed

src/librustdoc/html/highlight.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::iter::Peekable;
1212

1313
use rustc_lexer::{LiteralKind, TokenKind};
1414
use rustc_span::edition::Edition;
15-
use rustc_span::symbol::Ident;
15+
use rustc_span::symbol::Symbol;
1616
use rustc_span::with_default_session_globals;
1717

1818
/// Highlights `src`, returning the HTML output.
@@ -21,6 +21,7 @@ crate fn render_with_highlighting(
2121
class: Option<&str>,
2222
playground_button: Option<&str>,
2323
tooltip: Option<(Option<Edition>, &str)>,
24+
edition: Edition,
2425
) -> String {
2526
debug!("highlighting: ================\n{}\n==============", src);
2627
let mut out = String::with_capacity(src.len());
@@ -39,7 +40,7 @@ crate fn render_with_highlighting(
3940
}
4041

4142
write_header(&mut out, class);
42-
write_code(&mut out, &src);
43+
write_code(&mut out, &src, edition);
4344
write_footer(&mut out, playground_button);
4445

4546
out
@@ -50,10 +51,10 @@ fn write_header(out: &mut String, class: Option<&str>) {
5051
.unwrap()
5152
}
5253

53-
fn write_code(out: &mut String, src: &str) {
54+
fn write_code(out: &mut String, src: &str, edition: Edition) {
5455
// This replace allows to fix how the code source with DOS backline characters is displayed.
5556
let src = src.replace("\r\n", "\n");
56-
Classifier::new(&src).highlight(&mut |highlight| {
57+
Classifier::new(&src, edition).highlight(&mut |highlight| {
5758
match highlight {
5859
Highlight::Token { text, class } => string(out, Escape(text), class),
5960
Highlight::EnterSpan { class } => enter_span(out, class),
@@ -144,12 +145,19 @@ struct Classifier<'a> {
144145
in_attribute: bool,
145146
in_macro: bool,
146147
in_macro_nonterminal: bool,
148+
edition: Edition,
147149
}
148150

149151
impl<'a> Classifier<'a> {
150-
fn new(src: &str) -> Classifier<'_> {
152+
fn new(src: &str, edition: Edition) -> Classifier<'_> {
151153
let tokens = TokenIter { src }.peekable();
152-
Classifier { tokens, in_attribute: false, in_macro: false, in_macro_nonterminal: false }
154+
Classifier {
155+
tokens,
156+
in_attribute: false,
157+
in_macro: false,
158+
in_macro_nonterminal: false,
159+
edition,
160+
}
153161
}
154162

155163
/// Exhausts the `Classifier` writing the output into `sink`.
@@ -301,7 +309,7 @@ impl<'a> Classifier<'a> {
301309
"Option" | "Result" => Class::PreludeTy,
302310
"Some" | "None" | "Ok" | "Err" => Class::PreludeVal,
303311
// Keywords are also included in the identifier set.
304-
_ if Ident::from_str(text).is_reserved() => Class::KeyWord,
312+
_ if Symbol::intern(text).is_reserved(|| self.edition) => Class::KeyWord,
305313
_ if self.in_macro_nonterminal => {
306314
self.in_macro_nonterminal = false;
307315
Class::MacroNonTerminal

src/librustdoc/html/highlight/tests.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::write_code;
22
use expect_test::expect_file;
3+
use rustc_span::edition::Edition;
34

45
const STYLE: &str = r#"
56
<style>
@@ -18,7 +19,7 @@ fn test_html_highlighting() {
1819
let src = include_str!("fixtures/sample.rs");
1920
let html = {
2021
let mut out = String::new();
21-
write_code(&mut out, src);
22+
write_code(&mut out, src, Edition::Edition2018);
2223
format!("{}<pre><code>{}</code></pre>\n", STYLE, out)
2324
};
2425
expect_file!["fixtures/sample.html"].assert_eq(&html);
@@ -30,6 +31,6 @@ fn test_dos_backline() {
3031
println!(\"foo\");\r\n\
3132
}\r\n";
3233
let mut html = String::new();
33-
write_code(&mut html, src);
34+
write_code(&mut html, src, Edition::Edition2018);
3435
expect_file!["fixtures/dos_line.html"].assert_eq(&html);
3536
}

src/librustdoc/html/markdown.rs

+1
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
303303
)),
304304
playground_button.as_deref(),
305305
tooltip,
306+
edition,
306307
));
307308
Some(Event::Html(s.into()))
308309
}

src/librustdoc/html/render/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4747,6 +4747,7 @@ fn item_macro(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Mac
47474747
Some("macro"),
47484748
None,
47494749
None,
4750+
it.source.span().edition(),
47504751
))
47514752
});
47524753
document(w, cx, it, None)

src/librustdoc/html/sources.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::html::layout;
88
use crate::html::render::{SharedContext, BASIC_KEYWORDS};
99
use rustc_hir::def_id::LOCAL_CRATE;
1010
use rustc_session::Session;
11+
use rustc_span::edition::Edition;
1112
use rustc_span::source_map::FileName;
1213
use std::ffi::OsStr;
1314
use std::fs;
@@ -132,7 +133,7 @@ impl SourceCollector<'_, '_> {
132133
&self.scx.layout,
133134
&page,
134135
"",
135-
|buf: &mut _| print_src(buf, contents),
136+
|buf: &mut _| print_src(buf, contents, self.scx.edition),
136137
&self.scx.style_files,
137138
);
138139
self.scx.fs.write(&cur, v.as_bytes())?;
@@ -170,7 +171,7 @@ where
170171

171172
/// Wrapper struct to render the source code of a file. This will do things like
172173
/// adding line numbers to the left-hand side.
173-
fn print_src(buf: &mut Buffer, s: String) {
174+
fn print_src(buf: &mut Buffer, s: String, edition: Edition) {
174175
let lines = s.lines().count();
175176
let mut cols = 0;
176177
let mut tmp = lines;
@@ -183,5 +184,5 @@ fn print_src(buf: &mut Buffer, s: String) {
183184
write!(buf, "<span id=\"{0}\">{0:1$}</span>\n", i, cols);
184185
}
185186
write!(buf, "</pre>");
186-
write!(buf, "{}", highlight::render_with_highlighting(s, None, None, None));
187+
write!(buf, "{}", highlight::render_with_highlighting(s, None, None, None, edition));
187188
}

0 commit comments

Comments
 (0)