Skip to content

Commit 4c4c3d5

Browse files
committed
resolve: Restructure resolve_ident_in_lexical_scope for better clarity
1 parent 7af4ee8 commit 4c4c3d5

File tree

1 file changed

+37
-42
lines changed

1 file changed

+37
-42
lines changed

compiler/rustc_resolve/src/ident.rs

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
312312
let normalized_ident = Ident { span: normalized_span, ..ident };
313313

314314
// Walk backwards up the ribs in scope.
315-
let mut module = self.graph_root;
316315
for (i, rib) in ribs.iter().enumerate().rev() {
317316
debug!("walk rib\n{:?}", rib.bindings);
318317
// Use the rib kind to determine whether we are resolving parameters
@@ -328,51 +327,47 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
328327
*original_rib_ident_def,
329328
ribs,
330329
)));
330+
} else if let RibKind::Block(Some(module)) = rib.kind
331+
&& let Ok(binding) = self.cm().resolve_ident_in_module_unadjusted(
332+
ModuleOrUniformRoot::Module(module),
333+
ident,
334+
ns,
335+
parent_scope,
336+
Shadowing::Unrestricted,
337+
finalize.map(|finalize| Finalize { used: Used::Scope, ..finalize }),
338+
ignore_binding,
339+
None,
340+
)
341+
{
342+
// The ident resolves to an item in a block.
343+
return Some(LexicalScopeBinding::Item(binding));
344+
} else if let RibKind::Module(module) = rib.kind {
345+
// Encountered a module item, abandon ribs and look into that module and preludes.
346+
return self
347+
.cm()
348+
.early_resolve_ident_in_lexical_scope(
349+
orig_ident,
350+
ScopeSet::Late(ns, module, finalize.map(|finalize| finalize.node_id)),
351+
parent_scope,
352+
finalize,
353+
finalize.is_some(),
354+
ignore_binding,
355+
None,
356+
)
357+
.ok()
358+
.map(LexicalScopeBinding::Item);
331359
}
332360

333-
module = match rib.kind {
334-
RibKind::Module(module) | RibKind::Block(Some(module)) => module,
335-
RibKind::MacroDefinition(def) if def == self.macro_def(ident.span.ctxt()) => {
336-
// If an invocation of this macro created `ident`, give up on `ident`
337-
// and switch to `ident`'s source from the macro definition.
338-
ident.span.remove_mark();
339-
continue;
340-
}
341-
_ => continue,
342-
};
343-
344-
match module.kind {
345-
ModuleKind::Block => {} // We can see through blocks
346-
_ => break,
347-
}
348-
349-
let item = self.cm().resolve_ident_in_module_unadjusted(
350-
ModuleOrUniformRoot::Module(module),
351-
ident,
352-
ns,
353-
parent_scope,
354-
Shadowing::Unrestricted,
355-
finalize.map(|finalize| Finalize { used: Used::Scope, ..finalize }),
356-
ignore_binding,
357-
None,
358-
);
359-
if let Ok(binding) = item {
360-
// The ident resolves to an item.
361-
return Some(LexicalScopeBinding::Item(binding));
361+
if let RibKind::MacroDefinition(def) = rib.kind
362+
&& def == self.macro_def(ident.span.ctxt())
363+
{
364+
// If an invocation of this macro created `ident`, give up on `ident`
365+
// and switch to `ident`'s source from the macro definition.
366+
ident.span.remove_mark();
362367
}
363368
}
364-
self.cm()
365-
.early_resolve_ident_in_lexical_scope(
366-
orig_ident,
367-
ScopeSet::Late(ns, module, finalize.map(|finalize| finalize.node_id)),
368-
parent_scope,
369-
finalize,
370-
finalize.is_some(),
371-
ignore_binding,
372-
None,
373-
)
374-
.ok()
375-
.map(LexicalScopeBinding::Item)
369+
370+
unreachable!()
376371
}
377372

378373
/// Resolve an identifier in lexical scope.

0 commit comments

Comments
 (0)