Skip to content

Commit 1652915

Browse files
committed
resolve: Restructure resolve_ident_in_lexical_scope for better clarity
1 parent e6c96c1 commit 1652915

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
@@ -318,7 +318,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
318318
let normalized_ident = Ident { span: normalized_span, ..ident };
319319

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

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

384379
/// Resolve an identifier in lexical scope.

0 commit comments

Comments
 (0)