1
1
//! HirDisplay implementations for various hir types.
2
2
use hir_def:: {
3
3
adt:: VariantData ,
4
- generics:: { TypeParamProvenance , WherePredicate , WherePredicateTypeTarget } ,
4
+ generics:: {
5
+ TypeOrConstParamData , TypeParamProvenance , WherePredicate , WherePredicateTypeTarget ,
6
+ } ,
5
7
type_ref:: { TypeBound , TypeRef } ,
6
8
AdtId , GenericDefId ,
7
9
} ;
@@ -16,8 +18,8 @@ use syntax::SmolStr;
16
18
17
19
use crate :: {
18
20
Adt , Const , ConstParam , Enum , Field , Function , GenericParam , HasCrate , HasVisibility ,
19
- LifetimeParam , Module , Static , Struct , Trait , TyBuilder , Type , TypeAlias , TypeParam , Union ,
20
- Variant ,
21
+ LifetimeParam , Module , Static , Struct , Trait , TyBuilder , Type , TypeAlias , TypeOrConstParam ,
22
+ TypeParam , Union , Variant ,
21
23
} ;
22
24
23
25
impl HirDisplay for Function {
@@ -226,8 +228,17 @@ impl HirDisplay for GenericParam {
226
228
fn hir_fmt ( & self , f : & mut HirFormatter ) -> Result < ( ) , HirDisplayError > {
227
229
match self {
228
230
GenericParam :: TypeParam ( it) => it. hir_fmt ( f) ,
229
- GenericParam :: LifetimeParam ( it) => it. hir_fmt ( f) ,
230
231
GenericParam :: ConstParam ( it) => it. hir_fmt ( f) ,
232
+ GenericParam :: LifetimeParam ( it) => it. hir_fmt ( f) ,
233
+ }
234
+ }
235
+ }
236
+
237
+ impl HirDisplay for TypeOrConstParam {
238
+ fn hir_fmt ( & self , f : & mut HirFormatter ) -> Result < ( ) , HirDisplayError > {
239
+ match self . split ( f. db ) {
240
+ either:: Either :: Left ( x) => x. hir_fmt ( f) ,
241
+ either:: Either :: Right ( x) => x. hir_fmt ( f) ,
231
242
}
232
243
}
233
244
}
@@ -239,11 +250,11 @@ impl HirDisplay for TypeParam {
239
250
return Ok ( ( ) ) ;
240
251
}
241
252
242
- let bounds = f. db . generic_predicates_for_param ( self . id . parent , self . id , None ) ;
243
- let substs = TyBuilder :: type_params_subst ( f. db , self . id . parent ) ;
253
+ let bounds = f. db . generic_predicates_for_param ( self . id . parent ( ) , self . id . into ( ) , None ) ;
254
+ let substs = TyBuilder :: type_params_subst ( f. db , self . id . parent ( ) ) ;
244
255
let predicates: Vec < _ > =
245
256
bounds. iter ( ) . cloned ( ) . map ( |b| b. substitute ( Interner , & substs) ) . collect ( ) ;
246
- let krate = self . id . parent . krate ( f. db ) . id ;
257
+ let krate = self . id . parent ( ) . krate ( f. db ) . id ;
247
258
let sized_trait =
248
259
f. db . lang_item ( krate, SmolStr :: new_inline ( "sized" ) )
249
260
. and_then ( |lang_item| lang_item. as_trait ( ) ) ;
@@ -276,11 +287,11 @@ impl HirDisplay for ConstParam {
276
287
fn write_generic_params ( def : GenericDefId , f : & mut HirFormatter ) -> Result < ( ) , HirDisplayError > {
277
288
let params = f. db . generic_params ( def) ;
278
289
if params. lifetimes . is_empty ( )
279
- && params. consts . is_empty ( )
280
290
&& params
281
291
. types
282
292
. iter ( )
283
- . all ( |( _, param) | !matches ! ( param. provenance, TypeParamProvenance :: TypeParamList ) )
293
+ . filter_map ( |x| x. 1 . type_param ( ) )
294
+ . all ( |param| !matches ! ( param. provenance, TypeParamProvenance :: TypeParamList ) )
284
295
{
285
296
return Ok ( ( ) ) ;
286
297
}
@@ -300,23 +311,27 @@ fn write_generic_params(def: GenericDefId, f: &mut HirFormatter) -> Result<(), H
300
311
write ! ( f, "{}" , lifetime. name) ?;
301
312
}
302
313
for ( _, ty) in params. types . iter ( ) {
303
- if ty. provenance != TypeParamProvenance :: TypeParamList {
304
- continue ;
305
- }
306
- if let Some ( name) = & ty. name {
307
- delim ( f) ?;
308
- write ! ( f, "{}" , name) ?;
309
- if let Some ( default) = & ty. default {
310
- write ! ( f, " = " ) ?;
311
- default. hir_fmt ( f) ?;
314
+ if let Some ( name) = & ty. name ( ) {
315
+ match ty {
316
+ TypeOrConstParamData :: TypeParamData ( ty) => {
317
+ if ty. provenance != TypeParamProvenance :: TypeParamList {
318
+ continue ;
319
+ }
320
+ delim ( f) ?;
321
+ write ! ( f, "{}" , name) ?;
322
+ if let Some ( default) = & ty. default {
323
+ write ! ( f, " = " ) ?;
324
+ default. hir_fmt ( f) ?;
325
+ }
326
+ }
327
+ TypeOrConstParamData :: ConstParamData ( c) => {
328
+ delim ( f) ?;
329
+ write ! ( f, "const {}: " , name) ?;
330
+ c. ty . hir_fmt ( f) ?;
331
+ }
312
332
}
313
333
}
314
334
}
315
- for ( _, konst) in params. consts . iter ( ) {
316
- delim ( f) ?;
317
- write ! ( f, "const {}: " , konst. name) ?;
318
- konst. ty . hir_fmt ( f) ?;
319
- }
320
335
321
336
write ! ( f, ">" ) ?;
322
337
Ok ( ( ) )
@@ -328,7 +343,7 @@ fn write_where_clause(def: GenericDefId, f: &mut HirFormatter) -> Result<(), Hir
328
343
// unnamed type targets are displayed inline with the argument itself, e.g. `f: impl Y`.
329
344
let is_unnamed_type_target = |target : & WherePredicateTypeTarget | match target {
330
345
WherePredicateTypeTarget :: TypeRef ( _) => false ,
331
- WherePredicateTypeTarget :: TypeParam ( id) => params. types [ * id] . name . is_none ( ) ,
346
+ WherePredicateTypeTarget :: TypeOrConstParam ( id) => params. types [ * id] . name ( ) . is_none ( ) ,
332
347
} ;
333
348
334
349
let has_displayable_predicate = params
@@ -344,7 +359,7 @@ fn write_where_clause(def: GenericDefId, f: &mut HirFormatter) -> Result<(), Hir
344
359
345
360
let write_target = |target : & WherePredicateTypeTarget , f : & mut HirFormatter | match target {
346
361
WherePredicateTypeTarget :: TypeRef ( ty) => ty. hir_fmt ( f) ,
347
- WherePredicateTypeTarget :: TypeParam ( id) => match & params. types [ * id] . name {
362
+ WherePredicateTypeTarget :: TypeOrConstParam ( id) => match & params. types [ * id] . name ( ) {
348
363
Some ( name) => write ! ( f, "{}" , name) ,
349
364
None => write ! ( f, "{{unnamed}}" ) ,
350
365
} ,
0 commit comments