Skip to content

Commit 4817035

Browse files
Do macro expansion at AST level rather than HIR
1 parent cdb6696 commit 4817035

File tree

12 files changed

+256
-205
lines changed

12 files changed

+256
-205
lines changed

src/librustdoc/core.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use crate::clean::inline::build_trait;
3131
use crate::clean::{self, ItemId};
3232
use crate::config::{Options as RustdocOptions, OutputFormat, RenderOptions};
3333
use crate::formats::cache::Cache;
34+
use crate::html::macro_expansion::{ExpandedCode, source_macro_expansion};
3435
use crate::passes;
3536
use crate::passes::Condition::*;
3637
use crate::passes::collect_intra_doc_links::LinkCollector;
@@ -335,11 +336,19 @@ pub(crate) fn run_global_ctxt(
335336
show_coverage: bool,
336337
render_options: RenderOptions,
337338
output_format: OutputFormat,
338-
) -> (clean::Crate, RenderOptions, Cache) {
339+
) -> (clean::Crate, RenderOptions, Cache, FxHashMap<rustc_span::BytePos, Vec<ExpandedCode>>) {
339340
// Certain queries assume that some checks were run elsewhere
340341
// (see https://github.com/rust-lang/rust/pull/73566#issuecomment-656954425),
341342
// so type-check everything other than function bodies in this crate before running lints.
342343

344+
let expanded_macros = {
345+
// We need for these variables to be removed to ensure that the `Crate` won't be "stolen"
346+
// anymore.
347+
let (_resolver, krate) = &*tcx.resolver_for_lowering().borrow();
348+
349+
source_macro_expansion(&krate, &render_options, output_format, tcx.sess.source_map())
350+
};
351+
343352
// NOTE: this does not call `tcx.analysis()` so that we won't
344353
// typeck function bodies or run the default rustc lints.
345354
// (see `override_queries` in the `config`)
@@ -453,7 +462,7 @@ pub(crate) fn run_global_ctxt(
453462

454463
tcx.dcx().abort_if_errors();
455464

456-
(krate, ctxt.render_options, ctxt.cache)
465+
(krate, ctxt.render_options, ctxt.cache, expanded_macros)
457466
}
458467

459468
/// Due to <https://github.com/rust-lang/rust/pull/73566>,

src/librustdoc/formats/renderer.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,6 @@ pub(crate) trait FormatRenderer<'tcx>: Sized {
3131
/// reset the information between each call to `item` by using `restore_module_data`.
3232
type ModuleData;
3333

34-
/// Sets up any state required for the renderer. When this is called the cache has already been
35-
/// populated.
36-
fn init(
37-
krate: clean::Crate,
38-
options: RenderOptions,
39-
cache: Cache,
40-
tcx: TyCtxt<'tcx>,
41-
) -> Result<(Self, clean::Crate), Error>;
42-
4334
/// This method is called right before call [`Self::item`]. This method returns a type
4435
/// containing information that needs to be reset after the [`Self::item`] method has been
4536
/// called with the [`Self::restore_module_data`] method.
@@ -107,18 +98,23 @@ fn run_format_inner<'tcx, T: FormatRenderer<'tcx>>(
10798
}
10899

109100
/// Main method for rendering a crate.
110-
pub(crate) fn run_format<'tcx, T: FormatRenderer<'tcx>>(
101+
pub(crate) fn run_format<
102+
'tcx,
103+
T: FormatRenderer<'tcx>,
104+
F: FnOnce(clean::Crate, RenderOptions, Cache, TyCtxt<'tcx>) -> Result<(T, clean::Crate), Error>,
105+
>(
111106
krate: clean::Crate,
112107
options: RenderOptions,
113108
cache: Cache,
114109
tcx: TyCtxt<'tcx>,
110+
init: F,
115111
) -> Result<(), Error> {
116112
let prof = &tcx.sess.prof;
117113

118114
let emit_crate = options.should_emit_crate();
119115
let (mut format_renderer, krate) = prof
120116
.verbose_generic_activity_with_arg("create_renderer", T::descr())
121-
.run(|| T::init(krate, options, cache, tcx))?;
117+
.run(|| init(krate, options, cache, tcx))?;
122118

123119
if !emit_crate {
124120
return Ok(());

src/librustdoc/html/highlight.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ use rustc_span::{BytePos, DUMMY_SP, Span};
1818
use super::format::{self, write_str};
1919
use crate::clean::PrimitiveType;
2020
use crate::html::escape::EscapeBodyText;
21-
use crate::html::render::{Context, ExpandedCode, LinkFromSrc};
21+
use crate::html::macro_expansion::ExpandedCode;
22+
use crate::html::render::{Context, LinkFromSrc};
2223

2324
/// This type is needed in case we want to render links on items to allow to go to their definition.
2425
pub(crate) struct HrefContext<'a, 'tcx> {
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
use rustc_ast::{Crate, Expr, Item};
2+
use rustc_ast::visit::{Visitor, walk_crate, walk_expr, walk_item};
3+
use rustc_data_structures::fx::FxHashMap;
4+
use rustc_span::{BytePos, Span};
5+
use rustc_span::source_map::SourceMap;
6+
7+
use crate::config::{OutputFormat, RenderOptions};
8+
9+
/// It returns the expanded macros correspondence map.
10+
pub(crate) fn source_macro_expansion(krate: &Crate, render_options: &RenderOptions, output_format: OutputFormat, source_map: &SourceMap) -> FxHashMap<BytePos, Vec<ExpandedCode>> {
11+
if output_format == OutputFormat::Html && !render_options.html_no_source && render_options.generate_macro_expansion {
12+
let mut expanded_visitor = ExpandedCodeVisitor { expanded_codes: Vec::new(), source_map };
13+
walk_crate(&mut expanded_visitor, krate);
14+
expanded_visitor.compute_expanded()
15+
} else {
16+
Default::default()
17+
}
18+
}
19+
20+
/// Contains information about macro expansion in the source code pages.
21+
#[derive(Debug)]
22+
pub(crate) struct ExpandedCode {
23+
/// The line where the macro expansion starts.
24+
pub(crate) start_line: u32,
25+
/// The line where the macro expansion ends.
26+
pub(crate) end_line: u32,
27+
/// The source code of the expanded macro.
28+
pub(crate) code: String,
29+
/// The span of macro callsite.
30+
pub(crate) span: Span,
31+
}
32+
33+
/// Contains temporary information of macro expanded code.
34+
///
35+
/// As we go through the HIR visitor, if any span overlaps with another, they will
36+
/// both be merged.
37+
struct ExpandedCodeInfo {
38+
/// Callsite of the macro.
39+
span: Span,
40+
/// Expanded macro source code.
41+
code: String,
42+
/// Expanded span
43+
expanded_span: Span,
44+
}
45+
46+
/// HIR visitor which retrieves expanded macro.
47+
///
48+
/// Once done, the `expanded_codes` will be transformed into a vec of [`ExpandedCode`]
49+
/// which contains more information needed when running the source code highlighter.
50+
pub(crate) struct ExpandedCodeVisitor<'ast> {
51+
expanded_codes: Vec<ExpandedCodeInfo>,
52+
source_map: &'ast SourceMap,
53+
}
54+
55+
impl<'ast> ExpandedCodeVisitor<'ast> {
56+
fn handle_new_span<F: Fn() -> String>(&mut self, new_span: Span, f: F) {
57+
if new_span.is_dummy() || !new_span.from_expansion() {
58+
return;
59+
}
60+
let callsite_span = new_span.source_callsite();
61+
if let Some(index) =
62+
self.expanded_codes.iter().position(|info| info.span.overlaps(callsite_span))
63+
{
64+
let info = &mut self.expanded_codes[index];
65+
if new_span.contains(info.expanded_span) {
66+
// We replace the item.
67+
info.span = callsite_span;
68+
info.expanded_span = new_span;
69+
info.code = f();
70+
} else {
71+
// We push the new item after the existing one.
72+
let expanded_code = &mut self.expanded_codes[index];
73+
expanded_code.code.push('\n');
74+
expanded_code.code.push_str(&f());
75+
let lo = BytePos(expanded_code.expanded_span.lo().0.min(new_span.lo().0));
76+
let hi = BytePos(expanded_code.expanded_span.hi().0.min(new_span.hi().0));
77+
expanded_code.expanded_span = expanded_code.expanded_span.with_lo(lo).with_hi(hi);
78+
}
79+
} else {
80+
// We add a new item.
81+
self.expanded_codes.push(ExpandedCodeInfo {
82+
span: callsite_span,
83+
code: f(),
84+
expanded_span: new_span,
85+
});
86+
}
87+
}
88+
89+
fn compute_expanded(mut self) -> FxHashMap<BytePos, Vec<ExpandedCode>> {
90+
self.expanded_codes.sort_unstable_by(|item1, item2| item1.span.cmp(&item2.span));
91+
let mut expanded: FxHashMap<BytePos, Vec<ExpandedCode>> = FxHashMap::default();
92+
for ExpandedCodeInfo { span, code, .. } in self.expanded_codes {
93+
if let Ok(lines) = self.source_map.span_to_lines(span)
94+
&& !lines.lines.is_empty()
95+
{
96+
let mut out = String::new();
97+
super::highlight::write_code(&mut out, &code, None, None, None);
98+
let first = lines.lines.first().unwrap();
99+
let end = lines.lines.last().unwrap();
100+
expanded.entry(lines.file.start_pos).or_default().push(ExpandedCode {
101+
start_line: first.line_index as u32 + 1,
102+
end_line: end.line_index as u32 + 1,
103+
code: out,
104+
span,
105+
});
106+
}
107+
}
108+
expanded
109+
}
110+
}
111+
112+
// We need to use the AST pretty printing because:
113+
//
114+
// 1. HIR pretty printing doesn't display accurately the code (like `impl Trait`).
115+
// 2. `SourceMap::snippet_opt` might fail if the source is not available.
116+
impl<'ast> Visitor<'ast> for ExpandedCodeVisitor<'ast> {
117+
fn visit_expr(&mut self, expr: &'ast Expr) {
118+
if expr.span.from_expansion() {
119+
self.handle_new_span(expr.span, || rustc_ast_pretty::pprust::expr_to_string(expr));
120+
} else {
121+
walk_expr(self, expr);
122+
}
123+
}
124+
125+
fn visit_item(&mut self, item: &'ast Item) {
126+
if item.span.from_expansion() {
127+
self.handle_new_span(item.span, || rustc_ast_pretty::pprust::item_to_string(item));
128+
} else {
129+
walk_item(self, item);
130+
}
131+
}
132+
}

src/librustdoc/html/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub(crate) mod highlight;
44
pub(crate) mod layout;
55
mod length_limit;
66
// used by the error-index generator, so it needs to be public
7+
pub(crate) mod macro_expansion;
78
pub mod markdown;
89
pub(crate) mod render;
910
pub(crate) mod sources;

src/librustdoc/html/render/context.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ use crate::formats::item_type::ItemType;
2929
use crate::html::escape::Escape;
3030
use crate::html::format::join_with_double_colon;
3131
use crate::html::layout::{self, BufDisplay};
32+
use crate::html::macro_expansion::ExpandedCode;
3233
use crate::html::markdown::{self, ErrorCodes, IdMap, plain_text_summary};
33-
use crate::html::render::ExpandedCode;
3434
use crate::html::render::write_shared::write_shared;
3535
use crate::html::url_parts_builder::UrlPartsBuilder;
3636
use crate::html::{sources, static_files};
@@ -456,20 +456,13 @@ impl<'tcx> Context<'tcx> {
456456
}
457457
}
458458

459-
/// Generates the documentation for `crate` into the directory `dst`
460-
impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
461-
fn descr() -> &'static str {
462-
"html"
463-
}
464-
465-
const RUN_ON_MODULE: bool = true;
466-
type ModuleData = ContextInfo;
467-
468-
fn init(
459+
impl<'tcx> Context<'tcx> {
460+
pub(crate) fn init(
469461
krate: clean::Crate,
470462
options: RenderOptions,
471463
cache: Cache,
472464
tcx: TyCtxt<'tcx>,
465+
expanded_codes: FxHashMap<BytePos, Vec<ExpandedCode>>,
473466
) -> Result<(Self, clean::Crate), Error> {
474467
// need to save a copy of the options for rendering the index page
475468
let md_opts = options.clone();
@@ -488,7 +481,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
488481
generate_redirect_map,
489482
show_type_layout,
490483
generate_link_to_definition,
491-
generate_macro_expansion,
492484
call_locations,
493485
no_emit_shared,
494486
html_no_source,
@@ -547,13 +539,12 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
547539
}
548540
}
549541

550-
let (local_sources, matches, expanded_codes) = collect_spans_and_sources(
542+
let (local_sources, matches) = collect_spans_and_sources(
551543
tcx,
552544
&krate,
553545
&src_root,
554546
include_sources,
555547
generate_link_to_definition,
556-
generate_macro_expansion,
557548
);
558549

559550
let (sender, receiver) = channel();
@@ -605,6 +596,16 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
605596

606597
Ok((cx, krate))
607598
}
599+
}
600+
601+
/// Generates the documentation for `crate` into the directory `dst`
602+
impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
603+
fn descr() -> &'static str {
604+
"html"
605+
}
606+
607+
const RUN_ON_MODULE: bool = true;
608+
type ModuleData = ContextInfo;
608609

609610
fn save_module_data(&mut self) -> Self::ModuleData {
610611
self.deref_id_map.borrow_mut().clear();

src/librustdoc/html/render/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ use serde::{Serialize, Serializer};
6464
use tracing::{debug, info};
6565

6666
pub(crate) use self::context::*;
67-
pub(crate) use self::span_map::{ExpandedCode, LinkFromSrc, collect_spans_and_sources};
67+
pub(crate) use self::span_map::{LinkFromSrc, collect_spans_and_sources};
6868
pub(crate) use self::write_shared::*;
6969
use crate::clean::{self, ItemId, RenderedLink};
7070
use crate::display::{Joined as _, MaybeDisplay as _};

0 commit comments

Comments
 (0)