Rollup merge of #115485 - DaniPopes:rustdoc-macro-consts, r=jackh726,fmease
Format macro const literals with pretty printer Fixes #115295
This commit is contained in:
commit
cd5b5e08fe
@ -8,6 +8,7 @@
|
|||||||
#![cfg_attr(not(bootstrap), feature(coroutines))]
|
#![cfg_attr(not(bootstrap), feature(coroutines))]
|
||||||
#![feature(iter_from_coroutine)]
|
#![feature(iter_from_coroutine)]
|
||||||
#![feature(let_chains)]
|
#![feature(let_chains)]
|
||||||
|
#![feature(if_let_guard)]
|
||||||
#![feature(proc_macro_internals)]
|
#![feature(proc_macro_internals)]
|
||||||
#![feature(macro_metavar_expr)]
|
#![feature(macro_metavar_expr)]
|
||||||
#![feature(min_specialization)]
|
#![feature(min_specialization)]
|
||||||
|
@ -2386,31 +2386,32 @@ pub fn rendered_const<'tcx>(tcx: TyCtxt<'tcx>, body: hir::BodyId) -> String {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let classification = classify(value);
|
match classify(value) {
|
||||||
|
// For non-macro literals, we avoid invoking the pretty-printer and use the source snippet
|
||||||
if classification == Literal
|
// instead to preserve certain stylistic choices the user likely made for the sake of
|
||||||
&& !value.span.from_expansion()
|
// legibility, like:
|
||||||
&& let Ok(snippet) = tcx.sess.source_map().span_to_snippet(value.span)
|
|
||||||
{
|
|
||||||
// For literals, we avoid invoking the pretty-printer and use the source snippet instead to
|
|
||||||
// preserve certain stylistic choices the user likely made for the sake legibility like
|
|
||||||
//
|
//
|
||||||
// * hexadecimal notation
|
// * hexadecimal notation
|
||||||
// * underscores
|
// * underscores
|
||||||
// * character escapes
|
// * character escapes
|
||||||
//
|
//
|
||||||
// FIXME: This passes through `-/*spacer*/0` verbatim.
|
// FIXME: This passes through `-/*spacer*/0` verbatim.
|
||||||
snippet
|
Literal if !value.span.from_expansion()
|
||||||
} else if classification == Simple {
|
&& let Ok(snippet) = tcx.sess.source_map().span_to_snippet(value.span) => {
|
||||||
|
snippet
|
||||||
|
}
|
||||||
|
|
||||||
// Otherwise we prefer pretty-printing to get rid of extraneous whitespace, comments and
|
// Otherwise we prefer pretty-printing to get rid of extraneous whitespace, comments and
|
||||||
// other formatting artifacts.
|
// other formatting artifacts.
|
||||||
id_to_string(&hir, body.hir_id)
|
Literal | Simple => id_to_string(&hir, body.hir_id),
|
||||||
} else if tcx.def_kind(hir.body_owner_def_id(body).to_def_id()) == DefKind::AnonConst {
|
|
||||||
// FIXME: Omit the curly braces if the enclosing expression is an array literal
|
// FIXME: Omit the curly braces if the enclosing expression is an array literal
|
||||||
// with a repeated element (an `ExprKind::Repeat`) as in such case it
|
// with a repeated element (an `ExprKind::Repeat`) as in such case it
|
||||||
// would not actually need any disambiguation.
|
// would not actually need any disambiguation.
|
||||||
"{ _ }".to_owned()
|
Complex => if tcx.def_kind(hir.body_owner_def_id(body).to_def_id()) == DefKind::AnonConst {
|
||||||
} else {
|
"{ _ }".to_owned()
|
||||||
"_".to_owned()
|
} else {
|
||||||
|
"_".to_owned()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
40
tests/rustdoc/issue-115295-macro-const-display.rs
Normal file
40
tests/rustdoc/issue-115295-macro-const-display.rs
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
#![crate_name = "foo"]
|
||||||
|
|
||||||
|
// @has foo/trait.Trait.html
|
||||||
|
pub trait Trait<T> {}
|
||||||
|
|
||||||
|
// @has foo/struct.WithConst.html
|
||||||
|
pub struct WithConst<const N: usize>;
|
||||||
|
|
||||||
|
macro_rules! spans_from_macro {
|
||||||
|
() => {
|
||||||
|
impl WithConst<42> {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Trait<WithConst<42>> for WithConst<42> {}
|
||||||
|
impl Trait<WithConst<43>> for WithConst<{ 43 }> {}
|
||||||
|
impl Trait<WithConst<{ 44 }>> for WithConst<44> {}
|
||||||
|
pub struct Other {
|
||||||
|
pub field: WithConst<42>,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// @has - '//*[@class="impl"]//h3[@class="code-header"]' \
|
||||||
|
// "impl Trait<WithConst<41>> for WithConst<41>"
|
||||||
|
impl Trait<WithConst<41>> for WithConst<41> {}
|
||||||
|
|
||||||
|
// @has - '//*[@class="impl"]//h3[@class="code-header"]' \
|
||||||
|
// "impl WithConst<42>"
|
||||||
|
// @has - '//*[@class="impl"]//h3[@class="code-header"]' \
|
||||||
|
// "impl Trait<WithConst<42>> for WithConst<42>"
|
||||||
|
// @has - '//*[@class="impl"]//h3[@class="code-header"]' \
|
||||||
|
// "impl Trait<WithConst<43>> for WithConst<{ 43 }>"
|
||||||
|
// @has - '//*[@class="impl"]//h3[@class="code-header"]' \
|
||||||
|
// "impl Trait<WithConst<44>> for WithConst<44>"
|
||||||
|
|
||||||
|
// @has foo/struct.Other.html
|
||||||
|
// @has - //pre "pub field: WithConst<42>"
|
||||||
|
spans_from_macro!();
|
Loading…
x
Reference in New Issue
Block a user