Skip to content

Commit 5f3662e

Browse files
committed
minor: encode usage convention into API
1 parent 8b6ea15 commit 5f3662e

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

crates/hir_def/src/resolver.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ use crate::{
2828

2929
#[derive(Debug, Clone, Default)]
3030
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.
3235
scopes: Vec<Scope>,
3336
}
3437

@@ -123,6 +126,10 @@ impl Resolver {
123126
}
124127
}
125128

129+
fn scopes(&self) -> impl Iterator<Item = &Scope> {
130+
self.scopes.iter().rev()
131+
}
132+
126133
fn resolve_module_path(
127134
&self,
128135
db: &dyn DefDatabase,
@@ -177,7 +184,7 @@ impl Resolver {
177184
) -> Option<(TypeNs, Option<usize>)> {
178185
let first_name = path.segments().first()?;
179186
let skip_to_mod = path.kind != PathKind::Plain;
180-
for scope in self.scopes.iter().rev() {
187+
for scope in self.scopes() {
181188
match scope {
182189
Scope::ExprScope(_) => continue,
183190
Scope::GenericParams { .. } | Scope::ImplDefScope(_) if skip_to_mod => continue,
@@ -251,7 +258,7 @@ impl Resolver {
251258
let tmp = name![self];
252259
let first_name = if path.is_self() { &tmp } else { path.segments().first()? };
253260
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() {
255262
match scope {
256263
Scope::AdtScope(_)
257264
| Scope::ExprScope(_)
@@ -342,14 +349,14 @@ impl Resolver {
342349
}
343350

344351
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() {
346353
scope.process_names(db, f);
347354
}
348355
}
349356

350357
pub fn traits_in_scope(&self, db: &dyn DefDatabase) -> FxHashSet<TraitId> {
351358
let mut traits = FxHashSet::default();
352-
for scope in &self.scopes {
359+
for scope in self.scopes() {
353360
match scope {
354361
Scope::ModuleScope(m) => {
355362
if let Some(prelude) = m.def_map.prelude() {
@@ -384,7 +391,7 @@ impl Resolver {
384391
}
385392

386393
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 {
388395
Scope::ModuleScope(m) => Some((&*m.def_map, m.module_id)),
389396

390397
_ => None,
@@ -404,9 +411,7 @@ impl Resolver {
404411
pub fn where_predicates_in_scope(
405412
&self,
406413
) -> impl Iterator<Item = &crate::generics::WherePredicate> {
407-
self.scopes
408-
.iter()
409-
.rev()
414+
self.scopes()
410415
.filter_map(|scope| match scope {
411416
Scope::GenericParams { params, .. } => Some(params),
412417
_ => None,
@@ -415,14 +420,14 @@ impl Resolver {
415420
}
416421

417422
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 {
419424
Scope::GenericParams { def, .. } => Some(*def),
420425
_ => None,
421426
})
422427
}
423428

424429
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 {
426431
Scope::ExprScope(it) => Some(it.owner),
427432
_ => None,
428433
})

0 commit comments

Comments
 (0)