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 pub(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).ext;
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 pub(crate) fn resolve_ident_in_lexical_scope(
281 parent_scope: &ParentScope<'a>,
282 finalize: Option<Finalize>,
284 ignore_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 mut module = self.graph_root;
306 for i in (0..ribs.len()).rev() {
307 debug!("walk rib\n{:?}", ribs[i].bindings);
308 // Use the rib kind to determine whether we are resolving parameters
309 // (macro 2.0 hygiene) or local variables (`macro_rules` hygiene).
310 let rib_ident = if ribs[i].kind.contains_params() { normalized_ident } else { ident };
311 if let Some((original_rib_ident_def, res)) = ribs[i].bindings.get_key_value(&rib_ident)
313 // The ident resolves to a type parameter or local variable.
314 return Some(LexicalScopeBinding::Res(self.validate_res_from_ribs(
318 finalize.map(|finalize| finalize.path_span),
319 *original_rib_ident_def,
324 module = match ribs[i].kind {
325 ModuleRibKind(module) => module,
326 MacroDefinition(def) if def == self.macro_def(ident.span.ctxt()) => {
327 // If an invocation of this macro created `ident`, give up on `ident`
328 // and switch to `ident`'s source from the macro definition.
329 ident.span.remove_mark();
336 ModuleKind::Block(..) => {} // We can see through blocks
340 let item = self.resolve_ident_in_module_unadjusted(
341 ModuleOrUniformRoot::Module(module),
348 if let Ok(binding) = item {
349 // The ident resolves to an item.
350 return Some(LexicalScopeBinding::Item(binding));
353 self.early_resolve_ident_in_lexical_scope(
355 ScopeSet::Late(ns, module, finalize.map(|finalize| finalize.node_id)),
362 .map(LexicalScopeBinding::Item)
365 /// Resolve an identifier in lexical scope.
366 /// This is a variation of `fn resolve_ident_in_lexical_scope` that can be run during
367 /// expansion and import resolution (perhaps they can be merged in the future).
368 /// The function is used for resolving initial segments of macro paths (e.g., `foo` in
369 /// `foo::bar!(); or `foo!();`) and also for import paths on 2018 edition.
370 #[tracing::instrument(level = "debug", skip(self, scope_set))]
371 pub(crate) fn early_resolve_ident_in_lexical_scope(
374 scope_set: ScopeSet<'a>,
375 parent_scope: &ParentScope<'a>,
376 finalize: Option<Finalize>,
378 ignore_binding: Option<&'a NameBinding<'a>>,
379 ) -> Result<&'a NameBinding<'a>, Determinacy> {
380 bitflags::bitflags! {
382 const MACRO_RULES = 1 << 0;
383 const MODULE = 1 << 1;
384 const MISC_SUGGEST_CRATE = 1 << 2;
385 const MISC_SUGGEST_SELF = 1 << 3;
386 const MISC_FROM_PRELUDE = 1 << 4;
390 assert!(force || !finalize.is_some()); // `finalize` implies `force`
392 // Make sure `self`, `super` etc produce an error when passed to here.
393 if orig_ident.is_path_segment_keyword() {
394 return Err(Determinacy::Determined);
397 let (ns, macro_kind, is_import) = match scope_set {
398 ScopeSet::All(ns, is_import) => (ns, None, is_import),
399 ScopeSet::AbsolutePath(ns) => (ns, None, false),
400 ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind), false),
401 ScopeSet::Late(ns, ..) => (ns, None, false),
404 // This is *the* result, resolution from the scope closest to the resolved identifier.
405 // However, sometimes this result is "weak" because it comes from a glob import or
406 // a macro expansion, and in this case it cannot shadow names from outer scopes, e.g.
407 // mod m { ... } // solution in outer scope
409 // use prefix::*; // imports another `m` - innermost solution
410 // // weak, cannot shadow the outer `m`, need to report ambiguity error
413 // So we have to save the innermost solution and continue searching in outer scopes
414 // to detect potential ambiguities.
415 let mut innermost_result: Option<(&NameBinding<'_>, Flags)> = None;
416 let mut determinacy = Determinacy::Determined;
418 // Go through all the scopes and try to resolve the name.
419 let break_result = self.visit_scopes(
422 orig_ident.span.ctxt(),
423 |this, scope, use_prelude, ctxt| {
424 let ident = Ident::new(orig_ident.name, orig_ident.span.with_ctxt(ctxt));
425 let ok = |res, span, arenas| {
427 (res, ty::Visibility::Public, span, LocalExpnId::ROOT)
428 .to_name_binding(arenas),
432 let result = match scope {
433 Scope::DeriveHelpers(expn_id) => {
434 if let Some(attr) = this
437 .and_then(|attrs| attrs.iter().rfind(|i| ident == **i))
440 Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper),
441 ty::Visibility::Public,
445 .to_name_binding(this.arenas);
446 Ok((binding, Flags::empty()))
448 Err(Determinacy::Determined)
451 Scope::DeriveHelpersCompat => {
452 let mut result = Err(Determinacy::Determined);
453 for derive in parent_scope.derives {
454 let parent_scope = &ParentScope { derives: &[], ..*parent_scope };
455 match this.resolve_macro_path(
457 Some(MacroKind::Derive),
462 Ok((Some(ext), _)) => {
463 if ext.helper_attrs.contains(&ident.name) {
465 Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat),
472 Ok(_) | Err(Determinacy::Determined) => {}
473 Err(Determinacy::Undetermined) => {
474 result = Err(Determinacy::Undetermined)
480 Scope::MacroRules(macro_rules_scope) => match macro_rules_scope.get() {
481 MacroRulesScope::Binding(macro_rules_binding)
482 if ident == macro_rules_binding.ident =>
484 Ok((macro_rules_binding.binding, Flags::MACRO_RULES))
486 MacroRulesScope::Invocation(_) => Err(Determinacy::Undetermined),
487 _ => Err(Determinacy::Determined),
489 Scope::CrateRoot => {
490 let root_ident = Ident::new(kw::PathRoot, ident.span);
491 let root_module = this.resolve_crate_root(root_ident);
492 let binding = this.resolve_ident_in_module_ext(
493 ModuleOrUniformRoot::Module(root_module),
501 Ok(binding) => Ok((binding, Flags::MODULE | Flags::MISC_SUGGEST_CRATE)),
502 Err((Determinacy::Undetermined, Weak::No)) => {
503 return Some(Err(Determinacy::determined(force)));
505 Err((Determinacy::Undetermined, Weak::Yes)) => {
506 Err(Determinacy::Undetermined)
508 Err((Determinacy::Determined, _)) => Err(Determinacy::Determined),
511 Scope::Module(module, derive_fallback_lint_id) => {
512 let adjusted_parent_scope = &ParentScope { module, ..*parent_scope };
513 let binding = this.resolve_ident_in_module_unadjusted_ext(
514 ModuleOrUniformRoot::Module(module),
517 adjusted_parent_scope,
518 !matches!(scope_set, ScopeSet::Late(..)),
524 if let Some(lint_id) = derive_fallback_lint_id {
525 this.lint_buffer.buffer_lint_with_diagnostic(
526 PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
530 "cannot find {} `{}` in this scope",
534 BuiltinLintDiagnostics::ProcMacroDeriveResolutionFallback(
539 let misc_flags = if ptr::eq(module, this.graph_root) {
540 Flags::MISC_SUGGEST_CRATE
541 } else if module.is_normal() {
542 Flags::MISC_SUGGEST_SELF
546 Ok((binding, Flags::MODULE | misc_flags))
548 Err((Determinacy::Undetermined, Weak::No)) => {
549 return Some(Err(Determinacy::determined(force)));
551 Err((Determinacy::Undetermined, Weak::Yes)) => {
552 Err(Determinacy::Undetermined)
554 Err((Determinacy::Determined, _)) => Err(Determinacy::Determined),
557 Scope::RegisteredAttrs => match this.registered_attrs.get(&ident).cloned() {
559 Res::NonMacroAttr(NonMacroAttrKind::Registered),
563 None => Err(Determinacy::Determined),
565 Scope::MacroUsePrelude => {
566 match this.macro_use_prelude.get(&ident.name).cloned() {
567 Some(binding) => Ok((binding, Flags::MISC_FROM_PRELUDE)),
568 None => Err(Determinacy::determined(
569 this.graph_root.unexpanded_invocations.borrow().is_empty(),
573 Scope::BuiltinAttrs => {
574 if is_builtin_attr_name(ident.name) {
576 Res::NonMacroAttr(NonMacroAttrKind::Builtin(ident.name)),
581 Err(Determinacy::Determined)
584 Scope::ExternPrelude => {
585 match this.extern_prelude_get(ident, finalize.is_some()) {
586 Some(binding) => Ok((binding, Flags::empty())),
587 None => Err(Determinacy::determined(
588 this.graph_root.unexpanded_invocations.borrow().is_empty(),
592 Scope::ToolPrelude => match this.registered_tools.get(&ident).cloned() {
593 Some(ident) => ok(Res::ToolMod, ident.span, this.arenas),
594 None => Err(Determinacy::Determined),
596 Scope::StdLibPrelude => {
597 let mut result = Err(Determinacy::Determined);
598 if let Some(prelude) = this.prelude {
599 if let Ok(binding) = this.resolve_ident_in_module_unadjusted(
600 ModuleOrUniformRoot::Module(prelude),
607 if use_prelude || this.is_builtin_macro(binding.res()) {
608 result = Ok((binding, Flags::MISC_FROM_PRELUDE));
614 Scope::BuiltinTypes => match PrimTy::from_name(ident.name) {
615 Some(prim_ty) => ok(Res::PrimTy(prim_ty), DUMMY_SP, this.arenas),
616 None => Err(Determinacy::Determined),
622 if sub_namespace_match(binding.macro_kind(), macro_kind) =>
624 if finalize.is_none() || matches!(scope_set, ScopeSet::Late(..)) {
625 return Some(Ok(binding));
628 if let Some((innermost_binding, innermost_flags)) = innermost_result {
629 // Found another solution, if the first one was "weak", report an error.
630 let (res, innermost_res) = (binding.res(), innermost_binding.res());
631 if res != innermost_res {
632 let is_builtin = |res| {
633 matches!(res, Res::NonMacroAttr(NonMacroAttrKind::Builtin(..)))
636 Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper);
637 let derive_helper_compat =
638 Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat);
640 let ambiguity_error_kind = if is_import {
641 Some(AmbiguityKind::Import)
642 } else if is_builtin(innermost_res) || is_builtin(res) {
643 Some(AmbiguityKind::BuiltinAttr)
644 } else if innermost_res == derive_helper_compat
645 || res == derive_helper_compat && innermost_res != derive_helper
647 Some(AmbiguityKind::DeriveHelper)
648 } else if innermost_flags.contains(Flags::MACRO_RULES)
649 && flags.contains(Flags::MODULE)
650 && !this.disambiguate_macro_rules_vs_modularized(
654 || flags.contains(Flags::MACRO_RULES)
655 && innermost_flags.contains(Flags::MODULE)
656 && !this.disambiguate_macro_rules_vs_modularized(
661 Some(AmbiguityKind::MacroRulesVsModularized)
662 } else if innermost_binding.is_glob_import() {
663 Some(AmbiguityKind::GlobVsOuter)
664 } else if innermost_binding
665 .may_appear_after(parent_scope.expansion, binding)
667 Some(AmbiguityKind::MoreExpandedVsOuter)
671 if let Some(kind) = ambiguity_error_kind {
672 let misc = |f: Flags| {
673 if f.contains(Flags::MISC_SUGGEST_CRATE) {
674 AmbiguityErrorMisc::SuggestCrate
675 } else if f.contains(Flags::MISC_SUGGEST_SELF) {
676 AmbiguityErrorMisc::SuggestSelf
677 } else if f.contains(Flags::MISC_FROM_PRELUDE) {
678 AmbiguityErrorMisc::FromPrelude
680 AmbiguityErrorMisc::None
683 this.ambiguity_errors.push(AmbiguityError {
686 b1: innermost_binding,
688 misc1: misc(innermost_flags),
691 return Some(Ok(innermost_binding));
695 // Found the first solution.
696 innermost_result = Some((binding, flags));
699 Ok(..) | Err(Determinacy::Determined) => {}
700 Err(Determinacy::Undetermined) => determinacy = Determinacy::Undetermined,
707 if let Some(break_result) = break_result {
711 // The first found solution was the only one, return it.
712 if let Some((binding, _)) = innermost_result {
716 Err(Determinacy::determined(determinacy == Determinacy::Determined || force))
719 #[tracing::instrument(level = "debug", skip(self))]
720 pub(crate) fn maybe_resolve_ident_in_module(
722 module: ModuleOrUniformRoot<'a>,
725 parent_scope: &ParentScope<'a>,
726 ) -> Result<&'a NameBinding<'a>, Determinacy> {
727 self.resolve_ident_in_module_ext(module, ident, ns, parent_scope, None, None)
728 .map_err(|(determinacy, _)| determinacy)
731 #[tracing::instrument(level = "debug", skip(self))]
732 pub(crate) fn resolve_ident_in_module(
734 module: ModuleOrUniformRoot<'a>,
737 parent_scope: &ParentScope<'a>,
738 finalize: Option<Finalize>,
739 ignore_binding: Option<&'a NameBinding<'a>>,
740 ) -> Result<&'a NameBinding<'a>, Determinacy> {
741 self.resolve_ident_in_module_ext(module, ident, ns, parent_scope, finalize, ignore_binding)
742 .map_err(|(determinacy, _)| determinacy)
745 #[tracing::instrument(level = "debug", skip(self))]
746 fn resolve_ident_in_module_ext(
748 module: ModuleOrUniformRoot<'a>,
751 parent_scope: &ParentScope<'a>,
752 finalize: Option<Finalize>,
753 ignore_binding: Option<&'a NameBinding<'a>>,
754 ) -> Result<&'a NameBinding<'a>, (Determinacy, Weak)> {
755 let tmp_parent_scope;
756 let mut adjusted_parent_scope = parent_scope;
758 ModuleOrUniformRoot::Module(m) => {
759 if let Some(def) = ident.span.normalize_to_macros_2_0_and_adjust(m.expansion) {
761 ParentScope { module: self.expn_def_scope(def), ..*parent_scope };
762 adjusted_parent_scope = &tmp_parent_scope;
765 ModuleOrUniformRoot::ExternPrelude => {
766 ident.span.normalize_to_macros_2_0_and_adjust(ExpnId::root());
768 ModuleOrUniformRoot::CrateRootAndExternPrelude | ModuleOrUniformRoot::CurrentScope => {
772 self.resolve_ident_in_module_unadjusted_ext(
776 adjusted_parent_scope,
783 #[tracing::instrument(level = "debug", skip(self))]
784 fn resolve_ident_in_module_unadjusted(
786 module: ModuleOrUniformRoot<'a>,
789 parent_scope: &ParentScope<'a>,
790 finalize: Option<Finalize>,
791 ignore_binding: Option<&'a NameBinding<'a>>,
792 ) -> Result<&'a NameBinding<'a>, Determinacy> {
793 self.resolve_ident_in_module_unadjusted_ext(
802 .map_err(|(determinacy, _)| determinacy)
805 /// Attempts to resolve `ident` in namespaces `ns` of `module`.
806 /// Invariant: if `finalize` is `Some`, expansion and import resolution must be complete.
807 #[tracing::instrument(level = "debug", skip(self))]
808 fn resolve_ident_in_module_unadjusted_ext(
810 module: ModuleOrUniformRoot<'a>,
813 parent_scope: &ParentScope<'a>,
814 restricted_shadowing: bool,
815 finalize: Option<Finalize>,
816 // This binding should be ignored during in-module resolution, so that we don't get
817 // "self-confirming" import resolutions during import validation and checking.
818 ignore_binding: Option<&'a NameBinding<'a>>,
819 ) -> Result<&'a NameBinding<'a>, (Determinacy, Weak)> {
820 let module = match module {
821 ModuleOrUniformRoot::Module(module) => module,
822 ModuleOrUniformRoot::CrateRootAndExternPrelude => {
823 assert!(!restricted_shadowing);
824 let binding = self.early_resolve_ident_in_lexical_scope(
826 ScopeSet::AbsolutePath(ns),
832 return binding.map_err(|determinacy| (determinacy, Weak::No));
834 ModuleOrUniformRoot::ExternPrelude => {
835 assert!(!restricted_shadowing);
836 return if ns != TypeNS {
837 Err((Determined, Weak::No))
838 } else if let Some(binding) = self.extern_prelude_get(ident, finalize.is_some()) {
840 } else if !self.graph_root.unexpanded_invocations.borrow().is_empty() {
841 // Macro-expanded `extern crate` items can add names to extern prelude.
842 Err((Undetermined, Weak::No))
844 Err((Determined, Weak::No))
847 ModuleOrUniformRoot::CurrentScope => {
848 assert!(!restricted_shadowing);
850 if ident.name == kw::Crate || ident.name == kw::DollarCrate {
851 let module = self.resolve_crate_root(ident);
853 (module, ty::Visibility::Public, module.span, LocalExpnId::ROOT)
854 .to_name_binding(self.arenas);
856 } else if ident.name == kw::Super || ident.name == kw::SelfLower {
857 // FIXME: Implement these with renaming requirements so that e.g.
858 // `use super;` doesn't work, but `use super as name;` does.
859 // Fall through here to get an error from `early_resolve_...`.
863 let scopes = ScopeSet::All(ns, true);
864 let binding = self.early_resolve_ident_in_lexical_scope(
872 return binding.map_err(|determinacy| (determinacy, Weak::No));
876 let key = self.new_key(ident, ns);
878 self.resolution(module, key).try_borrow_mut().map_err(|_| (Determined, Weak::No))?; // This happens when there is a cycle of imports.
880 if let Some(Finalize { path_span, report_private, .. }) = finalize {
881 // If the primary binding is unusable, search further and return the shadowed glob
882 // binding if it exists. What we really want here is having two separate scopes in
883 // a module - one for non-globs and one for globs, but until that's done use this
884 // hack to avoid inconsistent resolution ICEs during import validation.
885 let binding = [resolution.binding, resolution.shadowed_glob]
887 .filter_map(|binding| match (binding, ignore_binding) {
888 (Some(binding), Some(ignored)) if ptr::eq(binding, ignored) => None,
892 let Some(binding) = binding else {
893 return Err((Determined, Weak::No));
896 if !self.is_accessible_from(binding.vis, parent_scope.module) {
898 self.privacy_errors.push(PrivacyError {
901 dedup_span: path_span,
904 return Err((Determined, Weak::No));
908 // Forbid expanded shadowing to avoid time travel.
909 if let Some(shadowed_glob) = resolution.shadowed_glob
910 && restricted_shadowing
911 && binding.expansion != LocalExpnId::ROOT
912 && binding.res() != shadowed_glob.res()
914 self.ambiguity_errors.push(AmbiguityError {
915 kind: AmbiguityKind::GlobVsExpanded,
919 misc1: AmbiguityErrorMisc::None,
920 misc2: AmbiguityErrorMisc::None,
924 if !restricted_shadowing && binding.expansion != LocalExpnId::ROOT {
925 if let NameBindingKind::Res(_, true) = binding.kind {
926 self.macro_expanded_macro_export_errors.insert((path_span, binding.span));
930 self.record_use(ident, binding, restricted_shadowing);
934 let check_usable = |this: &mut Self, binding: &'a NameBinding<'a>| {
935 if let Some(ignored) = ignore_binding && ptr::eq(binding, ignored) {
936 return Err((Determined, Weak::No));
938 let usable = this.is_accessible_from(binding.vis, parent_scope.module);
939 if usable { Ok(binding) } else { Err((Determined, Weak::No)) }
942 // Items and single imports are not shadowable, if we have one, then it's determined.
943 if let Some(binding) = resolution.binding {
944 if !binding.is_glob_import() {
945 return check_usable(self, binding);
949 // --- From now on we either have a glob resolution or no resolution. ---
951 // Check if one of single imports can still define the name,
952 // if it can then our result is not determined and can be invalidated.
953 for single_import in &resolution.single_imports {
954 if !self.is_accessible_from(single_import.vis.get(), parent_scope.module) {
957 let Some(module) = single_import.imported_module.get() else {
958 return Err((Undetermined, Weak::No));
960 let ImportKind::Single { source: ident, .. } = single_import.kind else {
963 match self.resolve_ident_in_module(
967 &single_import.parent_scope,
971 Err(Determined) => continue,
973 if !self.is_accessible_from(binding.vis, single_import.parent_scope.module) =>
977 Ok(_) | Err(Undetermined) => return Err((Undetermined, Weak::No)),
981 // So we have a resolution that's from a glob import. This resolution is determined
982 // if it cannot be shadowed by some new item/import expanded from a macro.
983 // This happens either if there are no unexpanded macros, or expanded names cannot
984 // shadow globs (that happens in macro namespace or with restricted shadowing).
986 // Additionally, any macro in any module can plant names in the root module if it creates
987 // `macro_export` macros, so the root module effectively has unresolved invocations if any
988 // module has unresolved invocations.
989 // However, it causes resolution/expansion to stuck too often (#53144), so, to make
990 // progress, we have to ignore those potential unresolved invocations from other modules
991 // and prohibit access to macro-expanded `macro_export` macros instead (unless restricted
992 // shadowing is enabled, see `macro_expanded_macro_export_errors`).
993 let unexpanded_macros = !module.unexpanded_invocations.borrow().is_empty();
994 if let Some(binding) = resolution.binding {
995 if !unexpanded_macros || ns == MacroNS || restricted_shadowing {
996 return check_usable(self, binding);
998 return Err((Undetermined, Weak::No));
1002 // --- From now on we have no resolution. ---
1004 // Now we are in situation when new item/import can appear only from a glob or a macro
1005 // expansion. With restricted shadowing names from globs and macro expansions cannot
1006 // shadow names from outer scopes, so we can freely fallback from module search to search
1007 // in outer scopes. For `early_resolve_ident_in_lexical_scope` to continue search in outer
1008 // scopes we return `Undetermined` with `Weak::Yes`.
1010 // Check if one of unexpanded macros can still define the name,
1011 // if it can then our "no resolution" result is not determined and can be invalidated.
1012 if unexpanded_macros {
1013 return Err((Undetermined, Weak::Yes));
1016 // Check if one of glob imports can still define the name,
1017 // if it can then our "no resolution" result is not determined and can be invalidated.
1018 for glob_import in module.globs.borrow().iter() {
1019 if !self.is_accessible_from(glob_import.vis.get(), parent_scope.module) {
1022 let module = match glob_import.imported_module.get() {
1023 Some(ModuleOrUniformRoot::Module(module)) => module,
1024 Some(_) => continue,
1025 None => return Err((Undetermined, Weak::Yes)),
1027 let tmp_parent_scope;
1028 let (mut adjusted_parent_scope, mut ident) =
1029 (parent_scope, ident.normalize_to_macros_2_0());
1030 match ident.span.glob_adjust(module.expansion, glob_import.span) {
1031 Some(Some(def)) => {
1033 ParentScope { module: self.expn_def_scope(def), ..*parent_scope };
1034 adjusted_parent_scope = &tmp_parent_scope;
1039 let result = self.resolve_ident_in_module_unadjusted(
1040 ModuleOrUniformRoot::Module(module),
1043 adjusted_parent_scope,
1049 Err(Determined) => continue,
1051 if !self.is_accessible_from(binding.vis, glob_import.parent_scope.module) =>
1055 Ok(_) | Err(Undetermined) => return Err((Undetermined, Weak::Yes)),
1059 // No resolution and no one else can define the name - determinate error.
1060 Err((Determined, Weak::No))
1063 /// Validate a local resolution (from ribs).
1064 #[tracing::instrument(level = "debug", skip(self, all_ribs))]
1065 fn validate_res_from_ribs(
1070 finalize: Option<Span>,
1071 original_rib_ident_def: Ident,
1072 all_ribs: &[Rib<'a>],
1074 const CG_BUG_STR: &str = "min_const_generics resolve check didn't stop compilation";
1075 debug!("validate_res_from_ribs({:?})", res);
1076 let ribs = &all_ribs[rib_index + 1..];
1078 // An invalid forward use of a generic parameter from a previous default.
1079 if let ForwardGenericParamBanRibKind = all_ribs[rib_index].kind {
1080 if let Some(span) = finalize {
1081 let res_error = if rib_ident.name == kw::SelfUpper {
1082 ResolutionError::SelfInGenericParamDefault
1084 ResolutionError::ForwardDeclaredGenericParam
1086 self.report_error(span, res_error);
1088 assert_eq!(res, Res::Err);
1094 use ResolutionError::*;
1095 let mut res_err = None;
1100 | ClosureOrAsyncRibKind
1102 | MacroDefinition(..)
1103 | ForwardGenericParamBanRibKind => {
1104 // Nothing to do. Continue.
1106 ItemRibKind(_) | FnItemRibKind | AssocItemRibKind => {
1107 // This was an attempt to access an upvar inside a
1108 // named function item. This is not allowed, so we
1110 if let Some(span) = finalize {
1111 // We don't immediately trigger a resolve error, because
1112 // we want certain other resolution errors (namely those
1113 // emitted for `ConstantItemRibKind` below) to take
1115 res_err = Some((span, CannotCaptureDynamicEnvironmentInFnItem));
1118 ConstantItemRibKind(_, item) => {
1119 // Still doesn't deal with upvars
1120 if let Some(span) = finalize {
1121 let (span, resolution_error) =
1122 if let Some((ident, constant_item_kind)) = item {
1123 let kind_str = match constant_item_kind {
1124 ConstantItemKind::Const => "const",
1125 ConstantItemKind::Static => "static",
1129 AttemptToUseNonConstantValueInConstant(
1130 ident, "let", kind_str,
1136 AttemptToUseNonConstantValueInConstant(
1137 original_rib_ident_def,
1143 self.report_error(span, resolution_error);
1147 ConstParamTyRibKind => {
1148 if let Some(span) = finalize {
1149 self.report_error(span, ParamInTyOfConstParam(rib_ident.name));
1153 InlineAsmSymRibKind => {
1154 if let Some(span) = finalize {
1155 self.report_error(span, InvalidAsmSym);
1161 if let Some((span, res_err)) = res_err {
1162 self.report_error(span, res_err);
1166 Res::Def(DefKind::TyParam, _) | Res::SelfTy { .. } => {
1168 let has_generic_params: HasGenericParams = match rib.kind {
1170 | ClosureOrAsyncRibKind
1173 | MacroDefinition(..)
1174 | InlineAsmSymRibKind
1175 | ForwardGenericParamBanRibKind => {
1176 // Nothing to do. Continue.
1180 ConstantItemRibKind(trivial, _) => {
1181 let features = self.session.features_untracked();
1182 // HACK(min_const_generics): We currently only allow `N` or `{ N }`.
1183 if !(trivial == HasGenericParams::Yes || features.generic_const_exprs) {
1184 // HACK(min_const_generics): If we encounter `Self` in an anonymous constant
1185 // we can't easily tell if it's generic at this stage, so we instead remember
1186 // this and then enforce the self type to be concrete later on.
1187 if let Res::SelfTy { trait_, alias_to: Some((def, _)) } = res {
1188 res = Res::SelfTy { trait_, alias_to: Some((def, true)) }
1190 if let Some(span) = finalize {
1193 ResolutionError::ParamInNonTrivialAnonConst {
1194 name: rib_ident.name,
1198 self.session.delay_span_bug(span, CG_BUG_STR);
1208 // This was an attempt to use a type parameter outside its scope.
1209 ItemRibKind(has_generic_params) => has_generic_params,
1210 FnItemRibKind => HasGenericParams::Yes,
1211 ConstParamTyRibKind => {
1212 if let Some(span) = finalize {
1215 ResolutionError::ParamInTyOfConstParam(rib_ident.name),
1222 if let Some(span) = finalize {
1225 ResolutionError::GenericParamsFromOuterFunction(
1234 Res::Def(DefKind::ConstParam, _) => {
1235 let mut ribs = ribs.iter().peekable();
1236 if let Some(Rib { kind: FnItemRibKind, .. }) = ribs.peek() {
1237 // When declaring const parameters inside function signatures, the first rib
1238 // is always a `FnItemRibKind`. In this case, we can skip it, to avoid it
1239 // (spuriously) conflicting with the const param.
1244 let has_generic_params = match rib.kind {
1246 | ClosureOrAsyncRibKind
1249 | MacroDefinition(..)
1250 | InlineAsmSymRibKind
1251 | ForwardGenericParamBanRibKind => continue,
1253 ConstantItemRibKind(trivial, _) => {
1254 let features = self.session.features_untracked();
1255 // HACK(min_const_generics): We currently only allow `N` or `{ N }`.
1256 if !(trivial == HasGenericParams::Yes || features.generic_const_exprs) {
1257 if let Some(span) = finalize {
1260 ResolutionError::ParamInNonTrivialAnonConst {
1261 name: rib_ident.name,
1265 self.session.delay_span_bug(span, CG_BUG_STR);
1274 ItemRibKind(has_generic_params) => has_generic_params,
1275 FnItemRibKind => HasGenericParams::Yes,
1276 ConstParamTyRibKind => {
1277 if let Some(span) = finalize {
1280 ResolutionError::ParamInTyOfConstParam(rib_ident.name),
1287 // This was an attempt to use a const parameter outside its scope.
1288 if let Some(span) = finalize {
1291 ResolutionError::GenericParamsFromOuterFunction(
1305 #[tracing::instrument(level = "debug", skip(self))]
1306 pub(crate) fn maybe_resolve_path(
1309 opt_ns: Option<Namespace>, // `None` indicates a module path in import
1310 parent_scope: &ParentScope<'a>,
1311 ) -> PathResult<'a> {
1312 self.resolve_path_with_ribs(path, opt_ns, parent_scope, None, None, None)
1315 #[tracing::instrument(level = "debug", skip(self))]
1316 pub(crate) fn resolve_path(
1319 opt_ns: Option<Namespace>, // `None` indicates a module path in import
1320 parent_scope: &ParentScope<'a>,
1321 finalize: Option<Finalize>,
1322 ignore_binding: Option<&'a NameBinding<'a>>,
1323 ) -> PathResult<'a> {
1324 self.resolve_path_with_ribs(path, opt_ns, parent_scope, finalize, None, ignore_binding)
1327 pub(crate) fn resolve_path_with_ribs(
1330 opt_ns: Option<Namespace>, // `None` indicates a module path in import
1331 parent_scope: &ParentScope<'a>,
1332 finalize: Option<Finalize>,
1333 ribs: Option<&PerNS<Vec<Rib<'a>>>>,
1334 ignore_binding: Option<&'a NameBinding<'a>>,
1335 ) -> PathResult<'a> {
1336 debug!("resolve_path(path={:?}, opt_ns={:?}, finalize={:?})", path, opt_ns, finalize);
1338 let mut module = None;
1339 let mut allow_super = true;
1340 let mut second_binding = None;
1342 for (i, &Segment { ident, id, .. }) in path.iter().enumerate() {
1343 debug!("resolve_path ident {} {:?} {:?}", i, ident, id);
1344 let record_segment_res = |this: &mut Self, res| {
1345 if finalize.is_some() {
1346 if let Some(id) = id {
1347 if !this.partial_res_map.contains_key(&id) {
1348 assert!(id != ast::DUMMY_NODE_ID, "Trying to resolve dummy id");
1349 this.record_partial_res(id, PartialRes::new(res));
1355 let is_last = i == path.len() - 1;
1356 let ns = if is_last { opt_ns.unwrap_or(TypeNS) } else { TypeNS };
1357 let name = ident.name;
1359 allow_super &= ns == TypeNS && (name == kw::SelfLower || name == kw::Super);
1362 if allow_super && name == kw::Super {
1363 let mut ctxt = ident.span.ctxt().normalize_to_macros_2_0();
1364 let self_module = match i {
1365 0 => Some(self.resolve_self(&mut ctxt, parent_scope.module)),
1367 Some(ModuleOrUniformRoot::Module(module)) => Some(module),
1371 if let Some(self_module) = self_module {
1372 if let Some(parent) = self_module.parent {
1373 module = Some(ModuleOrUniformRoot::Module(
1374 self.resolve_self(&mut ctxt, parent),
1379 return PathResult::failed(ident.span, false, finalize.is_some(), || {
1380 ("there are too many leading `super` keywords".to_string(), None)
1384 if name == kw::SelfLower {
1385 let mut ctxt = ident.span.ctxt().normalize_to_macros_2_0();
1386 module = Some(ModuleOrUniformRoot::Module(
1387 self.resolve_self(&mut ctxt, parent_scope.module),
1391 if name == kw::PathRoot && ident.span.rust_2018() {
1392 module = Some(ModuleOrUniformRoot::ExternPrelude);
1395 if name == kw::PathRoot && ident.span.rust_2015() && self.session.rust_2018() {
1396 // `::a::b` from 2015 macro on 2018 global edition
1397 module = Some(ModuleOrUniformRoot::CrateRootAndExternPrelude);
1400 if name == kw::PathRoot || name == kw::Crate || name == kw::DollarCrate {
1401 // `::a::b`, `crate::a::b` or `$crate::a::b`
1402 module = Some(ModuleOrUniformRoot::Module(self.resolve_crate_root(ident)));
1408 // Report special messages for path segment keywords in wrong positions.
1409 if ident.is_path_segment_keyword() && i != 0 {
1410 return PathResult::failed(ident.span, false, finalize.is_some(), || {
1411 let name_str = if name == kw::PathRoot {
1412 "crate root".to_string()
1414 format!("`{}`", name)
1416 let label = if i == 1 && path[0].ident.name == kw::PathRoot {
1417 format!("global paths cannot start with {}", name_str)
1419 format!("{} in paths can only be used in start position", name_str)
1425 enum FindBindingResult<'a> {
1426 Binding(Result<&'a NameBinding<'a>, Determinacy>),
1429 let find_binding_in_ns = |this: &mut Self, ns| {
1430 let binding = if let Some(module) = module {
1431 this.resolve_ident_in_module(
1439 } else if let Some(ribs) = ribs
1440 && let Some(TypeNS | ValueNS) = opt_ns
1442 match this.resolve_ident_in_lexical_scope(
1450 // we found a locally-imported or available item/module
1451 Some(LexicalScopeBinding::Item(binding)) => Ok(binding),
1452 // we found a local variable or type param
1453 Some(LexicalScopeBinding::Res(res)) => return FindBindingResult::Res(res),
1454 _ => Err(Determinacy::determined(finalize.is_some())),
1457 let scopes = ScopeSet::All(ns, opt_ns.is_none());
1458 this.early_resolve_ident_in_lexical_scope(
1467 FindBindingResult::Binding(binding)
1469 let binding = match find_binding_in_ns(self, ns) {
1470 FindBindingResult::Res(res) => {
1471 record_segment_res(self, res);
1472 return PathResult::NonModule(PartialRes::with_unresolved_segments(
1477 FindBindingResult::Binding(binding) => binding,
1482 second_binding = Some(binding);
1484 let res = binding.res();
1485 let maybe_assoc = opt_ns != Some(MacroNS) && PathSource::Type.is_expected(res);
1486 if let Some(next_module) = binding.module() {
1487 module = Some(ModuleOrUniformRoot::Module(next_module));
1488 record_segment_res(self, res);
1489 } else if res == Res::ToolMod && i + 1 != path.len() {
1490 if binding.is_import() {
1494 "cannot use a tool module through an import",
1496 .span_note(binding.span, "the tool module imported here")
1499 let res = Res::NonMacroAttr(NonMacroAttrKind::Tool);
1500 return PathResult::NonModule(PartialRes::new(res));
1501 } else if res == Res::Err {
1502 return PathResult::NonModule(PartialRes::new(Res::Err));
1503 } else if opt_ns.is_some() && (is_last || maybe_assoc) {
1504 self.lint_if_path_starts_with_module(finalize, path, second_binding);
1505 return PathResult::NonModule(PartialRes::with_unresolved_segments(
1510 return PathResult::failed(ident.span, is_last, finalize.is_some(), || {
1511 let label = format!(
1512 "`{ident}` is {} {}, not a module",
1520 Err(Undetermined) => return PathResult::Indeterminate,
1521 Err(Determined) => {
1522 if let Some(ModuleOrUniformRoot::Module(module)) = module {
1523 if opt_ns.is_some() && !module.is_normal() {
1524 return PathResult::NonModule(PartialRes::with_unresolved_segments(
1525 module.res().unwrap(),
1531 return PathResult::failed(ident.span, is_last, finalize.is_some(), || {
1532 self.report_path_resolution_error(
1547 self.lint_if_path_starts_with_module(finalize, path, second_binding);
1549 PathResult::Module(match module {
1550 Some(module) => module,
1551 None if path.is_empty() => ModuleOrUniformRoot::CurrentScope,
1552 _ => bug!("resolve_path: non-empty path `{:?}` has no module", path),