Skip to content

Add support for macro expansion in rustdoc source code pages #137229

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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
4 changes: 4 additions & 0 deletions src/doc/rustdoc/src/unstable-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -776,3 +776,7 @@ will be split as follows:
"you today?",
]
```

## `--generate-macro-expansion`: Generate macros expansion toggles in source code

This flag enables the generation of toggles to expand macros in the HTML source code pages.
11 changes: 11 additions & 0 deletions src/librustdoc/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ pub(crate) struct RenderOptions {
pub(crate) parts_out_dir: Option<PathToParts>,
/// disable minification of CSS/JS
pub(crate) disable_minification: bool,
/// If `true`, HTML source pages will generate the possibility to expand macros.
pub(crate) generate_macro_expansion: bool,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -783,6 +785,7 @@ impl Options {
let show_type_layout = matches.opt_present("show-type-layout");
let nocapture = matches.opt_present("nocapture");
let generate_link_to_definition = matches.opt_present("generate-link-to-definition");
let generate_macro_expansion = matches.opt_present("generate-macro-expansion");
let extern_html_root_takes_precedence =
matches.opt_present("extern-html-root-takes-precedence");
let html_no_source = matches.opt_present("html-no-source");
Expand All @@ -798,6 +801,13 @@ impl Options {
.with_note("`--generate-link-to-definition` option will be ignored")
.emit();
}
if generate_macro_expansion && (show_coverage || output_format != OutputFormat::Html) {
dcx.struct_warn(
"`--generate-macro-expansion` option can only be used with HTML output format",
)
.with_note("`--generate-macro-expansion` option will be ignored")
.emit();
}

let scrape_examples_options = ScrapeExamplesOptions::new(matches, dcx);
let with_examples = matches.opt_strs("with-examples");
Expand Down Expand Up @@ -878,6 +888,7 @@ impl Options {
unstable_features,
emit,
generate_link_to_definition,
generate_macro_expansion,
call_locations,
no_emit_shared: false,
html_no_source,
Expand Down
13 changes: 11 additions & 2 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use crate::clean::inline::build_trait;
use crate::clean::{self, ItemId};
use crate::config::{Options as RustdocOptions, OutputFormat, RenderOptions};
use crate::formats::cache::Cache;
use crate::html::macro_expansion::{ExpandedCode, source_macro_expansion};
use crate::passes;
use crate::passes::Condition::*;
use crate::passes::collect_intra_doc_links::LinkCollector;
Expand Down Expand Up @@ -335,11 +336,19 @@ pub(crate) fn run_global_ctxt(
show_coverage: bool,
render_options: RenderOptions,
output_format: OutputFormat,
) -> (clean::Crate, RenderOptions, Cache) {
) -> (clean::Crate, RenderOptions, Cache, FxHashMap<rustc_span::BytePos, Vec<ExpandedCode>>) {
// Certain queries assume that some checks were run elsewhere
// (see https://github.com/rust-lang/rust/pull/73566#issuecomment-656954425),
// so type-check everything other than function bodies in this crate before running lints.

let expanded_macros = {
// We need for these variables to be removed to ensure that the `Crate` won't be "stolen"
// anymore.
let (_resolver, krate) = &*tcx.resolver_for_lowering().borrow();

source_macro_expansion(&krate, &render_options, output_format, tcx.sess.source_map())
};

// NOTE: this does not call `tcx.analysis()` so that we won't
// typeck function bodies or run the default rustc lints.
// (see `override_queries` in the `config`)
Expand Down Expand Up @@ -453,7 +462,7 @@ pub(crate) fn run_global_ctxt(

tcx.dcx().abort_if_errors();

(krate, ctxt.render_options, ctxt.cache)
(krate, ctxt.render_options, ctxt.cache, expanded_macros)
}

/// Due to <https://github.com/rust-lang/rust/pull/73566>,
Expand Down
18 changes: 7 additions & 11 deletions src/librustdoc/formats/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,6 @@ pub(crate) trait FormatRenderer<'tcx>: Sized {
/// reset the information between each call to `item` by using `restore_module_data`.
type ModuleData;

/// Sets up any state required for the renderer. When this is called the cache has already been
/// populated.
fn init(
krate: clean::Crate,
options: RenderOptions,
cache: Cache,
tcx: TyCtxt<'tcx>,
) -> Result<(Self, clean::Crate), Error>;

/// This method is called right before call [`Self::item`]. This method returns a type
/// containing information that needs to be reset after the [`Self::item`] method has been
/// called with the [`Self::restore_module_data`] method.
Expand Down Expand Up @@ -107,18 +98,23 @@ fn run_format_inner<'tcx, T: FormatRenderer<'tcx>>(
}

/// Main method for rendering a crate.
pub(crate) fn run_format<'tcx, T: FormatRenderer<'tcx>>(
pub(crate) fn run_format<
'tcx,
T: FormatRenderer<'tcx>,
F: FnOnce(clean::Crate, RenderOptions, Cache, TyCtxt<'tcx>) -> Result<(T, clean::Crate), Error>,
>(
krate: clean::Crate,
options: RenderOptions,
cache: Cache,
tcx: TyCtxt<'tcx>,
init: F,
) -> Result<(), Error> {
let prof = &tcx.sess.prof;

let emit_crate = options.should_emit_crate();
let (mut format_renderer, krate) = prof
.verbose_generic_activity_with_arg("create_renderer", T::descr())
.run(|| T::init(krate, options, cache, tcx))?;
.run(|| init(krate, options, cache, tcx))?;

if !emit_crate {
return Ok(());
Expand Down
Loading
Loading