Skip to content

Commit 27fad2a

Browse files
committed
Lift segment check out of the loop in resolve_path_in_value_ns
1 parent a51267c commit 27fad2a

File tree

1 file changed

+57
-45
lines changed

1 file changed

+57
-45
lines changed

crates/hir-def/src/resolver.rs

Lines changed: 57 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -255,55 +255,67 @@ impl Resolver {
255255
return self.module_scope.resolve_path_in_value_ns(db, path);
256256
}
257257

258-
for scope in self.scopes() {
259-
match scope {
260-
Scope::ExprScope(_) if n_segments > 1 => continue,
261-
Scope::ExprScope(scope) => {
262-
let entry = scope
263-
.expr_scopes
264-
.entries(scope.scope_id)
265-
.iter()
266-
.find(|entry| entry.name() == first_name);
267-
268-
if let Some(e) = entry {
269-
return Some(ResolveValueResult::ValueNs(ValueNs::LocalBinding(e.pat())));
258+
if n_segments <= 1 {
259+
for scope in self.scopes() {
260+
match scope {
261+
Scope::ExprScope(scope) => {
262+
let entry = scope
263+
.expr_scopes
264+
.entries(scope.scope_id)
265+
.iter()
266+
.find(|entry| entry.name() == first_name);
267+
268+
if let Some(e) = entry {
269+
return Some(ResolveValueResult::ValueNs(ValueNs::LocalBinding(
270+
e.pat(),
271+
)));
272+
}
270273
}
271-
}
272-
Scope::GenericParams { params, def } if n_segments > 1 => {
273-
if let Some(id) = params.find_type_by_name(first_name, *def) {
274-
let ty = TypeNs::GenericParam(id);
275-
return Some(ResolveValueResult::Partial(ty, 1));
274+
Scope::GenericParams { params, def } => {
275+
if let Some(id) = params.find_const_by_name(first_name, *def) {
276+
let val = ValueNs::GenericParam(id);
277+
return Some(ResolveValueResult::ValueNs(val));
278+
}
276279
}
277-
}
278-
Scope::GenericParams { .. } if n_segments != 1 => continue,
279-
Scope::GenericParams { params, def } => {
280-
if let Some(id) = params.find_const_by_name(first_name, *def) {
281-
let val = ValueNs::GenericParam(id);
282-
return Some(ResolveValueResult::ValueNs(val));
280+
&Scope::ImplDefScope(impl_) => {
281+
if first_name == &name![Self] {
282+
return Some(ResolveValueResult::ValueNs(ValueNs::ImplSelf(impl_)));
283+
}
283284
}
284-
}
285-
286-
&Scope::ImplDefScope(impl_) => {
287-
if first_name == &name![Self] {
288-
return Some(if n_segments > 1 {
289-
ResolveValueResult::Partial(TypeNs::SelfType(impl_), 1)
290-
} else {
291-
ResolveValueResult::ValueNs(ValueNs::ImplSelf(impl_))
292-
});
285+
// bare `Self` doesn't work in the value namespace in a struct/enum definition
286+
Scope::AdtScope(_) => continue,
287+
Scope::BlockScope(m) => {
288+
if let Some(def) = m.resolve_path_in_value_ns(db, path) {
289+
return Some(def);
290+
}
293291
}
294292
}
295-
// bare `Self` doesn't work in the value namespace in a struct/enum definition
296-
Scope::AdtScope(_) if n_segments == 1 => continue,
297-
Scope::AdtScope(adt) => {
298-
if first_name == &name![Self] {
299-
let ty = TypeNs::AdtSelfType(*adt);
300-
return Some(ResolveValueResult::Partial(ty, 1));
293+
}
294+
} else {
295+
for scope in self.scopes() {
296+
match scope {
297+
Scope::ExprScope(_) => continue,
298+
Scope::GenericParams { params, def } => {
299+
if let Some(id) = params.find_type_by_name(first_name, *def) {
300+
let ty = TypeNs::GenericParam(id);
301+
return Some(ResolveValueResult::Partial(ty, 1));
302+
}
301303
}
302-
}
303-
304-
Scope::BlockScope(m) => {
305-
if let Some(def) = m.resolve_path_in_value_ns(db, path) {
306-
return Some(def);
304+
&Scope::ImplDefScope(impl_) => {
305+
if first_name == &name![Self] {
306+
return Some(ResolveValueResult::Partial(TypeNs::SelfType(impl_), 1));
307+
}
308+
}
309+
Scope::AdtScope(adt) => {
310+
if first_name == &name![Self] {
311+
let ty = TypeNs::AdtSelfType(*adt);
312+
return Some(ResolveValueResult::Partial(ty, 1));
313+
}
314+
}
315+
Scope::BlockScope(m) => {
316+
if let Some(def) = m.resolve_path_in_value_ns(db, path) {
317+
return Some(def);
318+
}
307319
}
308320
}
309321
}
@@ -316,8 +328,8 @@ impl Resolver {
316328
// If a path of the shape `u16::from_le_bytes` failed to resolve at all, then we fall back
317329
// to resolving to the primitive type, to allow this to still work in the presence of
318330
// `use core::u16;`.
319-
if path.kind == PathKind::Plain && path.segments().len() > 1 {
320-
if let Some(builtin) = BuiltinType::by_name(&path.segments()[0]) {
331+
if path.kind == PathKind::Plain && n_segments > 1 {
332+
if let Some(builtin) = BuiltinType::by_name(first_name) {
321333
return Some(ResolveValueResult::Partial(TypeNs::BuiltinType(builtin), 1));
322334
}
323335
}

0 commit comments

Comments
 (0)