@@ -195,6 +195,25 @@ crate fn strip_path(path: &Path) -> Path {
195
195
Path { global : path. global , res : path. res , segments }
196
196
}
197
197
198
+ crate fn qpath_to_string ( p : & hir:: QPath < ' _ > ) -> String {
199
+ let segments = match * p {
200
+ hir:: QPath :: Resolved ( _, ref path) => & path. segments ,
201
+ hir:: QPath :: TypeRelative ( _, ref segment) => return segment. ident . to_string ( ) ,
202
+ hir:: QPath :: LangItem ( lang_item, ..) => return lang_item. name ( ) . to_string ( ) ,
203
+ } ;
204
+
205
+ let mut s = String :: new ( ) ;
206
+ for ( i, seg) in segments. iter ( ) . enumerate ( ) {
207
+ if i > 0 {
208
+ s. push_str ( "::" ) ;
209
+ }
210
+ if seg. ident . name != kw:: PathRoot {
211
+ s. push_str ( & seg. ident . as_str ( ) ) ;
212
+ }
213
+ }
214
+ s
215
+ }
216
+
198
217
crate fn build_deref_target_impls ( cx : & DocContext < ' _ > , items : & [ Item ] , ret : & mut Vec < Item > ) {
199
218
let tcx = cx. tcx ;
200
219
@@ -232,6 +251,54 @@ impl ToSource for rustc_span::Span {
232
251
}
233
252
}
234
253
254
+ crate fn name_from_pat ( p : & hir:: Pat < ' _ > ) -> Symbol {
255
+ use rustc_hir:: * ;
256
+ debug ! ( "trying to get a name from pattern: {:?}" , p) ;
257
+
258
+ Symbol :: intern ( & match p. kind {
259
+ PatKind :: Wild => return kw:: Underscore ,
260
+ PatKind :: Binding ( _, _, ident, _) => return ident. name ,
261
+ PatKind :: TupleStruct ( ref p, ..) | PatKind :: Path ( ref p) => qpath_to_string ( p) ,
262
+ PatKind :: Struct ( ref name, ref fields, etc) => format ! (
263
+ "{} {{ {}{} }}" ,
264
+ qpath_to_string( name) ,
265
+ fields
266
+ . iter( )
267
+ . map( |fp| format!( "{}: {}" , fp. ident, name_from_pat( & fp. pat) ) )
268
+ . collect:: <Vec <String >>( )
269
+ . join( ", " ) ,
270
+ if etc { ", .." } else { "" }
271
+ ) ,
272
+ PatKind :: Or ( ref pats) => pats
273
+ . iter ( )
274
+ . map ( |p| name_from_pat ( & * * p) . to_string ( ) )
275
+ . collect :: < Vec < String > > ( )
276
+ . join ( " | " ) ,
277
+ PatKind :: Tuple ( ref elts, _) => format ! (
278
+ "({})" ,
279
+ elts. iter( )
280
+ . map( |p| name_from_pat( & * * p) . to_string( ) )
281
+ . collect:: <Vec <String >>( )
282
+ . join( ", " )
283
+ ) ,
284
+ PatKind :: Box ( ref p) => return name_from_pat ( & * * p) ,
285
+ PatKind :: Ref ( ref p, _) => return name_from_pat ( & * * p) ,
286
+ PatKind :: Lit ( ..) => {
287
+ warn ! (
288
+ "tried to get argument name from PatKind::Lit, which is silly in function arguments"
289
+ ) ;
290
+ return Symbol :: intern ( "()" ) ;
291
+ }
292
+ PatKind :: Range ( ..) => return kw:: Underscore ,
293
+ PatKind :: Slice ( ref begin, ref mid, ref end) => {
294
+ let begin = begin. iter ( ) . map ( |p| name_from_pat ( & * * p) . to_string ( ) ) ;
295
+ let mid = mid. as_ref ( ) . map ( |p| format ! ( "..{}" , name_from_pat( & * * p) ) ) . into_iter ( ) ;
296
+ let end = end. iter ( ) . map ( |p| name_from_pat ( & * * p) . to_string ( ) ) ;
297
+ format ! ( "[{}]" , begin. chain( mid) . chain( end) . collect:: <Vec <_>>( ) . join( ", " ) )
298
+ }
299
+ } )
300
+ }
301
+
235
302
crate fn print_const ( cx : & DocContext < ' _ > , n : & ' tcx ty:: Const < ' _ > ) -> String {
236
303
match n. val {
237
304
ty:: ConstKind :: Unevaluated ( def, _, promoted) => {
0 commit comments