Skip to content

Commit 2b858b7

Browse files
committed
Format macro const literals with pretty printer
1 parent a483969 commit 2b858b7

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
@@ -7,6 +7,7 @@
77
#![feature(generators)]
88
#![feature(iter_from_generator)]
99
#![feature(let_chains)]
10+
#![feature(if_let_guard)]
1011
#![feature(proc_macro_internals)]
1112
#![feature(macro_metavar_expr)]
1213
#![feature(min_specialization)]

compiler/rustc_metadata/src/rmeta/encoder.rs

+16-15
Original file line numberDiff line numberDiff line change
@@ -2375,31 +2375,32 @@ pub fn rendered_const<'tcx>(tcx: TyCtxt<'tcx>, body: hir::BodyId) -> String {
23752375
}
23762376
}
23772377

2378-
let classification = classify(value);
2379-
2380-
if classification == Literal
2381-
&& !value.span.from_expansion()
2382-
&& let Ok(snippet) = tcx.sess.source_map().span_to_snippet(value.span)
2383-
{
2384-
// For literals, we avoid invoking the pretty-printer and use the source snippet instead to
2385-
// preserve certain stylistic choices the user likely made for the sake legibility like
2378+
match classify(value) {
2379+
// For non-macro literals, we avoid invoking the pretty-printer and use the source snippet
2380+
// instead to preserve certain stylistic choices the user likely made for the sake of
2381+
// legibility, like:
23862382
//
23872383
// * hexadecimal notation
23882384
// * underscores
23892385
// * character escapes
23902386
//
23912387
// FIXME: This passes through `-/*spacer*/0` verbatim.
2392-
snippet
2393-
} else if classification == Simple {
2388+
Literal if !value.span.from_expansion()
2389+
&& let Ok(snippet) = tcx.sess.source_map().span_to_snippet(value.span) => {
2390+
snippet
2391+
}
2392+
23942393
// Otherwise we prefer pretty-printing to get rid of extraneous whitespace, comments and
23952394
// other formatting artifacts.
2396-
id_to_string(&hir, body.hir_id)
2397-
} else if tcx.def_kind(hir.body_owner_def_id(body).to_def_id()) == DefKind::AnonConst {
2395+
Literal | Simple => id_to_string(&hir, body.hir_id),
2396+
23982397
// FIXME: Omit the curly braces if the enclosing expression is an array literal
23992398
// with a repeated element (an `ExprKind::Repeat`) as in such case it
24002399
// would not actually need any disambiguation.
2401-
"{ _ }".to_owned()
2402-
} else {
2403-
"_".to_owned()
2400+
Complex => if tcx.def_kind(hir.body_owner_def_id(body).to_def_id()) == DefKind::AnonConst {
2401+
"{ _ }".to_owned()
2402+
} else {
2403+
"_".to_owned()
2404+
}
24042405
}
24052406
}
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)