• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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};
4 use rustc_hir::PrimTy;
5 use rustc_middle::bug;
6 use rustc_middle::ty;
7 use rustc_session::lint::builtin::PROC_MACRO_DERIVE_RESOLUTION_FALLBACK;
8 use rustc_session::lint::BuiltinLintDiagnostics;
9 use rustc_span::def_id::LocalDefId;
10 use rustc_span::hygiene::{ExpnId, ExpnKind, LocalExpnId, MacroKind, SyntaxContext};
11 use rustc_span::symbol::{kw, Ident};
12 use rustc_span::{Span, DUMMY_SP};
13 
14 use crate::errors::{ParamKindInEnumDiscriminant, ParamKindInNonTrivialAnonConst};
15 use crate::late::{
16     ConstantHasGenerics, HasGenericParams, NoConstantGenericsReason, PathSource, Rib, RibKind,
17 };
18 use crate::macros::{sub_namespace_match, MacroRulesScope};
19 use crate::BindingKey;
20 use crate::{errors, AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, Determinacy, Finalize};
21 use crate::{ImportKind, LexicalScopeBinding, Module, ModuleKind, ModuleOrUniformRoot};
22 use crate::{NameBinding, NameBindingKind, ParentScope, PathResult, PrivacyError, Res};
23 use crate::{ResolutionError, Resolver, Scope, ScopeSet, Segment, ToNameBinding, Weak};
24 
25 use Determinacy::*;
26 use Namespace::*;
27 
28 type Visibility = ty::Visibility<LocalDefId>;
29 
30 impl<'a, 'tcx> Resolver<'a, 'tcx> {
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.
visit_scopes<T>( &mut self, scope_set: ScopeSet<'a>, parent_scope: &ParentScope<'a>, ctxt: SyntaxContext, mut visitor: impl FnMut( &mut Self, Scope<'a>, bool, SyntaxContext, ) -> Option<T>, ) -> Option<T>34     pub(crate) fn visit_scopes<T>(
35         &mut self,
36         scope_set: ScopeSet<'a>,
37         parent_scope: &ParentScope<'a>,
38         ctxt: SyntaxContext,
39         mut visitor: impl FnMut(
40             &mut Self,
41             Scope<'a>,
42             /*use_prelude*/ bool,
43             SyntaxContext,
44         ) -> Option<T>,
45     ) -> Option<T> {
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):
52         // (Type NS)
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).
61         // (Value NS)
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).
67         // (Macro NS)
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).
86 
87         let rust_2015 = ctxt.edition().is_rust_2015();
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),
93         };
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(),
99         };
100         let mut scope = match ns {
101             _ if is_absolute_path => Scope::CrateRoot,
102             TypeNS | ValueNS => Scope::Module(module, None),
103             MacroNS => Scope::DeriveHelpers(parent_scope.expansion),
104         };
105         let mut ctxt = ctxt.normalize_to_macros_2_0();
106         let mut use_prelude = !module.no_implicit_prelude;
107 
108         loop {
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))
113                 }
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());
123                         } else {
124                             break;
125                         }
126                     }
127                     true
128                 }
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,
137             };
138 
139             if visit {
140                 if let break_result @ Some(..) = visitor(self, scope, use_prelude, ctxt) {
141                     return break_result;
142                 }
143             }
144 
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 {
151                         ExpnKind::Root
152                         | ExpnKind::Macro(MacroKind::Bang | MacroKind::Derive, _) => {
153                             Scope::DeriveHelpersCompat
154                         }
155                         _ => Scope::DeriveHelpers(expn_data.parent.expect_local()),
156                     }
157                 }
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)
162                     }
163                     MacroRulesScope::Invocation(invoc_id) => {
164                         Scope::MacroRules(self.invocation_parent_scopes[&invoc_id].macro_rules)
165                     }
166                     MacroRulesScope::Empty => Scope::Module(module, None),
167                 },
168                 Scope::CrateRoot => match ns {
169                     TypeNS => {
170                         ctxt.adjust(ExpnId::root());
171                         Scope::ExternPrelude
172                     }
173                     ValueNS | MacroNS => break,
174                 },
175                 Scope::Module(module, prev_lint_id) => {
176                     use_prelude = !module.no_implicit_prelude;
177                     let derive_fallback_lint_id = match scope_set {
178                         ScopeSet::Late(.., lint_id) => lint_id,
179                         _ => None,
180                     };
181                     match self.hygienic_lexical_parent(module, &mut ctxt, derive_fallback_lint_id) {
182                         Some((parent_module, lint_id)) => {
183                             Scope::Module(parent_module, lint_id.or(prev_lint_id))
184                         }
185                         None => {
186                             ctxt.adjust(ExpnId::root());
187                             match ns {
188                                 TypeNS => Scope::ExternPrelude,
189                                 ValueNS => Scope::StdLibPrelude,
190                                 MacroNS => Scope::MacroUsePrelude,
191                             }
192                         }
193                     }
194                 }
195                 Scope::MacroUsePrelude => Scope::StdLibPrelude,
196                 Scope::BuiltinAttrs => break, // nowhere else to search
197                 Scope::ExternPrelude if is_absolute_path => break,
198                 Scope::ExternPrelude => Scope::ToolPrelude,
199                 Scope::ToolPrelude => Scope::StdLibPrelude,
200                 Scope::StdLibPrelude => match ns {
201                     TypeNS => Scope::BuiltinTypes,
202                     ValueNS => break, // nowhere else to search
203                     MacroNS => Scope::BuiltinAttrs,
204                 },
205                 Scope::BuiltinTypes => break, // nowhere else to search
206             };
207         }
208 
209         None
210     }
211 
hygienic_lexical_parent( &mut self, module: Module<'a>, ctxt: &mut SyntaxContext, derive_fallback_lint_id: Option<NodeId>, ) -> Option<(Module<'a>, Option<NodeId>)>212     fn hygienic_lexical_parent(
213         &mut self,
214         module: Module<'a>,
215         ctxt: &mut SyntaxContext,
216         derive_fallback_lint_id: Option<NodeId>,
217     ) -> Option<(Module<'a>, Option<NodeId>)> {
218         if !module.expansion.outer_expn_is_descendant_of(*ctxt) {
219             return Some((self.expn_def_scope(ctxt.remove_mark()), None));
220         }
221 
222         if let ModuleKind::Block = module.kind {
223             return Some((module.parent.unwrap().nearest_item_scope(), None));
224         }
225 
226         // We need to support the next case under a deprecation warning
227         // ```
228         // struct MyStruct;
229         // ---- begin: this comes from a proc macro derive
230         // mod implementation_details {
231         //     // Note that `MyStruct` is not in scope here.
232         //     impl SomeTrait for MyStruct { ... }
233         // }
234         // ---- end
235         // ```
236         // So we have to fall back to the module's parent during lexical resolution in this case.
237         if derive_fallback_lint_id.is_some() {
238             if let Some(parent) = module.parent {
239                 // Inner module is inside the macro, parent module is outside of the macro.
240                 if module.expansion != parent.expansion
241                     && module.expansion.is_descendant_of(parent.expansion)
242                 {
243                     // The macro is a proc macro derive
244                     if let Some(def_id) = module.expansion.expn_data().macro_def_id {
245                         let ext = self.get_macro_by_def_id(def_id).ext;
246                         if ext.builtin_name.is_none()
247                             && ext.macro_kind() == MacroKind::Derive
248                             && parent.expansion.outer_expn_is_descendant_of(*ctxt)
249                         {
250                             return Some((parent, derive_fallback_lint_id));
251                         }
252                     }
253                 }
254             }
255         }
256 
257         None
258     }
259 
260     /// This resolves the identifier `ident` in the namespace `ns` in the current lexical scope.
261     /// More specifically, we proceed up the hierarchy of scopes and return the binding for
262     /// `ident` in the first scope that defines it (or None if no scopes define it).
263     ///
264     /// A block's items are above its local variables in the scope hierarchy, regardless of where
265     /// the items are defined in the block. For example,
266     /// ```rust
267     /// fn f() {
268     ///    g(); // Since there are no local variables in scope yet, this resolves to the item.
269     ///    let g = || {};
270     ///    fn g() {}
271     ///    g(); // This resolves to the local variable `g` since it shadows the item.
272     /// }
273     /// ```
274     ///
275     /// Invariant: This must only be called during main resolution, not during
276     /// import resolution.
277     #[instrument(level = "debug", skip(self, ribs))]
resolve_ident_in_lexical_scope( &mut self, mut ident: Ident, ns: Namespace, parent_scope: &ParentScope<'a>, finalize: Option<Finalize>, ribs: &[Rib<'a>], ignore_binding: Option<NameBinding<'a>>, ) -> Option<LexicalScopeBinding<'a>>278     pub(crate) fn resolve_ident_in_lexical_scope(
279         &mut self,
280         mut ident: Ident,
281         ns: Namespace,
282         parent_scope: &ParentScope<'a>,
283         finalize: Option<Finalize>,
284         ribs: &[Rib<'a>],
285         ignore_binding: Option<NameBinding<'a>>,
286     ) -> Option<LexicalScopeBinding<'a>> {
287         assert!(ns == TypeNS || ns == ValueNS);
288         let orig_ident = ident;
289         if ident.name == kw::Empty {
290             return Some(LexicalScopeBinding::Res(Res::Err));
291         }
292         let (general_span, normalized_span) = if ident.name == kw::SelfUpper {
293             // FIXME(jseyfried) improve `Self` hygiene
294             let empty_span = ident.span.with_ctxt(SyntaxContext::root());
295             (empty_span, empty_span)
296         } else if ns == TypeNS {
297             let normalized_span = ident.span.normalize_to_macros_2_0();
298             (normalized_span, normalized_span)
299         } else {
300             (ident.span.normalize_to_macro_rules(), ident.span.normalize_to_macros_2_0())
301         };
302         ident.span = general_span;
303         let normalized_ident = Ident { span: normalized_span, ..ident };
304 
305         // Walk backwards up the ribs in scope.
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)
313             {
314                 // The ident resolves to a type parameter or local variable.
315                 return Some(LexicalScopeBinding::Res(self.validate_res_from_ribs(
316                     i,
317                     rib_ident,
318                     *res,
319                     finalize.map(|finalize| finalize.path_span),
320                     *original_rib_ident_def,
321                     ribs,
322                 )));
323             }
324 
325             module = match ribs[i].kind {
326                 RibKind::Module(module) => module,
327                 RibKind::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();
331                     continue;
332                 }
333                 _ => continue,
334             };
335 
336             match module.kind {
337                 ModuleKind::Block => {} // We can see through blocks
338                 _ => break,
339             }
340 
341             let item = self.resolve_ident_in_module_unadjusted(
342                 ModuleOrUniformRoot::Module(module),
343                 ident,
344                 ns,
345                 parent_scope,
346                 finalize,
347                 ignore_binding,
348             );
349             if let Ok(binding) = item {
350                 // The ident resolves to an item.
351                 return Some(LexicalScopeBinding::Item(binding));
352             }
353         }
354         self.early_resolve_ident_in_lexical_scope(
355             orig_ident,
356             ScopeSet::Late(ns, module, finalize.map(|finalize| finalize.node_id)),
357             parent_scope,
358             finalize,
359             finalize.is_some(),
360             ignore_binding,
361         )
362         .ok()
363         .map(LexicalScopeBinding::Item)
364     }
365 
366     /// Resolve an identifier in lexical scope.
367     /// This is a variation of `fn resolve_ident_in_lexical_scope` that can be run during
368     /// expansion and import resolution (perhaps they can be merged in the future).
369     /// The function is used for resolving initial segments of macro paths (e.g., `foo` in
370     /// `foo::bar!();` or `foo!();`) and also for import paths on 2018 edition.
371     #[instrument(level = "debug", skip(self))]
early_resolve_ident_in_lexical_scope( &mut self, orig_ident: Ident, scope_set: ScopeSet<'a>, parent_scope: &ParentScope<'a>, finalize: Option<Finalize>, force: bool, ignore_binding: Option<NameBinding<'a>>, ) -> Result<NameBinding<'a>, Determinacy>372     pub(crate) fn early_resolve_ident_in_lexical_scope(
373         &mut self,
374         orig_ident: Ident,
375         scope_set: ScopeSet<'a>,
376         parent_scope: &ParentScope<'a>,
377         finalize: Option<Finalize>,
378         force: bool,
379         ignore_binding: Option<NameBinding<'a>>,
380     ) -> Result<NameBinding<'a>, Determinacy> {
381         bitflags::bitflags! {
382             struct Flags: u8 {
383                 const MACRO_RULES          = 1 << 0;
384                 const MODULE               = 1 << 1;
385                 const MISC_SUGGEST_CRATE   = 1 << 2;
386                 const MISC_SUGGEST_SELF    = 1 << 3;
387                 const MISC_FROM_PRELUDE    = 1 << 4;
388             }
389         }
390 
391         assert!(force || finalize.is_none()); // `finalize` implies `force`
392 
393         // Make sure `self`, `super` etc produce an error when passed to here.
394         if orig_ident.is_path_segment_keyword() {
395             return Err(Determinacy::Determined);
396         }
397 
398         let (ns, macro_kind) = match scope_set {
399             ScopeSet::All(ns) => (ns, None),
400             ScopeSet::AbsolutePath(ns) => (ns, None),
401             ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind)),
402             ScopeSet::Late(ns, ..) => (ns, None),
403         };
404 
405         // This is *the* result, resolution from the scope closest to the resolved identifier.
406         // However, sometimes this result is "weak" because it comes from a glob import or
407         // a macro expansion, and in this case it cannot shadow names from outer scopes, e.g.
408         // mod m { ... } // solution in outer scope
409         // {
410         //     use prefix::*; // imports another `m` - innermost solution
411         //                    // weak, cannot shadow the outer `m`, need to report ambiguity error
412         //     m::mac!();
413         // }
414         // So we have to save the innermost solution and continue searching in outer scopes
415         // to detect potential ambiguities.
416         let mut innermost_result: Option<(NameBinding<'_>, Flags)> = None;
417         let mut determinacy = Determinacy::Determined;
418 
419         // Go through all the scopes and try to resolve the name.
420         let break_result = self.visit_scopes(
421             scope_set,
422             parent_scope,
423             orig_ident.span.ctxt(),
424             |this, scope, use_prelude, ctxt| {
425                 let ident = Ident::new(orig_ident.name, orig_ident.span.with_ctxt(ctxt));
426                 let ok = |res, span, arenas| {
427                     Ok((
428                         (res, Visibility::Public, span, LocalExpnId::ROOT).to_name_binding(arenas),
429                         Flags::empty(),
430                     ))
431                 };
432                 let result = match scope {
433                     Scope::DeriveHelpers(expn_id) => {
434                         if let Some(attr) = this
435                             .helper_attrs
436                             .get(&expn_id)
437                             .and_then(|attrs| attrs.iter().rfind(|i| ident == **i))
438                         {
439                             let binding = (
440                                 Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper),
441                                 Visibility::Public,
442                                 attr.span,
443                                 expn_id,
444                             )
445                                 .to_name_binding(this.arenas);
446                             Ok((binding, Flags::empty()))
447                         } else {
448                             Err(Determinacy::Determined)
449                         }
450                     }
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(
456                                 derive,
457                                 Some(MacroKind::Derive),
458                                 parent_scope,
459                                 true,
460                                 force,
461                             ) {
462                                 Ok((Some(ext), _)) => {
463                                     if ext.helper_attrs.contains(&ident.name) {
464                                         result = ok(
465                                             Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat),
466                                             derive.span,
467                                             this.arenas,
468                                         );
469                                         break;
470                                     }
471                                 }
472                                 Ok(_) | Err(Determinacy::Determined) => {}
473                                 Err(Determinacy::Undetermined) => {
474                                     result = Err(Determinacy::Undetermined)
475                                 }
476                             }
477                         }
478                         result
479                     }
480                     Scope::MacroRules(macro_rules_scope) => match macro_rules_scope.get() {
481                         MacroRulesScope::Binding(macro_rules_binding)
482                             if ident == macro_rules_binding.ident =>
483                         {
484                             Ok((macro_rules_binding.binding, Flags::MACRO_RULES))
485                         }
486                         MacroRulesScope::Invocation(_) => Err(Determinacy::Undetermined),
487                         _ => Err(Determinacy::Determined),
488                     },
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),
494                             ident,
495                             ns,
496                             parent_scope,
497                             finalize,
498                             ignore_binding,
499                         );
500                         match binding {
501                             Ok(binding) => Ok((binding, Flags::MODULE | Flags::MISC_SUGGEST_CRATE)),
502                             Err((Determinacy::Undetermined, Weak::No)) => {
503                                 return Some(Err(Determinacy::determined(force)));
504                             }
505                             Err((Determinacy::Undetermined, Weak::Yes)) => {
506                                 Err(Determinacy::Undetermined)
507                             }
508                             Err((Determinacy::Determined, _)) => Err(Determinacy::Determined),
509                         }
510                     }
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),
515                             ident,
516                             ns,
517                             adjusted_parent_scope,
518                             !matches!(scope_set, ScopeSet::Late(..)),
519                             finalize,
520                             ignore_binding,
521                         );
522                         match binding {
523                             Ok(binding) => {
524                                 if let Some(lint_id) = derive_fallback_lint_id {
525                                     this.lint_buffer.buffer_lint_with_diagnostic(
526                                         PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
527                                         lint_id,
528                                         orig_ident.span,
529                                         format!(
530                                             "cannot find {} `{}` in this scope",
531                                             ns.descr(),
532                                             ident
533                                         ),
534                                         BuiltinLintDiagnostics::ProcMacroDeriveResolutionFallback(
535                                             orig_ident.span,
536                                         ),
537                                     );
538                                 }
539                                 let misc_flags = if module == this.graph_root {
540                                     Flags::MISC_SUGGEST_CRATE
541                                 } else if module.is_normal() {
542                                     Flags::MISC_SUGGEST_SELF
543                                 } else {
544                                     Flags::empty()
545                                 };
546                                 Ok((binding, Flags::MODULE | misc_flags))
547                             }
548                             Err((Determinacy::Undetermined, Weak::No)) => {
549                                 return Some(Err(Determinacy::determined(force)));
550                             }
551                             Err((Determinacy::Undetermined, Weak::Yes)) => {
552                                 Err(Determinacy::Undetermined)
553                             }
554                             Err((Determinacy::Determined, _)) => Err(Determinacy::Determined),
555                         }
556                     }
557                     Scope::MacroUsePrelude => {
558                         match this.macro_use_prelude.get(&ident.name).cloned() {
559                             Some(binding) => Ok((binding, Flags::MISC_FROM_PRELUDE)),
560                             None => Err(Determinacy::determined(
561                                 this.graph_root.unexpanded_invocations.borrow().is_empty(),
562                             )),
563                         }
564                     }
565                     Scope::BuiltinAttrs => {
566                         if is_builtin_attr_name(ident.name) {
567                             ok(
568                                 Res::NonMacroAttr(NonMacroAttrKind::Builtin(ident.name)),
569                                 DUMMY_SP,
570                                 this.arenas,
571                             )
572                         } else {
573                             Err(Determinacy::Determined)
574                         }
575                     }
576                     Scope::ExternPrelude => {
577                         match this.extern_prelude_get(ident, finalize.is_some()) {
578                             Some(binding) => Ok((binding, Flags::empty())),
579                             None => Err(Determinacy::determined(
580                                 this.graph_root.unexpanded_invocations.borrow().is_empty(),
581                             )),
582                         }
583                     }
584                     Scope::ToolPrelude => match this.registered_tools.get(&ident).cloned() {
585                         Some(ident) => ok(Res::ToolMod, ident.span, this.arenas),
586                         None => Err(Determinacy::Determined),
587                     },
588                     Scope::StdLibPrelude => {
589                         let mut result = Err(Determinacy::Determined);
590                         if let Some(prelude) = this.prelude {
591                             if let Ok(binding) = this.resolve_ident_in_module_unadjusted(
592                                 ModuleOrUniformRoot::Module(prelude),
593                                 ident,
594                                 ns,
595                                 parent_scope,
596                                 None,
597                                 ignore_binding,
598                             ) {
599                                 if use_prelude || this.is_builtin_macro(binding.res()) {
600                                     result = Ok((binding, Flags::MISC_FROM_PRELUDE));
601                                 }
602                             }
603                         }
604                         result
605                     }
606                     Scope::BuiltinTypes => match PrimTy::from_name(ident.name) {
607                         Some(prim_ty) => ok(Res::PrimTy(prim_ty), DUMMY_SP, this.arenas),
608                         None => Err(Determinacy::Determined),
609                     },
610                 };
611 
612                 match result {
613                     Ok((binding, flags))
614                         if sub_namespace_match(binding.macro_kind(), macro_kind) =>
615                     {
616                         if finalize.is_none() || matches!(scope_set, ScopeSet::Late(..)) {
617                             return Some(Ok(binding));
618                         }
619 
620                         if let Some((innermost_binding, innermost_flags)) = innermost_result {
621                             // Found another solution, if the first one was "weak", report an error.
622                             let (res, innermost_res) = (binding.res(), innermost_binding.res());
623                             if res != innermost_res {
624                                 let is_builtin = |res| {
625                                     matches!(res, Res::NonMacroAttr(NonMacroAttrKind::Builtin(..)))
626                                 };
627                                 let derive_helper =
628                                     Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper);
629                                 let derive_helper_compat =
630                                     Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat);
631 
632                                 let ambiguity_error_kind = if is_builtin(innermost_res)
633                                     || is_builtin(res)
634                                 {
635                                     Some(AmbiguityKind::BuiltinAttr)
636                                 } else if innermost_res == derive_helper_compat
637                                     || res == derive_helper_compat && innermost_res != derive_helper
638                                 {
639                                     Some(AmbiguityKind::DeriveHelper)
640                                 } else if innermost_flags.contains(Flags::MACRO_RULES)
641                                     && flags.contains(Flags::MODULE)
642                                     && !this.disambiguate_macro_rules_vs_modularized(
643                                         innermost_binding,
644                                         binding,
645                                     )
646                                     || flags.contains(Flags::MACRO_RULES)
647                                         && innermost_flags.contains(Flags::MODULE)
648                                         && !this.disambiguate_macro_rules_vs_modularized(
649                                             binding,
650                                             innermost_binding,
651                                         )
652                                 {
653                                     Some(AmbiguityKind::MacroRulesVsModularized)
654                                 } else if innermost_binding.is_glob_import() {
655                                     Some(AmbiguityKind::GlobVsOuter)
656                                 } else if innermost_binding
657                                     .may_appear_after(parent_scope.expansion, binding)
658                                 {
659                                     Some(AmbiguityKind::MoreExpandedVsOuter)
660                                 } else {
661                                     None
662                                 };
663                                 if let Some(kind) = ambiguity_error_kind {
664                                     let misc = |f: Flags| {
665                                         if f.contains(Flags::MISC_SUGGEST_CRATE) {
666                                             AmbiguityErrorMisc::SuggestCrate
667                                         } else if f.contains(Flags::MISC_SUGGEST_SELF) {
668                                             AmbiguityErrorMisc::SuggestSelf
669                                         } else if f.contains(Flags::MISC_FROM_PRELUDE) {
670                                             AmbiguityErrorMisc::FromPrelude
671                                         } else {
672                                             AmbiguityErrorMisc::None
673                                         }
674                                     };
675                                     this.ambiguity_errors.push(AmbiguityError {
676                                         kind,
677                                         ident: orig_ident,
678                                         b1: innermost_binding,
679                                         b2: binding,
680                                         misc1: misc(innermost_flags),
681                                         misc2: misc(flags),
682                                     });
683                                     return Some(Ok(innermost_binding));
684                                 }
685                             }
686                         } else {
687                             // Found the first solution.
688                             innermost_result = Some((binding, flags));
689                         }
690                     }
691                     Ok(..) | Err(Determinacy::Determined) => {}
692                     Err(Determinacy::Undetermined) => determinacy = Determinacy::Undetermined,
693                 }
694 
695                 None
696             },
697         );
698 
699         if let Some(break_result) = break_result {
700             return break_result;
701         }
702 
703         // The first found solution was the only one, return it.
704         if let Some((binding, _)) = innermost_result {
705             return Ok(binding);
706         }
707 
708         Err(Determinacy::determined(determinacy == Determinacy::Determined || force))
709     }
710 
711     #[instrument(level = "debug", skip(self))]
maybe_resolve_ident_in_module( &mut self, module: ModuleOrUniformRoot<'a>, ident: Ident, ns: Namespace, parent_scope: &ParentScope<'a>, ) -> Result<NameBinding<'a>, Determinacy>712     pub(crate) fn maybe_resolve_ident_in_module(
713         &mut self,
714         module: ModuleOrUniformRoot<'a>,
715         ident: Ident,
716         ns: Namespace,
717         parent_scope: &ParentScope<'a>,
718     ) -> Result<NameBinding<'a>, Determinacy> {
719         self.resolve_ident_in_module_ext(module, ident, ns, parent_scope, None, None)
720             .map_err(|(determinacy, _)| determinacy)
721     }
722 
723     #[instrument(level = "debug", skip(self))]
resolve_ident_in_module( &mut self, module: ModuleOrUniformRoot<'a>, ident: Ident, ns: Namespace, parent_scope: &ParentScope<'a>, finalize: Option<Finalize>, ignore_binding: Option<NameBinding<'a>>, ) -> Result<NameBinding<'a>, Determinacy>724     pub(crate) fn resolve_ident_in_module(
725         &mut self,
726         module: ModuleOrUniformRoot<'a>,
727         ident: Ident,
728         ns: Namespace,
729         parent_scope: &ParentScope<'a>,
730         finalize: Option<Finalize>,
731         ignore_binding: Option<NameBinding<'a>>,
732     ) -> Result<NameBinding<'a>, Determinacy> {
733         self.resolve_ident_in_module_ext(module, ident, ns, parent_scope, finalize, ignore_binding)
734             .map_err(|(determinacy, _)| determinacy)
735     }
736 
737     #[instrument(level = "debug", skip(self))]
resolve_ident_in_module_ext( &mut self, module: ModuleOrUniformRoot<'a>, mut ident: Ident, ns: Namespace, parent_scope: &ParentScope<'a>, finalize: Option<Finalize>, ignore_binding: Option<NameBinding<'a>>, ) -> Result<NameBinding<'a>, (Determinacy, Weak)>738     fn resolve_ident_in_module_ext(
739         &mut self,
740         module: ModuleOrUniformRoot<'a>,
741         mut ident: Ident,
742         ns: Namespace,
743         parent_scope: &ParentScope<'a>,
744         finalize: Option<Finalize>,
745         ignore_binding: Option<NameBinding<'a>>,
746     ) -> Result<NameBinding<'a>, (Determinacy, Weak)> {
747         let tmp_parent_scope;
748         let mut adjusted_parent_scope = parent_scope;
749         match module {
750             ModuleOrUniformRoot::Module(m) => {
751                 if let Some(def) = ident.span.normalize_to_macros_2_0_and_adjust(m.expansion) {
752                     tmp_parent_scope =
753                         ParentScope { module: self.expn_def_scope(def), ..*parent_scope };
754                     adjusted_parent_scope = &tmp_parent_scope;
755                 }
756             }
757             ModuleOrUniformRoot::ExternPrelude => {
758                 ident.span.normalize_to_macros_2_0_and_adjust(ExpnId::root());
759             }
760             ModuleOrUniformRoot::CrateRootAndExternPrelude | ModuleOrUniformRoot::CurrentScope => {
761                 // No adjustments
762             }
763         }
764         self.resolve_ident_in_module_unadjusted_ext(
765             module,
766             ident,
767             ns,
768             adjusted_parent_scope,
769             false,
770             finalize,
771             ignore_binding,
772         )
773     }
774 
775     #[instrument(level = "debug", skip(self))]
resolve_ident_in_module_unadjusted( &mut self, module: ModuleOrUniformRoot<'a>, ident: Ident, ns: Namespace, parent_scope: &ParentScope<'a>, finalize: Option<Finalize>, ignore_binding: Option<NameBinding<'a>>, ) -> Result<NameBinding<'a>, Determinacy>776     fn resolve_ident_in_module_unadjusted(
777         &mut self,
778         module: ModuleOrUniformRoot<'a>,
779         ident: Ident,
780         ns: Namespace,
781         parent_scope: &ParentScope<'a>,
782         finalize: Option<Finalize>,
783         ignore_binding: Option<NameBinding<'a>>,
784     ) -> Result<NameBinding<'a>, Determinacy> {
785         self.resolve_ident_in_module_unadjusted_ext(
786             module,
787             ident,
788             ns,
789             parent_scope,
790             false,
791             finalize,
792             ignore_binding,
793         )
794         .map_err(|(determinacy, _)| determinacy)
795     }
796 
797     /// Attempts to resolve `ident` in namespaces `ns` of `module`.
798     /// Invariant: if `finalize` is `Some`, expansion and import resolution must be complete.
799     #[instrument(level = "debug", skip(self))]
resolve_ident_in_module_unadjusted_ext( &mut self, module: ModuleOrUniformRoot<'a>, ident: Ident, ns: Namespace, parent_scope: &ParentScope<'a>, restricted_shadowing: bool, finalize: Option<Finalize>, ignore_binding: Option<NameBinding<'a>>, ) -> Result<NameBinding<'a>, (Determinacy, Weak)>800     fn resolve_ident_in_module_unadjusted_ext(
801         &mut self,
802         module: ModuleOrUniformRoot<'a>,
803         ident: Ident,
804         ns: Namespace,
805         parent_scope: &ParentScope<'a>,
806         restricted_shadowing: bool,
807         finalize: Option<Finalize>,
808         // This binding should be ignored during in-module resolution, so that we don't get
809         // "self-confirming" import resolutions during import validation and checking.
810         ignore_binding: Option<NameBinding<'a>>,
811     ) -> Result<NameBinding<'a>, (Determinacy, Weak)> {
812         let module = match module {
813             ModuleOrUniformRoot::Module(module) => module,
814             ModuleOrUniformRoot::CrateRootAndExternPrelude => {
815                 assert!(!restricted_shadowing);
816                 let binding = self.early_resolve_ident_in_lexical_scope(
817                     ident,
818                     ScopeSet::AbsolutePath(ns),
819                     parent_scope,
820                     finalize,
821                     finalize.is_some(),
822                     ignore_binding,
823                 );
824                 return binding.map_err(|determinacy| (determinacy, Weak::No));
825             }
826             ModuleOrUniformRoot::ExternPrelude => {
827                 assert!(!restricted_shadowing);
828                 return if ns != TypeNS {
829                     Err((Determined, Weak::No))
830                 } else if let Some(binding) = self.extern_prelude_get(ident, finalize.is_some()) {
831                     Ok(binding)
832                 } else if !self.graph_root.unexpanded_invocations.borrow().is_empty() {
833                     // Macro-expanded `extern crate` items can add names to extern prelude.
834                     Err((Undetermined, Weak::No))
835                 } else {
836                     Err((Determined, Weak::No))
837                 };
838             }
839             ModuleOrUniformRoot::CurrentScope => {
840                 assert!(!restricted_shadowing);
841                 if ns == TypeNS {
842                     if ident.name == kw::Crate || ident.name == kw::DollarCrate {
843                         let module = self.resolve_crate_root(ident);
844                         let binding = (module, Visibility::Public, module.span, LocalExpnId::ROOT)
845                             .to_name_binding(self.arenas);
846                         return Ok(binding);
847                     } else if ident.name == kw::Super || ident.name == kw::SelfLower {
848                         // FIXME: Implement these with renaming requirements so that e.g.
849                         // `use super;` doesn't work, but `use super as name;` does.
850                         // Fall through here to get an error from `early_resolve_...`.
851                     }
852                 }
853 
854                 let binding = self.early_resolve_ident_in_lexical_scope(
855                     ident,
856                     ScopeSet::All(ns),
857                     parent_scope,
858                     finalize,
859                     finalize.is_some(),
860                     ignore_binding,
861                 );
862                 return binding.map_err(|determinacy| (determinacy, Weak::No));
863             }
864         };
865 
866         let key = BindingKey::new(ident, ns);
867         let resolution =
868             self.resolution(module, key).try_borrow_mut().map_err(|_| (Determined, Weak::No))?; // This happens when there is a cycle of imports.
869 
870         // If the primary binding is unusable, search further and return the shadowed glob
871         // binding if it exists. What we really want here is having two separate scopes in
872         // a module - one for non-globs and one for globs, but until that's done use this
873         // hack to avoid inconsistent resolution ICEs during import validation.
874         let binding = [resolution.binding, resolution.shadowed_glob]
875             .into_iter()
876             .find_map(|binding| if binding == ignore_binding { None } else { binding });
877 
878         if let Some(Finalize { path_span, report_private, .. }) = finalize {
879             let Some(binding) = binding else {
880                 return Err((Determined, Weak::No));
881             };
882 
883             if !self.is_accessible_from(binding.vis, parent_scope.module) {
884                 if report_private {
885                     self.privacy_errors.push(PrivacyError {
886                         ident,
887                         binding,
888                         dedup_span: path_span,
889                         outermost_res: None,
890                         parent_scope: *parent_scope,
891                     });
892                 } else {
893                     return Err((Determined, Weak::No));
894                 }
895             }
896 
897             // Forbid expanded shadowing to avoid time travel.
898             if let Some(shadowed_glob) = resolution.shadowed_glob
899                 && restricted_shadowing
900                 && binding.expansion != LocalExpnId::ROOT
901                 && binding.res() != shadowed_glob.res()
902             {
903                 self.ambiguity_errors.push(AmbiguityError {
904                     kind: AmbiguityKind::GlobVsExpanded,
905                     ident,
906                     b1: binding,
907                     b2: shadowed_glob,
908                     misc1: AmbiguityErrorMisc::None,
909                     misc2: AmbiguityErrorMisc::None,
910                 });
911             }
912 
913             if !restricted_shadowing && binding.expansion != LocalExpnId::ROOT {
914                 if let NameBindingKind::Import { import, .. } = binding.kind
915                     && matches!(import.kind, ImportKind::MacroExport) {
916                     self.macro_expanded_macro_export_errors.insert((path_span, binding.span));
917                 }
918             }
919 
920             self.record_use(ident, binding, restricted_shadowing);
921             return Ok(binding);
922         }
923 
924         let check_usable = |this: &mut Self, binding: NameBinding<'a>| {
925             let usable = this.is_accessible_from(binding.vis, parent_scope.module);
926             if usable { Ok(binding) } else { Err((Determined, Weak::No)) }
927         };
928 
929         // Items and single imports are not shadowable, if we have one, then it's determined.
930         if let Some(binding) = binding {
931             if !binding.is_glob_import() {
932                 return check_usable(self, binding);
933             }
934         }
935 
936         // --- From now on we either have a glob resolution or no resolution. ---
937 
938         // Check if one of single imports can still define the name,
939         // if it can then our result is not determined and can be invalidated.
940         for single_import in &resolution.single_imports {
941             let Some(import_vis) = single_import.vis.get() else {
942                 continue;
943             };
944             if !self.is_accessible_from(import_vis, parent_scope.module) {
945                 continue;
946             }
947             if let Some(ignored) = ignore_binding &&
948                 let NameBindingKind::Import { import, .. } = ignored.kind &&
949                 import == *single_import {
950                 // Ignore not just the binding itself, but if it has a shadowed_glob,
951                 // ignore that, too, because this loop is supposed to only process
952                 // named imports.
953                 continue;
954             }
955             let Some(module) = single_import.imported_module.get() else {
956                 return Err((Undetermined, Weak::No));
957             };
958             let ImportKind::Single { source: ident, .. } = single_import.kind else {
959                 unreachable!();
960             };
961             match self.resolve_ident_in_module(
962                 module,
963                 ident,
964                 ns,
965                 &single_import.parent_scope,
966                 None,
967                 ignore_binding,
968             ) {
969                 Err(Determined) => continue,
970                 Ok(binding)
971                     if !self.is_accessible_from(binding.vis, single_import.parent_scope.module) =>
972                 {
973                     continue;
974                 }
975                 Ok(_) | Err(Undetermined) => return Err((Undetermined, Weak::No)),
976             }
977         }
978 
979         // So we have a resolution that's from a glob import. This resolution is determined
980         // if it cannot be shadowed by some new item/import expanded from a macro.
981         // This happens either if there are no unexpanded macros, or expanded names cannot
982         // shadow globs (that happens in macro namespace or with restricted shadowing).
983         //
984         // Additionally, any macro in any module can plant names in the root module if it creates
985         // `macro_export` macros, so the root module effectively has unresolved invocations if any
986         // module has unresolved invocations.
987         // However, it causes resolution/expansion to stuck too often (#53144), so, to make
988         // progress, we have to ignore those potential unresolved invocations from other modules
989         // and prohibit access to macro-expanded `macro_export` macros instead (unless restricted
990         // shadowing is enabled, see `macro_expanded_macro_export_errors`).
991         let unexpanded_macros = !module.unexpanded_invocations.borrow().is_empty();
992         if let Some(binding) = binding {
993             if !unexpanded_macros || ns == MacroNS || restricted_shadowing {
994                 return check_usable(self, binding);
995             } else {
996                 return Err((Undetermined, Weak::No));
997             }
998         }
999 
1000         // --- From now on we have no resolution. ---
1001 
1002         // Now we are in situation when new item/import can appear only from a glob or a macro
1003         // expansion. With restricted shadowing names from globs and macro expansions cannot
1004         // shadow names from outer scopes, so we can freely fallback from module search to search
1005         // in outer scopes. For `early_resolve_ident_in_lexical_scope` to continue search in outer
1006         // scopes we return `Undetermined` with `Weak::Yes`.
1007 
1008         // Check if one of unexpanded macros can still define the name,
1009         // if it can then our "no resolution" result is not determined and can be invalidated.
1010         if unexpanded_macros {
1011             return Err((Undetermined, Weak::Yes));
1012         }
1013 
1014         // Check if one of glob imports can still define the name,
1015         // if it can then our "no resolution" result is not determined and can be invalidated.
1016         for glob_import in module.globs.borrow().iter() {
1017             let Some(import_vis) = glob_import.vis.get() else {
1018                 continue;
1019             };
1020             if !self.is_accessible_from(import_vis, parent_scope.module) {
1021                 continue;
1022             }
1023             let module = match glob_import.imported_module.get() {
1024                 Some(ModuleOrUniformRoot::Module(module)) => module,
1025                 Some(_) => continue,
1026                 None => return Err((Undetermined, Weak::Yes)),
1027             };
1028             let tmp_parent_scope;
1029             let (mut adjusted_parent_scope, mut ident) =
1030                 (parent_scope, ident.normalize_to_macros_2_0());
1031             match ident.span.glob_adjust(module.expansion, glob_import.span) {
1032                 Some(Some(def)) => {
1033                     tmp_parent_scope =
1034                         ParentScope { module: self.expn_def_scope(def), ..*parent_scope };
1035                     adjusted_parent_scope = &tmp_parent_scope;
1036                 }
1037                 Some(None) => {}
1038                 None => continue,
1039             };
1040             let result = self.resolve_ident_in_module_unadjusted(
1041                 ModuleOrUniformRoot::Module(module),
1042                 ident,
1043                 ns,
1044                 adjusted_parent_scope,
1045                 None,
1046                 ignore_binding,
1047             );
1048 
1049             match result {
1050                 Err(Determined) => continue,
1051                 Ok(binding)
1052                     if !self.is_accessible_from(binding.vis, glob_import.parent_scope.module) =>
1053                 {
1054                     continue;
1055                 }
1056                 Ok(_) | Err(Undetermined) => return Err((Undetermined, Weak::Yes)),
1057             }
1058         }
1059 
1060         // No resolution and no one else can define the name - determinate error.
1061         Err((Determined, Weak::No))
1062     }
1063 
1064     /// Validate a local resolution (from ribs).
1065     #[instrument(level = "debug", skip(self, all_ribs))]
validate_res_from_ribs( &mut self, rib_index: usize, rib_ident: Ident, mut res: Res, finalize: Option<Span>, original_rib_ident_def: Ident, all_ribs: &[Rib<'a>], ) -> Res1066     fn validate_res_from_ribs(
1067         &mut self,
1068         rib_index: usize,
1069         rib_ident: Ident,
1070         mut res: Res,
1071         finalize: Option<Span>,
1072         original_rib_ident_def: Ident,
1073         all_ribs: &[Rib<'a>],
1074     ) -> Res {
1075         const CG_BUG_STR: &str = "min_const_generics resolve check didn't stop compilation";
1076         debug!("validate_res_from_ribs({:?})", res);
1077         let ribs = &all_ribs[rib_index + 1..];
1078 
1079         // An invalid forward use of a generic parameter from a previous default.
1080         if let RibKind::ForwardGenericParamBan = all_ribs[rib_index].kind {
1081             if let Some(span) = finalize {
1082                 let res_error = if rib_ident.name == kw::SelfUpper {
1083                     ResolutionError::SelfInGenericParamDefault
1084                 } else {
1085                     ResolutionError::ForwardDeclaredGenericParam
1086                 };
1087                 self.report_error(span, res_error);
1088             }
1089             assert_eq!(res, Res::Err);
1090             return Res::Err;
1091         }
1092 
1093         match res {
1094             Res::Local(_) => {
1095                 use ResolutionError::*;
1096                 let mut res_err = None;
1097 
1098                 for rib in ribs {
1099                     match rib.kind {
1100                         RibKind::Normal
1101                         | RibKind::ClosureOrAsync
1102                         | RibKind::Module(..)
1103                         | RibKind::MacroDefinition(..)
1104                         | RibKind::ForwardGenericParamBan => {
1105                             // Nothing to do. Continue.
1106                         }
1107                         RibKind::Item(_) | RibKind::AssocItem => {
1108                             // This was an attempt to access an upvar inside a
1109                             // named function item. This is not allowed, so we
1110                             // report an error.
1111                             if let Some(span) = finalize {
1112                                 // We don't immediately trigger a resolve error, because
1113                                 // we want certain other resolution errors (namely those
1114                                 // emitted for `ConstantItemRibKind` below) to take
1115                                 // precedence.
1116                                 res_err = Some((span, CannotCaptureDynamicEnvironmentInFnItem));
1117                             }
1118                         }
1119                         RibKind::ConstantItem(_, item) => {
1120                             // Still doesn't deal with upvars
1121                             if let Some(span) = finalize {
1122                                 let (span, resolution_error) = match item {
1123                                     None if rib_ident.as_str() == "self" => (span, LowercaseSelf),
1124                                     None => (
1125                                         rib_ident.span,
1126                                         AttemptToUseNonConstantValueInConstant(
1127                                             original_rib_ident_def,
1128                                             "const",
1129                                             "let",
1130                                         ),
1131                                     ),
1132                                     Some((ident, kind)) => (
1133                                         span,
1134                                         AttemptToUseNonConstantValueInConstant(
1135                                             ident,
1136                                             "let",
1137                                             kind.as_str(),
1138                                         ),
1139                                     ),
1140                                 };
1141                                 self.report_error(span, resolution_error);
1142                             }
1143                             return Res::Err;
1144                         }
1145                         RibKind::ConstParamTy => {
1146                             if let Some(span) = finalize {
1147                                 self.report_error(
1148                                     span,
1149                                     ParamInTyOfConstParam {
1150                                         name: rib_ident.name,
1151                                         param_kind: None,
1152                                     },
1153                                 );
1154                             }
1155                             return Res::Err;
1156                         }
1157                         RibKind::InlineAsmSym => {
1158                             if let Some(span) = finalize {
1159                                 self.report_error(span, InvalidAsmSym);
1160                             }
1161                             return Res::Err;
1162                         }
1163                     }
1164                 }
1165                 if let Some((span, res_err)) = res_err {
1166                     self.report_error(span, res_err);
1167                     return Res::Err;
1168                 }
1169             }
1170             Res::Def(DefKind::TyParam, _) | Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } => {
1171                 for rib in ribs {
1172                     let has_generic_params: HasGenericParams = match rib.kind {
1173                         RibKind::Normal
1174                         | RibKind::ClosureOrAsync
1175                         | RibKind::Module(..)
1176                         | RibKind::MacroDefinition(..)
1177                         | RibKind::InlineAsmSym
1178                         | RibKind::AssocItem
1179                         | RibKind::ForwardGenericParamBan => {
1180                             // Nothing to do. Continue.
1181                             continue;
1182                         }
1183 
1184                         RibKind::ConstantItem(trivial, _) => {
1185                             if let ConstantHasGenerics::No(cause) = trivial {
1186                                 // HACK(min_const_generics): If we encounter `Self` in an anonymous
1187                                 // constant we can't easily tell if it's generic at this stage, so
1188                                 // we instead remember this and then enforce the self type to be
1189                                 // concrete later on.
1190                                 if let Res::SelfTyAlias {
1191                                     alias_to: def,
1192                                     forbid_generic: _,
1193                                     is_trait_impl,
1194                                 } = res
1195                                 {
1196                                     res = Res::SelfTyAlias {
1197                                         alias_to: def,
1198                                         forbid_generic: true,
1199                                         is_trait_impl,
1200                                     }
1201                                 } else {
1202                                     if let Some(span) = finalize {
1203                                         let error = match cause {
1204                                             NoConstantGenericsReason::IsEnumDiscriminant => {
1205                                                 ResolutionError::ParamInEnumDiscriminant {
1206                                                     name: rib_ident.name,
1207                                                     param_kind: ParamKindInEnumDiscriminant::Type,
1208                                                 }
1209                                             }
1210                                             NoConstantGenericsReason::NonTrivialConstArg => {
1211                                                 ResolutionError::ParamInNonTrivialAnonConst {
1212                                                     name: rib_ident.name,
1213                                                     param_kind:
1214                                                         ParamKindInNonTrivialAnonConst::Type,
1215                                                 }
1216                                             }
1217                                         };
1218                                         self.report_error(span, error);
1219                                         self.tcx.sess.delay_span_bug(span, CG_BUG_STR);
1220                                     }
1221 
1222                                     return Res::Err;
1223                                 }
1224                             }
1225 
1226                             continue;
1227                         }
1228 
1229                         // This was an attempt to use a type parameter outside its scope.
1230                         RibKind::Item(has_generic_params) => has_generic_params,
1231                         RibKind::ConstParamTy => {
1232                             if let Some(span) = finalize {
1233                                 self.report_error(
1234                                     span,
1235                                     ResolutionError::ParamInTyOfConstParam {
1236                                         name: rib_ident.name,
1237                                         param_kind: Some(errors::ParamKindInTyOfConstParam::Type),
1238                                     },
1239                                 );
1240                             }
1241                             return Res::Err;
1242                         }
1243                     };
1244 
1245                     if let Some(span) = finalize {
1246                         self.report_error(
1247                             span,
1248                             ResolutionError::GenericParamsFromOuterFunction(
1249                                 res,
1250                                 has_generic_params,
1251                             ),
1252                         );
1253                     }
1254                     return Res::Err;
1255                 }
1256             }
1257             Res::Def(DefKind::ConstParam, _) => {
1258                 for rib in ribs {
1259                     let has_generic_params = match rib.kind {
1260                         RibKind::Normal
1261                         | RibKind::ClosureOrAsync
1262                         | RibKind::Module(..)
1263                         | RibKind::MacroDefinition(..)
1264                         | RibKind::InlineAsmSym
1265                         | RibKind::AssocItem
1266                         | RibKind::ForwardGenericParamBan => continue,
1267 
1268                         RibKind::ConstantItem(trivial, _) => {
1269                             if let ConstantHasGenerics::No(cause) = trivial {
1270                                 if let Some(span) = finalize {
1271                                     let error = match cause {
1272                                         NoConstantGenericsReason::IsEnumDiscriminant => {
1273                                             ResolutionError::ParamInEnumDiscriminant {
1274                                                 name: rib_ident.name,
1275                                                 param_kind: ParamKindInEnumDiscriminant::Const,
1276                                             }
1277                                         }
1278                                         NoConstantGenericsReason::NonTrivialConstArg => {
1279                                             ResolutionError::ParamInNonTrivialAnonConst {
1280                                                 name: rib_ident.name,
1281                                                 param_kind: ParamKindInNonTrivialAnonConst::Const {
1282                                                     name: rib_ident.name,
1283                                                 },
1284                                             }
1285                                         }
1286                                     };
1287                                     self.report_error(span, error);
1288                                 }
1289 
1290                                 return Res::Err;
1291                             }
1292 
1293                             continue;
1294                         }
1295 
1296                         RibKind::Item(has_generic_params) => has_generic_params,
1297                         RibKind::ConstParamTy => {
1298                             if let Some(span) = finalize {
1299                                 self.report_error(
1300                                     span,
1301                                     ResolutionError::ParamInTyOfConstParam {
1302                                         name: rib_ident.name,
1303                                         param_kind: Some(errors::ParamKindInTyOfConstParam::Const),
1304                                     },
1305                                 );
1306                             }
1307                             return Res::Err;
1308                         }
1309                     };
1310 
1311                     // This was an attempt to use a const parameter outside its scope.
1312                     if let Some(span) = finalize {
1313                         self.report_error(
1314                             span,
1315                             ResolutionError::GenericParamsFromOuterFunction(
1316                                 res,
1317                                 has_generic_params,
1318                             ),
1319                         );
1320                     }
1321                     return Res::Err;
1322                 }
1323             }
1324             _ => {}
1325         }
1326         res
1327     }
1328 
1329     #[instrument(level = "debug", skip(self))]
maybe_resolve_path( &mut self, path: &[Segment], opt_ns: Option<Namespace>, parent_scope: &ParentScope<'a>, ) -> PathResult<'a>1330     pub(crate) fn maybe_resolve_path(
1331         &mut self,
1332         path: &[Segment],
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, None, None, None)
1337     }
1338 
1339     #[instrument(level = "debug", skip(self))]
resolve_path( &mut self, path: &[Segment], opt_ns: Option<Namespace>, parent_scope: &ParentScope<'a>, finalize: Option<Finalize>, ignore_binding: Option<NameBinding<'a>>, ) -> PathResult<'a>1340     pub(crate) fn resolve_path(
1341         &mut self,
1342         path: &[Segment],
1343         opt_ns: Option<Namespace>, // `None` indicates a module path in import
1344         parent_scope: &ParentScope<'a>,
1345         finalize: Option<Finalize>,
1346         ignore_binding: Option<NameBinding<'a>>,
1347     ) -> PathResult<'a> {
1348         self.resolve_path_with_ribs(path, opt_ns, parent_scope, finalize, None, ignore_binding)
1349     }
1350 
resolve_path_with_ribs( &mut self, path: &[Segment], opt_ns: Option<Namespace>, parent_scope: &ParentScope<'a>, finalize: Option<Finalize>, ribs: Option<&PerNS<Vec<Rib<'a>>>>, ignore_binding: Option<NameBinding<'a>>, ) -> PathResult<'a>1351     pub(crate) fn resolve_path_with_ribs(
1352         &mut self,
1353         path: &[Segment],
1354         opt_ns: Option<Namespace>, // `None` indicates a module path in import
1355         parent_scope: &ParentScope<'a>,
1356         finalize: Option<Finalize>,
1357         ribs: Option<&PerNS<Vec<Rib<'a>>>>,
1358         ignore_binding: Option<NameBinding<'a>>,
1359     ) -> PathResult<'a> {
1360         let mut module = None;
1361         let mut allow_super = true;
1362         let mut second_binding = None;
1363 
1364         // We'll provide more context to the privacy errors later, up to `len`.
1365         let privacy_errors_len = self.privacy_errors.len();
1366 
1367         for (segment_idx, &Segment { ident, id, .. }) in path.iter().enumerate() {
1368             debug!("resolve_path ident {} {:?} {:?}", segment_idx, 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));
1375                         }
1376                     }
1377                 }
1378             };
1379 
1380             let is_last = segment_idx + 1 == path.len();
1381             let ns = if is_last { opt_ns.unwrap_or(TypeNS) } else { TypeNS };
1382             let name = ident.name;
1383 
1384             allow_super &= ns == TypeNS && (name == kw::SelfLower || name == kw::Super);
1385 
1386             if ns == TypeNS {
1387                 if allow_super && name == kw::Super {
1388                     let mut ctxt = ident.span.ctxt().normalize_to_macros_2_0();
1389                     let self_module = match segment_idx {
1390                         0 => Some(self.resolve_self(&mut ctxt, parent_scope.module)),
1391                         _ => match module {
1392                             Some(ModuleOrUniformRoot::Module(module)) => Some(module),
1393                             _ => None,
1394                         },
1395                     };
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),
1400                             ));
1401                             continue;
1402                         }
1403                     }
1404                     return PathResult::failed(
1405                         ident.span,
1406                         false,
1407                         finalize.is_some(),
1408                         module,
1409                         || ("there are too many leading `super` keywords".to_string(), None),
1410                     );
1411                 }
1412                 if segment_idx == 0 {
1413                     if name == kw::SelfLower {
1414                         let mut ctxt = ident.span.ctxt().normalize_to_macros_2_0();
1415                         module = Some(ModuleOrUniformRoot::Module(
1416                             self.resolve_self(&mut ctxt, parent_scope.module),
1417                         ));
1418                         continue;
1419                     }
1420                     if name == kw::PathRoot && ident.span.rust_2018() {
1421                         module = Some(ModuleOrUniformRoot::ExternPrelude);
1422                         continue;
1423                     }
1424                     if name == kw::PathRoot
1425                         && ident.span.is_rust_2015()
1426                         && self.tcx.sess.rust_2018()
1427                     {
1428                         // `::a::b` from 2015 macro on 2018 global edition
1429                         module = Some(ModuleOrUniformRoot::CrateRootAndExternPrelude);
1430                         continue;
1431                     }
1432                     if name == kw::PathRoot || name == kw::Crate || name == kw::DollarCrate {
1433                         // `::a::b`, `crate::a::b` or `$crate::a::b`
1434                         module = Some(ModuleOrUniformRoot::Module(self.resolve_crate_root(ident)));
1435                         continue;
1436                     }
1437                 }
1438             }
1439 
1440             // Report special messages for path segment keywords in wrong positions.
1441             if ident.is_path_segment_keyword() && segment_idx != 0 {
1442                 return PathResult::failed(ident.span, false, finalize.is_some(), module, || {
1443                     let name_str = if name == kw::PathRoot {
1444                         "crate root".to_string()
1445                     } else {
1446                         format!("`{}`", name)
1447                     };
1448                     let label = if segment_idx == 1 && path[0].ident.name == kw::PathRoot {
1449                         format!("global paths cannot start with {}", name_str)
1450                     } else {
1451                         format!("{} in paths can only be used in start position", name_str)
1452                     };
1453                     (label, None)
1454                 });
1455             }
1456 
1457             let binding = if let Some(module) = module {
1458                 self.resolve_ident_in_module(
1459                     module,
1460                     ident,
1461                     ns,
1462                     parent_scope,
1463                     finalize,
1464                     ignore_binding,
1465                 )
1466             } else if let Some(ribs) = ribs && let Some(TypeNS | ValueNS) = opt_ns {
1467                 match self.resolve_ident_in_lexical_scope(
1468                     ident,
1469                     ns,
1470                     parent_scope,
1471                     finalize,
1472                     &ribs[ns],
1473                     ignore_binding,
1474                 ) {
1475                     // we found a locally-imported or available item/module
1476                     Some(LexicalScopeBinding::Item(binding)) => Ok(binding),
1477                     // we found a local variable or type param
1478                     Some(LexicalScopeBinding::Res(res)) => {
1479                         record_segment_res(self, res);
1480                         return PathResult::NonModule(PartialRes::with_unresolved_segments(
1481                             res,
1482                             path.len() - 1,
1483                         ));
1484                     }
1485                     _ => Err(Determinacy::determined(finalize.is_some())),
1486                 }
1487             } else {
1488                 self.early_resolve_ident_in_lexical_scope(
1489                     ident,
1490                     ScopeSet::All(ns),
1491                     parent_scope,
1492                     finalize,
1493                     finalize.is_some(),
1494                     ignore_binding,
1495                 )
1496             };
1497 
1498             match binding {
1499                 Ok(binding) => {
1500                     if segment_idx == 1 {
1501                         second_binding = Some(binding);
1502                     }
1503                     let res = binding.res();
1504 
1505                     // Mark every privacy error in this path with the res to the last element. This allows us
1506                     // to detect the item the user cares about and either find an alternative import, or tell
1507                     // the user it is not accessible.
1508                     for error in &mut self.privacy_errors[privacy_errors_len..] {
1509                         error.outermost_res = Some((res, ident));
1510                     }
1511 
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 && !is_last && opt_ns.is_some() {
1517                         if binding.is_import() {
1518                             self.tcx.sess.emit_err(errors::ToolModuleImported {
1519                                 span: ident.span,
1520                                 import: binding.span,
1521                             });
1522                         }
1523                         let res = Res::NonMacroAttr(NonMacroAttrKind::Tool);
1524                         return PathResult::NonModule(PartialRes::new(res));
1525                     } else if res == Res::Err {
1526                         return PathResult::NonModule(PartialRes::new(Res::Err));
1527                     } else if opt_ns.is_some() && (is_last || maybe_assoc) {
1528                         self.lint_if_path_starts_with_module(finalize, path, second_binding);
1529                         record_segment_res(self, res);
1530                         return PathResult::NonModule(PartialRes::with_unresolved_segments(
1531                             res,
1532                             path.len() - segment_idx - 1,
1533                         ));
1534                     } else {
1535                         return PathResult::failed(
1536                             ident.span,
1537                             is_last,
1538                             finalize.is_some(),
1539                             module,
1540                             || {
1541                                 let label = format!(
1542                                     "`{ident}` is {} {}, not a module",
1543                                     res.article(),
1544                                     res.descr()
1545                                 );
1546                                 (label, None)
1547                             },
1548                         );
1549                     }
1550                 }
1551                 Err(Undetermined) => return PathResult::Indeterminate,
1552                 Err(Determined) => {
1553                     if let Some(ModuleOrUniformRoot::Module(module)) = module {
1554                         if opt_ns.is_some() && !module.is_normal() {
1555                             return PathResult::NonModule(PartialRes::with_unresolved_segments(
1556                                 module.res().unwrap(),
1557                                 path.len() - segment_idx,
1558                             ));
1559                         }
1560                     }
1561 
1562                     return PathResult::failed(
1563                         ident.span,
1564                         is_last,
1565                         finalize.is_some(),
1566                         module,
1567                         || {
1568                             self.report_path_resolution_error(
1569                                 path,
1570                                 opt_ns,
1571                                 parent_scope,
1572                                 ribs,
1573                                 ignore_binding,
1574                                 module,
1575                                 segment_idx,
1576                                 ident,
1577                             )
1578                         },
1579                     );
1580                 }
1581             }
1582         }
1583 
1584         self.lint_if_path_starts_with_module(finalize, path, second_binding);
1585 
1586         PathResult::Module(match module {
1587             Some(module) => module,
1588             None if path.is_empty() => ModuleOrUniformRoot::CurrentScope,
1589             _ => bug!("resolve_path: non-empty path `{:?}` has no module", path),
1590         })
1591     }
1592 }
1593