Skip to content

Commit c30e1d7

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

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

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)]

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
}

0 commit comments

Comments
 (0)