From 9015c414331fc94eb9e95e878601c0a7cf3db911 Mon Sep 17 00:00:00 2001 From: marmeladema Date: Tue, 9 Jun 2020 23:04:58 +0100 Subject: [PATCH] save_analysis: improve pretty printing of enum --- src/librustc_hir_pretty/lib.rs | 10 ++++++++++ src/librustc_save_analysis/lib.rs | 9 ++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/librustc_hir_pretty/lib.rs b/src/librustc_hir_pretty/lib.rs index 4d04159022a..da8956ccb4d 100644 --- a/src/librustc_hir_pretty/lib.rs +++ b/src/librustc_hir_pretty/lib.rs @@ -239,6 +239,16 @@ pub fn fn_to_string( to_string(NO_ANN, |s| s.print_fn(decl, header, name, generics, vis, arg_names, body_id)) } +pub fn enum_def_to_string( + enum_definition: &hir::EnumDef<'_>, + generics: &hir::Generics<'_>, + name: Symbol, + span: rustc_span::Span, + visibility: &hir::Visibility<'_>, +) -> String { + to_string(NO_ANN, |s| s.print_enum_def(enum_definition, generics, name, span, visibility)) +} + impl<'a> State<'a> { pub fn cbox(&mut self, u: usize) { self.s.cbox(u); diff --git a/src/librustc_save_analysis/lib.rs b/src/librustc_save_analysis/lib.rs index cc0f9b1c06e..1ecec5c1c68 100644 --- a/src/librustc_save_analysis/lib.rs +++ b/src/librustc_save_analysis/lib.rs @@ -17,7 +17,7 @@ use rustc_hir::def::{CtorOf, DefKind as HirDefKind, Res}; use rustc_hir::def_id::{DefId, LOCAL_CRATE}; use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::Node; -use rustc_hir_pretty::{fn_to_string, ty_to_string}; +use rustc_hir_pretty::{enum_def_to_string, fn_to_string, ty_to_string}; use rustc_middle::hir::map::Map; use rustc_middle::middle::cstore::ExternCrate; use rustc_middle::middle::privacy::AccessLevels; @@ -292,13 +292,12 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> { attributes: lower_attributes(item.attrs.to_vec(), self), })) } - hir::ItemKind::Enum(ref def, _) => { + hir::ItemKind::Enum(ref def, ref generics) => { let name = item.ident.to_string(); let qualname = format!("::{}", self.tcx.def_path_str(def_id)); filter!(self.span_utils, item.ident.span); - let variants_str = - def.variants.iter().map(|v| v.ident.to_string()).collect::>().join(", "); - let value = format!("{}::{{{}}}", name, variants_str); + let value = + enum_def_to_string(def, generics, item.ident.name, item.span, &item.vis); Some(Data::DefData(Def { kind: DefKind::Enum, id: id_from_def_id(def_id),