@@ -35,7 +35,7 @@ pub mod term_search;
3535
3636mod display;
3737
38- use std:: { iter , mem:: discriminant, ops:: ControlFlow } ;
38+ use std:: { mem:: discriminant, ops:: ControlFlow } ;
3939
4040use arrayvec:: ArrayVec ;
4141use base_db:: { CrateDisplayName , CrateId , CrateOrigin , FileId } ;
@@ -52,7 +52,6 @@ use hir_def::{
5252 path:: ImportAlias ,
5353 per_ns:: PerNs ,
5454 resolver:: { HasResolver , Resolver } ,
55- src:: HasSource as _,
5655 AssocItemId , AssocItemLoc , AttrDefId , ConstId , ConstParamId , CrateRootModuleId , DefWithBodyId ,
5756 EnumId , EnumVariantId , ExternCrateId , FunctionId , GenericDefId , GenericParamId , HasModule ,
5857 ImplId , InTypeConstId , ItemContainerId , LifetimeParamId , LocalFieldId , Lookup , MacroExpander ,
@@ -1965,7 +1964,7 @@ impl Function {
19651964 . enumerate ( )
19661965 . map ( |( idx, ty) | {
19671966 let ty = Type { env : environment. clone ( ) , ty : ty. clone ( ) } ;
1968- Param { func : self , ty, idx }
1967+ Param { func : Callee :: Def ( CallableDefId :: FunctionId ( self . id ) ) , ty, idx }
19691968 } )
19701969 . collect ( )
19711970 }
@@ -1991,7 +1990,7 @@ impl Function {
19911990 . skip ( skip)
19921991 . map ( |( idx, ty) | {
19931992 let ty = Type { env : environment. clone ( ) , ty : ty. clone ( ) } ;
1994- Param { func : self , ty, idx }
1993+ Param { func : Callee :: Def ( CallableDefId :: FunctionId ( self . id ) ) , ty, idx }
19951994 } )
19961995 . collect ( )
19971996 }
@@ -2037,7 +2036,7 @@ impl Function {
20372036 . skip ( skip)
20382037 . map ( |( idx, ty) | {
20392038 let ty = Type { env : environment. clone ( ) , ty : ty. clone ( ) } ;
2040- Param { func : self , ty, idx }
2039+ Param { func : Callee :: Def ( CallableDefId :: FunctionId ( self . id ) ) , ty, idx }
20412040 } )
20422041 . collect ( )
20432042 }
@@ -2167,17 +2166,24 @@ impl From<hir_ty::Mutability> for Access {
21672166
21682167#[ derive( Clone , PartialEq , Eq , Hash , Debug ) ]
21692168pub struct Param {
2170- func : Function ,
2169+ func : Callee ,
21712170 /// The index in parameter list, including self parameter.
21722171 idx : usize ,
21732172 ty : Type ,
21742173}
21752174
21762175impl Param {
2177- pub fn parent_fn ( & self ) -> Function {
2178- self . func
2176+ pub fn parent_fn ( & self ) -> Option < Function > {
2177+ match self . func {
2178+ Callee :: Def ( CallableDefId :: FunctionId ( f) ) => Some ( f. into ( ) ) ,
2179+ _ => None ,
2180+ }
21792181 }
21802182
2183+ // pub fn parent_closure(&self) -> Option<Closure> {
2184+ // self.func.as_ref().right().cloned()
2185+ // }
2186+
21812187 pub fn index ( & self ) -> usize {
21822188 self . idx
21832189 }
@@ -2191,7 +2197,11 @@ impl Param {
21912197 }
21922198
21932199 pub fn as_local ( & self , db : & dyn HirDatabase ) -> Option < Local > {
2194- let parent = DefWithBodyId :: FunctionId ( self . func . into ( ) ) ;
2200+ let parent = match self . func {
2201+ Callee :: Def ( CallableDefId :: FunctionId ( it) ) => DefWithBodyId :: FunctionId ( it) ,
2202+ Callee :: Closure ( closure) => db. lookup_intern_closure ( closure. into ( ) ) . 0 ,
2203+ _ => return None ,
2204+ } ;
21952205 let body = db. body ( parent) ;
21962206 if let Some ( self_param) = body. self_param . filter ( |_| self . idx == 0 ) {
21972207 Some ( Local { parent, binding_id : self_param } )
@@ -2205,18 +2215,45 @@ impl Param {
22052215 }
22062216
22072217 pub fn pattern_source ( & self , db : & dyn HirDatabase ) -> Option < ast:: Pat > {
2208- self . source ( db) . and_then ( |p| p. value . pat ( ) )
2218+ self . source ( db) . and_then ( |p| p. value . right ( ) ? . pat ( ) )
22092219 }
22102220
2211- pub fn source ( & self , db : & dyn HirDatabase ) -> Option < InFile < ast:: Param > > {
2212- let InFile { file_id, value } = self . func . source ( db) ?;
2213- let params = value. param_list ( ) ?;
2214- if params. self_param ( ) . is_some ( ) {
2215- params. params ( ) . nth ( self . idx . checked_sub ( params. self_param ( ) . is_some ( ) as usize ) ?)
2216- } else {
2217- params. params ( ) . nth ( self . idx )
2221+ pub fn source (
2222+ & self ,
2223+ db : & dyn HirDatabase ,
2224+ ) -> Option < InFile < Either < ast:: SelfParam , ast:: Param > > > {
2225+ match self . func {
2226+ Callee :: Def ( CallableDefId :: FunctionId ( func) ) => {
2227+ let InFile { file_id, value } = Function { id : func } . source ( db) ?;
2228+ let params = value. param_list ( ) ?;
2229+ if let Some ( self_param) = params. self_param ( ) {
2230+ if let Some ( idx) = self . idx . checked_sub ( 1 as usize ) {
2231+ params. params ( ) . nth ( idx) . map ( Either :: Right )
2232+ } else {
2233+ Some ( Either :: Left ( self_param) )
2234+ }
2235+ } else {
2236+ params. params ( ) . nth ( self . idx ) . map ( Either :: Right )
2237+ }
2238+ . map ( |value| InFile { file_id, value } )
2239+ }
2240+ Callee :: Closure ( closure) => {
2241+ let InternedClosure ( owner, expr_id) = db. lookup_intern_closure ( closure. into ( ) ) ;
2242+ let ( _, source_map) = db. body_with_source_map ( owner) ;
2243+ let ast @ InFile { file_id, value } = source_map. expr_syntax ( expr_id) . ok ( ) ?;
2244+ let root = db. parse_or_expand ( file_id) ;
2245+ match value. to_node ( & root) {
2246+ ast:: Expr :: ClosureExpr ( it) => it
2247+ . param_list ( ) ?
2248+ . params ( )
2249+ . nth ( self . idx )
2250+ . map ( Either :: Right )
2251+ . map ( |value| InFile { file_id : ast. file_id , value } ) ,
2252+ _ => None ,
2253+ }
2254+ }
2255+ _ => None ,
22182256 }
2219- . map ( |value| InFile { file_id, value } )
22202257 }
22212258}
22222259
@@ -4919,7 +4956,7 @@ pub struct Callable {
49194956 pub ( crate ) is_bound_method : bool ,
49204957}
49214958
4922- #[ derive( Debug ) ]
4959+ #[ derive( Copy , Clone , PartialEq , Eq , Hash , Debug ) ]
49234960enum Callee {
49244961 Def ( CallableDefId ) ,
49254962 Closure ( ClosureId ) ,
@@ -4960,43 +4997,15 @@ impl Callable {
49604997 pub fn n_params ( & self ) -> usize {
49614998 self . sig . params ( ) . len ( ) - if self . is_bound_method { 1 } else { 0 }
49624999 }
4963- pub fn params (
4964- & self ,
4965- db : & dyn HirDatabase ,
4966- ) -> Vec < ( Option < Either < ast:: SelfParam , ast:: Pat > > , Type ) > {
4967- let types = self
4968- . sig
5000+ pub fn params ( & self ) -> Vec < Param > {
5001+ self . sig
49695002 . params ( )
49705003 . iter ( )
5004+ . enumerate ( )
49715005 . skip ( if self . is_bound_method { 1 } else { 0 } )
4972- . map ( |ty| self . ty . derived ( ty. clone ( ) ) ) ;
4973- let map_param = |it : ast:: Param | it. pat ( ) . map ( Either :: Right ) ;
4974- let patterns = match self . callee {
4975- Callee :: Def ( CallableDefId :: FunctionId ( func) ) => {
4976- let src = func. lookup ( db. upcast ( ) ) . source ( db. upcast ( ) ) ;
4977- src. value . param_list ( ) . map ( |param_list| {
4978- param_list
4979- . self_param ( )
4980- . map ( |it| Some ( Either :: Left ( it) ) )
4981- . filter ( |_| !self . is_bound_method )
4982- . into_iter ( )
4983- . chain ( param_list. params ( ) . map ( map_param) )
4984- } )
4985- }
4986- Callee :: Closure ( closure_id) => match closure_source ( db, closure_id) {
4987- Some ( src) => src. param_list ( ) . map ( |param_list| {
4988- param_list
4989- . self_param ( )
4990- . map ( |it| Some ( Either :: Left ( it) ) )
4991- . filter ( |_| !self . is_bound_method )
4992- . into_iter ( )
4993- . chain ( param_list. params ( ) . map ( map_param) )
4994- } ) ,
4995- None => None ,
4996- } ,
4997- _ => None ,
4998- } ;
4999- patterns. into_iter ( ) . flatten ( ) . chain ( iter:: repeat ( None ) ) . zip ( types) . collect ( )
5006+ . map ( |( idx, ty) | ( idx, self . ty . derived ( ty. clone ( ) ) ) )
5007+ . map ( |( idx, ty) | Param { func : self . callee , idx, ty } )
5008+ . collect ( )
50005009 }
50015010 pub fn return_type ( & self ) -> Type {
50025011 self . ty . derived ( self . sig . ret ( ) . clone ( ) )
@@ -5006,18 +5015,6 @@ impl Callable {
50065015 }
50075016}
50085017
5009- fn closure_source ( db : & dyn HirDatabase , closure : ClosureId ) -> Option < ast:: ClosureExpr > {
5010- let InternedClosure ( owner, expr_id) = db. lookup_intern_closure ( closure. into ( ) ) ;
5011- let ( _, source_map) = db. body_with_source_map ( owner) ;
5012- let ast = source_map. expr_syntax ( expr_id) . ok ( ) ?;
5013- let root = ast. file_syntax ( db. upcast ( ) ) ;
5014- let expr = ast. value . to_node ( & root) ;
5015- match expr {
5016- ast:: Expr :: ClosureExpr ( it) => Some ( it) ,
5017- _ => None ,
5018- }
5019- }
5020-
50215018#[ derive( Clone , Debug , Eq , PartialEq ) ]
50225019pub struct Layout ( Arc < TyLayout > , Arc < TargetDataLayout > ) ;
50235020
0 commit comments