Skip to content

Commit d6f2352

Browse files
bors[bot]matklad
andauthored
Merge #9757
9757: intenral: completion cleanups r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents e7be544 + 5f3662e commit d6f2352

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
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
})

crates/ide_completion/src/completions/unqualified_path.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,11 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
8484
}
8585

8686
ctx.process_all_names(&mut |name, res| {
87-
if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) | ScopeDef::Label(_) =
88-
res
89-
{
90-
cov_mark::hit!(unqualified_skip_lifetime_completion);
91-
return;
92-
}
9387
let add_resolution = match res {
88+
ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) | ScopeDef::Label(_) => {
89+
cov_mark::hit!(unqualified_skip_lifetime_completion);
90+
return;
91+
}
9492
ScopeDef::ImplSelfType(_) => {
9593
!ctx.previous_token_is(syntax::T![impl]) && !ctx.previous_token_is(syntax::T![for])
9694
}
@@ -306,4 +304,25 @@ pub mod prelude {
306304
"#]],
307305
);
308306
}
307+
308+
#[test]
309+
fn local_variable_shadowing() {
310+
// FIXME: this isn't actually correct, should emit `x` only once.
311+
check(
312+
r#"
313+
fn main() {
314+
let x = 92;
315+
{
316+
let x = 92;
317+
x$0;
318+
}
319+
}
320+
"#,
321+
expect![[r#"
322+
lc x i32
323+
lc x i32
324+
fn main() fn()
325+
"#]],
326+
);
327+
}
309328
}

0 commit comments

Comments
 (0)