11use crate :: hir;
22use crate :: hir:: def:: Namespace ;
3- use crate :: hir:: map:: DefPathData ;
3+ use crate :: hir:: map:: { DefPathData , DisambiguatedDefPathData } ;
44use crate :: hir:: def_id:: { CrateNum , DefId , CRATE_DEF_INDEX , LOCAL_CRATE } ;
55use crate :: middle:: cstore:: { ExternCrate , ExternCrateSource } ;
66use crate :: middle:: region;
@@ -313,13 +313,13 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
313313 visible_parent, actual_parent,
314314 ) ;
315315
316- let data = cur_def_key. disambiguated_data . data ;
316+ let mut data = cur_def_key. disambiguated_data . data ;
317317 debug ! (
318318 "try_print_visible_def_path: data={:?} visible_parent={:?} actual_parent={:?}" ,
319319 data, visible_parent, actual_parent,
320320 ) ;
321321
322- let symbol = match data {
322+ match data {
323323 // In order to output a path that could actually be imported (valid and visible),
324324 // we need to handle re-exports correctly.
325325 //
@@ -351,27 +351,30 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
351351 // the children of the visible parent (as was done when computing
352352 // `visible_parent_map`), looking for the specific child we currently have and then
353353 // have access to the re-exported name.
354- DefPathData :: Module ( actual_name ) |
355- DefPathData :: TypeNs ( actual_name ) if Some ( visible_parent) != actual_parent => {
356- self . tcx ( ) . item_children ( visible_parent)
354+ DefPathData :: Module ( ref mut name ) |
355+ DefPathData :: TypeNs ( ref mut name ) if Some ( visible_parent) != actual_parent => {
356+ let reexport = self . tcx ( ) . item_children ( visible_parent)
357357 . iter ( )
358358 . find ( |child| child. def . def_id ( ) == def_id)
359- . map ( |child| child. ident . as_str ( ) )
360- . unwrap_or_else ( || actual_name. as_str ( ) )
359+ . map ( |child| child. ident . as_interned_str ( ) ) ;
360+ if let Some ( reexport) = reexport {
361+ * name = reexport;
362+ }
361363 }
362- _ => {
363- data. get_opt_name ( ) . map ( |n| n. as_str ( ) ) . unwrap_or_else ( || {
364- // Re-exported `extern crate` (#43189).
365- if let DefPathData :: CrateRoot = data {
366- self . tcx ( ) . original_crate_name ( def_id. krate ) . as_str ( )
367- } else {
368- Symbol :: intern ( "<unnamed>" ) . as_str ( )
369- }
370- } )
371- } ,
372- } ;
373- debug ! ( "try_print_visible_def_path: symbol={:?}" , symbol) ;
374- Ok ( ( self . path_append ( Ok , & symbol) ?, true ) )
364+ // Re-exported `extern crate` (#43189).
365+ DefPathData :: CrateRoot => {
366+ data = DefPathData :: Module (
367+ self . tcx ( ) . original_crate_name ( def_id. krate ) . as_interned_str ( ) ,
368+ ) ;
369+ }
370+ _ => { }
371+ }
372+ debug ! ( "try_print_visible_def_path: data={:?}" , data) ;
373+
374+ Ok ( ( self . path_append ( Ok , & DisambiguatedDefPathData {
375+ data,
376+ disambiguator : 0 ,
377+ } ) ?, true ) )
375378 }
376379
377380 fn pretty_path_qualified (
@@ -932,10 +935,18 @@ impl<F: fmt::Write> Printer<'gcx, 'tcx> for FmtPrinter<'_, 'gcx, 'tcx, F> {
932935 // only occur very early in the compiler pipeline.
933936 let parent_def_id = DefId { index : key. parent . unwrap ( ) , ..def_id } ;
934937 let span = self . tcx . def_span ( def_id) ;
935- return self . path_append (
936- |cx| cx. print_def_path ( parent_def_id, & [ ] ) ,
937- & format ! ( "<impl at {:?}>" , span) ,
938- ) ;
938+
939+ self = self . print_def_path ( parent_def_id, & [ ] ) ?;
940+
941+ // HACK(eddyb) copy of `path_append` to avoid
942+ // constructing a `DisambiguatedDefPathData`.
943+ if !self . empty_path {
944+ write ! ( self , "::" ) ?;
945+ }
946+ write ! ( self , "<impl at {:?}>" , span) ?;
947+ self . empty_path = false ;
948+
949+ return Ok ( self ) ;
939950 }
940951 }
941952
@@ -995,6 +1006,7 @@ impl<F: fmt::Write> Printer<'gcx, 'tcx> for FmtPrinter<'_, 'gcx, 'tcx, F> {
9951006 fn path_append_impl (
9961007 mut self ,
9971008 print_prefix : impl FnOnce ( Self ) -> Result < Self :: Path , Self :: Error > ,
1009+ _disambiguated_data : & DisambiguatedDefPathData ,
9981010 self_ty : Ty < ' tcx > ,
9991011 trait_ref : Option < ty:: TraitRef < ' tcx > > ,
10001012 ) -> Result < Self :: Path , Self :: Error > {
@@ -1012,17 +1024,35 @@ impl<F: fmt::Write> Printer<'gcx, 'tcx> for FmtPrinter<'_, 'gcx, 'tcx, F> {
10121024 fn path_append (
10131025 mut self ,
10141026 print_prefix : impl FnOnce ( Self ) -> Result < Self :: Path , Self :: Error > ,
1015- text : & str ,
1027+ disambiguated_data : & DisambiguatedDefPathData ,
10161028 ) -> Result < Self :: Path , Self :: Error > {
10171029 self = print_prefix ( self ) ?;
10181030
1019- // FIXME(eddyb) `text` should never be empty, but it
1031+ // Skip `::{{constructor}}` on tuple/unit structs.
1032+ match disambiguated_data. data {
1033+ DefPathData :: StructCtor => return Ok ( self ) ,
1034+ _ => { }
1035+ }
1036+
1037+ // FIXME(eddyb) `name` should never be empty, but it
10201038 // currently is for `extern { ... }` "foreign modules".
1021- if !text. is_empty ( ) {
1039+ let name = disambiguated_data. data . as_interned_str ( ) . as_str ( ) ;
1040+ if !name. is_empty ( ) {
10221041 if !self . empty_path {
10231042 write ! ( self , "::" ) ?;
10241043 }
1025- write ! ( self , "{}" , text) ?;
1044+ write ! ( self , "{}" , name) ?;
1045+
1046+ // FIXME(eddyb) this will print e.g. `{{closure}}#3`, but it
1047+ // might be nicer to use something else, e.g. `{closure#3}`.
1048+ let dis = disambiguated_data. disambiguator ;
1049+ let print_dis =
1050+ disambiguated_data. data . get_opt_name ( ) . is_none ( ) ||
1051+ dis != 0 && self . tcx . sess . verbose ( ) ;
1052+ if print_dis {
1053+ write ! ( self , "#{}" , dis) ?;
1054+ }
1055+
10261056 self . empty_path = false ;
10271057 }
10281058
0 commit comments