Skip to content

Commit 621d870

Browse files
committed
resolve: Tweak top level logic and comments in resolve_ident_in_scope_set
1 parent 47edfd3 commit 621d870

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

compiler/rustc_resolve/src/ident.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
415415
ScopeSet::ExternPrelude => (TypeNS, None),
416416
ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind)),
417417
};
418+
let derive_fallback_lint_id = match finalize {
419+
Some(Finalize { node_id, stage: Stage::Late, .. }) => Some(node_id),
420+
_ => None,
421+
};
418422

419423
// This is *the* result, resolution from the scope closest to the resolved identifier.
420424
// However, sometimes this result is "weak" because it comes from a glob import or
@@ -433,16 +437,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
433437
let mut extern_prelude_flag_binding = None;
434438

435439
// Go through all the scopes and try to resolve the name.
436-
let derive_fallback_lint_id = match finalize {
437-
Some(Finalize { node_id, stage: Stage::Late, .. }) => Some(node_id),
438-
_ => None,
439-
};
440440
let break_result = self.visit_scopes(
441441
scope_set,
442442
parent_scope,
443443
orig_ident.span.ctxt(),
444444
derive_fallback_lint_id,
445445
|this, scope, use_prelude, ctxt| {
446+
// We can break with an error at this step, it means we cannot determine the
447+
// resolution right now, but we must block and wait until we can instead of
448+
// considering outer scopes.
446449
match this.reborrow().resolve_ident_in_scope(
447450
orig_ident,
448451
ns,
@@ -459,11 +462,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
459462
&mut extern_prelude_item_binding,
460463
&mut extern_prelude_flag_binding,
461464
)? {
462-
Ok((binding, flags)) => {
463-
if !sub_namespace_match(binding.macro_kinds(), macro_kind) {
464-
return ControlFlow::Continue(());
465-
}
466-
465+
Ok((binding, flags))
466+
if sub_namespace_match(binding.macro_kinds(), macro_kind) =>
467+
{
467468
// Below we report various ambiguity errors.
468469
// We do not need to report them if we are either in speculative resolution,
469470
// or in late resolution when everything is already imported and expanded
@@ -484,31 +485,32 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
484485
extern_prelude_item_binding,
485486
extern_prelude_flag_binding,
486487
) {
488+
// No need to search for more potential ambiguities, one is enough.
487489
return ControlFlow::Break(Ok(innermost_binding));
488490
}
489491
} else {
490492
// Found the first solution.
491493
innermost_result = Some((binding, flags));
492494
}
493495
}
494-
Err(Determinacy::Determined) => {}
496+
Ok(_) | Err(Determinacy::Determined) => {}
495497
Err(Determinacy::Undetermined) => determinacy = Determinacy::Undetermined,
496498
}
497499

498500
ControlFlow::Continue(())
499501
},
500502
);
501503

504+
// Scope visiting returned some result early.
502505
if let Some(break_result) = break_result {
503506
return break_result;
504507
}
505508

506-
// The first found solution was the only one, return it.
507-
if let Some((binding, _)) = innermost_result {
508-
return Ok(binding);
509+
// Scope visiting walked all the scopes and maybe found something in one of them.
510+
match innermost_result {
511+
Some((binding, _)) => Ok(binding),
512+
None => Err(Determinacy::determined(determinacy == Determinacy::Determined || force)),
509513
}
510-
511-
Err(Determinacy::determined(determinacy == Determinacy::Determined || force))
512514
}
513515

514516
fn resolve_ident_in_scope<'r>(

0 commit comments

Comments
 (0)