@@ -315,6 +315,25 @@ crate fn strip_path(path: &Path) -> Path {
315
315
Path { global : path. global , res : path. res , segments }
316
316
}
317
317
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
+
318
337
crate fn build_deref_target_impls ( cx : & DocContext < ' _ > , items : & [ Item ] , ret : & mut Vec < Item > ) {
319
338
let tcx = cx. tcx ;
320
339
@@ -352,6 +371,54 @@ impl ToSource for rustc_span::Span {
352
371
}
353
372
}
354
373
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 ( ..) => return kw:: Underscore ,
413
+ PatKind :: Slice ( ref begin, ref mid, ref end) => {
414
+ let begin = begin. iter ( ) . map ( |p| name_from_pat ( & * * p) . to_string ( ) ) ;
415
+ let mid = mid. as_ref ( ) . map ( |p| format ! ( "..{}" , name_from_pat( & * * p) ) ) . into_iter ( ) ;
416
+ let end = end. iter ( ) . map ( |p| name_from_pat ( & * * p) . to_string ( ) ) ;
417
+ format ! ( "[{}]" , begin. chain( mid) . chain( end) . collect:: <Vec <_>>( ) . join( ", " ) )
418
+ }
419
+ } )
420
+ }
421
+
355
422
crate fn print_const ( cx : & DocContext < ' _ > , n : & ' tcx ty:: Const < ' _ > ) -> String {
356
423
match n. val {
357
424
ty:: ConstKind :: Unevaluated ( def, _, promoted) => {
0 commit comments