Skip to content

Commit c88cbeb

Browse files
committed
resolve: Restructure resolve_ident_in_lexical_scope for better clarity
1 parent f74c11d commit c88cbeb

File tree

1 file changed

+36
-41
lines changed

1 file changed

+36
-41
lines changed

compiler/rustc_resolve/src/ident.rs

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
307307
let normalized_ident = Ident { span: normalized_span, ..ident };
308308

309309
// Walk backwards up the ribs in scope.
310-
let mut module = self.graph_root;
311310
for (i, rib) in ribs.iter().enumerate().rev() {
312311
debug!("walk rib\n{:?}", rib.bindings);
313312
// Use the rib kind to determine whether we are resolving parameters
@@ -323,50 +322,46 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
323322
*original_rib_ident_def,
324323
ribs,
325324
)));
325+
} else if let RibKind::Block(Some(module)) = rib.kind
326+
&& let Ok(binding) = self.resolve_ident_in_module_unadjusted(
327+
ModuleOrUniformRoot::Module(module),
328+
ident,
329+
ns,
330+
parent_scope,
331+
Shadowing::Unrestricted,
332+
finalize.map(|finalize| Finalize { used: Used::Scope, ..finalize }),
333+
ignore_binding,
334+
None,
335+
)
336+
{
337+
// The ident resolves to an item in a block.
338+
return Some(LexicalScopeBinding::Item(binding));
339+
} else if let RibKind::Module(module) = rib.kind {
340+
// Encountered a module item, abandon ribs and look into that module and preludes.
341+
return self
342+
.early_resolve_ident_in_lexical_scope(
343+
orig_ident,
344+
ScopeSet::Late(ns, module, finalize.map(|finalize| finalize.node_id)),
345+
parent_scope,
346+
finalize,
347+
finalize.is_some(),
348+
ignore_binding,
349+
None,
350+
)
351+
.ok()
352+
.map(LexicalScopeBinding::Item);
326353
}
327354

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

372367
/// Resolve an identifier in lexical scope.

0 commit comments

Comments
 (0)