Skip to content

Commit 7cb3ff1

Browse files
committed
Format macro const literals with pretty printer
1 parent 5b88d65 commit 7cb3ff1

File tree

3 files changed

+57
-14
lines changed

3 files changed

+57
-14
lines changed

Diff for: compiler/rustc_metadata/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![feature(generators)]
55
#![feature(iter_from_generator)]
66
#![feature(let_chains)]
7+
#![feature(if_let_guard)]
78
#![feature(proc_macro_internals)]
89
#![feature(macro_metavar_expr)]
910
#![feature(min_specialization)]

Diff for: compiler/rustc_metadata/src/rmeta/encoder.rs

+16-14
Original file line numberDiff line numberDiff line change
@@ -2369,30 +2369,32 @@ pub fn rendered_const<'tcx>(tcx: TyCtxt<'tcx>, body: hir::BodyId) -> String {
23692369
}
23702370
}
23712371

2372-
let classification = classify(value);
2373-
2374-
if classification == Literal
2375-
&& !value.span.from_expansion()
2376-
&& let Ok(snippet) = tcx.sess.source_map().span_to_snippet(value.span) {
2377-
// For literals, we avoid invoking the pretty-printer and use the source snippet instead to
2378-
// preserve certain stylistic choices the user likely made for the sake legibility like
2372+
match classify(value) {
2373+
// For non-macro literals, we avoid invoking the pretty-printer and use the source snippet
2374+
// instead to preserve certain stylistic choices the user likely made for the sake of
2375+
// legibility, like:
23792376
//
23802377
// * hexadecimal notation
23812378
// * underscores
23822379
// * character escapes
23832380
//
23842381
// FIXME: This passes through `-/*spacer*/0` verbatim.
2385-
snippet
2386-
} else if classification == Simple {
2382+
Literal if !value.span.from_expansion()
2383+
&& let Ok(snippet) = tcx.sess.source_map().span_to_snippet(value.span) => {
2384+
snippet
2385+
}
2386+
23872387
// Otherwise we prefer pretty-printing to get rid of extraneous whitespace, comments and
23882388
// other formatting artifacts.
2389-
id_to_string(&hir, body.hir_id)
2390-
} else if tcx.def_kind(hir.body_owner_def_id(body).to_def_id()) == DefKind::AnonConst {
2389+
Literal | Simple => id_to_string(&hir, body.hir_id),
2390+
23912391
// FIXME: Omit the curly braces if the enclosing expression is an array literal
23922392
// with a repeated element (an `ExprKind::Repeat`) as in such case it
23932393
// would not actually need any disambiguation.
2394-
"{ _ }".to_owned()
2395-
} else {
2396-
"_".to_owned()
2394+
Complex => if tcx.def_kind(hir.body_owner_def_id(body).to_def_id()) == DefKind::AnonConst {
2395+
"{ _ }".to_owned()
2396+
} else {
2397+
"_".to_owned()
2398+
}
23972399
}
23982400
}

Diff for: tests/rustdoc/issue-115295-macro-const-display.rs

+40
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)