11//! HirDisplay implementations for various hir types.
22use hir_def:: {
33 adt:: VariantData ,
4- generics:: { TypeParamProvenance , WherePredicate , WherePredicateTypeTarget } ,
4+ generics:: {
5+ TypeOrConstParamData , TypeParamProvenance , WherePredicate , WherePredicateTypeTarget ,
6+ } ,
57 type_ref:: { TypeBound , TypeRef } ,
68 AdtId , GenericDefId ,
79} ;
@@ -16,8 +18,8 @@ use syntax::SmolStr;
1618
1719use crate :: {
1820 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 ,
2123} ;
2224
2325impl HirDisplay for Function {
@@ -226,8 +228,17 @@ impl HirDisplay for GenericParam {
226228 fn hir_fmt ( & self , f : & mut HirFormatter ) -> Result < ( ) , HirDisplayError > {
227229 match self {
228230 GenericParam :: TypeParam ( it) => it. hir_fmt ( f) ,
229- GenericParam :: LifetimeParam ( it) => it. hir_fmt ( f) ,
230231 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) ,
231242 }
232243 }
233244}
@@ -239,11 +250,11 @@ impl HirDisplay for TypeParam {
239250 return Ok ( ( ) ) ;
240251 }
241252
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 ( ) ) ;
244255 let predicates: Vec < _ > =
245256 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 ;
247258 let sized_trait =
248259 f. db . lang_item ( krate, SmolStr :: new_inline ( "sized" ) )
249260 . and_then ( |lang_item| lang_item. as_trait ( ) ) ;
@@ -276,11 +287,11 @@ impl HirDisplay for ConstParam {
276287fn write_generic_params ( def : GenericDefId , f : & mut HirFormatter ) -> Result < ( ) , HirDisplayError > {
277288 let params = f. db . generic_params ( def) ;
278289 if params. lifetimes . is_empty ( )
279- && params. consts . is_empty ( )
280290 && params
281291 . types
282292 . 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 ) )
284295 {
285296 return Ok ( ( ) ) ;
286297 }
@@ -300,23 +311,27 @@ fn write_generic_params(def: GenericDefId, f: &mut HirFormatter) -> Result<(), H
300311 write ! ( f, "{}" , lifetime. name) ?;
301312 }
302313 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+ }
312332 }
313333 }
314334 }
315- for ( _, konst) in params. consts . iter ( ) {
316- delim ( f) ?;
317- write ! ( f, "const {}: " , konst. name) ?;
318- konst. ty . hir_fmt ( f) ?;
319- }
320335
321336 write ! ( f, ">" ) ?;
322337 Ok ( ( ) )
@@ -328,7 +343,7 @@ fn write_where_clause(def: GenericDefId, f: &mut HirFormatter) -> Result<(), Hir
328343 // unnamed type targets are displayed inline with the argument itself, e.g. `f: impl Y`.
329344 let is_unnamed_type_target = |target : & WherePredicateTypeTarget | match target {
330345 WherePredicateTypeTarget :: TypeRef ( _) => false ,
331- WherePredicateTypeTarget :: TypeParam ( id) => params. types [ * id] . name . is_none ( ) ,
346+ WherePredicateTypeTarget :: TypeOrConstParam ( id) => params. types [ * id] . name ( ) . is_none ( ) ,
332347 } ;
333348
334349 let has_displayable_predicate = params
@@ -344,7 +359,7 @@ fn write_where_clause(def: GenericDefId, f: &mut HirFormatter) -> Result<(), Hir
344359
345360 let write_target = |target : & WherePredicateTypeTarget , f : & mut HirFormatter | match target {
346361 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 ( ) {
348363 Some ( name) => write ! ( f, "{}" , name) ,
349364 None => write ! ( f, "{{unnamed}}" ) ,
350365 } ,
0 commit comments