1
Fork 0

Replace &DocCtxt -> TyCtxt in macro matcher rendering

This commit is contained in:
David Tolnay 2021-12-28 13:43:08 -08:00
parent 336c85a053
commit 544a6bb7e7
No known key found for this signature in database
GPG key ID: F9BA143B95FF6D82

View file

@ -486,13 +486,13 @@ crate const DOC_RUST_LANG_ORG_CHANNEL: &str = env!("DOC_RUST_LANG_ORG_CHANNEL");
/// Render a sequence of macro arms in a format suitable for displaying to the user /// Render a sequence of macro arms in a format suitable for displaying to the user
/// as part of an item declaration. /// as part of an item declaration.
pub(super) fn render_macro_arms<'a>( pub(super) fn render_macro_arms<'a>(
cx: &DocContext<'_>, tcx: TyCtxt<'_>,
matchers: impl Iterator<Item = &'a TokenTree>, matchers: impl Iterator<Item = &'a TokenTree>,
arm_delim: &str, arm_delim: &str,
) -> String { ) -> String {
let mut out = String::new(); let mut out = String::new();
for matcher in matchers { for matcher in matchers {
writeln!(out, " {} => {{ ... }}{}", render_macro_matcher(cx, matcher), arm_delim) writeln!(out, " {} => {{ ... }}{}", render_macro_matcher(tcx, matcher), arm_delim)
.unwrap(); .unwrap();
} }
out out
@ -500,8 +500,8 @@ pub(super) fn render_macro_arms<'a>(
/// Render a macro matcher in a format suitable for displaying to the user /// Render a macro matcher in a format suitable for displaying to the user
/// as part of an item declaration. /// as part of an item declaration.
pub(super) fn render_macro_matcher(cx: &DocContext<'_>, matcher: &TokenTree) -> String { pub(super) fn render_macro_matcher(tcx: TyCtxt<'_>, matcher: &TokenTree) -> String {
if let Some(snippet) = snippet_equal_to_token(cx, matcher) { if let Some(snippet) = snippet_equal_to_token(tcx, matcher) {
snippet snippet
} else { } else {
rustc_ast_pretty::pprust::tt_to_string(matcher) rustc_ast_pretty::pprust::tt_to_string(matcher)
@ -510,11 +510,11 @@ pub(super) fn render_macro_matcher(cx: &DocContext<'_>, matcher: &TokenTree) ->
/// Find the source snippet for this token's Span, reparse it, and return the /// Find the source snippet for this token's Span, reparse it, and return the
/// snippet if the reparsed TokenTree matches the argument TokenTree. /// snippet if the reparsed TokenTree matches the argument TokenTree.
fn snippet_equal_to_token(cx: &DocContext<'_>, matcher: &TokenTree) -> Option<String> { fn snippet_equal_to_token(tcx: TyCtxt<'_>, matcher: &TokenTree) -> Option<String> {
// Find what rustc thinks is the source snippet. // Find what rustc thinks is the source snippet.
// This may not actually be anything meaningful if this matcher was itself // This may not actually be anything meaningful if this matcher was itself
// generated by a macro. // generated by a macro.
let source_map = cx.sess().source_map(); let source_map = tcx.sess.source_map();
let span = matcher.span(); let span = matcher.span();
let snippet = source_map.span_to_snippet(span).ok()?; let snippet = source_map.span_to_snippet(span).ok()?;
@ -561,21 +561,21 @@ pub(super) fn display_macro_source(
let matchers = tts.chunks(4).map(|arm| &arm[0]); let matchers = tts.chunks(4).map(|arm| &arm[0]);
if def.macro_rules { if def.macro_rules {
format!("macro_rules! {} {{\n{}}}", name, render_macro_arms(cx, matchers, ";")) format!("macro_rules! {} {{\n{}}}", name, render_macro_arms(cx.tcx, matchers, ";"))
} else { } else {
if matchers.len() <= 1 { if matchers.len() <= 1 {
format!( format!(
"{}macro {}{} {{\n ...\n}}", "{}macro {}{} {{\n ...\n}}",
vis.to_src_with_space(cx.tcx, def_id), vis.to_src_with_space(cx.tcx, def_id),
name, name,
matchers.map(|matcher| render_macro_matcher(cx, matcher)).collect::<String>(), matchers.map(|matcher| render_macro_matcher(cx.tcx, matcher)).collect::<String>(),
) )
} else { } else {
format!( format!(
"{}macro {} {{\n{}}}", "{}macro {} {{\n{}}}",
vis.to_src_with_space(cx.tcx, def_id), vis.to_src_with_space(cx.tcx, def_id),
name, name,
render_macro_arms(cx, matchers, ","), render_macro_arms(cx.tcx, matchers, ","),
) )
} }
} }