@@ -64,7 +64,6 @@ use hir_expand::{
64
64
use hir_ty:: {
65
65
all_super_traits, autoderef, check_orphan_rules,
66
66
consteval:: { try_const_usize, unknown_const_as_generic, ConstExt } ,
67
- db:: InternedClosure ,
68
67
diagnostics:: BodyValidationDiagnostic ,
69
68
error_lifetime, known_const_to_ast,
70
69
layout:: { Layout as TyLayout , RustcEnumVariantIdx , RustcFieldIdx , TagEncoding } ,
@@ -1099,6 +1098,35 @@ pub enum FieldSource {
1099
1098
Pos ( ast:: TupleField ) ,
1100
1099
}
1101
1100
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
+
1102
1130
impl Field {
1103
1131
pub fn name ( & self , db : & dyn HirDatabase ) -> Name {
1104
1132
self . parent . variant_data ( db) . fields ( ) [ self . id ] . name . clone ( )
@@ -2216,47 +2244,9 @@ impl Param {
2216
2244
}
2217
2245
}
2218
2246
2219
- pub fn pattern_source ( & self , db : & dyn HirDatabase ) -> Option < ast:: Pat > {
2247
+ pub fn pattern_source ( self , db : & dyn HirDatabase ) -> Option < ast:: Pat > {
2220
2248
self . source ( db) . and_then ( |p| p. value . right ( ) ?. pat ( ) )
2221
2249
}
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
- }
2260
2250
}
2261
2251
2262
2252
#[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
@@ -2280,14 +2270,6 @@ impl SelfParam {
2280
2270
. unwrap_or ( Access :: Owned )
2281
2271
}
2282
2272
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
-
2291
2273
pub fn parent_fn ( & self ) -> Function {
2292
2274
Function :: from ( self . func )
2293
2275
}
@@ -3458,13 +3440,6 @@ impl Label {
3458
3440
let body = db. body ( self . parent ) ;
3459
3441
body[ self . label_id ] . name . clone ( )
3460
3442
}
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
- }
3468
3443
}
3469
3444
3470
3445
#[ derive( Clone , Copy , Debug , PartialEq , Eq , Hash ) ]
0 commit comments