@@ -47,10 +47,10 @@ use hir_def::{
47
47
per_ns:: PerNs ,
48
48
resolver:: { HasResolver , Resolver } ,
49
49
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 ,
54
54
} ;
55
55
use hir_expand:: { name:: name, MacroCallKind , MacroDefId , MacroDefKind } ;
56
56
use hir_ty:: {
@@ -71,7 +71,7 @@ use itertools::Itertools;
71
71
use nameres:: diagnostics:: DefDiagnosticKind ;
72
72
use once_cell:: unsync:: Lazy ;
73
73
use rustc_hash:: FxHashSet ;
74
- use stdx:: { format_to, impl_from} ;
74
+ use stdx:: { format_to, impl_from, never } ;
75
75
use syntax:: {
76
76
ast:: { self , HasAttrs as _, HasDocComments , HasName } ,
77
77
AstNode , AstPtr , SmolStr , SyntaxNodePtr , T ,
@@ -2007,32 +2007,31 @@ impl_from!(
2007
2007
impl GenericDef {
2008
2008
pub fn params ( self , db : & dyn HirDatabase ) -> Vec < GenericParam > {
2009
2009
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
+ } ) ;
2015
2017
let lt_params = generics
2016
2018
. lifetimes
2017
2019
. iter ( )
2018
2020
. map ( |( local_id, _) | LifetimeParam {
2019
2021
id : LifetimeParamId { parent : self . into ( ) , local_id } ,
2020
2022
} )
2021
2023
. 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 ( )
2028
2025
}
2029
2026
2030
- pub fn type_params ( self , db : & dyn HirDatabase ) -> Vec < TypeParam > {
2027
+ pub fn type_params ( self , db : & dyn HirDatabase ) -> Vec < TypeOrConstParam > {
2031
2028
let generics = db. generic_params ( self . into ( ) ) ;
2032
2029
generics
2033
2030
. types
2034
2031
. 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
+ } )
2036
2035
. collect ( )
2037
2036
}
2038
2037
}
@@ -2221,38 +2220,41 @@ impl Label {
2221
2220
#[ derive( Clone , Copy , Debug , PartialEq , Eq , Hash ) ]
2222
2221
pub enum GenericParam {
2223
2222
TypeParam ( TypeParam ) ,
2224
- LifetimeParam ( LifetimeParam ) ,
2225
2223
ConstParam ( ConstParam ) ,
2224
+ LifetimeParam ( LifetimeParam ) ,
2226
2225
}
2227
- impl_from ! ( TypeParam , LifetimeParam , ConstParam for GenericParam ) ;
2226
+ impl_from ! ( TypeParam , ConstParam , LifetimeParam for GenericParam ) ;
2228
2227
2229
2228
impl GenericParam {
2230
2229
pub fn module ( self , db : & dyn HirDatabase ) -> Module {
2231
2230
match self {
2232
2231
GenericParam :: TypeParam ( it) => it. module ( db) ,
2233
- GenericParam :: LifetimeParam ( it) => it. module ( db) ,
2234
2232
GenericParam :: ConstParam ( it) => it. module ( db) ,
2233
+ GenericParam :: LifetimeParam ( it) => it. module ( db) ,
2235
2234
}
2236
2235
}
2237
2236
2238
2237
pub fn name ( self , db : & dyn HirDatabase ) -> Name {
2239
2238
match self {
2240
2239
GenericParam :: TypeParam ( it) => it. name ( db) ,
2241
- GenericParam :: LifetimeParam ( it) => it. name ( db) ,
2242
2240
GenericParam :: ConstParam ( it) => it. name ( db) ,
2241
+ GenericParam :: LifetimeParam ( it) => it. name ( db) ,
2243
2242
}
2244
2243
}
2245
2244
}
2246
2245
2247
2246
#[ derive( Clone , Copy , Debug , PartialEq , Eq , Hash ) ]
2248
2247
pub struct TypeParam {
2249
- pub ( crate ) id : TypeParamId ,
2248
+ pub ( crate ) id : TypeOrConstParamId ,
2250
2249
}
2251
2250
2252
2251
impl TypeParam {
2252
+ pub fn merge ( self ) -> TypeOrConstParam {
2253
+ TypeOrConstParam { id : self . id }
2254
+ }
2255
+
2253
2256
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)
2256
2258
}
2257
2259
2258
2260
pub fn module ( self , db : & dyn HirDatabase ) -> Module {
@@ -2315,13 +2317,23 @@ impl LifetimeParam {
2315
2317
2316
2318
#[ derive( Clone , Copy , Debug , PartialEq , Eq , Hash ) ]
2317
2319
pub struct ConstParam {
2318
- pub ( crate ) id : ConstParamId ,
2320
+ pub ( crate ) id : TypeOrConstParamId ,
2319
2321
}
2320
2322
2321
2323
impl ConstParam {
2324
+ pub fn merge ( self ) -> TypeOrConstParam {
2325
+ TypeOrConstParam { id : self . id }
2326
+ }
2327
+
2322
2328
pub fn name ( self , db : & dyn HirDatabase ) -> Name {
2323
2329
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
+ }
2325
2337
}
2326
2338
2327
2339
pub fn module ( self , db : & dyn HirDatabase ) -> Module {
@@ -2335,7 +2347,49 @@ impl ConstParam {
2335
2347
pub fn ty ( self , db : & dyn HirDatabase ) -> Type {
2336
2348
let def = self . id . parent ;
2337
2349
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
+ }
2339
2393
}
2340
2394
}
2341
2395
0 commit comments