@@ -315,6 +315,25 @@ crate fn strip_path(path: &Path) -> Path {
315315 Path { global : path. global , res : path. res , segments }
316316}
317317
318+ crate fn qpath_to_string ( p : & hir:: QPath < ' _ > ) -> String {
319+ let segments = match * p {
320+ hir:: QPath :: Resolved ( _, ref path) => & path. segments ,
321+ hir:: QPath :: TypeRelative ( _, ref segment) => return segment. ident . to_string ( ) ,
322+ hir:: QPath :: LangItem ( lang_item, ..) => return lang_item. name ( ) . to_string ( ) ,
323+ } ;
324+
325+ let mut s = String :: new ( ) ;
326+ for ( i, seg) in segments. iter ( ) . enumerate ( ) {
327+ if i > 0 {
328+ s. push_str ( "::" ) ;
329+ }
330+ if seg. ident . name != kw:: PathRoot {
331+ s. push_str ( & seg. ident . as_str ( ) ) ;
332+ }
333+ }
334+ s
335+ }
336+
318337crate fn build_deref_target_impls ( cx : & DocContext < ' _ > , items : & [ Item ] , ret : & mut Vec < Item > ) {
319338 let tcx = cx. tcx ;
320339
@@ -352,6 +371,57 @@ impl ToSource for rustc_span::Span {
352371 }
353372}
354373
374+ crate fn name_from_pat ( p : & hir:: Pat < ' _ > ) -> Symbol {
375+ use rustc_hir:: * ;
376+ debug ! ( "trying to get a name from pattern: {:?}" , p) ;
377+
378+ Symbol :: intern ( & match p. kind {
379+ PatKind :: Wild => return kw:: Underscore ,
380+ PatKind :: Binding ( _, _, ident, _) => return ident. name ,
381+ PatKind :: TupleStruct ( ref p, ..) | PatKind :: Path ( ref p) => qpath_to_string ( p) ,
382+ PatKind :: Struct ( ref name, ref fields, etc) => format ! (
383+ "{} {{ {}{} }}" ,
384+ qpath_to_string( name) ,
385+ fields
386+ . iter( )
387+ . map( |fp| format!( "{}: {}" , fp. ident, name_from_pat( & fp. pat) ) )
388+ . collect:: <Vec <String >>( )
389+ . join( ", " ) ,
390+ if etc { ", .." } else { "" }
391+ ) ,
392+ PatKind :: Or ( ref pats) => pats
393+ . iter ( )
394+ . map ( |p| name_from_pat ( & * * p) . to_string ( ) )
395+ . collect :: < Vec < String > > ( )
396+ . join ( " | " ) ,
397+ PatKind :: Tuple ( ref elts, _) => format ! (
398+ "({})" ,
399+ elts. iter( )
400+ . map( |p| name_from_pat( & * * p) . to_string( ) )
401+ . collect:: <Vec <String >>( )
402+ . join( ", " )
403+ ) ,
404+ PatKind :: Box ( ref p) => return name_from_pat ( & * * p) ,
405+ PatKind :: Ref ( ref p, _) => return name_from_pat ( & * * p) ,
406+ PatKind :: Lit ( ..) => {
407+ warn ! (
408+ "tried to get argument name from PatKind::Lit, which is silly in function arguments"
409+ ) ;
410+ return Symbol :: intern ( "()" ) ;
411+ }
412+ PatKind :: Range ( ..) => panic ! (
413+ "tried to get argument name from PatKind::Range, \
414+ which is not allowed in function arguments"
415+ ) ,
416+ PatKind :: Slice ( ref begin, ref mid, ref end) => {
417+ let begin = begin. iter ( ) . map ( |p| name_from_pat ( & * * p) . to_string ( ) ) ;
418+ let mid = mid. as_ref ( ) . map ( |p| format ! ( "..{}" , name_from_pat( & * * p) ) ) . into_iter ( ) ;
419+ let end = end. iter ( ) . map ( |p| name_from_pat ( & * * p) . to_string ( ) ) ;
420+ format ! ( "[{}]" , begin. chain( mid) . chain( end) . collect:: <Vec <_>>( ) . join( ", " ) )
421+ }
422+ } )
423+ }
424+
355425crate fn print_const ( cx : & DocContext < ' _ > , n : & ' tcx ty:: Const < ' _ > ) -> String {
356426 match n. val {
357427 ty:: ConstKind :: Unevaluated ( def, _, promoted) => {
0 commit comments