Skip to content

Commit f5fddc7

Browse files
Improve code and add test with macro coming from another file from the same crate
1 parent 0e9b126 commit f5fddc7

File tree

7 files changed

+92
-80
lines changed

7 files changed

+92
-80
lines changed

src/librustdoc/html/highlight.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -298,21 +298,17 @@ fn empty_line_number(out: &mut impl Write, _: u32, extra: &'static str) {
298298
out.write_str(extra).unwrap();
299299
}
300300

301-
fn get_next_expansion<'a>(
302-
expanded_codes: Option<&'a Vec<ExpandedCode>>,
301+
fn get_next_expansion(
302+
expanded_codes: &[ExpandedCode],
303303
line: u32,
304304
span: Span,
305-
) -> Option<&'a ExpandedCode> {
306-
if let Some(expanded_codes) = expanded_codes {
307-
expanded_codes.iter().find(|code| code.start_line == line && code.span.lo() > span.lo())
308-
} else {
309-
None
310-
}
305+
) -> Option<&ExpandedCode> {
306+
expanded_codes.iter().find(|code| code.start_line == line && code.span.lo() > span.lo())
311307
}
312308

313309
fn get_expansion<'a, W: Write>(
314310
token_handler: &mut TokenHandler<'_, '_, W>,
315-
expanded_codes: Option<&'a Vec<ExpandedCode>>,
311+
expanded_codes: &'a [ExpandedCode],
316312
line: u32,
317313
span: Span,
318314
) -> Option<&'a ExpandedCode> {
@@ -356,7 +352,7 @@ fn start_expansion(out: &mut Vec<(Cow<'_, str>, Option<Class>)>, expanded_code:
356352

357353
fn end_expansion<'a, W: Write>(
358354
token_handler: &mut TokenHandler<'_, '_, W>,
359-
expanded_codes: Option<&'a Vec<ExpandedCode>>,
355+
expanded_codes: &'a [ExpandedCode],
360356
expansion_start_tags: &[(&'static str, Class)],
361357
line: u32,
362358
span: Span,
@@ -467,8 +463,8 @@ pub(super) fn write_code(
467463
let expanded_codes = c.context.shared.expanded_codes.get(&c.file_span.lo())?;
468464
Some((expanded_codes, c.file_span))
469465
}) {
470-
Some((expanded_codes, file_span)) => (Some(expanded_codes), file_span),
471-
None => (None, DUMMY_SP),
466+
Some((expanded_codes, file_span)) => (expanded_codes.as_slice(), file_span),
467+
None => (&[] as &[ExpandedCode], DUMMY_SP),
472468
};
473469
let mut current_expansion = get_expansion(&mut token_handler, expanded_codes, line, file_span);
474470
token_handler.write_pending_elems(None);
@@ -1257,7 +1253,7 @@ fn string<W: Write>(
12571253
if let Some(Class::Backline(line)) = klass {
12581254
write_line_number_callback(out, line, "\n");
12591255
} else if let Some(Class::Expansion) = klass {
1260-
// Nothing to escape here so we get the text to write it directly.
1256+
// This has already been escaped so we get the text to write it directly.
12611257
out.write_str(text.0).unwrap();
12621258
} else if let Some(closing_tag) =
12631259
string_without_closing_tag(out, text, klass, href_context, open_tag)

src/librustdoc/html/macro_expansion.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,16 @@ pub(crate) struct ExpandedCode {
4545
struct ExpandedCodeInfo {
4646
/// Callsite of the macro.
4747
span: Span,
48-
/// Expanded macro source code.
48+
/// Expanded macro source code (HTML escaped).
4949
code: String,
50-
/// Expanded span
50+
/// Span of macro-generated code.
5151
expanded_span: Span,
5252
}
5353

5454
/// HIR visitor which retrieves expanded macro.
5555
///
5656
/// Once done, the `expanded_codes` will be transformed into a vec of [`ExpandedCode`]
57-
/// which contains more information needed when running the source code highlighter.
57+
/// which contains the information needed when running the source code highlighter.
5858
pub(crate) struct ExpandedCodeVisitor<'ast> {
5959
expanded_codes: Vec<ExpandedCodeInfo>,
6060
source_map: &'ast SourceMap,
@@ -71,7 +71,7 @@ impl<'ast> ExpandedCodeVisitor<'ast> {
7171
{
7272
let info = &mut self.expanded_codes[index];
7373
if new_span.contains(info.expanded_span) {
74-
// We replace the item.
74+
// New macro expansion recursively contains the old one, so replace it.
7575
info.span = callsite_span;
7676
info.expanded_span = new_span;
7777
info.code = f();

src/librustdoc/html/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ pub(crate) mod format;
33
pub(crate) mod highlight;
44
pub(crate) mod layout;
55
mod length_limit;
6-
// used by the error-index generator, so it needs to be public
76
pub(crate) mod macro_expansion;
7+
// used by the error-index generator, so it needs to be public
88
pub mod markdown;
99
pub(crate) mod render;
1010
pub(crate) mod sources;

src/librustdoc/lib.rs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,14 @@ fn opts() -> Vec<RustcOptGroup> {
665665
"disable the minification of CSS/JS files (perma-unstable, do not use with cached files)",
666666
"",
667667
),
668+
opt(
669+
Unstable,
670+
Flag,
671+
"",
672+
"generate-macro-expansion",
673+
"Add possibility to expand macros in the HTML source code pages",
674+
"",
675+
),
668676
// deprecated / removed options
669677
opt(
670678
Stable,
@@ -706,14 +714,6 @@ fn opts() -> Vec<RustcOptGroup> {
706714
"removed, see issue #44136 <https://github.com/rust-lang/rust/issues/44136> for more information",
707715
"[rust]",
708716
),
709-
opt(
710-
Unstable,
711-
Flag,
712-
"",
713-
"generate-macro-expansion",
714-
"Add possibility to expand macros in the HTML source code pages",
715-
"",
716-
),
717717
]
718718
}
719719

@@ -958,15 +958,7 @@ fn main_args(early_dcx: &mut EarlyDiagCtxt, at_args: &[String]) {
958958
)
959959
}),
960960
config::OutputFormat::Json => sess.time("render_json", || {
961-
run_renderer(
962-
krate,
963-
render_opts,
964-
cache,
965-
tcx,
966-
|krate, render_opts, cache, tcx| {
967-
json::JsonRenderer::init(krate, render_opts, cache, tcx)
968-
},
969-
)
961+
run_renderer(krate, render_opts, cache, tcx, json::JsonRenderer::init)
970962
}),
971963
// Already handled above with doctest runners.
972964
config::OutputFormat::Doctest => unreachable!(),

tests/rustdoc-gui/macro-expansion.goml

Lines changed: 58 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -28,86 +28,99 @@ define-function: (
2828
)
2929

3030
// First we check the derive macro expansion at line 33.
31-
call-function: ("check-expansion", {"line": 33, "original_content": "Debug"})
31+
call-function: ("check-expansion", {"line": 35, "original_content": "Debug"})
3232
// Then we check the `bar` macro expansion at line 41.
33-
call-function: ("check-expansion", {"line": 41, "original_content": "bar!(y)"})
33+
call-function: ("check-expansion", {"line": 43, "original_content": "bar!(y)"})
3434
// Then we check the `println` macro expansion at line 42-44.
35-
call-function: ("check-expansion", {"line": 42, "original_content": 'println!("
36-
43 {y}
37-
44 ")'})
35+
call-function: ("check-expansion", {"line": 44, "original_content": 'println!("
36+
45 {y}
37+
46 ")'})
3838

3939
// Then finally we check when there are two macro calls on a same line.
40-
assert-count: ("#expand-50 ~ .original", 2)
41-
assert-count: ("#expand-50 ~ .expanded", 2)
40+
assert-count: ("#expand-52 ~ .original", 2)
41+
assert-count: ("#expand-52 ~ .expanded", 2)
4242

4343
store-value: (repeat_o, '/following-sibling::*[@class="original"]')
4444
store-value: (repeat_e, '/following-sibling::*[@class="expanded"]')
45-
assert-text: ('//*[@id="expand-50"]' + |repeat_o|, "stringify!(foo)")
46-
assert-text: ('//*[@id="expand-50"]' + |repeat_o| + |repeat_o|, "stringify!(bar)")
47-
assert-text: ('//*[@id="expand-50"]' + |repeat_e|, '"foo"')
48-
assert-text: ('//*[@id="expand-50"]' + |repeat_e| + |repeat_e|, '"bar"')
45+
store-value: (expand_id, "expand-52")
46+
assert-text: ('//*[@id="' + |expand_id| + '"]' + |repeat_o|, "stringify!(foo)")
47+
assert-text: ('//*[@id="' + |expand_id| + '"]' + |repeat_o| + |repeat_o|, "stringify!(bar)")
48+
assert-text: ('//*[@id="' + |expand_id| + '"]' + |repeat_e|, '"foo"')
49+
assert-text: ('//*[@id="' + |expand_id| + '"]' + |repeat_e| + |repeat_e|, '"bar"')
4950

5051
// The "original" content should be expanded.
51-
assert-css: ('//*[@id="expand-50"]' + |repeat_o|, {"display": "inline"})
52-
assert-css: ('//*[@id="expand-50"]' + |repeat_o| + |repeat_o|, {"display": "inline"})
52+
assert-css: ('//*[@id="' + |expand_id| + '"]' + |repeat_o|, {"display": "inline"})
53+
assert-css: ('//*[@id="' + |expand_id| + '"]' + |repeat_o| + |repeat_o|, {"display": "inline"})
5354
// The expanded macro should be hidden.
54-
assert-css: ('//*[@id="expand-50"]' + |repeat_e|, {"display": "none"})
55-
assert-css: ('//*[@id="expand-50"]' + |repeat_e| + |repeat_e|, {"display": "none"})
55+
assert-css: ('//*[@id="' + |expand_id| + '"]' + |repeat_e|, {"display": "none"})
56+
assert-css: ('//*[@id="' + |expand_id| + '"]' + |repeat_e| + |repeat_e|, {"display": "none"})
5657

5758
// We "expand" the macro (because the line starts with a string, the label is not at the "top
5859
// level" of the `<code>`, so we need to use a different selector).
59-
click: "#expand-50"
60+
click: "#" + |expand_id|
6061
// The "original" content is hidden.
61-
assert-css: ('//*[@id="expand-50"]' + |repeat_o|, {"display": "none"})
62-
assert-css: ('//*[@id="expand-50"]' + |repeat_o| + |repeat_o|, {"display": "none"})
62+
assert-css: ('//*[@id="' + |expand_id| + '"]' + |repeat_o|, {"display": "none"})
63+
assert-css: ('//*[@id="' + |expand_id| + '"]' + |repeat_o| + |repeat_o|, {"display": "none"})
6364
// The expanded macro is visible.
64-
assert-css: ('//*[@id="expand-50"]' + |repeat_e|, {"display": "inline"})
65-
assert-css: ('//*[@id="expand-50"]' + |repeat_e| + |repeat_e|, {"display": "inline"})
65+
assert-css: ('//*[@id="' + |expand_id| + '"]' + |repeat_e|, {"display": "inline"})
66+
assert-css: ('//*[@id="' + |expand_id| + '"]' + |repeat_e| + |repeat_e|, {"display": "inline"})
6667

6768
// We collapse the macro.
68-
click: "#expand-50"
69+
click: "#" + |expand_id|
6970
// The "original" content is expanded.
70-
assert-css: ('//*[@id="expand-50"]' + |repeat_o|, {"display": "inline"})
71-
assert-css: ('//*[@id="expand-50"]' + |repeat_o| + |repeat_o|, {"display": "inline"})
71+
assert-css: ('//*[@id="' + |expand_id| + '"]' + |repeat_o|, {"display": "inline"})
72+
assert-css: ('//*[@id="' + |expand_id| + '"]' + |repeat_o| + |repeat_o|, {"display": "inline"})
7273
// The expanded macro is hidden.
73-
assert-css: ('//*[@id="expand-50"]' + |repeat_e|, {"display": "none"})
74-
assert-css: ('//*[@id="expand-50"]' + |repeat_e| + |repeat_e|, {"display": "none"})
74+
assert-css: ('//*[@id="' + |expand_id| + '"]' + |repeat_e|, {"display": "none"})
75+
assert-css: ('//*[@id="' + |expand_id| + '"]' + |repeat_e| + |repeat_e|, {"display": "none"})
7576

76-
// Checking the line 46 `println` which needs to be handled differently because the line number is
77+
// Checking the line 48 `println` which needs to be handled differently because the line number is
7778
// inside a "comment" span.
78-
assert-text: ("#expand-46 ~ .original", 'println!("
79-
47 {y}
80-
48 ")')
79+
store-value: (expand_id, "expand-48")
80+
assert-text: ("#" + |expand_id| + " ~ .original", 'println!("
81+
49 {y}
82+
50 ")')
8183
// The "original" content should be expanded.
82-
assert-css: ("#expand-46 ~ .original", {"display": "inline"})
84+
assert-css: ("#" + |expand_id| + " ~ .original", {"display": "inline"})
8385
// The expanded macro should be hidden.
84-
assert-css: ("#expand-46 ~ .expanded", {"display": "none"})
86+
assert-css: ("#" + |expand_id| + " ~ .expanded", {"display": "none"})
8587

8688
// We "expand" the macro.
87-
click: "#expand-46"
89+
click: "#" + |expand_id|
8890
// The "original" content is hidden.
89-
assert-css: ("#expand-46 ~ .original", {"display": "none"})
91+
assert-css: ("#" + |expand_id| + " ~ .original", {"display": "none"})
9092
// The expanded macro is visible.
91-
assert-css: ("#expand-46 ~ .expanded", {"display": "inline"})
93+
assert-css: ("#" + |expand_id| + " ~ .expanded", {"display": "inline"})
9294

9395
// We collapse the macro.
94-
click: "#expand-46"
96+
click: "#" + |expand_id|
9597
// The "original" content is expanded.
96-
assert-css: ("#expand-46 ~ .original", {"display": "inline"})
98+
assert-css: ("#" + |expand_id| + " ~ .original", {"display": "inline"})
9799
// The expanded macro is hidden.
98-
assert-css: ("#expand-46 ~ .expanded", {"display": "none"})
100+
assert-css: ("#" + |expand_id| + " ~ .expanded", {"display": "none"})
99101

100102
// Ensure that the toggles are focusable and can be interacted with keyboard.
101-
focus: "//a[@id='27']"
103+
focus: "//a[@id='29']"
102104
press-key: "Tab"
103-
assert: "#expand-27:focus"
104-
assert-css: ("#expand-27 ~ .expanded", {"display": "none"})
105-
assert-css: ("#expand-27 ~ .original", {"display": "inline"})
105+
store-value: (expand_id, "expand-29")
106+
assert: "#" + |expand_id| + ":focus"
107+
assert-css: ("#" + |expand_id| +" ~ .expanded", {"display": "none"})
108+
assert-css: ("#" + |expand_id| +" ~ .original", {"display": "inline"})
106109
// We now expand the macro.
107110
press-key: "Space"
108-
assert-css: ("#expand-27 ~ .expanded", {"display": "inline"})
109-
assert-css: ("#expand-27 ~ .original", {"display": "none"})
111+
assert-css: ("#" + |expand_id| + " ~ .expanded", {"display": "inline"})
112+
assert-css: ("#" + |expand_id| + " ~ .original", {"display": "none"})
110113
// We collapse the macro.
111114
press-key: "Space"
112-
assert-css: ("#expand-27 ~ .expanded", {"display": "none"})
113-
assert-css: ("#expand-27 ~ .original", {"display": "inline"})
115+
assert-css: ("#" + |expand_id| + " ~ .expanded", {"display": "none"})
116+
assert-css: ("#" + |expand_id| + " ~ .original", {"display": "inline"})
117+
118+
// Now we check a macro coming from another file.
119+
store-value: (expand_id, "expand-55")
120+
// We "expand" the macro.
121+
click: "#" + |expand_id|
122+
// The "original" content is hidden.
123+
assert-css: ("#" + |expand_id| + " ~ .original", {"display": "none"})
124+
// The expanded macro is visible.
125+
assert-css: ("#" + |expand_id| + " ~ .expanded", {"display": "inline"})
126+
assert-text: ("#" + |expand_id| + " ~ .expanded", "{ y += 2; };")

tests/rustdoc-gui/src/macro_expansion/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Test crate used to check the `--generate-macro-expansion` option.
22
//@ compile-flags: -Zunstable-options --generate-macro-expansion --generate-link-to-definition
33

4+
mod other;
5+
46
#[macro_export]
57
macro_rules! bar {
68
($x:ident) => {{
@@ -48,4 +50,7 @@ fn foo() {
4850
");
4951
let s = y_f("\
5052
bla", stringify!(foo), stringify!(bar));
53+
54+
// Macro from another file.
55+
other_macro!(y);
5156
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#[macro_export]
2+
macro_rules! other_macro {
3+
($x:ident) => {{
4+
$x += 2;
5+
}}
6+
}

0 commit comments

Comments
 (0)