Skip to content

Commit cd5b5e0

Browse files
authored
Rollup merge of #115485 - DaniPopes:rustdoc-macro-consts, r=jackh726,fmease
Format macro const literals with pretty printer Fixes #115295
2 parents 9bd71af + 2b858b7 commit cd5b5e0

File tree

3 files changed

+57
-15
lines changed

3 files changed

+57
-15
lines changed

compiler/rustc_metadata/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#![cfg_attr(not(bootstrap), feature(coroutines))]
99
#![feature(iter_from_coroutine)]
1010
#![feature(let_chains)]
11+
#![feature(if_let_guard)]
1112
#![feature(proc_macro_internals)]
1213
#![feature(macro_metavar_expr)]
1314
#![feature(min_specialization)]

compiler/rustc_metadata/src/rmeta/encoder.rs

+16-15
Original file line numberDiff line numberDiff line change
@@ -2386,31 +2386,32 @@ pub fn rendered_const<'tcx>(tcx: TyCtxt<'tcx>, body: hir::BodyId) -> String {
23862386
}
23872387
}
23882388

2389-
let classification = classify(value);
2390-
2391-
if classification == Literal
2392-
&& !value.span.from_expansion()
2393-
&& let Ok(snippet) = tcx.sess.source_map().span_to_snippet(value.span)
2394-
{
2395-
// For literals, we avoid invoking the pretty-printer and use the source snippet instead to
2396-
// preserve certain stylistic choices the user likely made for the sake legibility like
2389+
match classify(value) {
2390+
// For non-macro literals, we avoid invoking the pretty-printer and use the source snippet
2391+
// instead to preserve certain stylistic choices the user likely made for the sake of
2392+
// legibility, like:
23972393
//
23982394
// * hexadecimal notation
23992395
// * underscores
24002396
// * character escapes
24012397
//
24022398
// FIXME: This passes through `-/*spacer*/0` verbatim.
2403-
snippet
2404-
} else if classification == Simple {
2399+
Literal if !value.span.from_expansion()
2400+
&& let Ok(snippet) = tcx.sess.source_map().span_to_snippet(value.span) => {
2401+
snippet
2402+
}
2403+
24052404
// Otherwise we prefer pretty-printing to get rid of extraneous whitespace, comments and
24062405
// other formatting artifacts.
2407-
id_to_string(&hir, body.hir_id)
2408-
} else if tcx.def_kind(hir.body_owner_def_id(body).to_def_id()) == DefKind::AnonConst {
2406+
Literal | Simple => id_to_string(&hir, body.hir_id),
2407+
24092408
// FIXME: Omit the curly braces if the enclosing expression is an array literal
24102409
// with a repeated element (an `ExprKind::Repeat`) as in such case it
24112410
// would not actually need any disambiguation.
2412-
"{ _ }".to_owned()
2413-
} else {
2414-
"_".to_owned()
2411+
Complex => if tcx.def_kind(hir.body_owner_def_id(body).to_def_id()) == DefKind::AnonConst {
2412+
"{ _ }".to_owned()
2413+
} else {
2414+
"_".to_owned()
2415+
}
24152416
}
24162417
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#![crate_name = "foo"]
2+
3+
// @has foo/trait.Trait.html
4+
pub trait Trait<T> {}
5+
6+
// @has foo/struct.WithConst.html
7+
pub struct WithConst<const N: usize>;
8+
9+
macro_rules! spans_from_macro {
10+
() => {
11+
impl WithConst<42> {
12+
pub fn new() -> Self {
13+
Self
14+
}
15+
}
16+
impl Trait<WithConst<42>> for WithConst<42> {}
17+
impl Trait<WithConst<43>> for WithConst<{ 43 }> {}
18+
impl Trait<WithConst<{ 44 }>> for WithConst<44> {}
19+
pub struct Other {
20+
pub field: WithConst<42>,
21+
}
22+
};
23+
}
24+
25+
// @has - '//*[@class="impl"]//h3[@class="code-header"]' \
26+
// "impl Trait<WithConst<41>> for WithConst<41>"
27+
impl Trait<WithConst<41>> for WithConst<41> {}
28+
29+
// @has - '//*[@class="impl"]//h3[@class="code-header"]' \
30+
// "impl WithConst<42>"
31+
// @has - '//*[@class="impl"]//h3[@class="code-header"]' \
32+
// "impl Trait<WithConst<42>> for WithConst<42>"
33+
// @has - '//*[@class="impl"]//h3[@class="code-header"]' \
34+
// "impl Trait<WithConst<43>> for WithConst<{ 43 }>"
35+
// @has - '//*[@class="impl"]//h3[@class="code-header"]' \
36+
// "impl Trait<WithConst<44>> for WithConst<44>"
37+
38+
// @has foo/struct.Other.html
39+
// @has - //pre "pub field: WithConst<42>"
40+
spans_from_macro!();

0 commit comments

Comments
 (0)