@@ -64,7 +64,6 @@ use hir_expand::{
6464use hir_ty:: {
6565 all_super_traits, autoderef, check_orphan_rules,
6666 consteval:: { try_const_usize, unknown_const_as_generic, ConstExt } ,
67- db:: InternedClosure ,
6867 diagnostics:: BodyValidationDiagnostic ,
6968 error_lifetime, known_const_to_ast,
7069 layout:: { Layout as TyLayout , RustcEnumVariantIdx , RustcFieldIdx , TagEncoding } ,
@@ -1099,6 +1098,35 @@ pub enum FieldSource {
10991098 Pos ( ast:: TupleField ) ,
11001099}
11011100
1101+ impl AstNode for FieldSource {
1102+ fn can_cast ( kind : syntax:: SyntaxKind ) -> bool
1103+ where
1104+ Self : Sized ,
1105+ {
1106+ ast:: RecordField :: can_cast ( kind) || ast:: TupleField :: can_cast ( kind)
1107+ }
1108+
1109+ fn cast ( syntax : SyntaxNode ) -> Option < Self >
1110+ where
1111+ Self : Sized ,
1112+ {
1113+ if ast:: RecordField :: can_cast ( syntax. kind ( ) ) {
1114+ <ast:: RecordField as AstNode >:: cast ( syntax) . map ( FieldSource :: Named )
1115+ } else if ast:: TupleField :: can_cast ( syntax. kind ( ) ) {
1116+ <ast:: TupleField as AstNode >:: cast ( syntax) . map ( FieldSource :: Pos )
1117+ } else {
1118+ None
1119+ }
1120+ }
1121+
1122+ fn syntax ( & self ) -> & SyntaxNode {
1123+ match self {
1124+ FieldSource :: Named ( it) => it. syntax ( ) ,
1125+ FieldSource :: Pos ( it) => it. syntax ( ) ,
1126+ }
1127+ }
1128+ }
1129+
11021130impl Field {
11031131 pub fn name ( & self , db : & dyn HirDatabase ) -> Name {
11041132 self . parent . variant_data ( db) . fields ( ) [ self . id ] . name . clone ( )
@@ -2216,47 +2244,9 @@ impl Param {
22162244 }
22172245 }
22182246
2219- pub fn pattern_source ( & self , db : & dyn HirDatabase ) -> Option < ast:: Pat > {
2247+ pub fn pattern_source ( self , db : & dyn HirDatabase ) -> Option < ast:: Pat > {
22202248 self . source ( db) . and_then ( |p| p. value . right ( ) ?. pat ( ) )
22212249 }
2222-
2223- pub fn source (
2224- & self ,
2225- db : & dyn HirDatabase ,
2226- ) -> Option < InFile < Either < ast:: SelfParam , ast:: Param > > > {
2227- match self . func {
2228- Callee :: Def ( CallableDefId :: FunctionId ( func) ) => {
2229- let InFile { file_id, value } = Function { id : func } . source ( db) ?;
2230- let params = value. param_list ( ) ?;
2231- if let Some ( self_param) = params. self_param ( ) {
2232- if let Some ( idx) = self . idx . checked_sub ( 1 ) {
2233- params. params ( ) . nth ( idx) . map ( Either :: Right )
2234- } else {
2235- Some ( Either :: Left ( self_param) )
2236- }
2237- } else {
2238- params. params ( ) . nth ( self . idx ) . map ( Either :: Right )
2239- }
2240- . map ( |value| InFile { file_id, value } )
2241- }
2242- Callee :: Closure ( closure, _) => {
2243- let InternedClosure ( owner, expr_id) = db. lookup_intern_closure ( closure. into ( ) ) ;
2244- let ( _, source_map) = db. body_with_source_map ( owner) ;
2245- let ast @ InFile { file_id, value } = source_map. expr_syntax ( expr_id) . ok ( ) ?;
2246- let root = db. parse_or_expand ( file_id) ;
2247- match value. to_node ( & root) {
2248- ast:: Expr :: ClosureExpr ( it) => it
2249- . param_list ( ) ?
2250- . params ( )
2251- . nth ( self . idx )
2252- . map ( Either :: Right )
2253- . map ( |value| InFile { file_id : ast. file_id , value } ) ,
2254- _ => None ,
2255- }
2256- }
2257- _ => None ,
2258- }
2259- }
22602250}
22612251
22622252#[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
@@ -2280,14 +2270,6 @@ impl SelfParam {
22802270 . unwrap_or ( Access :: Owned )
22812271 }
22822272
2283- pub fn source ( & self , db : & dyn HirDatabase ) -> Option < InFile < ast:: SelfParam > > {
2284- let InFile { file_id, value } = Function :: from ( self . func ) . source ( db) ?;
2285- value
2286- . param_list ( )
2287- . and_then ( |params| params. self_param ( ) )
2288- . map ( |value| InFile { file_id, value } )
2289- }
2290-
22912273 pub fn parent_fn ( & self ) -> Function {
22922274 Function :: from ( self . func )
22932275 }
@@ -3458,13 +3440,6 @@ impl Label {
34583440 let body = db. body ( self . parent ) ;
34593441 body[ self . label_id ] . name . clone ( )
34603442 }
3461-
3462- pub fn source ( self , db : & dyn HirDatabase ) -> InFile < ast:: Label > {
3463- let ( _body, source_map) = db. body_with_source_map ( self . parent ) ;
3464- let src = source_map. label_syntax ( self . label_id ) ;
3465- let root = src. file_syntax ( db. upcast ( ) ) ;
3466- src. map ( |ast| ast. to_node ( & root) )
3467- }
34683443}
34693444
34703445#[ derive( Clone , Copy , Debug , PartialEq , Eq , Hash ) ]
0 commit comments