@@ -47,10 +47,10 @@ use hir_def::{
4747 per_ns:: PerNs ,
4848 resolver:: { HasResolver , Resolver } ,
4949 src:: HasSource as _,
50- AdtId , AssocItemId , AssocItemLoc , AttrDefId , ConstId , ConstParamId , DefWithBodyId , EnumId ,
51- FunctionId , GenericDefId , HasModule , ImplId , ItemContainerId , LifetimeParamId ,
52- LocalEnumVariantId , LocalFieldId , Lookup , ModuleId , StaticId , StructId , TraitId , TypeAliasId ,
53- TypeParamId , UnionId ,
50+ AdtId , AssocItemId , AssocItemLoc , AttrDefId , ConstId , DefWithBodyId , EnumId , FunctionId ,
51+ GenericDefId , HasModule , ImplId , ItemContainerId , LifetimeParamId , LocalEnumVariantId ,
52+ LocalFieldId , Lookup , ModuleId , StaticId , StructId , TraitId , TypeAliasId , TypeOrConstParamId ,
53+ UnionId ,
5454} ;
5555use hir_expand:: { name:: name, MacroCallKind , MacroDefId , MacroDefKind } ;
5656use hir_ty:: {
@@ -71,7 +71,7 @@ use itertools::Itertools;
7171use nameres:: diagnostics:: DefDiagnosticKind ;
7272use once_cell:: unsync:: Lazy ;
7373use rustc_hash:: FxHashSet ;
74- use stdx:: { format_to, impl_from} ;
74+ use stdx:: { format_to, impl_from, never } ;
7575use syntax:: {
7676 ast:: { self , HasAttrs as _, HasDocComments , HasName } ,
7777 AstNode , AstPtr , SmolStr , SyntaxKind , SyntaxNodePtr ,
@@ -1981,32 +1981,31 @@ impl_from!(
19811981impl GenericDef {
19821982 pub fn params ( self , db : & dyn HirDatabase ) -> Vec < GenericParam > {
19831983 let generics = db. generic_params ( self . into ( ) ) ;
1984- let ty_params = generics
1985- . types
1986- . iter ( )
1987- . map ( |( local_id, _) | TypeParam { id : TypeParamId { parent : self . into ( ) , local_id } } )
1988- . map ( GenericParam :: TypeParam ) ;
1984+ let ty_params = generics. types . iter ( ) . map ( |( local_id, _) | {
1985+ let toc = TypeOrConstParam { id : TypeOrConstParamId { parent : self . into ( ) , local_id } } ;
1986+ match toc. split ( db) {
1987+ Either :: Left ( x) => GenericParam :: ConstParam ( x) ,
1988+ Either :: Right ( x) => GenericParam :: TypeParam ( x) ,
1989+ }
1990+ } ) ;
19891991 let lt_params = generics
19901992 . lifetimes
19911993 . iter ( )
19921994 . map ( |( local_id, _) | LifetimeParam {
19931995 id : LifetimeParamId { parent : self . into ( ) , local_id } ,
19941996 } )
19951997 . map ( GenericParam :: LifetimeParam ) ;
1996- let const_params = generics
1997- . consts
1998- . iter ( )
1999- . map ( |( local_id, _) | ConstParam { id : ConstParamId { parent : self . into ( ) , local_id } } )
2000- . map ( GenericParam :: ConstParam ) ;
2001- ty_params. chain ( lt_params) . chain ( const_params) . collect ( )
1998+ ty_params. chain ( lt_params) . collect ( )
20021999 }
20032000
2004- pub fn type_params ( self , db : & dyn HirDatabase ) -> Vec < TypeParam > {
2001+ pub fn type_params ( self , db : & dyn HirDatabase ) -> Vec < TypeOrConstParam > {
20052002 let generics = db. generic_params ( self . into ( ) ) ;
20062003 generics
20072004 . types
20082005 . iter ( )
2009- . map ( |( local_id, _) | TypeParam { id : TypeParamId { parent : self . into ( ) , local_id } } )
2006+ . map ( |( local_id, _) | TypeOrConstParam {
2007+ id : TypeOrConstParamId { parent : self . into ( ) , local_id } ,
2008+ } )
20102009 . collect ( )
20112010 }
20122011}
@@ -2190,38 +2189,41 @@ impl Label {
21902189#[ derive( Clone , Copy , Debug , PartialEq , Eq , Hash ) ]
21912190pub enum GenericParam {
21922191 TypeParam ( TypeParam ) ,
2193- LifetimeParam ( LifetimeParam ) ,
21942192 ConstParam ( ConstParam ) ,
2193+ LifetimeParam ( LifetimeParam ) ,
21952194}
2196- impl_from ! ( TypeParam , LifetimeParam , ConstParam for GenericParam ) ;
2195+ impl_from ! ( TypeParam , ConstParam , LifetimeParam for GenericParam ) ;
21972196
21982197impl GenericParam {
21992198 pub fn module ( self , db : & dyn HirDatabase ) -> Module {
22002199 match self {
22012200 GenericParam :: TypeParam ( it) => it. module ( db) ,
2202- GenericParam :: LifetimeParam ( it) => it. module ( db) ,
22032201 GenericParam :: ConstParam ( it) => it. module ( db) ,
2202+ GenericParam :: LifetimeParam ( it) => it. module ( db) ,
22042203 }
22052204 }
22062205
22072206 pub fn name ( self , db : & dyn HirDatabase ) -> Name {
22082207 match self {
22092208 GenericParam :: TypeParam ( it) => it. name ( db) ,
2210- GenericParam :: LifetimeParam ( it) => it. name ( db) ,
22112209 GenericParam :: ConstParam ( it) => it. name ( db) ,
2210+ GenericParam :: LifetimeParam ( it) => it. name ( db) ,
22122211 }
22132212 }
22142213}
22152214
22162215#[ derive( Clone , Copy , Debug , PartialEq , Eq , Hash ) ]
22172216pub struct TypeParam {
2218- pub ( crate ) id : TypeParamId ,
2217+ pub ( crate ) id : TypeOrConstParamId ,
22192218}
22202219
22212220impl TypeParam {
2221+ pub fn merge ( self ) -> TypeOrConstParam {
2222+ TypeOrConstParam { id : self . id }
2223+ }
2224+
22222225 pub fn name ( self , db : & dyn HirDatabase ) -> Name {
2223- let params = db. generic_params ( self . id . parent ) ;
2224- params. types [ self . id . local_id ] . name . clone ( ) . unwrap_or_else ( Name :: missing)
2226+ self . merge ( ) . name ( db)
22252227 }
22262228
22272229 pub fn module ( self , db : & dyn HirDatabase ) -> Module {
@@ -2281,13 +2283,23 @@ impl LifetimeParam {
22812283
22822284#[ derive( Clone , Copy , Debug , PartialEq , Eq , Hash ) ]
22832285pub struct ConstParam {
2284- pub ( crate ) id : ConstParamId ,
2286+ pub ( crate ) id : TypeOrConstParamId ,
22852287}
22862288
22872289impl ConstParam {
2290+ pub fn merge ( self ) -> TypeOrConstParam {
2291+ TypeOrConstParam { id : self . id }
2292+ }
2293+
22882294 pub fn name ( self , db : & dyn HirDatabase ) -> Name {
22892295 let params = db. generic_params ( self . id . parent ) ;
2290- params. consts [ self . id . local_id ] . name . clone ( )
2296+ match params. types [ self . id . local_id ] . name ( ) {
2297+ Some ( x) => x. clone ( ) ,
2298+ None => {
2299+ never ! ( ) ;
2300+ Name :: missing ( )
2301+ }
2302+ }
22912303 }
22922304
22932305 pub fn module ( self , db : & dyn HirDatabase ) -> Module {
@@ -2301,7 +2313,49 @@ impl ConstParam {
23012313 pub fn ty ( self , db : & dyn HirDatabase ) -> Type {
23022314 let def = self . id . parent ;
23032315 let krate = def. module ( db. upcast ( ) ) . krate ( ) ;
2304- Type :: new ( db, krate, def, db. const_param_ty ( self . id ) )
2316+ Type :: new ( db, krate, def, db. const_param_ty ( self . id ) . unwrap ( ) )
2317+ }
2318+ }
2319+
2320+ #[ derive( Clone , Copy , Debug , PartialEq , Eq , Hash ) ]
2321+ pub struct TypeOrConstParam {
2322+ pub ( crate ) id : TypeOrConstParamId ,
2323+ }
2324+
2325+ impl TypeOrConstParam {
2326+ pub fn name ( self , db : & dyn HirDatabase ) -> Name {
2327+ let params = db. generic_params ( self . id . parent ) ;
2328+ match params. types [ self . id . local_id ] . name ( ) {
2329+ Some ( n) => n. clone ( ) ,
2330+ _ => Name :: missing ( ) ,
2331+ }
2332+ }
2333+
2334+ pub fn module ( self , db : & dyn HirDatabase ) -> Module {
2335+ self . id . parent . module ( db. upcast ( ) ) . into ( )
2336+ }
2337+
2338+ pub fn parent ( self , _db : & dyn HirDatabase ) -> GenericDef {
2339+ self . id . parent . into ( )
2340+ }
2341+
2342+ pub fn split ( self , db : & dyn HirDatabase ) -> Either < ConstParam , TypeParam > {
2343+ let params = db. generic_params ( self . id . parent ) ;
2344+ match & params. types [ self . id . local_id ] {
2345+ hir_def:: generics:: TypeOrConstParamData :: TypeParamData ( _) => {
2346+ Either :: Right ( TypeParam { id : self . id } )
2347+ }
2348+ hir_def:: generics:: TypeOrConstParamData :: ConstParamData ( _) => {
2349+ Either :: Left ( ConstParam { id : self . id } )
2350+ }
2351+ }
2352+ }
2353+
2354+ pub fn ty ( self , db : & dyn HirDatabase ) -> Type {
2355+ match self . split ( db) {
2356+ Either :: Left ( x) => x. ty ( db) ,
2357+ Either :: Right ( x) => x. ty ( db) ,
2358+ }
23052359 }
23062360}
23072361
0 commit comments