@@ -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 , SyntaxNodePtr , T ,
@@ -2007,32 +2007,31 @@ impl_from!(
20072007impl GenericDef {
20082008 pub fn params ( self , db : & dyn HirDatabase ) -> Vec < GenericParam > {
20092009 let generics = db. generic_params ( self . into ( ) ) ;
2010- let ty_params = generics
2011- . types
2012- . iter ( )
2013- . map ( |( local_id, _) | TypeParam { id : TypeParamId { parent : self . into ( ) , local_id } } )
2014- . map ( GenericParam :: TypeParam ) ;
2010+ let ty_params = generics. types . iter ( ) . map ( |( local_id, _) | {
2011+ let toc = TypeOrConstParam { id : TypeOrConstParamId { parent : self . into ( ) , local_id } } ;
2012+ match toc. split ( db) {
2013+ Either :: Left ( x) => GenericParam :: ConstParam ( x) ,
2014+ Either :: Right ( x) => GenericParam :: TypeParam ( x) ,
2015+ }
2016+ } ) ;
20152017 let lt_params = generics
20162018 . lifetimes
20172019 . iter ( )
20182020 . map ( |( local_id, _) | LifetimeParam {
20192021 id : LifetimeParamId { parent : self . into ( ) , local_id } ,
20202022 } )
20212023 . map ( GenericParam :: LifetimeParam ) ;
2022- let const_params = generics
2023- . consts
2024- . iter ( )
2025- . map ( |( local_id, _) | ConstParam { id : ConstParamId { parent : self . into ( ) , local_id } } )
2026- . map ( GenericParam :: ConstParam ) ;
2027- ty_params. chain ( lt_params) . chain ( const_params) . collect ( )
2024+ ty_params. chain ( lt_params) . collect ( )
20282025 }
20292026
2030- pub fn type_params ( self , db : & dyn HirDatabase ) -> Vec < TypeParam > {
2027+ pub fn type_params ( self , db : & dyn HirDatabase ) -> Vec < TypeOrConstParam > {
20312028 let generics = db. generic_params ( self . into ( ) ) ;
20322029 generics
20332030 . types
20342031 . iter ( )
2035- . map ( |( local_id, _) | TypeParam { id : TypeParamId { parent : self . into ( ) , local_id } } )
2032+ . map ( |( local_id, _) | TypeOrConstParam {
2033+ id : TypeOrConstParamId { parent : self . into ( ) , local_id } ,
2034+ } )
20362035 . collect ( )
20372036 }
20382037}
@@ -2221,38 +2220,41 @@ impl Label {
22212220#[ derive( Clone , Copy , Debug , PartialEq , Eq , Hash ) ]
22222221pub enum GenericParam {
22232222 TypeParam ( TypeParam ) ,
2224- LifetimeParam ( LifetimeParam ) ,
22252223 ConstParam ( ConstParam ) ,
2224+ LifetimeParam ( LifetimeParam ) ,
22262225}
2227- impl_from ! ( TypeParam , LifetimeParam , ConstParam for GenericParam ) ;
2226+ impl_from ! ( TypeParam , ConstParam , LifetimeParam for GenericParam ) ;
22282227
22292228impl GenericParam {
22302229 pub fn module ( self , db : & dyn HirDatabase ) -> Module {
22312230 match self {
22322231 GenericParam :: TypeParam ( it) => it. module ( db) ,
2233- GenericParam :: LifetimeParam ( it) => it. module ( db) ,
22342232 GenericParam :: ConstParam ( it) => it. module ( db) ,
2233+ GenericParam :: LifetimeParam ( it) => it. module ( db) ,
22352234 }
22362235 }
22372236
22382237 pub fn name ( self , db : & dyn HirDatabase ) -> Name {
22392238 match self {
22402239 GenericParam :: TypeParam ( it) => it. name ( db) ,
2241- GenericParam :: LifetimeParam ( it) => it. name ( db) ,
22422240 GenericParam :: ConstParam ( it) => it. name ( db) ,
2241+ GenericParam :: LifetimeParam ( it) => it. name ( db) ,
22432242 }
22442243 }
22452244}
22462245
22472246#[ derive( Clone , Copy , Debug , PartialEq , Eq , Hash ) ]
22482247pub struct TypeParam {
2249- pub ( crate ) id : TypeParamId ,
2248+ pub ( crate ) id : TypeOrConstParamId ,
22502249}
22512250
22522251impl TypeParam {
2252+ pub fn merge ( self ) -> TypeOrConstParam {
2253+ TypeOrConstParam { id : self . id }
2254+ }
2255+
22532256 pub fn name ( self , db : & dyn HirDatabase ) -> Name {
2254- let params = db. generic_params ( self . id . parent ) ;
2255- params. types [ self . id . local_id ] . name . clone ( ) . unwrap_or_else ( Name :: missing)
2257+ self . merge ( ) . name ( db)
22562258 }
22572259
22582260 pub fn module ( self , db : & dyn HirDatabase ) -> Module {
@@ -2315,13 +2317,23 @@ impl LifetimeParam {
23152317
23162318#[ derive( Clone , Copy , Debug , PartialEq , Eq , Hash ) ]
23172319pub struct ConstParam {
2318- pub ( crate ) id : ConstParamId ,
2320+ pub ( crate ) id : TypeOrConstParamId ,
23192321}
23202322
23212323impl ConstParam {
2324+ pub fn merge ( self ) -> TypeOrConstParam {
2325+ TypeOrConstParam { id : self . id }
2326+ }
2327+
23222328 pub fn name ( self , db : & dyn HirDatabase ) -> Name {
23232329 let params = db. generic_params ( self . id . parent ) ;
2324- params. consts [ self . id . local_id ] . name . clone ( )
2330+ match params. types [ self . id . local_id ] . name ( ) {
2331+ Some ( x) => x. clone ( ) ,
2332+ None => {
2333+ never ! ( ) ;
2334+ Name :: missing ( )
2335+ }
2336+ }
23252337 }
23262338
23272339 pub fn module ( self , db : & dyn HirDatabase ) -> Module {
@@ -2335,7 +2347,49 @@ impl ConstParam {
23352347 pub fn ty ( self , db : & dyn HirDatabase ) -> Type {
23362348 let def = self . id . parent ;
23372349 let krate = def. module ( db. upcast ( ) ) . krate ( ) ;
2338- Type :: new ( db, krate, def, db. const_param_ty ( self . id ) )
2350+ Type :: new ( db, krate, def, db. const_param_ty ( self . id ) . unwrap ( ) )
2351+ }
2352+ }
2353+
2354+ #[ derive( Clone , Copy , Debug , PartialEq , Eq , Hash ) ]
2355+ pub struct TypeOrConstParam {
2356+ pub ( crate ) id : TypeOrConstParamId ,
2357+ }
2358+
2359+ impl TypeOrConstParam {
2360+ pub fn name ( self , db : & dyn HirDatabase ) -> Name {
2361+ let params = db. generic_params ( self . id . parent ) ;
2362+ match params. types [ self . id . local_id ] . name ( ) {
2363+ Some ( n) => n. clone ( ) ,
2364+ _ => Name :: missing ( ) ,
2365+ }
2366+ }
2367+
2368+ pub fn module ( self , db : & dyn HirDatabase ) -> Module {
2369+ self . id . parent . module ( db. upcast ( ) ) . into ( )
2370+ }
2371+
2372+ pub fn parent ( self , _db : & dyn HirDatabase ) -> GenericDef {
2373+ self . id . parent . into ( )
2374+ }
2375+
2376+ pub fn split ( self , db : & dyn HirDatabase ) -> Either < ConstParam , TypeParam > {
2377+ let params = db. generic_params ( self . id . parent ) ;
2378+ match & params. types [ self . id . local_id ] {
2379+ hir_def:: generics:: TypeOrConstParamData :: TypeParamData ( _) => {
2380+ Either :: Right ( TypeParam { id : self . id } )
2381+ }
2382+ hir_def:: generics:: TypeOrConstParamData :: ConstParamData ( _) => {
2383+ Either :: Left ( ConstParam { id : self . id } )
2384+ }
2385+ }
2386+ }
2387+
2388+ pub fn ty ( self , db : & dyn HirDatabase ) -> Type {
2389+ match self . split ( db) {
2390+ Either :: Left ( x) => x. ty ( db) ,
2391+ Either :: Right ( x) => x. ty ( db) ,
2392+ }
23392393 }
23402394}
23412395
0 commit comments