2 use rustc_feature::is_builtin_attr_name;
3 use rustc_hir::def::{DefKind, Namespace, NonMacroAttrKind, PartialRes, PerNS};
7 use rustc_span::def_id::LocalDefId;
8 use rustc_span::edition::Edition;
9 use rustc_span::hygiene::{ExpnId, ExpnKind, LocalExpnId, MacroKind, SyntaxContext};
10 use rustc_span::symbol::{kw, Ident};
11 use rustc_span::{Span, DUMMY_SP};
16 ConstantHasGenerics, ConstantItemKind, HasGenericParams, PathSource, Rib, RibKind,
18 use crate::macros::{sub_namespace_match, MacroRulesScope};
19 use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, Determinacy, Finalize};
20 use crate::{Import, ImportKind, LexicalScopeBinding, Module, ModuleKind, ModuleOrUniformRoot};
21 use crate::{NameBinding, NameBindingKind, ParentScope, PathResult, PrivacyError, Res};
22 use crate::{ResolutionError, Resolver, Scope, ScopeSet, Segment, ToNameBinding, Weak};
28 type Visibility = ty::Visibility<LocalDefId>;
30 impl<'a> Resolver<'a> {
31 /// A generic scope visitor.
32 /// Visits scopes in order to resolve some identifier in them or perform other actions.
33 /// If the callback returns `Some` result, we stop visiting scopes and return it.
34 pub(crate) fn visit_scopes<T>(
36 scope_set: ScopeSet<'a>,
37 parent_scope: &ParentScope<'a>,
39 mut visitor: impl FnMut(
46 // General principles:
47 // 1. Not controlled (user-defined) names should have higher priority than controlled names
48 // built into the language or standard library. This way we can add new names into the
49 // language or standard library without breaking user code.
50 // 2. "Closed set" below means new names cannot appear after the current resolution attempt.
51 // Places to search (in order of decreasing priority):
53 // 1. FIXME: Ribs (type parameters), there's no necessary infrastructure yet
54 // (open set, not controlled).
55 // 2. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
56 // (open, not controlled).
57 // 3. Extern prelude (open, the open part is from macro expansions, not controlled).
58 // 4. Tool modules (closed, controlled right now, but not in the future).
59 // 5. Standard library prelude (de-facto closed, controlled).
60 // 6. Language prelude (closed, controlled).
62 // 1. FIXME: Ribs (local variables), there's no necessary infrastructure yet
63 // (open set, not controlled).
64 // 2. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
65 // (open, not controlled).
66 // 3. Standard library prelude (de-facto closed, controlled).
68 // 1-3. Derive helpers (open, not controlled). All ambiguities with other names
69 // are currently reported as errors. They should be higher in priority than preludes
70 // and probably even names in modules according to the "general principles" above. They
71 // also should be subject to restricted shadowing because are effectively produced by
72 // derives (you need to resolve the derive first to add helpers into scope), but they
73 // should be available before the derive is expanded for compatibility.
74 // It's mess in general, so we are being conservative for now.
75 // 1-3. `macro_rules` (open, not controlled), loop through `macro_rules` scopes. Have higher
76 // priority than prelude macros, but create ambiguities with macros in modules.
77 // 1-3. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
78 // (open, not controlled). Have higher priority than prelude macros, but create
79 // ambiguities with `macro_rules`.
80 // 4. `macro_use` prelude (open, the open part is from macro expansions, not controlled).
81 // 4a. User-defined prelude from macro-use
82 // (open, the open part is from macro expansions, not controlled).
83 // 4b. "Standard library prelude" part implemented through `macro-use` (closed, controlled).
84 // 4c. Standard library prelude (de-facto closed, controlled).
85 // 6. Language prelude: builtin attributes (closed, controlled).
87 let rust_2015 = ctxt.edition() == Edition::Edition2015;
88 let (ns, macro_kind, is_absolute_path) = match scope_set {
89 ScopeSet::All(ns, _) => (ns, None, false),
90 ScopeSet::AbsolutePath(ns) => (ns, None, true),
91 ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind), false),
92 ScopeSet::Late(ns, ..) => (ns, None, false),
94 let module = match scope_set {
95 // Start with the specified module.
96 ScopeSet::Late(_, module, _) => module,
97 // Jump out of trait or enum modules, they do not act as scopes.
98 _ => parent_scope.module.nearest_item_scope(),
100 let mut scope = match ns {
101 _ if is_absolute_path => Scope::CrateRoot,
102 TypeNS | ValueNS => Scope::Module(module),
103 MacroNS => Scope::DeriveHelpers(parent_scope.expansion),
105 let mut ctxt = ctxt.normalize_to_macros_2_0();
106 let mut use_prelude = !module.no_implicit_prelude;
109 let visit = match scope {
110 // Derive helpers are not in scope when resolving derives in the same container.
111 Scope::DeriveHelpers(expn_id) => {
112 !(expn_id == parent_scope.expansion && macro_kind == Some(MacroKind::Derive))
114 Scope::DeriveHelpersCompat => true,
115 Scope::MacroRules(macro_rules_scope) => {
116 // Use "path compression" on `macro_rules` scope chains. This is an optimization
117 // used to avoid long scope chains, see the comments on `MacroRulesScopeRef`.
118 // As another consequence of this optimization visitors never observe invocation
119 // scopes for macros that were already expanded.
120 while let MacroRulesScope::Invocation(invoc_id) = macro_rules_scope.get() {
121 if let Some(next_scope) = self.output_macro_rules_scopes.get(&invoc_id) {
122 macro_rules_scope.set(next_scope.get());
129 Scope::CrateRoot => true,
130 Scope::Module(..) => true,
131 Scope::MacroUsePrelude => use_prelude || rust_2015,
132 Scope::BuiltinAttrs => true,
133 Scope::ExternPrelude => use_prelude || is_absolute_path,
134 Scope::ToolPrelude => use_prelude,
135 Scope::StdLibPrelude => use_prelude || ns == MacroNS,
136 Scope::BuiltinTypes => true,
140 if let break_result @ Some(..) = visitor(self, scope, use_prelude, ctxt) {
145 scope = match scope {
146 Scope::DeriveHelpers(LocalExpnId::ROOT) => Scope::DeriveHelpersCompat,
147 Scope::DeriveHelpers(expn_id) => {
148 // Derive helpers are not visible to code generated by bang or derive macros.
149 let expn_data = expn_id.expn_data();
150 match expn_data.kind {
152 | ExpnKind::Macro(MacroKind::Bang | MacroKind::Derive, _) => {
153 Scope::DeriveHelpersCompat
155 _ => Scope::DeriveHelpers(expn_data.parent.expect_local()),
158 Scope::DeriveHelpersCompat => Scope::MacroRules(parent_scope.macro_rules),
159 Scope::MacroRules(macro_rules_scope) => match macro_rules_scope.get() {
160 MacroRulesScope::Binding(binding) => {
161 Scope::MacroRules(binding.parent_macro_rules_scope)
163 MacroRulesScope::Invocation(invoc_id) => {
164 Scope::MacroRules(self.invocation_parent_scopes[&invoc_id].macro_rules)
166 MacroRulesScope::Empty => Scope::Module(module),
168 Scope::CrateRoot => match ns {
170 ctxt.adjust(ExpnId::root());
173 ValueNS | MacroNS => break,
175 Scope::Module(module) => {
176 use_prelude = !module.no_implicit_prelude;
177 match self.hygienic_lexical_parent(module, &mut ctxt) {
178 Some(parent_module) => Scope::Module(parent_module),
180 ctxt.adjust(ExpnId::root());
182 TypeNS => Scope::ExternPrelude,
183 ValueNS => Scope::StdLibPrelude,
184 MacroNS => Scope::MacroUsePrelude,
189 Scope::MacroUsePrelude => Scope::StdLibPrelude,
190 Scope::BuiltinAttrs => break, // nowhere else to search
191 Scope::ExternPrelude if is_absolute_path => break,
192 Scope::ExternPrelude => Scope::ToolPrelude,
193 Scope::ToolPrelude => Scope::StdLibPrelude,
194 Scope::StdLibPrelude => match ns {
195 TypeNS => Scope::BuiltinTypes,
196 ValueNS => break, // nowhere else to search
197 MacroNS => Scope::BuiltinAttrs,
199 Scope::BuiltinTypes => break, // nowhere else to search
206 fn hygienic_lexical_parent(
209 ctxt: &mut SyntaxContext,
210 ) -> Option<Module<'a>> {
211 if !module.expansion.outer_expn_is_descendant_of(*ctxt) {
212 return Some(self.expn_def_scope(ctxt.remove_mark()));
215 if let ModuleKind::Block = module.kind {
216 return Some(module.parent.unwrap().nearest_item_scope());
222 /// This resolves the identifier `ident` in the namespace `ns` in the current lexical scope.
223 /// More specifically, we proceed up the hierarchy of scopes and return the binding for
224 /// `ident` in the first scope that defines it (or None if no scopes define it).
226 /// A block's items are above its local variables in the scope hierarchy, regardless of where
227 /// the items are defined in the block. For example,
230 /// g(); // Since there are no local variables in scope yet, this resolves to the item.
233 /// g(); // This resolves to the local variable `g` since it shadows the item.
237 /// Invariant: This must only be called during main resolution, not during
238 /// import resolution.
239 #[instrument(level = "debug", skip(self, ribs))]
240 pub(crate) fn resolve_ident_in_lexical_scope(
244 parent_scope: &ParentScope<'a>,
245 finalize: Option<Finalize>,
247 ignore_binding: Option<&'a NameBinding<'a>>,
248 ) -> Option<LexicalScopeBinding<'a>> {
249 assert!(ns == TypeNS || ns == ValueNS);
250 let orig_ident = ident;
251 if ident.name == kw::Empty {
252 return Some(LexicalScopeBinding::Res(Res::Err));
254 let (general_span, normalized_span) = if ident.name == kw::SelfUpper {
255 // FIXME(jseyfried) improve `Self` hygiene
256 let empty_span = ident.span.with_ctxt(SyntaxContext::root());
257 (empty_span, empty_span)
258 } else if ns == TypeNS {
259 let normalized_span = ident.span.normalize_to_macros_2_0();
260 (normalized_span, normalized_span)
262 (ident.span.normalize_to_macro_rules(), ident.span.normalize_to_macros_2_0())
264 ident.span = general_span;
265 let normalized_ident = Ident { span: normalized_span, ..ident };
267 // Walk backwards up the ribs in scope.
268 let mut module = self.graph_root;
269 for i in (0..ribs.len()).rev() {
270 debug!("walk rib\n{:?}", ribs[i].bindings);
271 // Use the rib kind to determine whether we are resolving parameters
272 // (macro 2.0 hygiene) or local variables (`macro_rules` hygiene).
273 let rib_ident = if ribs[i].kind.contains_params() { normalized_ident } else { ident };
274 if let Some((original_rib_ident_def, res)) = ribs[i].bindings.get_key_value(&rib_ident)
276 // The ident resolves to a type parameter or local variable.
277 return Some(LexicalScopeBinding::Res(self.validate_res_from_ribs(
281 finalize.map(|finalize| finalize.path_span),
282 *original_rib_ident_def,
287 module = match ribs[i].kind {
288 ModuleRibKind(module) => module,
289 MacroDefinition(def) if def == self.macro_def(ident.span.ctxt()) => {
290 // If an invocation of this macro created `ident`, give up on `ident`
291 // and switch to `ident`'s source from the macro definition.
292 ident.span.remove_mark();
299 ModuleKind::Block => {} // We can see through blocks
303 let item = self.resolve_ident_in_module_unadjusted(
304 ModuleOrUniformRoot::Module(module),
311 if let Ok(binding) = item {
312 // The ident resolves to an item.
313 return Some(LexicalScopeBinding::Item(binding));
316 self.early_resolve_ident_in_lexical_scope(
318 ScopeSet::Late(ns, module, finalize.map(|finalize| finalize.node_id)),
325 .map(LexicalScopeBinding::Item)
328 /// Resolve an identifier in lexical scope.
329 /// This is a variation of `fn resolve_ident_in_lexical_scope` that can be run during
330 /// expansion and import resolution (perhaps they can be merged in the future).
331 /// The function is used for resolving initial segments of macro paths (e.g., `foo` in
332 /// `foo::bar!(); or `foo!();`) and also for import paths on 2018 edition.
333 #[instrument(level = "debug", skip(self, scope_set))]
334 pub(crate) fn early_resolve_ident_in_lexical_scope(
337 scope_set: ScopeSet<'a>,
338 parent_scope: &ParentScope<'a>,
339 finalize: Option<Finalize>,
341 ignore_binding: Option<&'a NameBinding<'a>>,
342 ) -> Result<&'a NameBinding<'a>, Determinacy> {
343 bitflags::bitflags! {
345 const MACRO_RULES = 1 << 0;
346 const MODULE = 1 << 1;
347 const MISC_SUGGEST_CRATE = 1 << 2;
348 const MISC_SUGGEST_SELF = 1 << 3;
349 const MISC_FROM_PRELUDE = 1 << 4;
353 assert!(force || !finalize.is_some()); // `finalize` implies `force`
355 // Make sure `self`, `super` etc produce an error when passed to here.
356 if orig_ident.is_path_segment_keyword() {
357 return Err(Determinacy::Determined);
360 let (ns, macro_kind, is_import) = match scope_set {
361 ScopeSet::All(ns, is_import) => (ns, None, is_import),
362 ScopeSet::AbsolutePath(ns) => (ns, None, false),
363 ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind), false),
364 ScopeSet::Late(ns, ..) => (ns, None, false),
367 // This is *the* result, resolution from the scope closest to the resolved identifier.
368 // However, sometimes this result is "weak" because it comes from a glob import or
369 // a macro expansion, and in this case it cannot shadow names from outer scopes, e.g.
370 // mod m { ... } // solution in outer scope
372 // use prefix::*; // imports another `m` - innermost solution
373 // // weak, cannot shadow the outer `m`, need to report ambiguity error
376 // So we have to save the innermost solution and continue searching in outer scopes
377 // to detect potential ambiguities.
378 let mut innermost_result: Option<(&NameBinding<'_>, Flags)> = None;
379 let mut determinacy = Determinacy::Determined;
381 // Go through all the scopes and try to resolve the name.
382 let break_result = self.visit_scopes(
385 orig_ident.span.ctxt(),
386 |this, scope, use_prelude, ctxt| {
387 let ident = Ident::new(orig_ident.name, orig_ident.span.with_ctxt(ctxt));
388 let ok = |res, span, arenas| {
390 (res, Visibility::Public, span, LocalExpnId::ROOT).to_name_binding(arenas),
394 let result = match scope {
395 Scope::DeriveHelpers(expn_id) => {
396 if let Some(attr) = this
399 .and_then(|attrs| attrs.iter().rfind(|i| ident == **i))
402 Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper),
407 .to_name_binding(this.arenas);
408 Ok((binding, Flags::empty()))
410 Err(Determinacy::Determined)
413 Scope::DeriveHelpersCompat => {
414 let mut result = Err(Determinacy::Determined);
415 for derive in parent_scope.derives {
416 let parent_scope = &ParentScope { derives: &[], ..*parent_scope };
417 match this.resolve_macro_path(
419 Some(MacroKind::Derive),
424 Ok((Some(ext), _)) => {
425 if ext.helper_attrs.contains(&ident.name) {
427 Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat),
434 Ok(_) | Err(Determinacy::Determined) => {}
435 Err(Determinacy::Undetermined) => {
436 result = Err(Determinacy::Undetermined)
442 Scope::MacroRules(macro_rules_scope) => match macro_rules_scope.get() {
443 MacroRulesScope::Binding(macro_rules_binding)
444 if ident == macro_rules_binding.ident =>
446 Ok((macro_rules_binding.binding, Flags::MACRO_RULES))
448 MacroRulesScope::Invocation(_) => Err(Determinacy::Undetermined),
449 _ => Err(Determinacy::Determined),
451 Scope::CrateRoot => {
452 let root_ident = Ident::new(kw::PathRoot, ident.span);
453 let root_module = this.resolve_crate_root(root_ident);
454 let binding = this.resolve_ident_in_module_ext(
455 ModuleOrUniformRoot::Module(root_module),
463 Ok(binding) => Ok((binding, Flags::MODULE | Flags::MISC_SUGGEST_CRATE)),
464 Err((Determinacy::Undetermined, Weak::No)) => {
465 return Some(Err(Determinacy::determined(force)));
467 Err((Determinacy::Undetermined, Weak::Yes)) => {
468 Err(Determinacy::Undetermined)
470 Err((Determinacy::Determined, _)) => Err(Determinacy::Determined),
473 Scope::Module(module) => {
474 let adjusted_parent_scope = &ParentScope { module, ..*parent_scope };
475 let binding = this.resolve_ident_in_module_unadjusted_ext(
476 ModuleOrUniformRoot::Module(module),
479 adjusted_parent_scope,
480 !matches!(scope_set, ScopeSet::Late(..)),
486 let misc_flags = if ptr::eq(module, this.graph_root) {
487 Flags::MISC_SUGGEST_CRATE
488 } else if module.is_normal() {
489 Flags::MISC_SUGGEST_SELF
493 Ok((binding, Flags::MODULE | misc_flags))
495 Err((Determinacy::Undetermined, Weak::No)) => {
496 return Some(Err(Determinacy::determined(force)));
498 Err((Determinacy::Undetermined, Weak::Yes)) => {
499 Err(Determinacy::Undetermined)
501 Err((Determinacy::Determined, _)) => Err(Determinacy::Determined),
504 Scope::MacroUsePrelude => {
505 match this.macro_use_prelude.get(&ident.name).cloned() {
506 Some(binding) => Ok((binding, Flags::MISC_FROM_PRELUDE)),
507 None => Err(Determinacy::determined(
508 this.graph_root.unexpanded_invocations.borrow().is_empty(),
512 Scope::BuiltinAttrs => {
513 if is_builtin_attr_name(ident.name) {
515 Res::NonMacroAttr(NonMacroAttrKind::Builtin(ident.name)),
520 Err(Determinacy::Determined)
523 Scope::ExternPrelude => {
524 match this.extern_prelude_get(ident, finalize.is_some()) {
525 Some(binding) => Ok((binding, Flags::empty())),
526 None => Err(Determinacy::determined(
527 this.graph_root.unexpanded_invocations.borrow().is_empty(),
531 Scope::ToolPrelude => match this.registered_tools.get(&ident).cloned() {
532 Some(ident) => ok(Res::ToolMod, ident.span, this.arenas),
533 None => Err(Determinacy::Determined),
535 Scope::StdLibPrelude => {
536 let mut result = Err(Determinacy::Determined);
537 if let Some(prelude) = this.prelude {
538 if let Ok(binding) = this.resolve_ident_in_module_unadjusted(
539 ModuleOrUniformRoot::Module(prelude),
546 if use_prelude || this.is_builtin_macro(binding.res()) {
547 result = Ok((binding, Flags::MISC_FROM_PRELUDE));
553 Scope::BuiltinTypes => match PrimTy::from_name(ident.name) {
554 Some(prim_ty) => ok(Res::PrimTy(prim_ty), DUMMY_SP, this.arenas),
555 None => Err(Determinacy::Determined),
561 if sub_namespace_match(binding.macro_kind(), macro_kind) =>
563 if finalize.is_none() || matches!(scope_set, ScopeSet::Late(..)) {
564 return Some(Ok(binding));
567 if let Some((innermost_binding, innermost_flags)) = innermost_result {
568 // Found another solution, if the first one was "weak", report an error.
569 let (res, innermost_res) = (binding.res(), innermost_binding.res());
570 if res != innermost_res {
571 let is_builtin = |res| {
572 matches!(res, Res::NonMacroAttr(NonMacroAttrKind::Builtin(..)))
575 Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper);
576 let derive_helper_compat =
577 Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat);
579 let ambiguity_error_kind = if is_import {
580 Some(AmbiguityKind::Import)
581 } else if is_builtin(innermost_res) || is_builtin(res) {
582 Some(AmbiguityKind::BuiltinAttr)
583 } else if innermost_res == derive_helper_compat
584 || res == derive_helper_compat && innermost_res != derive_helper
586 Some(AmbiguityKind::DeriveHelper)
587 } else if innermost_flags.contains(Flags::MACRO_RULES)
588 && flags.contains(Flags::MODULE)
589 && !this.disambiguate_macro_rules_vs_modularized(
593 || flags.contains(Flags::MACRO_RULES)
594 && innermost_flags.contains(Flags::MODULE)
595 && !this.disambiguate_macro_rules_vs_modularized(
600 Some(AmbiguityKind::MacroRulesVsModularized)
601 } else if innermost_binding.is_glob_import() {
602 Some(AmbiguityKind::GlobVsOuter)
603 } else if innermost_binding
604 .may_appear_after(parent_scope.expansion, binding)
606 Some(AmbiguityKind::MoreExpandedVsOuter)
610 if let Some(kind) = ambiguity_error_kind {
611 let misc = |f: Flags| {
612 if f.contains(Flags::MISC_SUGGEST_CRATE) {
613 AmbiguityErrorMisc::SuggestCrate
614 } else if f.contains(Flags::MISC_SUGGEST_SELF) {
615 AmbiguityErrorMisc::SuggestSelf
616 } else if f.contains(Flags::MISC_FROM_PRELUDE) {
617 AmbiguityErrorMisc::FromPrelude
619 AmbiguityErrorMisc::None
622 this.ambiguity_errors.push(AmbiguityError {
625 b1: innermost_binding,
627 misc1: misc(innermost_flags),
630 return Some(Ok(innermost_binding));
634 // Found the first solution.
635 innermost_result = Some((binding, flags));
638 Ok(..) | Err(Determinacy::Determined) => {}
639 Err(Determinacy::Undetermined) => determinacy = Determinacy::Undetermined,
646 if let Some(break_result) = break_result {
650 // The first found solution was the only one, return it.
651 if let Some((binding, _)) = innermost_result {
655 Err(Determinacy::determined(determinacy == Determinacy::Determined || force))
658 #[instrument(level = "debug", skip(self))]
659 pub(crate) fn maybe_resolve_ident_in_module(
661 module: ModuleOrUniformRoot<'a>,
664 parent_scope: &ParentScope<'a>,
665 ) -> Result<&'a NameBinding<'a>, Determinacy> {
666 self.resolve_ident_in_module_ext(module, ident, ns, parent_scope, None, None)
667 .map_err(|(determinacy, _)| determinacy)
670 #[instrument(level = "debug", skip(self))]
671 pub(crate) fn resolve_ident_in_module(
673 module: ModuleOrUniformRoot<'a>,
676 parent_scope: &ParentScope<'a>,
677 finalize: Option<Finalize>,
678 ignore_binding: Option<&'a NameBinding<'a>>,
679 ) -> Result<&'a NameBinding<'a>, Determinacy> {
680 self.resolve_ident_in_module_ext(module, ident, ns, parent_scope, finalize, ignore_binding)
681 .map_err(|(determinacy, _)| determinacy)
684 #[instrument(level = "debug", skip(self))]
685 fn resolve_ident_in_module_ext(
687 module: ModuleOrUniformRoot<'a>,
690 parent_scope: &ParentScope<'a>,
691 finalize: Option<Finalize>,
692 ignore_binding: Option<&'a NameBinding<'a>>,
693 ) -> Result<&'a NameBinding<'a>, (Determinacy, Weak)> {
694 let tmp_parent_scope;
695 let mut adjusted_parent_scope = parent_scope;
697 ModuleOrUniformRoot::Module(m) => {
698 if let Some(def) = ident.span.normalize_to_macros_2_0_and_adjust(m.expansion) {
700 ParentScope { module: self.expn_def_scope(def), ..*parent_scope };
701 adjusted_parent_scope = &tmp_parent_scope;
704 ModuleOrUniformRoot::ExternPrelude => {
705 ident.span.normalize_to_macros_2_0_and_adjust(ExpnId::root());
707 ModuleOrUniformRoot::CrateRootAndExternPrelude | ModuleOrUniformRoot::CurrentScope => {
711 self.resolve_ident_in_module_unadjusted_ext(
715 adjusted_parent_scope,
722 #[instrument(level = "debug", skip(self))]
723 fn resolve_ident_in_module_unadjusted(
725 module: ModuleOrUniformRoot<'a>,
728 parent_scope: &ParentScope<'a>,
729 finalize: Option<Finalize>,
730 ignore_binding: Option<&'a NameBinding<'a>>,
731 ) -> Result<&'a NameBinding<'a>, Determinacy> {
732 self.resolve_ident_in_module_unadjusted_ext(
741 .map_err(|(determinacy, _)| determinacy)
744 /// Attempts to resolve `ident` in namespaces `ns` of `module`.
745 /// Invariant: if `finalize` is `Some`, expansion and import resolution must be complete.
746 #[instrument(level = "debug", skip(self))]
747 fn resolve_ident_in_module_unadjusted_ext(
749 module: ModuleOrUniformRoot<'a>,
752 parent_scope: &ParentScope<'a>,
753 restricted_shadowing: bool,
754 finalize: Option<Finalize>,
755 // This binding should be ignored during in-module resolution, so that we don't get
756 // "self-confirming" import resolutions during import validation and checking.
757 ignore_binding: Option<&'a NameBinding<'a>>,
758 ) -> Result<&'a NameBinding<'a>, (Determinacy, Weak)> {
759 let module = match module {
760 ModuleOrUniformRoot::Module(module) => module,
761 ModuleOrUniformRoot::CrateRootAndExternPrelude => {
762 assert!(!restricted_shadowing);
763 let binding = self.early_resolve_ident_in_lexical_scope(
765 ScopeSet::AbsolutePath(ns),
771 return binding.map_err(|determinacy| (determinacy, Weak::No));
773 ModuleOrUniformRoot::ExternPrelude => {
774 assert!(!restricted_shadowing);
775 return if ns != TypeNS {
776 Err((Determined, Weak::No))
777 } else if let Some(binding) = self.extern_prelude_get(ident, finalize.is_some()) {
779 } else if !self.graph_root.unexpanded_invocations.borrow().is_empty() {
780 // Macro-expanded `extern crate` items can add names to extern prelude.
781 Err((Undetermined, Weak::No))
783 Err((Determined, Weak::No))
786 ModuleOrUniformRoot::CurrentScope => {
787 assert!(!restricted_shadowing);
789 if ident.name == kw::Crate || ident.name == kw::DollarCrate {
790 let module = self.resolve_crate_root(ident);
791 let binding = (module, Visibility::Public, module.span, LocalExpnId::ROOT)
792 .to_name_binding(self.arenas);
794 } else if ident.name == kw::Super || ident.name == kw::SelfLower {
795 // FIXME: Implement these with renaming requirements so that e.g.
796 // `use super;` doesn't work, but `use super as name;` does.
797 // Fall through here to get an error from `early_resolve_...`.
801 let scopes = ScopeSet::All(ns, true);
802 let binding = self.early_resolve_ident_in_lexical_scope(
810 return binding.map_err(|determinacy| (determinacy, Weak::No));
814 let key = self.new_key(ident, ns);
816 self.resolution(module, key).try_borrow_mut().map_err(|_| (Determined, Weak::No))?; // This happens when there is a cycle of imports.
818 if let Some(Finalize { path_span, report_private, .. }) = finalize {
819 // If the primary binding is unusable, search further and return the shadowed glob
820 // binding if it exists. What we really want here is having two separate scopes in
821 // a module - one for non-globs and one for globs, but until that's done use this
822 // hack to avoid inconsistent resolution ICEs during import validation.
823 let binding = [resolution.binding, resolution.shadowed_glob]
825 .filter_map(|binding| match (binding, ignore_binding) {
826 (Some(binding), Some(ignored)) if ptr::eq(binding, ignored) => None,
830 let Some(binding) = binding else {
831 return Err((Determined, Weak::No));
834 if !self.is_accessible_from(binding.vis, parent_scope.module) {
836 self.privacy_errors.push(PrivacyError {
839 dedup_span: path_span,
842 return Err((Determined, Weak::No));
846 // Forbid expanded shadowing to avoid time travel.
847 if let Some(shadowed_glob) = resolution.shadowed_glob
848 && restricted_shadowing
849 && binding.expansion != LocalExpnId::ROOT
850 && binding.res() != shadowed_glob.res()
852 self.ambiguity_errors.push(AmbiguityError {
853 kind: AmbiguityKind::GlobVsExpanded,
857 misc1: AmbiguityErrorMisc::None,
858 misc2: AmbiguityErrorMisc::None,
862 if !restricted_shadowing && binding.expansion != LocalExpnId::ROOT {
863 if let NameBindingKind::Import {
864 import: Import { kind: ImportKind::MacroExport, .. },
868 self.macro_expanded_macro_export_errors.insert((path_span, binding.span));
872 self.record_use(ident, binding, restricted_shadowing);
876 let check_usable = |this: &mut Self, binding: &'a NameBinding<'a>| {
877 if let Some(ignored) = ignore_binding && ptr::eq(binding, ignored) {
878 return Err((Determined, Weak::No));
880 let usable = this.is_accessible_from(binding.vis, parent_scope.module);
881 if usable { Ok(binding) } else { Err((Determined, Weak::No)) }
884 // Items and single imports are not shadowable, if we have one, then it's determined.
885 if let Some(binding) = resolution.binding {
886 if !binding.is_glob_import() {
887 return check_usable(self, binding);
891 // --- From now on we either have a glob resolution or no resolution. ---
893 // Check if one of single imports can still define the name,
894 // if it can then our result is not determined and can be invalidated.
895 for single_import in &resolution.single_imports {
896 let Some(import_vis) = single_import.vis.get() else {
899 if !self.is_accessible_from(import_vis, parent_scope.module) {
902 let Some(module) = single_import.imported_module.get() else {
903 return Err((Undetermined, Weak::No));
905 let ImportKind::Single { source: ident, .. } = single_import.kind else {
908 match self.resolve_ident_in_module(
912 &single_import.parent_scope,
916 Err(Determined) => continue,
918 if !self.is_accessible_from(binding.vis, single_import.parent_scope.module) =>
922 Ok(_) | Err(Undetermined) => return Err((Undetermined, Weak::No)),
926 // So we have a resolution that's from a glob import. This resolution is determined
927 // if it cannot be shadowed by some new item/import expanded from a macro.
928 // This happens either if there are no unexpanded macros, or expanded names cannot
929 // shadow globs (that happens in macro namespace or with restricted shadowing).
931 // Additionally, any macro in any module can plant names in the root module if it creates
932 // `macro_export` macros, so the root module effectively has unresolved invocations if any
933 // module has unresolved invocations.
934 // However, it causes resolution/expansion to stuck too often (#53144), so, to make
935 // progress, we have to ignore those potential unresolved invocations from other modules
936 // and prohibit access to macro-expanded `macro_export` macros instead (unless restricted
937 // shadowing is enabled, see `macro_expanded_macro_export_errors`).
938 let unexpanded_macros = !module.unexpanded_invocations.borrow().is_empty();
939 if let Some(binding) = resolution.binding {
940 if !unexpanded_macros || ns == MacroNS || restricted_shadowing {
941 return check_usable(self, binding);
943 return Err((Undetermined, Weak::No));
947 // --- From now on we have no resolution. ---
949 // Now we are in situation when new item/import can appear only from a glob or a macro
950 // expansion. With restricted shadowing names from globs and macro expansions cannot
951 // shadow names from outer scopes, so we can freely fallback from module search to search
952 // in outer scopes. For `early_resolve_ident_in_lexical_scope` to continue search in outer
953 // scopes we return `Undetermined` with `Weak::Yes`.
955 // Check if one of unexpanded macros can still define the name,
956 // if it can then our "no resolution" result is not determined and can be invalidated.
957 if unexpanded_macros {
958 return Err((Undetermined, Weak::Yes));
961 // Check if one of glob imports can still define the name,
962 // if it can then our "no resolution" result is not determined and can be invalidated.
963 for glob_import in module.globs.borrow().iter() {
964 let Some(import_vis) = glob_import.vis.get() else {
967 if !self.is_accessible_from(import_vis, parent_scope.module) {
970 let module = match glob_import.imported_module.get() {
971 Some(ModuleOrUniformRoot::Module(module)) => module,
973 None => return Err((Undetermined, Weak::Yes)),
975 let tmp_parent_scope;
976 let (mut adjusted_parent_scope, mut ident) =
977 (parent_scope, ident.normalize_to_macros_2_0());
978 match ident.span.glob_adjust(module.expansion, glob_import.span) {
981 ParentScope { module: self.expn_def_scope(def), ..*parent_scope };
982 adjusted_parent_scope = &tmp_parent_scope;
987 let result = self.resolve_ident_in_module_unadjusted(
988 ModuleOrUniformRoot::Module(module),
991 adjusted_parent_scope,
997 Err(Determined) => continue,
999 if !self.is_accessible_from(binding.vis, glob_import.parent_scope.module) =>
1003 Ok(_) | Err(Undetermined) => return Err((Undetermined, Weak::Yes)),
1007 // No resolution and no one else can define the name - determinate error.
1008 Err((Determined, Weak::No))
1011 /// Validate a local resolution (from ribs).
1012 #[instrument(level = "debug", skip(self, all_ribs))]
1013 fn validate_res_from_ribs(
1018 finalize: Option<Span>,
1019 original_rib_ident_def: Ident,
1020 all_ribs: &[Rib<'a>],
1022 const CG_BUG_STR: &str = "min_const_generics resolve check didn't stop compilation";
1023 debug!("validate_res_from_ribs({:?})", res);
1024 let ribs = &all_ribs[rib_index + 1..];
1026 // An invalid forward use of a generic parameter from a previous default.
1027 if let ForwardGenericParamBanRibKind = all_ribs[rib_index].kind {
1028 if let Some(span) = finalize {
1029 let res_error = if rib_ident.name == kw::SelfUpper {
1030 ResolutionError::SelfInGenericParamDefault
1032 ResolutionError::ForwardDeclaredGenericParam
1034 self.report_error(span, res_error);
1036 assert_eq!(res, Res::Err);
1042 use ResolutionError::*;
1043 let mut res_err = None;
1048 | ClosureOrAsyncRibKind
1050 | MacroDefinition(..)
1051 | ForwardGenericParamBanRibKind => {
1052 // Nothing to do. Continue.
1054 ItemRibKind(_) | AssocItemRibKind => {
1055 // This was an attempt to access an upvar inside a
1056 // named function item. This is not allowed, so we
1058 if let Some(span) = finalize {
1059 // We don't immediately trigger a resolve error, because
1060 // we want certain other resolution errors (namely those
1061 // emitted for `ConstantItemRibKind` below) to take
1063 res_err = Some((span, CannotCaptureDynamicEnvironmentInFnItem));
1066 ConstantItemRibKind(_, item) => {
1067 // Still doesn't deal with upvars
1068 if let Some(span) = finalize {
1069 let (span, resolution_error) =
1070 if let Some((ident, constant_item_kind)) = item {
1071 let kind_str = match constant_item_kind {
1072 ConstantItemKind::Const => "const",
1073 ConstantItemKind::Static => "static",
1077 AttemptToUseNonConstantValueInConstant(
1078 ident, "let", kind_str,
1084 AttemptToUseNonConstantValueInConstant(
1085 original_rib_ident_def,
1091 self.report_error(span, resolution_error);
1095 ConstParamTyRibKind => {
1096 if let Some(span) = finalize {
1097 self.report_error(span, ParamInTyOfConstParam(rib_ident.name));
1101 InlineAsmSymRibKind => {
1102 if let Some(span) = finalize {
1103 self.report_error(span, InvalidAsmSym);
1109 if let Some((span, res_err)) = res_err {
1110 self.report_error(span, res_err);
1114 Res::Def(DefKind::TyParam, _) | Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } => {
1116 let has_generic_params: HasGenericParams = match rib.kind {
1118 | ClosureOrAsyncRibKind
1120 | MacroDefinition(..)
1121 | InlineAsmSymRibKind
1123 | ForwardGenericParamBanRibKind => {
1124 // Nothing to do. Continue.
1128 ConstantItemRibKind(trivial, _) => {
1129 let features = self.session.features_untracked();
1130 // HACK(min_const_generics): We currently only allow `N` or `{ N }`.
1131 if !(trivial == ConstantHasGenerics::Yes
1132 || features.generic_const_exprs)
1134 // HACK(min_const_generics): If we encounter `Self` in an anonymous
1135 // constant we can't easily tell if it's generic at this stage, so
1136 // we instead remember this and then enforce the self type to be
1137 // concrete later on.
1138 if let Res::SelfTyAlias {
1144 res = Res::SelfTyAlias {
1146 forbid_generic: true,
1150 if let Some(span) = finalize {
1153 ResolutionError::ParamInNonTrivialAnonConst {
1154 name: rib_ident.name,
1158 self.session.delay_span_bug(span, CG_BUG_STR);
1168 // This was an attempt to use a type parameter outside its scope.
1169 ItemRibKind(has_generic_params) => has_generic_params,
1170 ConstParamTyRibKind => {
1171 if let Some(span) = finalize {
1174 ResolutionError::ParamInTyOfConstParam(rib_ident.name),
1181 if let Some(span) = finalize {
1184 ResolutionError::GenericParamsFromOuterFunction(
1193 Res::Def(DefKind::ConstParam, _) => {
1195 let has_generic_params = match rib.kind {
1197 | ClosureOrAsyncRibKind
1199 | MacroDefinition(..)
1200 | InlineAsmSymRibKind
1202 | ForwardGenericParamBanRibKind => continue,
1204 ConstantItemRibKind(trivial, _) => {
1205 let features = self.session.features_untracked();
1206 // HACK(min_const_generics): We currently only allow `N` or `{ N }`.
1207 if !(trivial == ConstantHasGenerics::Yes
1208 || features.generic_const_exprs)
1210 if let Some(span) = finalize {
1213 ResolutionError::ParamInNonTrivialAnonConst {
1214 name: rib_ident.name,
1218 self.session.delay_span_bug(span, CG_BUG_STR);
1227 ItemRibKind(has_generic_params) => has_generic_params,
1228 ConstParamTyRibKind => {
1229 if let Some(span) = finalize {
1232 ResolutionError::ParamInTyOfConstParam(rib_ident.name),
1239 // This was an attempt to use a const parameter outside its scope.
1240 if let Some(span) = finalize {
1243 ResolutionError::GenericParamsFromOuterFunction(
1257 #[instrument(level = "debug", skip(self))]
1258 pub(crate) fn maybe_resolve_path(
1261 opt_ns: Option<Namespace>, // `None` indicates a module path in import
1262 parent_scope: &ParentScope<'a>,
1263 ) -> PathResult<'a> {
1264 self.resolve_path_with_ribs(path, opt_ns, parent_scope, None, None, None)
1267 #[instrument(level = "debug", skip(self))]
1268 pub(crate) fn resolve_path(
1271 opt_ns: Option<Namespace>, // `None` indicates a module path in import
1272 parent_scope: &ParentScope<'a>,
1273 finalize: Option<Finalize>,
1274 ignore_binding: Option<&'a NameBinding<'a>>,
1275 ) -> PathResult<'a> {
1276 self.resolve_path_with_ribs(path, opt_ns, parent_scope, finalize, None, ignore_binding)
1279 pub(crate) fn resolve_path_with_ribs(
1282 opt_ns: Option<Namespace>, // `None` indicates a module path in import
1283 parent_scope: &ParentScope<'a>,
1284 finalize: Option<Finalize>,
1285 ribs: Option<&PerNS<Vec<Rib<'a>>>>,
1286 ignore_binding: Option<&'a NameBinding<'a>>,
1287 ) -> PathResult<'a> {
1288 debug!("resolve_path(path={:?}, opt_ns={:?}, finalize={:?})", path, opt_ns, finalize);
1290 let mut module = None;
1291 let mut allow_super = true;
1292 let mut second_binding = None;
1294 for (i, &Segment { ident, id, .. }) in path.iter().enumerate() {
1295 debug!("resolve_path ident {} {:?} {:?}", i, ident, id);
1296 let record_segment_res = |this: &mut Self, res| {
1297 if finalize.is_some() {
1298 if let Some(id) = id {
1299 if !this.partial_res_map.contains_key(&id) {
1300 assert!(id != ast::DUMMY_NODE_ID, "Trying to resolve dummy id");
1301 this.record_partial_res(id, PartialRes::new(res));
1307 let is_last = i == path.len() - 1;
1308 let ns = if is_last { opt_ns.unwrap_or(TypeNS) } else { TypeNS };
1309 let name = ident.name;
1311 allow_super &= ns == TypeNS && (name == kw::SelfLower || name == kw::Super);
1314 if allow_super && name == kw::Super {
1315 let mut ctxt = ident.span.ctxt().normalize_to_macros_2_0();
1316 let self_module = match i {
1317 0 => Some(self.resolve_self(&mut ctxt, parent_scope.module)),
1319 Some(ModuleOrUniformRoot::Module(module)) => Some(module),
1323 if let Some(self_module) = self_module {
1324 if let Some(parent) = self_module.parent {
1325 module = Some(ModuleOrUniformRoot::Module(
1326 self.resolve_self(&mut ctxt, parent),
1331 return PathResult::failed(ident.span, false, finalize.is_some(), || {
1332 ("there are too many leading `super` keywords".to_string(), None)
1336 if name == kw::SelfLower {
1337 let mut ctxt = ident.span.ctxt().normalize_to_macros_2_0();
1338 module = Some(ModuleOrUniformRoot::Module(
1339 self.resolve_self(&mut ctxt, parent_scope.module),
1343 if name == kw::PathRoot && ident.span.rust_2018() {
1344 module = Some(ModuleOrUniformRoot::ExternPrelude);
1347 if name == kw::PathRoot && ident.span.rust_2015() && self.session.rust_2018() {
1348 // `::a::b` from 2015 macro on 2018 global edition
1349 module = Some(ModuleOrUniformRoot::CrateRootAndExternPrelude);
1352 if name == kw::PathRoot || name == kw::Crate || name == kw::DollarCrate {
1353 // `::a::b`, `crate::a::b` or `$crate::a::b`
1354 module = Some(ModuleOrUniformRoot::Module(self.resolve_crate_root(ident)));
1360 // Report special messages for path segment keywords in wrong positions.
1361 if ident.is_path_segment_keyword() && i != 0 {
1362 return PathResult::failed(ident.span, false, finalize.is_some(), || {
1363 let name_str = if name == kw::PathRoot {
1364 "crate root".to_string()
1366 format!("`{}`", name)
1368 let label = if i == 1 && path[0].ident.name == kw::PathRoot {
1369 format!("global paths cannot start with {}", name_str)
1371 format!("{} in paths can only be used in start position", name_str)
1377 enum FindBindingResult<'a> {
1378 Binding(Result<&'a NameBinding<'a>, Determinacy>),
1381 let find_binding_in_ns = |this: &mut Self, ns| {
1382 let binding = if let Some(module) = module {
1383 this.resolve_ident_in_module(
1391 } else if let Some(ribs) = ribs
1392 && let Some(TypeNS | ValueNS) = opt_ns
1394 match this.resolve_ident_in_lexical_scope(
1402 // we found a locally-imported or available item/module
1403 Some(LexicalScopeBinding::Item(binding)) => Ok(binding),
1404 // we found a local variable or type param
1405 Some(LexicalScopeBinding::Res(res)) => return FindBindingResult::Res(res),
1406 _ => Err(Determinacy::determined(finalize.is_some())),
1409 let scopes = ScopeSet::All(ns, opt_ns.is_none());
1410 this.early_resolve_ident_in_lexical_scope(
1419 FindBindingResult::Binding(binding)
1421 let binding = match find_binding_in_ns(self, ns) {
1422 FindBindingResult::Res(res) => {
1423 record_segment_res(self, res);
1424 return PathResult::NonModule(PartialRes::with_unresolved_segments(
1429 FindBindingResult::Binding(binding) => binding,
1434 second_binding = Some(binding);
1436 let res = binding.res();
1437 let maybe_assoc = opt_ns != Some(MacroNS) && PathSource::Type.is_expected(res);
1438 if let Some(next_module) = binding.module() {
1439 module = Some(ModuleOrUniformRoot::Module(next_module));
1440 record_segment_res(self, res);
1441 } else if res == Res::ToolMod && i + 1 != path.len() {
1442 if binding.is_import() {
1446 "cannot use a tool module through an import",
1448 .span_note(binding.span, "the tool module imported here")
1451 let res = Res::NonMacroAttr(NonMacroAttrKind::Tool);
1452 return PathResult::NonModule(PartialRes::new(res));
1453 } else if res == Res::Err {
1454 return PathResult::NonModule(PartialRes::new(Res::Err));
1455 } else if opt_ns.is_some() && (is_last || maybe_assoc) {
1456 self.lint_if_path_starts_with_module(finalize, path, second_binding);
1457 record_segment_res(self, res);
1458 return PathResult::NonModule(PartialRes::with_unresolved_segments(
1463 return PathResult::failed(ident.span, is_last, finalize.is_some(), || {
1464 let label = format!(
1465 "`{ident}` is {} {}, not a module",
1473 Err(Undetermined) => return PathResult::Indeterminate,
1474 Err(Determined) => {
1475 if let Some(ModuleOrUniformRoot::Module(module)) = module {
1476 if opt_ns.is_some() && !module.is_normal() {
1477 return PathResult::NonModule(PartialRes::with_unresolved_segments(
1478 module.res().unwrap(),
1484 return PathResult::failed(ident.span, is_last, finalize.is_some(), || {
1485 self.report_path_resolution_error(
1500 self.lint_if_path_starts_with_module(finalize, path, second_binding);
1502 PathResult::Module(match module {
1503 Some(module) => module,
1504 None if path.is_empty() => ModuleOrUniformRoot::CurrentScope,
1505 _ => bug!("resolve_path: non-empty path `{:?}` has no module", path),