1 use rustc_ast::{self as ast, NodeId};
2 use rustc_feature::is_builtin_attr_name;
3 use rustc_hir::def::{DefKind, Namespace, NonMacroAttrKind, PartialRes, PerNS};
7 use rustc_session::lint::builtin::PROC_MACRO_DERIVE_RESOLUTION_FALLBACK;
8 use rustc_session::lint::BuiltinLintDiagnostics;
9 use rustc_span::edition::Edition;
10 use rustc_span::hygiene::{ExpnId, ExpnKind, LocalExpnId, MacroKind, SyntaxContext};
11 use rustc_span::symbol::{kw, Ident};
12 use rustc_span::{Span, DUMMY_SP};
16 use crate::late::{ConstantItemKind, HasGenericParams, PathSource, Rib, RibKind};
17 use crate::macros::{sub_namespace_match, MacroRulesScope};
18 use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, Determinacy, Finalize};
19 use crate::{ImportKind, LexicalScopeBinding, Module, ModuleKind, ModuleOrUniformRoot};
20 use crate::{NameBinding, NameBindingKind, ParentScope, PathResult, PrivacyError, Res};
21 use crate::{ResolutionError, Resolver, Scope, ScopeSet, Segment, ToNameBinding, Weak};
27 impl<'a> Resolver<'a> {
28 /// A generic scope visitor.
29 /// Visits scopes in order to resolve some identifier in them or perform other actions.
30 /// If the callback returns `Some` result, we stop visiting scopes and return it.
31 crate fn visit_scopes<T>(
33 scope_set: ScopeSet<'a>,
34 parent_scope: &ParentScope<'a>,
36 mut visitor: impl FnMut(
43 // General principles:
44 // 1. Not controlled (user-defined) names should have higher priority than controlled names
45 // built into the language or standard library. This way we can add new names into the
46 // language or standard library without breaking user code.
47 // 2. "Closed set" below means new names cannot appear after the current resolution attempt.
48 // Places to search (in order of decreasing priority):
50 // 1. FIXME: Ribs (type parameters), there's no necessary infrastructure yet
51 // (open set, not controlled).
52 // 2. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
53 // (open, not controlled).
54 // 3. Extern prelude (open, the open part is from macro expansions, not controlled).
55 // 4. Tool modules (closed, controlled right now, but not in the future).
56 // 5. Standard library prelude (de-facto closed, controlled).
57 // 6. Language prelude (closed, controlled).
59 // 1. FIXME: Ribs (local variables), there's no necessary infrastructure yet
60 // (open set, not controlled).
61 // 2. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
62 // (open, not controlled).
63 // 3. Standard library prelude (de-facto closed, controlled).
65 // 1-3. Derive helpers (open, not controlled). All ambiguities with other names
66 // are currently reported as errors. They should be higher in priority than preludes
67 // and probably even names in modules according to the "general principles" above. They
68 // also should be subject to restricted shadowing because are effectively produced by
69 // derives (you need to resolve the derive first to add helpers into scope), but they
70 // should be available before the derive is expanded for compatibility.
71 // It's mess in general, so we are being conservative for now.
72 // 1-3. `macro_rules` (open, not controlled), loop through `macro_rules` scopes. Have higher
73 // priority than prelude macros, but create ambiguities with macros in modules.
74 // 1-3. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
75 // (open, not controlled). Have higher priority than prelude macros, but create
76 // ambiguities with `macro_rules`.
77 // 4. `macro_use` prelude (open, the open part is from macro expansions, not controlled).
78 // 4a. User-defined prelude from macro-use
79 // (open, the open part is from macro expansions, not controlled).
80 // 4b. "Standard library prelude" part implemented through `macro-use` (closed, controlled).
81 // 4c. Standard library prelude (de-facto closed, controlled).
82 // 6. Language prelude: builtin attributes (closed, controlled).
84 let rust_2015 = ctxt.edition() == Edition::Edition2015;
85 let (ns, macro_kind, is_absolute_path) = match scope_set {
86 ScopeSet::All(ns, _) => (ns, None, false),
87 ScopeSet::AbsolutePath(ns) => (ns, None, true),
88 ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind), false),
89 ScopeSet::Late(ns, ..) => (ns, None, false),
91 let module = match scope_set {
92 // Start with the specified module.
93 ScopeSet::Late(_, module, _) => module,
94 // Jump out of trait or enum modules, they do not act as scopes.
95 _ => parent_scope.module.nearest_item_scope(),
97 let mut scope = match ns {
98 _ if is_absolute_path => Scope::CrateRoot,
99 TypeNS | ValueNS => Scope::Module(module, None),
100 MacroNS => Scope::DeriveHelpers(parent_scope.expansion),
102 let mut ctxt = ctxt.normalize_to_macros_2_0();
103 let mut use_prelude = !module.no_implicit_prelude;
106 let visit = match scope {
107 // Derive helpers are not in scope when resolving derives in the same container.
108 Scope::DeriveHelpers(expn_id) => {
109 !(expn_id == parent_scope.expansion && macro_kind == Some(MacroKind::Derive))
111 Scope::DeriveHelpersCompat => true,
112 Scope::MacroRules(macro_rules_scope) => {
113 // Use "path compression" on `macro_rules` scope chains. This is an optimization
114 // used to avoid long scope chains, see the comments on `MacroRulesScopeRef`.
115 // As another consequence of this optimization visitors never observe invocation
116 // scopes for macros that were already expanded.
117 while let MacroRulesScope::Invocation(invoc_id) = macro_rules_scope.get() {
118 if let Some(next_scope) = self.output_macro_rules_scopes.get(&invoc_id) {
119 macro_rules_scope.set(next_scope.get());
126 Scope::CrateRoot => true,
127 Scope::Module(..) => true,
128 Scope::RegisteredAttrs => use_prelude,
129 Scope::MacroUsePrelude => use_prelude || rust_2015,
130 Scope::BuiltinAttrs => true,
131 Scope::ExternPrelude => use_prelude || is_absolute_path,
132 Scope::ToolPrelude => use_prelude,
133 Scope::StdLibPrelude => use_prelude || ns == MacroNS,
134 Scope::BuiltinTypes => true,
138 if let break_result @ Some(..) = visitor(self, scope, use_prelude, ctxt) {
143 scope = match scope {
144 Scope::DeriveHelpers(LocalExpnId::ROOT) => Scope::DeriveHelpersCompat,
145 Scope::DeriveHelpers(expn_id) => {
146 // Derive helpers are not visible to code generated by bang or derive macros.
147 let expn_data = expn_id.expn_data();
148 match expn_data.kind {
150 | ExpnKind::Macro(MacroKind::Bang | MacroKind::Derive, _) => {
151 Scope::DeriveHelpersCompat
153 _ => Scope::DeriveHelpers(expn_data.parent.expect_local()),
156 Scope::DeriveHelpersCompat => Scope::MacroRules(parent_scope.macro_rules),
157 Scope::MacroRules(macro_rules_scope) => match macro_rules_scope.get() {
158 MacroRulesScope::Binding(binding) => {
159 Scope::MacroRules(binding.parent_macro_rules_scope)
161 MacroRulesScope::Invocation(invoc_id) => {
162 Scope::MacroRules(self.invocation_parent_scopes[&invoc_id].macro_rules)
164 MacroRulesScope::Empty => Scope::Module(module, None),
166 Scope::CrateRoot => match ns {
168 ctxt.adjust(ExpnId::root());
171 ValueNS | MacroNS => break,
173 Scope::Module(module, prev_lint_id) => {
174 use_prelude = !module.no_implicit_prelude;
175 let derive_fallback_lint_id = match scope_set {
176 ScopeSet::Late(.., lint_id) => lint_id,
179 match self.hygienic_lexical_parent(module, &mut ctxt, derive_fallback_lint_id) {
180 Some((parent_module, lint_id)) => {
181 Scope::Module(parent_module, lint_id.or(prev_lint_id))
184 ctxt.adjust(ExpnId::root());
186 TypeNS => Scope::ExternPrelude,
187 ValueNS => Scope::StdLibPrelude,
188 MacroNS => Scope::RegisteredAttrs,
193 Scope::RegisteredAttrs => Scope::MacroUsePrelude,
194 Scope::MacroUsePrelude => Scope::StdLibPrelude,
195 Scope::BuiltinAttrs => break, // nowhere else to search
196 Scope::ExternPrelude if is_absolute_path => break,
197 Scope::ExternPrelude => Scope::ToolPrelude,
198 Scope::ToolPrelude => Scope::StdLibPrelude,
199 Scope::StdLibPrelude => match ns {
200 TypeNS => Scope::BuiltinTypes,
201 ValueNS => break, // nowhere else to search
202 MacroNS => Scope::BuiltinAttrs,
204 Scope::BuiltinTypes => break, // nowhere else to search
211 fn hygienic_lexical_parent(
214 ctxt: &mut SyntaxContext,
215 derive_fallback_lint_id: Option<NodeId>,
216 ) -> Option<(Module<'a>, Option<NodeId>)> {
217 if !module.expansion.outer_expn_is_descendant_of(*ctxt) {
218 return Some((self.expn_def_scope(ctxt.remove_mark()), None));
221 if let ModuleKind::Block(..) = module.kind {
222 return Some((module.parent.unwrap().nearest_item_scope(), None));
225 // We need to support the next case under a deprecation warning
228 // ---- begin: this comes from a proc macro derive
229 // mod implementation_details {
230 // // Note that `MyStruct` is not in scope here.
231 // impl SomeTrait for MyStruct { ... }
235 // So we have to fall back to the module's parent during lexical resolution in this case.
236 if derive_fallback_lint_id.is_some() {
237 if let Some(parent) = module.parent {
238 // Inner module is inside the macro, parent module is outside of the macro.
239 if module.expansion != parent.expansion
240 && module.expansion.is_descendant_of(parent.expansion)
242 // The macro is a proc macro derive
243 if let Some(def_id) = module.expansion.expn_data().macro_def_id {
244 let ext = self.get_macro_by_def_id(def_id);
245 if ext.builtin_name.is_none()
246 && ext.macro_kind() == MacroKind::Derive
247 && parent.expansion.outer_expn_is_descendant_of(*ctxt)
249 return Some((parent, derive_fallback_lint_id));
259 /// This resolves the identifier `ident` in the namespace `ns` in the current lexical scope.
260 /// More specifically, we proceed up the hierarchy of scopes and return the binding for
261 /// `ident` in the first scope that defines it (or None if no scopes define it).
263 /// A block's items are above its local variables in the scope hierarchy, regardless of where
264 /// the items are defined in the block. For example,
267 /// g(); // Since there are no local variables in scope yet, this resolves to the item.
270 /// g(); // This resolves to the local variable `g` since it shadows the item.
274 /// Invariant: This must only be called during main resolution, not during
275 /// import resolution.
276 #[tracing::instrument(level = "debug", skip(self, ribs))]
277 crate fn resolve_ident_in_lexical_scope(
281 parent_scope: &ParentScope<'a>,
282 finalize_full: Finalize,
284 unusable_binding: Option<&'a NameBinding<'a>>,
285 ) -> Option<LexicalScopeBinding<'a>> {
286 assert!(ns == TypeNS || ns == ValueNS);
287 let orig_ident = ident;
288 if ident.name == kw::Empty {
289 return Some(LexicalScopeBinding::Res(Res::Err));
291 let (general_span, normalized_span) = if ident.name == kw::SelfUpper {
292 // FIXME(jseyfried) improve `Self` hygiene
293 let empty_span = ident.span.with_ctxt(SyntaxContext::root());
294 (empty_span, empty_span)
295 } else if ns == TypeNS {
296 let normalized_span = ident.span.normalize_to_macros_2_0();
297 (normalized_span, normalized_span)
299 (ident.span.normalize_to_macro_rules(), ident.span.normalize_to_macros_2_0())
301 ident.span = general_span;
302 let normalized_ident = Ident { span: normalized_span, ..ident };
304 // Walk backwards up the ribs in scope.
305 let finalize = finalize_full.path_span();
306 let mut module = self.graph_root;
307 for i in (0..ribs.len()).rev() {
308 debug!("walk rib\n{:?}", ribs[i].bindings);
309 // Use the rib kind to determine whether we are resolving parameters
310 // (macro 2.0 hygiene) or local variables (`macro_rules` hygiene).
311 let rib_ident = if ribs[i].kind.contains_params() { normalized_ident } else { ident };
312 if let Some((original_rib_ident_def, res)) = ribs[i].bindings.get_key_value(&rib_ident)
314 // The ident resolves to a type parameter or local variable.
315 return Some(LexicalScopeBinding::Res(self.validate_res_from_ribs(
320 *original_rib_ident_def,
325 module = match ribs[i].kind {
326 ModuleRibKind(module) => module,
327 MacroDefinition(def) if def == self.macro_def(ident.span.ctxt()) => {
328 // If an invocation of this macro created `ident`, give up on `ident`
329 // and switch to `ident`'s source from the macro definition.
330 ident.span.remove_mark();
337 ModuleKind::Block(..) => {} // We can see through blocks
341 let item = self.resolve_ident_in_module_unadjusted(
342 ModuleOrUniformRoot::Module(module),
350 if let Ok(binding) = item {
351 // The ident resolves to an item.
352 return Some(LexicalScopeBinding::Item(binding));
355 self.early_resolve_ident_in_lexical_scope(
357 ScopeSet::Late(ns, module, finalize_full.node_id()),
365 .map(LexicalScopeBinding::Item)
368 /// Resolve an identifier in lexical scope.
369 /// This is a variation of `fn resolve_ident_in_lexical_scope` that can be run during
370 /// expansion and import resolution (perhaps they can be merged in the future).
371 /// The function is used for resolving initial segments of macro paths (e.g., `foo` in
372 /// `foo::bar!(); or `foo!();`) and also for import paths on 2018 edition.
373 #[tracing::instrument(level = "debug", skip(self, scope_set))]
374 crate fn early_resolve_ident_in_lexical_scope(
377 scope_set: ScopeSet<'a>,
378 parent_scope: &ParentScope<'a>,
379 finalize: Option<Span>,
381 last_import_segment: bool,
382 unusable_binding: Option<&'a NameBinding<'a>>,
383 ) -> Result<&'a NameBinding<'a>, Determinacy> {
384 bitflags::bitflags! {
386 const MACRO_RULES = 1 << 0;
387 const MODULE = 1 << 1;
388 const MISC_SUGGEST_CRATE = 1 << 2;
389 const MISC_SUGGEST_SELF = 1 << 3;
390 const MISC_FROM_PRELUDE = 1 << 4;
394 assert!(force || !finalize.is_some()); // `finalize` implies `force`
396 // Make sure `self`, `super` etc produce an error when passed to here.
397 if orig_ident.is_path_segment_keyword() {
398 return Err(Determinacy::Determined);
401 let (ns, macro_kind, is_import) = match scope_set {
402 ScopeSet::All(ns, is_import) => (ns, None, is_import),
403 ScopeSet::AbsolutePath(ns) => (ns, None, false),
404 ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind), false),
405 ScopeSet::Late(ns, ..) => (ns, None, false),
408 // This is *the* result, resolution from the scope closest to the resolved identifier.
409 // However, sometimes this result is "weak" because it comes from a glob import or
410 // a macro expansion, and in this case it cannot shadow names from outer scopes, e.g.
411 // mod m { ... } // solution in outer scope
413 // use prefix::*; // imports another `m` - innermost solution
414 // // weak, cannot shadow the outer `m`, need to report ambiguity error
417 // So we have to save the innermost solution and continue searching in outer scopes
418 // to detect potential ambiguities.
419 let mut innermost_result: Option<(&NameBinding<'_>, Flags)> = None;
420 let mut determinacy = Determinacy::Determined;
422 // Go through all the scopes and try to resolve the name.
423 let break_result = self.visit_scopes(
426 orig_ident.span.ctxt(),
427 |this, scope, use_prelude, ctxt| {
428 let ident = Ident::new(orig_ident.name, orig_ident.span.with_ctxt(ctxt));
429 let ok = |res, span, arenas| {
431 (res, ty::Visibility::Public, span, LocalExpnId::ROOT)
432 .to_name_binding(arenas),
436 let result = match scope {
437 Scope::DeriveHelpers(expn_id) => {
438 if let Some(attr) = this
441 .and_then(|attrs| attrs.iter().rfind(|i| ident == **i))
444 Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper),
445 ty::Visibility::Public,
449 .to_name_binding(this.arenas);
450 Ok((binding, Flags::empty()))
452 Err(Determinacy::Determined)
455 Scope::DeriveHelpersCompat => {
456 let mut result = Err(Determinacy::Determined);
457 for derive in parent_scope.derives {
458 let parent_scope = &ParentScope { derives: &[], ..*parent_scope };
459 match this.resolve_macro_path(
461 Some(MacroKind::Derive),
466 Ok((Some(ext), _)) => {
467 if ext.helper_attrs.contains(&ident.name) {
469 Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat),
476 Ok(_) | Err(Determinacy::Determined) => {}
477 Err(Determinacy::Undetermined) => {
478 result = Err(Determinacy::Undetermined)
484 Scope::MacroRules(macro_rules_scope) => match macro_rules_scope.get() {
485 MacroRulesScope::Binding(macro_rules_binding)
486 if ident == macro_rules_binding.ident =>
488 Ok((macro_rules_binding.binding, Flags::MACRO_RULES))
490 MacroRulesScope::Invocation(_) => Err(Determinacy::Undetermined),
491 _ => Err(Determinacy::Determined),
493 Scope::CrateRoot => {
494 let root_ident = Ident::new(kw::PathRoot, ident.span);
495 let root_module = this.resolve_crate_root(root_ident);
496 let binding = this.resolve_ident_in_module_ext(
497 ModuleOrUniformRoot::Module(root_module),
506 Ok(binding) => Ok((binding, Flags::MODULE | Flags::MISC_SUGGEST_CRATE)),
507 Err((Determinacy::Undetermined, Weak::No)) => {
508 return Some(Err(Determinacy::determined(force)));
510 Err((Determinacy::Undetermined, Weak::Yes)) => {
511 Err(Determinacy::Undetermined)
513 Err((Determinacy::Determined, _)) => Err(Determinacy::Determined),
516 Scope::Module(module, derive_fallback_lint_id) => {
517 let adjusted_parent_scope = &ParentScope { module, ..*parent_scope };
518 let binding = this.resolve_ident_in_module_unadjusted_ext(
519 ModuleOrUniformRoot::Module(module),
522 adjusted_parent_scope,
523 !matches!(scope_set, ScopeSet::Late(..)),
530 if let Some(lint_id) = derive_fallback_lint_id {
531 this.lint_buffer.buffer_lint_with_diagnostic(
532 PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
536 "cannot find {} `{}` in this scope",
540 BuiltinLintDiagnostics::ProcMacroDeriveResolutionFallback(
545 let misc_flags = if ptr::eq(module, this.graph_root) {
546 Flags::MISC_SUGGEST_CRATE
547 } else if module.is_normal() {
548 Flags::MISC_SUGGEST_SELF
552 Ok((binding, Flags::MODULE | misc_flags))
554 Err((Determinacy::Undetermined, Weak::No)) => {
555 return Some(Err(Determinacy::determined(force)));
557 Err((Determinacy::Undetermined, Weak::Yes)) => {
558 Err(Determinacy::Undetermined)
560 Err((Determinacy::Determined, _)) => Err(Determinacy::Determined),
563 Scope::RegisteredAttrs => match this.registered_attrs.get(&ident).cloned() {
565 Res::NonMacroAttr(NonMacroAttrKind::Registered),
569 None => Err(Determinacy::Determined),
571 Scope::MacroUsePrelude => {
572 match this.macro_use_prelude.get(&ident.name).cloned() {
573 Some(binding) => Ok((binding, Flags::MISC_FROM_PRELUDE)),
574 None => Err(Determinacy::determined(
575 this.graph_root.unexpanded_invocations.borrow().is_empty(),
579 Scope::BuiltinAttrs => {
580 if is_builtin_attr_name(ident.name) {
582 Res::NonMacroAttr(NonMacroAttrKind::Builtin(ident.name)),
587 Err(Determinacy::Determined)
590 Scope::ExternPrelude => {
591 match this.extern_prelude_get(ident, finalize.is_some()) {
592 Some(binding) => Ok((binding, Flags::empty())),
593 None => Err(Determinacy::determined(
594 this.graph_root.unexpanded_invocations.borrow().is_empty(),
598 Scope::ToolPrelude => match this.registered_tools.get(&ident).cloned() {
599 Some(ident) => ok(Res::ToolMod, ident.span, this.arenas),
600 None => Err(Determinacy::Determined),
602 Scope::StdLibPrelude => {
603 let mut result = Err(Determinacy::Determined);
604 if let Some(prelude) = this.prelude {
605 if let Ok(binding) = this.resolve_ident_in_module_unadjusted(
606 ModuleOrUniformRoot::Module(prelude),
614 if use_prelude || this.is_builtin_macro(binding.res()) {
615 result = Ok((binding, Flags::MISC_FROM_PRELUDE));
621 Scope::BuiltinTypes => match PrimTy::from_name(ident.name) {
622 Some(prim_ty) => ok(Res::PrimTy(prim_ty), DUMMY_SP, this.arenas),
623 None => Err(Determinacy::Determined),
629 if sub_namespace_match(binding.macro_kind(), macro_kind) =>
631 if finalize.is_none() || matches!(scope_set, ScopeSet::Late(..)) {
632 return Some(Ok(binding));
635 if let Some((innermost_binding, innermost_flags)) = innermost_result {
636 // Found another solution, if the first one was "weak", report an error.
637 let (res, innermost_res) = (binding.res(), innermost_binding.res());
638 if res != innermost_res {
639 let is_builtin = |res| {
640 matches!(res, Res::NonMacroAttr(NonMacroAttrKind::Builtin(..)))
643 Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper);
644 let derive_helper_compat =
645 Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat);
647 let ambiguity_error_kind = if is_import {
648 Some(AmbiguityKind::Import)
649 } else if is_builtin(innermost_res) || is_builtin(res) {
650 Some(AmbiguityKind::BuiltinAttr)
651 } else if innermost_res == derive_helper_compat
652 || res == derive_helper_compat && innermost_res != derive_helper
654 Some(AmbiguityKind::DeriveHelper)
655 } else if innermost_flags.contains(Flags::MACRO_RULES)
656 && flags.contains(Flags::MODULE)
657 && !this.disambiguate_macro_rules_vs_modularized(
661 || flags.contains(Flags::MACRO_RULES)
662 && innermost_flags.contains(Flags::MODULE)
663 && !this.disambiguate_macro_rules_vs_modularized(
668 Some(AmbiguityKind::MacroRulesVsModularized)
669 } else if innermost_binding.is_glob_import() {
670 Some(AmbiguityKind::GlobVsOuter)
671 } else if innermost_binding
672 .may_appear_after(parent_scope.expansion, binding)
674 Some(AmbiguityKind::MoreExpandedVsOuter)
678 if let Some(kind) = ambiguity_error_kind {
679 let misc = |f: Flags| {
680 if f.contains(Flags::MISC_SUGGEST_CRATE) {
681 AmbiguityErrorMisc::SuggestCrate
682 } else if f.contains(Flags::MISC_SUGGEST_SELF) {
683 AmbiguityErrorMisc::SuggestSelf
684 } else if f.contains(Flags::MISC_FROM_PRELUDE) {
685 AmbiguityErrorMisc::FromPrelude
687 AmbiguityErrorMisc::None
690 this.ambiguity_errors.push(AmbiguityError {
693 b1: innermost_binding,
695 misc1: misc(innermost_flags),
698 return Some(Ok(innermost_binding));
702 // Found the first solution.
703 innermost_result = Some((binding, flags));
706 Ok(..) | Err(Determinacy::Determined) => {}
707 Err(Determinacy::Undetermined) => determinacy = Determinacy::Undetermined,
714 if let Some(break_result) = break_result {
718 // The first found solution was the only one, return it.
719 if let Some((binding, _)) = innermost_result {
723 Err(Determinacy::determined(determinacy == Determinacy::Determined || force))
726 #[tracing::instrument(level = "debug", skip(self))]
727 crate fn maybe_resolve_ident_in_module(
729 module: ModuleOrUniformRoot<'a>,
732 parent_scope: &ParentScope<'a>,
733 ) -> Result<&'a NameBinding<'a>, Determinacy> {
734 self.resolve_ident_in_module_ext(module, ident, ns, parent_scope, None, false, None)
735 .map_err(|(determinacy, _)| determinacy)
738 #[tracing::instrument(level = "debug", skip(self))]
739 crate fn resolve_ident_in_module(
741 module: ModuleOrUniformRoot<'a>,
744 parent_scope: &ParentScope<'a>,
745 finalize: Option<Span>,
746 // We are resolving a last import segment during import validation.
747 last_import_segment: bool,
748 // This binding should be ignored during in-module resolution, so that we don't get
749 // "self-confirming" import resolutions during import validation.
750 unusable_binding: Option<&'a NameBinding<'a>>,
751 ) -> Result<&'a NameBinding<'a>, Determinacy> {
752 self.resolve_ident_in_module_ext(
761 .map_err(|(determinacy, _)| determinacy)
764 #[tracing::instrument(level = "debug", skip(self))]
765 fn resolve_ident_in_module_ext(
767 module: ModuleOrUniformRoot<'a>,
770 parent_scope: &ParentScope<'a>,
771 finalize: Option<Span>,
772 last_import_segment: bool,
773 unusable_binding: Option<&'a NameBinding<'a>>,
774 ) -> Result<&'a NameBinding<'a>, (Determinacy, Weak)> {
775 let tmp_parent_scope;
776 let mut adjusted_parent_scope = parent_scope;
778 ModuleOrUniformRoot::Module(m) => {
779 if let Some(def) = ident.span.normalize_to_macros_2_0_and_adjust(m.expansion) {
781 ParentScope { module: self.expn_def_scope(def), ..*parent_scope };
782 adjusted_parent_scope = &tmp_parent_scope;
785 ModuleOrUniformRoot::ExternPrelude => {
786 ident.span.normalize_to_macros_2_0_and_adjust(ExpnId::root());
788 ModuleOrUniformRoot::CrateRootAndExternPrelude | ModuleOrUniformRoot::CurrentScope => {
792 self.resolve_ident_in_module_unadjusted_ext(
796 adjusted_parent_scope,
804 #[tracing::instrument(level = "debug", skip(self))]
805 fn resolve_ident_in_module_unadjusted(
807 module: ModuleOrUniformRoot<'a>,
810 parent_scope: &ParentScope<'a>,
811 finalize: Option<Span>,
812 last_import_segment: bool,
813 unusable_binding: Option<&'a NameBinding<'a>>,
814 ) -> Result<&'a NameBinding<'a>, Determinacy> {
815 self.resolve_ident_in_module_unadjusted_ext(
825 .map_err(|(determinacy, _)| determinacy)
828 /// Attempts to resolve `ident` in namespaces `ns` of `module`.
829 /// Invariant: if `finalize` is `Some`, expansion and import resolution must be complete.
830 #[tracing::instrument(level = "debug", skip(self))]
831 fn resolve_ident_in_module_unadjusted_ext(
833 module: ModuleOrUniformRoot<'a>,
836 parent_scope: &ParentScope<'a>,
837 restricted_shadowing: bool,
838 finalize: Option<Span>,
839 last_import_segment: bool,
840 unusable_binding: Option<&'a NameBinding<'a>>,
841 ) -> Result<&'a NameBinding<'a>, (Determinacy, Weak)> {
842 let module = match module {
843 ModuleOrUniformRoot::Module(module) => module,
844 ModuleOrUniformRoot::CrateRootAndExternPrelude => {
845 assert!(!restricted_shadowing);
846 let binding = self.early_resolve_ident_in_lexical_scope(
848 ScopeSet::AbsolutePath(ns),
855 return binding.map_err(|determinacy| (determinacy, Weak::No));
857 ModuleOrUniformRoot::ExternPrelude => {
858 assert!(!restricted_shadowing);
859 return if ns != TypeNS {
860 Err((Determined, Weak::No))
861 } else if let Some(binding) = self.extern_prelude_get(ident, finalize.is_some()) {
863 } else if !self.graph_root.unexpanded_invocations.borrow().is_empty() {
864 // Macro-expanded `extern crate` items can add names to extern prelude.
865 Err((Undetermined, Weak::No))
867 Err((Determined, Weak::No))
870 ModuleOrUniformRoot::CurrentScope => {
871 assert!(!restricted_shadowing);
873 if ident.name == kw::Crate || ident.name == kw::DollarCrate {
874 let module = self.resolve_crate_root(ident);
876 (module, ty::Visibility::Public, module.span, LocalExpnId::ROOT)
877 .to_name_binding(self.arenas);
879 } else if ident.name == kw::Super || ident.name == kw::SelfLower {
880 // FIXME: Implement these with renaming requirements so that e.g.
881 // `use super;` doesn't work, but `use super as name;` does.
882 // Fall through here to get an error from `early_resolve_...`.
886 let scopes = ScopeSet::All(ns, true);
887 let binding = self.early_resolve_ident_in_lexical_scope(
896 return binding.map_err(|determinacy| (determinacy, Weak::No));
900 let key = self.new_key(ident, ns);
902 self.resolution(module, key).try_borrow_mut().map_err(|_| (Determined, Weak::No))?; // This happens when there is a cycle of imports.
904 if let Some(path_span) = finalize {
905 // If the primary binding is unusable, search further and return the shadowed glob
906 // binding if it exists. What we really want here is having two separate scopes in
907 // a module - one for non-globs and one for globs, but until that's done use this
908 // hack to avoid inconsistent resolution ICEs during import validation.
909 let binding = [resolution.binding, resolution.shadowed_glob]
911 .filter_map(|binding| match (binding, unusable_binding) {
912 (Some(binding), Some(unusable_binding))
913 if ptr::eq(binding, unusable_binding) =>
920 let Some(binding) = binding else {
921 return Err((Determined, Weak::No));
924 if !self.is_accessible_from(binding.vis, parent_scope.module) {
925 if last_import_segment {
926 return Err((Determined, Weak::No));
928 self.privacy_errors.push(PrivacyError {
931 dedup_span: path_span,
936 // Forbid expanded shadowing to avoid time travel.
937 if let Some(shadowed_glob) = resolution.shadowed_glob
938 && restricted_shadowing
939 && binding.expansion != LocalExpnId::ROOT
940 && binding.res() != shadowed_glob.res()
942 self.ambiguity_errors.push(AmbiguityError {
943 kind: AmbiguityKind::GlobVsExpanded,
947 misc1: AmbiguityErrorMisc::None,
948 misc2: AmbiguityErrorMisc::None,
952 if !restricted_shadowing && binding.expansion != LocalExpnId::ROOT {
953 if let NameBindingKind::Res(_, true) = binding.kind {
954 self.macro_expanded_macro_export_errors.insert((path_span, binding.span));
958 self.record_use(ident, binding, restricted_shadowing);
962 let check_usable = |this: &mut Self, binding: &'a NameBinding<'a>| {
963 if let Some(unusable_binding) = unusable_binding {
964 if ptr::eq(binding, unusable_binding) {
965 return Err((Determined, Weak::No));
968 let usable = this.is_accessible_from(binding.vis, parent_scope.module);
969 if usable { Ok(binding) } else { Err((Determined, Weak::No)) }
972 // Items and single imports are not shadowable, if we have one, then it's determined.
973 if let Some(binding) = resolution.binding {
974 if !binding.is_glob_import() {
975 return check_usable(self, binding);
979 // --- From now on we either have a glob resolution or no resolution. ---
981 // Check if one of single imports can still define the name,
982 // if it can then our result is not determined and can be invalidated.
983 for single_import in &resolution.single_imports {
984 if !self.is_accessible_from(single_import.vis.get(), parent_scope.module) {
987 let Some(module) = single_import.imported_module.get() else {
988 return Err((Undetermined, Weak::No));
990 let ImportKind::Single { source: ident, .. } = single_import.kind else {
993 match self.resolve_ident_in_module(
997 &single_import.parent_scope,
1002 Err(Determined) => continue,
1004 if !self.is_accessible_from(binding.vis, single_import.parent_scope.module) =>
1008 Ok(_) | Err(Undetermined) => return Err((Undetermined, Weak::No)),
1012 // So we have a resolution that's from a glob import. This resolution is determined
1013 // if it cannot be shadowed by some new item/import expanded from a macro.
1014 // This happens either if there are no unexpanded macros, or expanded names cannot
1015 // shadow globs (that happens in macro namespace or with restricted shadowing).
1017 // Additionally, any macro in any module can plant names in the root module if it creates
1018 // `macro_export` macros, so the root module effectively has unresolved invocations if any
1019 // module has unresolved invocations.
1020 // However, it causes resolution/expansion to stuck too often (#53144), so, to make
1021 // progress, we have to ignore those potential unresolved invocations from other modules
1022 // and prohibit access to macro-expanded `macro_export` macros instead (unless restricted
1023 // shadowing is enabled, see `macro_expanded_macro_export_errors`).
1024 let unexpanded_macros = !module.unexpanded_invocations.borrow().is_empty();
1025 if let Some(binding) = resolution.binding {
1026 if !unexpanded_macros || ns == MacroNS || restricted_shadowing {
1027 return check_usable(self, binding);
1029 return Err((Undetermined, Weak::No));
1033 // --- From now on we have no resolution. ---
1035 // Now we are in situation when new item/import can appear only from a glob or a macro
1036 // expansion. With restricted shadowing names from globs and macro expansions cannot
1037 // shadow names from outer scopes, so we can freely fallback from module search to search
1038 // in outer scopes. For `early_resolve_ident_in_lexical_scope` to continue search in outer
1039 // scopes we return `Undetermined` with `Weak::Yes`.
1041 // Check if one of unexpanded macros can still define the name,
1042 // if it can then our "no resolution" result is not determined and can be invalidated.
1043 if unexpanded_macros {
1044 return Err((Undetermined, Weak::Yes));
1047 // Check if one of glob imports can still define the name,
1048 // if it can then our "no resolution" result is not determined and can be invalidated.
1049 for glob_import in module.globs.borrow().iter() {
1050 if !self.is_accessible_from(glob_import.vis.get(), parent_scope.module) {
1053 let module = match glob_import.imported_module.get() {
1054 Some(ModuleOrUniformRoot::Module(module)) => module,
1055 Some(_) => continue,
1056 None => return Err((Undetermined, Weak::Yes)),
1058 let tmp_parent_scope;
1059 let (mut adjusted_parent_scope, mut ident) =
1060 (parent_scope, ident.normalize_to_macros_2_0());
1061 match ident.span.glob_adjust(module.expansion, glob_import.span) {
1062 Some(Some(def)) => {
1064 ParentScope { module: self.expn_def_scope(def), ..*parent_scope };
1065 adjusted_parent_scope = &tmp_parent_scope;
1070 let result = self.resolve_ident_in_module_unadjusted(
1071 ModuleOrUniformRoot::Module(module),
1074 adjusted_parent_scope,
1076 last_import_segment,
1081 Err(Determined) => continue,
1083 if !self.is_accessible_from(binding.vis, glob_import.parent_scope.module) =>
1087 Ok(_) | Err(Undetermined) => return Err((Undetermined, Weak::Yes)),
1091 // No resolution and no one else can define the name - determinate error.
1092 Err((Determined, Weak::No))
1095 /// Validate a local resolution (from ribs).
1096 #[tracing::instrument(level = "debug", skip(self, all_ribs))]
1097 fn validate_res_from_ribs(
1102 finalize: Option<Span>,
1103 original_rib_ident_def: Ident,
1104 all_ribs: &[Rib<'a>],
1106 const CG_BUG_STR: &str = "min_const_generics resolve check didn't stop compilation";
1107 debug!("validate_res_from_ribs({:?})", res);
1108 let ribs = &all_ribs[rib_index + 1..];
1110 // An invalid forward use of a generic parameter from a previous default.
1111 if let ForwardGenericParamBanRibKind = all_ribs[rib_index].kind {
1112 if let Some(span) = finalize {
1113 let res_error = if rib_ident.name == kw::SelfUpper {
1114 ResolutionError::SelfInGenericParamDefault
1116 ResolutionError::ForwardDeclaredGenericParam
1118 self.report_error(span, res_error);
1120 assert_eq!(res, Res::Err);
1126 use ResolutionError::*;
1127 let mut res_err = None;
1132 | ClosureOrAsyncRibKind
1134 | MacroDefinition(..)
1135 | ForwardGenericParamBanRibKind => {
1136 // Nothing to do. Continue.
1138 ItemRibKind(_) | FnItemRibKind | AssocItemRibKind => {
1139 // This was an attempt to access an upvar inside a
1140 // named function item. This is not allowed, so we
1142 if let Some(span) = finalize {
1143 // We don't immediately trigger a resolve error, because
1144 // we want certain other resolution errors (namely those
1145 // emitted for `ConstantItemRibKind` below) to take
1147 res_err = Some((span, CannotCaptureDynamicEnvironmentInFnItem));
1150 ConstantItemRibKind(_, item) => {
1151 // Still doesn't deal with upvars
1152 if let Some(span) = finalize {
1153 let (span, resolution_error) =
1154 if let Some((ident, constant_item_kind)) = item {
1155 let kind_str = match constant_item_kind {
1156 ConstantItemKind::Const => "const",
1157 ConstantItemKind::Static => "static",
1161 AttemptToUseNonConstantValueInConstant(
1162 ident, "let", kind_str,
1168 AttemptToUseNonConstantValueInConstant(
1169 original_rib_ident_def,
1175 self.report_error(span, resolution_error);
1179 ConstParamTyRibKind => {
1180 if let Some(span) = finalize {
1181 self.report_error(span, ParamInTyOfConstParam(rib_ident.name));
1187 if let Some((span, res_err)) = res_err {
1188 self.report_error(span, res_err);
1192 Res::Def(DefKind::TyParam, _) | Res::SelfTy { .. } => {
1194 let has_generic_params: HasGenericParams = match rib.kind {
1196 | ClosureOrAsyncRibKind
1199 | MacroDefinition(..)
1200 | ForwardGenericParamBanRibKind => {
1201 // Nothing to do. Continue.
1205 ConstantItemRibKind(trivial, _) => {
1206 let features = self.session.features_untracked();
1207 // HACK(min_const_generics): We currently only allow `N` or `{ N }`.
1208 if !(trivial || features.generic_const_exprs) {
1209 // HACK(min_const_generics): If we encounter `Self` in an anonymous constant
1210 // we can't easily tell if it's generic at this stage, so we instead remember
1211 // this and then enforce the self type to be concrete later on.
1212 if let Res::SelfTy { trait_, alias_to: Some((def, _)) } = res {
1213 res = Res::SelfTy { trait_, alias_to: Some((def, true)) }
1215 if let Some(span) = finalize {
1218 ResolutionError::ParamInNonTrivialAnonConst {
1219 name: rib_ident.name,
1223 self.session.delay_span_bug(span, CG_BUG_STR);
1233 // This was an attempt to use a type parameter outside its scope.
1234 ItemRibKind(has_generic_params) => has_generic_params,
1235 FnItemRibKind => HasGenericParams::Yes,
1236 ConstParamTyRibKind => {
1237 if let Some(span) = finalize {
1240 ResolutionError::ParamInTyOfConstParam(rib_ident.name),
1247 if let Some(span) = finalize {
1250 ResolutionError::GenericParamsFromOuterFunction(
1259 Res::Def(DefKind::ConstParam, _) => {
1260 let mut ribs = ribs.iter().peekable();
1261 if let Some(Rib { kind: FnItemRibKind, .. }) = ribs.peek() {
1262 // When declaring const parameters inside function signatures, the first rib
1263 // is always a `FnItemRibKind`. In this case, we can skip it, to avoid it
1264 // (spuriously) conflicting with the const param.
1269 let has_generic_params = match rib.kind {
1271 | ClosureOrAsyncRibKind
1274 | MacroDefinition(..)
1275 | ForwardGenericParamBanRibKind => continue,
1277 ConstantItemRibKind(trivial, _) => {
1278 let features = self.session.features_untracked();
1279 // HACK(min_const_generics): We currently only allow `N` or `{ N }`.
1280 if !(trivial || features.generic_const_exprs) {
1281 if let Some(span) = finalize {
1284 ResolutionError::ParamInNonTrivialAnonConst {
1285 name: rib_ident.name,
1289 self.session.delay_span_bug(span, CG_BUG_STR);
1298 ItemRibKind(has_generic_params) => has_generic_params,
1299 FnItemRibKind => HasGenericParams::Yes,
1300 ConstParamTyRibKind => {
1301 if let Some(span) = finalize {
1304 ResolutionError::ParamInTyOfConstParam(rib_ident.name),
1311 // This was an attempt to use a const parameter outside its scope.
1312 if let Some(span) = finalize {
1315 ResolutionError::GenericParamsFromOuterFunction(
1329 #[tracing::instrument(level = "debug", skip(self))]
1330 crate fn maybe_resolve_path(
1333 opt_ns: Option<Namespace>, // `None` indicates a module path in import
1334 parent_scope: &ParentScope<'a>,
1335 ) -> PathResult<'a> {
1336 self.resolve_path_with_ribs(path, opt_ns, parent_scope, Finalize::No, None, None)
1339 #[tracing::instrument(level = "debug", skip(self))]
1340 crate fn resolve_path(
1343 opt_ns: Option<Namespace>, // `None` indicates a module path in import
1344 parent_scope: &ParentScope<'a>,
1346 unusable_binding: Option<&'a NameBinding<'a>>,
1347 ) -> PathResult<'a> {
1348 self.resolve_path_with_ribs(path, opt_ns, parent_scope, finalize, None, unusable_binding)
1351 crate fn resolve_path_with_ribs(
1354 opt_ns: Option<Namespace>, // `None` indicates a module path in import
1355 parent_scope: &ParentScope<'a>,
1356 finalize_full: Finalize,
1357 ribs: Option<&PerNS<Vec<Rib<'a>>>>,
1358 unusable_binding: Option<&'a NameBinding<'a>>,
1359 ) -> PathResult<'a> {
1360 debug!("resolve_path(path={:?}, opt_ns={:?}, finalize={:?})", path, opt_ns, finalize_full);
1362 let finalize = finalize_full.path_span();
1363 let mut module = None;
1364 let mut allow_super = true;
1365 let mut second_binding = None;
1367 for (i, &Segment { ident, id, has_generic_args: _ }) in path.iter().enumerate() {
1368 debug!("resolve_path ident {} {:?} {:?}", i, ident, id);
1369 let record_segment_res = |this: &mut Self, res| {
1370 if finalize.is_some() {
1371 if let Some(id) = id {
1372 if !this.partial_res_map.contains_key(&id) {
1373 assert!(id != ast::DUMMY_NODE_ID, "Trying to resolve dummy id");
1374 this.record_partial_res(id, PartialRes::new(res));
1380 let is_last = i == path.len() - 1;
1381 let ns = if is_last { opt_ns.unwrap_or(TypeNS) } else { TypeNS };
1382 let name = ident.name;
1384 allow_super &= ns == TypeNS && (name == kw::SelfLower || name == kw::Super);
1387 if allow_super && name == kw::Super {
1388 let mut ctxt = ident.span.ctxt().normalize_to_macros_2_0();
1389 let self_module = match i {
1390 0 => Some(self.resolve_self(&mut ctxt, parent_scope.module)),
1392 Some(ModuleOrUniformRoot::Module(module)) => Some(module),
1396 if let Some(self_module) = self_module {
1397 if let Some(parent) = self_module.parent {
1398 module = Some(ModuleOrUniformRoot::Module(
1399 self.resolve_self(&mut ctxt, parent),
1404 return PathResult::failed(ident.span, false, finalize.is_some(), || {
1405 ("there are too many leading `super` keywords".to_string(), None)
1409 if name == kw::SelfLower {
1410 let mut ctxt = ident.span.ctxt().normalize_to_macros_2_0();
1411 module = Some(ModuleOrUniformRoot::Module(
1412 self.resolve_self(&mut ctxt, parent_scope.module),
1416 if name == kw::PathRoot && ident.span.rust_2018() {
1417 module = Some(ModuleOrUniformRoot::ExternPrelude);
1420 if name == kw::PathRoot && ident.span.rust_2015() && self.session.rust_2018() {
1421 // `::a::b` from 2015 macro on 2018 global edition
1422 module = Some(ModuleOrUniformRoot::CrateRootAndExternPrelude);
1425 if name == kw::PathRoot || name == kw::Crate || name == kw::DollarCrate {
1426 // `::a::b`, `crate::a::b` or `$crate::a::b`
1427 module = Some(ModuleOrUniformRoot::Module(self.resolve_crate_root(ident)));
1433 // Report special messages for path segment keywords in wrong positions.
1434 if ident.is_path_segment_keyword() && i != 0 {
1435 return PathResult::failed(ident.span, false, finalize.is_some(), || {
1436 let name_str = if name == kw::PathRoot {
1437 "crate root".to_string()
1439 format!("`{}`", name)
1441 let label = if i == 1 && path[0].ident.name == kw::PathRoot {
1442 format!("global paths cannot start with {}", name_str)
1444 format!("{} in paths can only be used in start position", name_str)
1450 enum FindBindingResult<'a> {
1451 Binding(Result<&'a NameBinding<'a>, Determinacy>),
1454 let find_binding_in_ns = |this: &mut Self, ns| {
1455 let binding = if let Some(module) = module {
1456 this.resolve_ident_in_module(
1465 } else if let Some(ribs) = ribs
1466 && let Some(TypeNS | ValueNS) = opt_ns
1468 match this.resolve_ident_in_lexical_scope(
1476 // we found a locally-imported or available item/module
1477 Some(LexicalScopeBinding::Item(binding)) => Ok(binding),
1478 // we found a local variable or type param
1479 Some(LexicalScopeBinding::Res(res)) => return FindBindingResult::Res(res),
1480 _ => Err(Determinacy::determined(finalize.is_some())),
1483 let scopes = ScopeSet::All(ns, opt_ns.is_none());
1484 this.early_resolve_ident_in_lexical_scope(
1494 FindBindingResult::Binding(binding)
1496 let binding = match find_binding_in_ns(self, ns) {
1497 FindBindingResult::Res(res) => {
1498 record_segment_res(self, res);
1499 return PathResult::NonModule(PartialRes::with_unresolved_segments(
1504 FindBindingResult::Binding(binding) => binding,
1509 second_binding = Some(binding);
1511 let res = binding.res();
1512 let maybe_assoc = opt_ns != Some(MacroNS) && PathSource::Type.is_expected(res);
1513 if let Some(next_module) = binding.module() {
1514 module = Some(ModuleOrUniformRoot::Module(next_module));
1515 record_segment_res(self, res);
1516 } else if res == Res::ToolMod && i + 1 != path.len() {
1517 if binding.is_import() {
1521 "cannot use a tool module through an import",
1523 .span_note(binding.span, "the tool module imported here")
1526 let res = Res::NonMacroAttr(NonMacroAttrKind::Tool);
1527 return PathResult::NonModule(PartialRes::new(res));
1528 } else if res == Res::Err {
1529 return PathResult::NonModule(PartialRes::new(Res::Err));
1530 } else if opt_ns.is_some() && (is_last || maybe_assoc) {
1531 self.lint_if_path_starts_with_module(finalize_full, path, second_binding);
1532 return PathResult::NonModule(PartialRes::with_unresolved_segments(
1537 return PathResult::failed(ident.span, is_last, finalize.is_some(), || {
1538 let label = format!(
1539 "`{ident}` is {} {}, not a module",
1547 Err(Undetermined) => return PathResult::Indeterminate,
1548 Err(Determined) => {
1549 if let Some(ModuleOrUniformRoot::Module(module)) = module {
1550 if opt_ns.is_some() && !module.is_normal() {
1551 return PathResult::NonModule(PartialRes::with_unresolved_segments(
1552 module.res().unwrap(),
1558 return PathResult::failed(ident.span, is_last, finalize.is_some(), || {
1559 self.report_path_resolution_error(
1574 self.lint_if_path_starts_with_module(finalize_full, path, second_binding);
1576 PathResult::Module(match module {
1577 Some(module) => module,
1578 None if path.is_empty() => ModuleOrUniformRoot::CurrentScope,
1579 _ => bug!("resolve_path: non-empty path `{:?}` has no module", path),