@@ -28,7 +28,10 @@ use crate::{
28
28
29
29
#[ derive( Debug , Clone , Default ) ]
30
30
pub struct Resolver {
31
- // FIXME: all usages generally call `.rev`, so maybe reverse once in construction?
31
+ /// The stack of scopes, where the inner-most scope is the last item.
32
+ ///
33
+ /// When using, you generally want to process the scopes in reverse order,
34
+ /// there's `scopes` *method* for that.
32
35
scopes : Vec < Scope > ,
33
36
}
34
37
@@ -123,6 +126,10 @@ impl Resolver {
123
126
}
124
127
}
125
128
129
+ fn scopes ( & self ) -> impl Iterator < Item = & Scope > {
130
+ self . scopes . iter ( ) . rev ( )
131
+ }
132
+
126
133
fn resolve_module_path (
127
134
& self ,
128
135
db : & dyn DefDatabase ,
@@ -177,7 +184,7 @@ impl Resolver {
177
184
) -> Option < ( TypeNs , Option < usize > ) > {
178
185
let first_name = path. segments ( ) . first ( ) ?;
179
186
let skip_to_mod = path. kind != PathKind :: Plain ;
180
- for scope in self . scopes . iter ( ) . rev ( ) {
187
+ for scope in self . scopes ( ) {
181
188
match scope {
182
189
Scope :: ExprScope ( _) => continue ,
183
190
Scope :: GenericParams { .. } | Scope :: ImplDefScope ( _) if skip_to_mod => continue ,
@@ -251,7 +258,7 @@ impl Resolver {
251
258
let tmp = name ! [ self ] ;
252
259
let first_name = if path. is_self ( ) { & tmp } else { path. segments ( ) . first ( ) ? } ;
253
260
let skip_to_mod = path. kind != PathKind :: Plain && !path. is_self ( ) ;
254
- for scope in self . scopes . iter ( ) . rev ( ) {
261
+ for scope in self . scopes ( ) {
255
262
match scope {
256
263
Scope :: AdtScope ( _)
257
264
| Scope :: ExprScope ( _)
@@ -342,14 +349,14 @@ impl Resolver {
342
349
}
343
350
344
351
pub fn process_all_names ( & self , db : & dyn DefDatabase , f : & mut dyn FnMut ( Name , ScopeDef ) ) {
345
- for scope in self . scopes . iter ( ) . rev ( ) {
352
+ for scope in self . scopes ( ) {
346
353
scope. process_names ( db, f) ;
347
354
}
348
355
}
349
356
350
357
pub fn traits_in_scope ( & self , db : & dyn DefDatabase ) -> FxHashSet < TraitId > {
351
358
let mut traits = FxHashSet :: default ( ) ;
352
- for scope in & self . scopes {
359
+ for scope in self . scopes ( ) {
353
360
match scope {
354
361
Scope :: ModuleScope ( m) => {
355
362
if let Some ( prelude) = m. def_map . prelude ( ) {
@@ -384,7 +391,7 @@ impl Resolver {
384
391
}
385
392
386
393
fn module_scope ( & self ) -> Option < ( & DefMap , LocalModuleId ) > {
387
- self . scopes . iter ( ) . rev ( ) . find_map ( |scope| match scope {
394
+ self . scopes ( ) . find_map ( |scope| match scope {
388
395
Scope :: ModuleScope ( m) => Some ( ( & * m. def_map , m. module_id ) ) ,
389
396
390
397
_ => None ,
@@ -404,9 +411,7 @@ impl Resolver {
404
411
pub fn where_predicates_in_scope (
405
412
& self ,
406
413
) -> impl Iterator < Item = & crate :: generics:: WherePredicate > {
407
- self . scopes
408
- . iter ( )
409
- . rev ( )
414
+ self . scopes ( )
410
415
. filter_map ( |scope| match scope {
411
416
Scope :: GenericParams { params, .. } => Some ( params) ,
412
417
_ => None ,
@@ -415,14 +420,14 @@ impl Resolver {
415
420
}
416
421
417
422
pub fn generic_def ( & self ) -> Option < GenericDefId > {
418
- self . scopes . iter ( ) . rev ( ) . find_map ( |scope| match scope {
423
+ self . scopes ( ) . find_map ( |scope| match scope {
419
424
Scope :: GenericParams { def, .. } => Some ( * def) ,
420
425
_ => None ,
421
426
} )
422
427
}
423
428
424
429
pub fn body_owner ( & self ) -> Option < DefWithBodyId > {
425
- self . scopes . iter ( ) . rev ( ) . find_map ( |scope| match scope {
430
+ self . scopes ( ) . find_map ( |scope| match scope {
426
431
Scope :: ExprScope ( it) => Some ( it. owner ) ,
427
432
_ => None ,
428
433
} )
0 commit comments