]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_resolve/src/ident.rs
Rollup merge of #104304 - uweigand:s390x-profiler, r=Mark-Simulacrum
[rust.git] / compiler / rustc_resolve / src / ident.rs
1 use rustc_ast as ast;
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_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};
12
13 use std::ptr;
14
15 use crate::late::{
16     ConstantHasGenerics, ConstantItemKind, HasGenericParams, PathSource, Rib, RibKind,
17 };
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};
23
24 use Determinacy::*;
25 use Namespace::*;
26 use RibKind::*;
27
28 type Visibility = ty::Visibility<LocalDefId>;
29
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>(
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() == 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),
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),
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),
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) => {
176                     use_prelude = !module.no_implicit_prelude;
177                     match self.hygienic_lexical_parent(module, &mut ctxt) {
178                         Some(parent_module) => Scope::Module(parent_module),
179                         None => {
180                             ctxt.adjust(ExpnId::root());
181                             match ns {
182                                 TypeNS => Scope::ExternPrelude,
183                                 ValueNS => Scope::StdLibPrelude,
184                                 MacroNS => Scope::MacroUsePrelude,
185                             }
186                         }
187                     }
188                 }
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,
198                 },
199                 Scope::BuiltinTypes => break, // nowhere else to search
200             };
201         }
202
203         None
204     }
205
206     fn hygienic_lexical_parent(
207         &mut self,
208         module: Module<'a>,
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()));
213         }
214
215         if let ModuleKind::Block = module.kind {
216             return Some(module.parent.unwrap().nearest_item_scope());
217         }
218
219         None
220     }
221
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).
225     ///
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,
228     /// ```rust
229     /// fn f() {
230     ///    g(); // Since there are no local variables in scope yet, this resolves to the item.
231     ///    let g = || {};
232     ///    fn g() {}
233     ///    g(); // This resolves to the local variable `g` since it shadows the item.
234     /// }
235     /// ```
236     ///
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(
241         &mut self,
242         mut ident: Ident,
243         ns: Namespace,
244         parent_scope: &ParentScope<'a>,
245         finalize: Option<Finalize>,
246         ribs: &[Rib<'a>],
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));
253         }
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)
261         } else {
262             (ident.span.normalize_to_macro_rules(), ident.span.normalize_to_macros_2_0())
263         };
264         ident.span = general_span;
265         let normalized_ident = Ident { span: normalized_span, ..ident };
266
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)
275             {
276                 // The ident resolves to a type parameter or local variable.
277                 return Some(LexicalScopeBinding::Res(self.validate_res_from_ribs(
278                     i,
279                     rib_ident,
280                     *res,
281                     finalize.map(|finalize| finalize.path_span),
282                     *original_rib_ident_def,
283                     ribs,
284                 )));
285             }
286
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();
293                     continue;
294                 }
295                 _ => continue,
296             };
297
298             match module.kind {
299                 ModuleKind::Block => {} // We can see through blocks
300                 _ => break,
301             }
302
303             let item = self.resolve_ident_in_module_unadjusted(
304                 ModuleOrUniformRoot::Module(module),
305                 ident,
306                 ns,
307                 parent_scope,
308                 finalize,
309                 ignore_binding,
310             );
311             if let Ok(binding) = item {
312                 // The ident resolves to an item.
313                 return Some(LexicalScopeBinding::Item(binding));
314             }
315         }
316         self.early_resolve_ident_in_lexical_scope(
317             orig_ident,
318             ScopeSet::Late(ns, module, finalize.map(|finalize| finalize.node_id)),
319             parent_scope,
320             finalize,
321             finalize.is_some(),
322             ignore_binding,
323         )
324         .ok()
325         .map(LexicalScopeBinding::Item)
326     }
327
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(
335         &mut self,
336         orig_ident: Ident,
337         scope_set: ScopeSet<'a>,
338         parent_scope: &ParentScope<'a>,
339         finalize: Option<Finalize>,
340         force: bool,
341         ignore_binding: Option<&'a NameBinding<'a>>,
342     ) -> Result<&'a NameBinding<'a>, Determinacy> {
343         bitflags::bitflags! {
344             struct Flags: u8 {
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;
350             }
351         }
352
353         assert!(force || !finalize.is_some()); // `finalize` implies `force`
354
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);
358         }
359
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),
365         };
366
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
371         // {
372         //     use prefix::*; // imports another `m` - innermost solution
373         //                    // weak, cannot shadow the outer `m`, need to report ambiguity error
374         //     m::mac!();
375         // }
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;
380
381         // Go through all the scopes and try to resolve the name.
382         let break_result = self.visit_scopes(
383             scope_set,
384             parent_scope,
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| {
389                     Ok((
390                         (res, Visibility::Public, span, LocalExpnId::ROOT).to_name_binding(arenas),
391                         Flags::empty(),
392                     ))
393                 };
394                 let result = match scope {
395                     Scope::DeriveHelpers(expn_id) => {
396                         if let Some(attr) = this
397                             .helper_attrs
398                             .get(&expn_id)
399                             .and_then(|attrs| attrs.iter().rfind(|i| ident == **i))
400                         {
401                             let binding = (
402                                 Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper),
403                                 Visibility::Public,
404                                 attr.span,
405                                 expn_id,
406                             )
407                                 .to_name_binding(this.arenas);
408                             Ok((binding, Flags::empty()))
409                         } else {
410                             Err(Determinacy::Determined)
411                         }
412                     }
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(
418                                 derive,
419                                 Some(MacroKind::Derive),
420                                 parent_scope,
421                                 true,
422                                 force,
423                             ) {
424                                 Ok((Some(ext), _)) => {
425                                     if ext.helper_attrs.contains(&ident.name) {
426                                         result = ok(
427                                             Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat),
428                                             derive.span,
429                                             this.arenas,
430                                         );
431                                         break;
432                                     }
433                                 }
434                                 Ok(_) | Err(Determinacy::Determined) => {}
435                                 Err(Determinacy::Undetermined) => {
436                                     result = Err(Determinacy::Undetermined)
437                                 }
438                             }
439                         }
440                         result
441                     }
442                     Scope::MacroRules(macro_rules_scope) => match macro_rules_scope.get() {
443                         MacroRulesScope::Binding(macro_rules_binding)
444                             if ident == macro_rules_binding.ident =>
445                         {
446                             Ok((macro_rules_binding.binding, Flags::MACRO_RULES))
447                         }
448                         MacroRulesScope::Invocation(_) => Err(Determinacy::Undetermined),
449                         _ => Err(Determinacy::Determined),
450                     },
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),
456                             ident,
457                             ns,
458                             parent_scope,
459                             finalize,
460                             ignore_binding,
461                         );
462                         match binding {
463                             Ok(binding) => Ok((binding, Flags::MODULE | Flags::MISC_SUGGEST_CRATE)),
464                             Err((Determinacy::Undetermined, Weak::No)) => {
465                                 return Some(Err(Determinacy::determined(force)));
466                             }
467                             Err((Determinacy::Undetermined, Weak::Yes)) => {
468                                 Err(Determinacy::Undetermined)
469                             }
470                             Err((Determinacy::Determined, _)) => Err(Determinacy::Determined),
471                         }
472                     }
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),
477                             ident,
478                             ns,
479                             adjusted_parent_scope,
480                             !matches!(scope_set, ScopeSet::Late(..)),
481                             finalize,
482                             ignore_binding,
483                         );
484                         match binding {
485                             Ok(binding) => {
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
490                                 } else {
491                                     Flags::empty()
492                                 };
493                                 Ok((binding, Flags::MODULE | misc_flags))
494                             }
495                             Err((Determinacy::Undetermined, Weak::No)) => {
496                                 return Some(Err(Determinacy::determined(force)));
497                             }
498                             Err((Determinacy::Undetermined, Weak::Yes)) => {
499                                 Err(Determinacy::Undetermined)
500                             }
501                             Err((Determinacy::Determined, _)) => Err(Determinacy::Determined),
502                         }
503                     }
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(),
509                             )),
510                         }
511                     }
512                     Scope::BuiltinAttrs => {
513                         if is_builtin_attr_name(ident.name) {
514                             ok(
515                                 Res::NonMacroAttr(NonMacroAttrKind::Builtin(ident.name)),
516                                 DUMMY_SP,
517                                 this.arenas,
518                             )
519                         } else {
520                             Err(Determinacy::Determined)
521                         }
522                     }
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(),
528                             )),
529                         }
530                     }
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),
534                     },
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),
540                                 ident,
541                                 ns,
542                                 parent_scope,
543                                 None,
544                                 ignore_binding,
545                             ) {
546                                 if use_prelude || this.is_builtin_macro(binding.res()) {
547                                     result = Ok((binding, Flags::MISC_FROM_PRELUDE));
548                                 }
549                             }
550                         }
551                         result
552                     }
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),
556                     },
557                 };
558
559                 match result {
560                     Ok((binding, flags))
561                         if sub_namespace_match(binding.macro_kind(), macro_kind) =>
562                     {
563                         if finalize.is_none() || matches!(scope_set, ScopeSet::Late(..)) {
564                             return Some(Ok(binding));
565                         }
566
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(..)))
573                                 };
574                                 let derive_helper =
575                                     Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper);
576                                 let derive_helper_compat =
577                                     Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat);
578
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
585                                 {
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(
590                                         innermost_binding,
591                                         binding,
592                                     )
593                                     || flags.contains(Flags::MACRO_RULES)
594                                         && innermost_flags.contains(Flags::MODULE)
595                                         && !this.disambiguate_macro_rules_vs_modularized(
596                                             binding,
597                                             innermost_binding,
598                                         )
599                                 {
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)
605                                 {
606                                     Some(AmbiguityKind::MoreExpandedVsOuter)
607                                 } else {
608                                     None
609                                 };
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
618                                         } else {
619                                             AmbiguityErrorMisc::None
620                                         }
621                                     };
622                                     this.ambiguity_errors.push(AmbiguityError {
623                                         kind,
624                                         ident: orig_ident,
625                                         b1: innermost_binding,
626                                         b2: binding,
627                                         misc1: misc(innermost_flags),
628                                         misc2: misc(flags),
629                                     });
630                                     return Some(Ok(innermost_binding));
631                                 }
632                             }
633                         } else {
634                             // Found the first solution.
635                             innermost_result = Some((binding, flags));
636                         }
637                     }
638                     Ok(..) | Err(Determinacy::Determined) => {}
639                     Err(Determinacy::Undetermined) => determinacy = Determinacy::Undetermined,
640                 }
641
642                 None
643             },
644         );
645
646         if let Some(break_result) = break_result {
647             return break_result;
648         }
649
650         // The first found solution was the only one, return it.
651         if let Some((binding, _)) = innermost_result {
652             return Ok(binding);
653         }
654
655         Err(Determinacy::determined(determinacy == Determinacy::Determined || force))
656     }
657
658     #[instrument(level = "debug", skip(self))]
659     pub(crate) fn maybe_resolve_ident_in_module(
660         &mut self,
661         module: ModuleOrUniformRoot<'a>,
662         ident: Ident,
663         ns: Namespace,
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)
668     }
669
670     #[instrument(level = "debug", skip(self))]
671     pub(crate) fn resolve_ident_in_module(
672         &mut self,
673         module: ModuleOrUniformRoot<'a>,
674         ident: Ident,
675         ns: Namespace,
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)
682     }
683
684     #[instrument(level = "debug", skip(self))]
685     fn resolve_ident_in_module_ext(
686         &mut self,
687         module: ModuleOrUniformRoot<'a>,
688         mut ident: Ident,
689         ns: Namespace,
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;
696         match module {
697             ModuleOrUniformRoot::Module(m) => {
698                 if let Some(def) = ident.span.normalize_to_macros_2_0_and_adjust(m.expansion) {
699                     tmp_parent_scope =
700                         ParentScope { module: self.expn_def_scope(def), ..*parent_scope };
701                     adjusted_parent_scope = &tmp_parent_scope;
702                 }
703             }
704             ModuleOrUniformRoot::ExternPrelude => {
705                 ident.span.normalize_to_macros_2_0_and_adjust(ExpnId::root());
706             }
707             ModuleOrUniformRoot::CrateRootAndExternPrelude | ModuleOrUniformRoot::CurrentScope => {
708                 // No adjustments
709             }
710         }
711         self.resolve_ident_in_module_unadjusted_ext(
712             module,
713             ident,
714             ns,
715             adjusted_parent_scope,
716             false,
717             finalize,
718             ignore_binding,
719         )
720     }
721
722     #[instrument(level = "debug", skip(self))]
723     fn resolve_ident_in_module_unadjusted(
724         &mut self,
725         module: ModuleOrUniformRoot<'a>,
726         ident: Ident,
727         ns: Namespace,
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(
733             module,
734             ident,
735             ns,
736             parent_scope,
737             false,
738             finalize,
739             ignore_binding,
740         )
741         .map_err(|(determinacy, _)| determinacy)
742     }
743
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(
748         &mut self,
749         module: ModuleOrUniformRoot<'a>,
750         ident: Ident,
751         ns: Namespace,
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(
764                     ident,
765                     ScopeSet::AbsolutePath(ns),
766                     parent_scope,
767                     finalize,
768                     finalize.is_some(),
769                     ignore_binding,
770                 );
771                 return binding.map_err(|determinacy| (determinacy, Weak::No));
772             }
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()) {
778                     Ok(binding)
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))
782                 } else {
783                     Err((Determined, Weak::No))
784                 };
785             }
786             ModuleOrUniformRoot::CurrentScope => {
787                 assert!(!restricted_shadowing);
788                 if ns == TypeNS {
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);
793                         return Ok(binding);
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_...`.
798                     }
799                 }
800
801                 let scopes = ScopeSet::All(ns, true);
802                 let binding = self.early_resolve_ident_in_lexical_scope(
803                     ident,
804                     scopes,
805                     parent_scope,
806                     finalize,
807                     finalize.is_some(),
808                     ignore_binding,
809                 );
810                 return binding.map_err(|determinacy| (determinacy, Weak::No));
811             }
812         };
813
814         let key = self.new_key(ident, ns);
815         let resolution =
816             self.resolution(module, key).try_borrow_mut().map_err(|_| (Determined, Weak::No))?; // This happens when there is a cycle of imports.
817
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]
824                 .into_iter()
825                 .filter_map(|binding| match (binding, ignore_binding) {
826                     (Some(binding), Some(ignored)) if ptr::eq(binding, ignored) => None,
827                     _ => binding,
828                 })
829                 .next();
830             let Some(binding) = binding else {
831                 return Err((Determined, Weak::No));
832             };
833
834             if !self.is_accessible_from(binding.vis, parent_scope.module) {
835                 if report_private {
836                     self.privacy_errors.push(PrivacyError {
837                         ident,
838                         binding,
839                         dedup_span: path_span,
840                     });
841                 } else {
842                     return Err((Determined, Weak::No));
843                 }
844             }
845
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()
851             {
852                 self.ambiguity_errors.push(AmbiguityError {
853                     kind: AmbiguityKind::GlobVsExpanded,
854                     ident,
855                     b1: binding,
856                     b2: shadowed_glob,
857                     misc1: AmbiguityErrorMisc::None,
858                     misc2: AmbiguityErrorMisc::None,
859                 });
860             }
861
862             if !restricted_shadowing && binding.expansion != LocalExpnId::ROOT {
863                 if let NameBindingKind::Import {
864                     import: Import { kind: ImportKind::MacroExport, .. },
865                     ..
866                 } = binding.kind
867                 {
868                     self.macro_expanded_macro_export_errors.insert((path_span, binding.span));
869                 }
870             }
871
872             self.record_use(ident, binding, restricted_shadowing);
873             return Ok(binding);
874         }
875
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));
879             }
880             let usable = this.is_accessible_from(binding.vis, parent_scope.module);
881             if usable { Ok(binding) } else { Err((Determined, Weak::No)) }
882         };
883
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);
888             }
889         }
890
891         // --- From now on we either have a glob resolution or no resolution. ---
892
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 {
897                 continue;
898             };
899             if !self.is_accessible_from(import_vis, parent_scope.module) {
900                 continue;
901             }
902             let Some(module) = single_import.imported_module.get() else {
903                 return Err((Undetermined, Weak::No));
904             };
905             let ImportKind::Single { source: ident, .. } = single_import.kind else {
906                 unreachable!();
907             };
908             match self.resolve_ident_in_module(
909                 module,
910                 ident,
911                 ns,
912                 &single_import.parent_scope,
913                 None,
914                 ignore_binding,
915             ) {
916                 Err(Determined) => continue,
917                 Ok(binding)
918                     if !self.is_accessible_from(binding.vis, single_import.parent_scope.module) =>
919                 {
920                     continue;
921                 }
922                 Ok(_) | Err(Undetermined) => return Err((Undetermined, Weak::No)),
923             }
924         }
925
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).
930         //
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);
942             } else {
943                 return Err((Undetermined, Weak::No));
944             }
945         }
946
947         // --- From now on we have no resolution. ---
948
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`.
954
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));
959         }
960
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 {
965                 continue;
966             };
967             if !self.is_accessible_from(import_vis, parent_scope.module) {
968                 continue;
969             }
970             let module = match glob_import.imported_module.get() {
971                 Some(ModuleOrUniformRoot::Module(module)) => module,
972                 Some(_) => continue,
973                 None => return Err((Undetermined, Weak::Yes)),
974             };
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) {
979                 Some(Some(def)) => {
980                     tmp_parent_scope =
981                         ParentScope { module: self.expn_def_scope(def), ..*parent_scope };
982                     adjusted_parent_scope = &tmp_parent_scope;
983                 }
984                 Some(None) => {}
985                 None => continue,
986             };
987             let result = self.resolve_ident_in_module_unadjusted(
988                 ModuleOrUniformRoot::Module(module),
989                 ident,
990                 ns,
991                 adjusted_parent_scope,
992                 None,
993                 ignore_binding,
994             );
995
996             match result {
997                 Err(Determined) => continue,
998                 Ok(binding)
999                     if !self.is_accessible_from(binding.vis, glob_import.parent_scope.module) =>
1000                 {
1001                     continue;
1002                 }
1003                 Ok(_) | Err(Undetermined) => return Err((Undetermined, Weak::Yes)),
1004             }
1005         }
1006
1007         // No resolution and no one else can define the name - determinate error.
1008         Err((Determined, Weak::No))
1009     }
1010
1011     /// Validate a local resolution (from ribs).
1012     #[instrument(level = "debug", skip(self, all_ribs))]
1013     fn validate_res_from_ribs(
1014         &mut self,
1015         rib_index: usize,
1016         rib_ident: Ident,
1017         mut res: Res,
1018         finalize: Option<Span>,
1019         original_rib_ident_def: Ident,
1020         all_ribs: &[Rib<'a>],
1021     ) -> Res {
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..];
1025
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
1031                 } else {
1032                     ResolutionError::ForwardDeclaredGenericParam
1033                 };
1034                 self.report_error(span, res_error);
1035             }
1036             assert_eq!(res, Res::Err);
1037             return Res::Err;
1038         }
1039
1040         match res {
1041             Res::Local(_) => {
1042                 use ResolutionError::*;
1043                 let mut res_err = None;
1044
1045                 for rib in ribs {
1046                     match rib.kind {
1047                         NormalRibKind
1048                         | ClosureOrAsyncRibKind
1049                         | ModuleRibKind(..)
1050                         | MacroDefinition(..)
1051                         | ForwardGenericParamBanRibKind => {
1052                             // Nothing to do. Continue.
1053                         }
1054                         ItemRibKind(_) | AssocItemRibKind => {
1055                             // This was an attempt to access an upvar inside a
1056                             // named function item. This is not allowed, so we
1057                             // report an error.
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
1062                                 // precedence.
1063                                 res_err = Some((span, CannotCaptureDynamicEnvironmentInFnItem));
1064                             }
1065                         }
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",
1074                                         };
1075                                         (
1076                                             span,
1077                                             AttemptToUseNonConstantValueInConstant(
1078                                                 ident, "let", kind_str,
1079                                             ),
1080                                         )
1081                                     } else {
1082                                         (
1083                                             rib_ident.span,
1084                                             AttemptToUseNonConstantValueInConstant(
1085                                                 original_rib_ident_def,
1086                                                 "const",
1087                                                 "let",
1088                                             ),
1089                                         )
1090                                     };
1091                                 self.report_error(span, resolution_error);
1092                             }
1093                             return Res::Err;
1094                         }
1095                         ConstParamTyRibKind => {
1096                             if let Some(span) = finalize {
1097                                 self.report_error(span, ParamInTyOfConstParam(rib_ident.name));
1098                             }
1099                             return Res::Err;
1100                         }
1101                         InlineAsmSymRibKind => {
1102                             if let Some(span) = finalize {
1103                                 self.report_error(span, InvalidAsmSym);
1104                             }
1105                             return Res::Err;
1106                         }
1107                     }
1108                 }
1109                 if let Some((span, res_err)) = res_err {
1110                     self.report_error(span, res_err);
1111                     return Res::Err;
1112                 }
1113             }
1114             Res::Def(DefKind::TyParam, _) | Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } => {
1115                 for rib in ribs {
1116                     let has_generic_params: HasGenericParams = match rib.kind {
1117                         NormalRibKind
1118                         | ClosureOrAsyncRibKind
1119                         | ModuleRibKind(..)
1120                         | MacroDefinition(..)
1121                         | InlineAsmSymRibKind
1122                         | AssocItemRibKind
1123                         | ForwardGenericParamBanRibKind => {
1124                             // Nothing to do. Continue.
1125                             continue;
1126                         }
1127
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)
1133                             {
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 {
1139                                     alias_to: def,
1140                                     forbid_generic: _,
1141                                     is_trait_impl,
1142                                 } = res
1143                                 {
1144                                     res = Res::SelfTyAlias {
1145                                         alias_to: def,
1146                                         forbid_generic: true,
1147                                         is_trait_impl,
1148                                     }
1149                                 } else {
1150                                     if let Some(span) = finalize {
1151                                         self.report_error(
1152                                             span,
1153                                             ResolutionError::ParamInNonTrivialAnonConst {
1154                                                 name: rib_ident.name,
1155                                                 is_type: true,
1156                                             },
1157                                         );
1158                                         self.session.delay_span_bug(span, CG_BUG_STR);
1159                                     }
1160
1161                                     return Res::Err;
1162                                 }
1163                             }
1164
1165                             continue;
1166                         }
1167
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 {
1172                                 self.report_error(
1173                                     span,
1174                                     ResolutionError::ParamInTyOfConstParam(rib_ident.name),
1175                                 );
1176                             }
1177                             return Res::Err;
1178                         }
1179                     };
1180
1181                     if let Some(span) = finalize {
1182                         self.report_error(
1183                             span,
1184                             ResolutionError::GenericParamsFromOuterFunction(
1185                                 res,
1186                                 has_generic_params,
1187                             ),
1188                         );
1189                     }
1190                     return Res::Err;
1191                 }
1192             }
1193             Res::Def(DefKind::ConstParam, _) => {
1194                 for rib in ribs {
1195                     let has_generic_params = match rib.kind {
1196                         NormalRibKind
1197                         | ClosureOrAsyncRibKind
1198                         | ModuleRibKind(..)
1199                         | MacroDefinition(..)
1200                         | InlineAsmSymRibKind
1201                         | AssocItemRibKind
1202                         | ForwardGenericParamBanRibKind => continue,
1203
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)
1209                             {
1210                                 if let Some(span) = finalize {
1211                                     self.report_error(
1212                                         span,
1213                                         ResolutionError::ParamInNonTrivialAnonConst {
1214                                             name: rib_ident.name,
1215                                             is_type: false,
1216                                         },
1217                                     );
1218                                     self.session.delay_span_bug(span, CG_BUG_STR);
1219                                 }
1220
1221                                 return Res::Err;
1222                             }
1223
1224                             continue;
1225                         }
1226
1227                         ItemRibKind(has_generic_params) => has_generic_params,
1228                         ConstParamTyRibKind => {
1229                             if let Some(span) = finalize {
1230                                 self.report_error(
1231                                     span,
1232                                     ResolutionError::ParamInTyOfConstParam(rib_ident.name),
1233                                 );
1234                             }
1235                             return Res::Err;
1236                         }
1237                     };
1238
1239                     // This was an attempt to use a const parameter outside its scope.
1240                     if let Some(span) = finalize {
1241                         self.report_error(
1242                             span,
1243                             ResolutionError::GenericParamsFromOuterFunction(
1244                                 res,
1245                                 has_generic_params,
1246                             ),
1247                         );
1248                     }
1249                     return Res::Err;
1250                 }
1251             }
1252             _ => {}
1253         }
1254         res
1255     }
1256
1257     #[instrument(level = "debug", skip(self))]
1258     pub(crate) fn maybe_resolve_path(
1259         &mut self,
1260         path: &[Segment],
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)
1265     }
1266
1267     #[instrument(level = "debug", skip(self))]
1268     pub(crate) fn resolve_path(
1269         &mut self,
1270         path: &[Segment],
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)
1277     }
1278
1279     pub(crate) fn resolve_path_with_ribs(
1280         &mut self,
1281         path: &[Segment],
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);
1289
1290         let mut module = None;
1291         let mut allow_super = true;
1292         let mut second_binding = None;
1293
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));
1302                         }
1303                     }
1304                 }
1305             };
1306
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;
1310
1311             allow_super &= ns == TypeNS && (name == kw::SelfLower || name == kw::Super);
1312
1313             if ns == TypeNS {
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)),
1318                         _ => match module {
1319                             Some(ModuleOrUniformRoot::Module(module)) => Some(module),
1320                             _ => None,
1321                         },
1322                     };
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),
1327                             ));
1328                             continue;
1329                         }
1330                     }
1331                     return PathResult::failed(ident.span, false, finalize.is_some(), || {
1332                         ("there are too many leading `super` keywords".to_string(), None)
1333                     });
1334                 }
1335                 if i == 0 {
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),
1340                         ));
1341                         continue;
1342                     }
1343                     if name == kw::PathRoot && ident.span.rust_2018() {
1344                         module = Some(ModuleOrUniformRoot::ExternPrelude);
1345                         continue;
1346                     }
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);
1350                         continue;
1351                     }
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)));
1355                         continue;
1356                     }
1357                 }
1358             }
1359
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()
1365                     } else {
1366                         format!("`{}`", name)
1367                     };
1368                     let label = if i == 1 && path[0].ident.name == kw::PathRoot {
1369                         format!("global paths cannot start with {}", name_str)
1370                     } else {
1371                         format!("{} in paths can only be used in start position", name_str)
1372                     };
1373                     (label, None)
1374                 });
1375             }
1376
1377             enum FindBindingResult<'a> {
1378                 Binding(Result<&'a NameBinding<'a>, Determinacy>),
1379                 Res(Res),
1380             }
1381             let find_binding_in_ns = |this: &mut Self, ns| {
1382                 let binding = if let Some(module) = module {
1383                     this.resolve_ident_in_module(
1384                         module,
1385                         ident,
1386                         ns,
1387                         parent_scope,
1388                         finalize,
1389                         ignore_binding,
1390                     )
1391                 } else if let Some(ribs) = ribs
1392                     && let Some(TypeNS | ValueNS) = opt_ns
1393                 {
1394                     match this.resolve_ident_in_lexical_scope(
1395                         ident,
1396                         ns,
1397                         parent_scope,
1398                         finalize,
1399                         &ribs[ns],
1400                         ignore_binding,
1401                     ) {
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())),
1407                     }
1408                 } else {
1409                     let scopes = ScopeSet::All(ns, opt_ns.is_none());
1410                     this.early_resolve_ident_in_lexical_scope(
1411                         ident,
1412                         scopes,
1413                         parent_scope,
1414                         finalize,
1415                         finalize.is_some(),
1416                         ignore_binding,
1417                     )
1418                 };
1419                 FindBindingResult::Binding(binding)
1420             };
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(
1425                         res,
1426                         path.len() - 1,
1427                     ));
1428                 }
1429                 FindBindingResult::Binding(binding) => binding,
1430             };
1431             match binding {
1432                 Ok(binding) => {
1433                     if i == 1 {
1434                         second_binding = Some(binding);
1435                     }
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() {
1443                             self.session
1444                                 .struct_span_err(
1445                                     ident.span,
1446                                     "cannot use a tool module through an import",
1447                                 )
1448                                 .span_note(binding.span, "the tool module imported here")
1449                                 .emit();
1450                         }
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(
1459                             res,
1460                             path.len() - i - 1,
1461                         ));
1462                     } else {
1463                         return PathResult::failed(ident.span, is_last, finalize.is_some(), || {
1464                             let label = format!(
1465                                 "`{ident}` is {} {}, not a module",
1466                                 res.article(),
1467                                 res.descr()
1468                             );
1469                             (label, None)
1470                         });
1471                     }
1472                 }
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(),
1479                                 path.len() - i,
1480                             ));
1481                         }
1482                     }
1483
1484                     return PathResult::failed(ident.span, is_last, finalize.is_some(), || {
1485                         self.report_path_resolution_error(
1486                             path,
1487                             opt_ns,
1488                             parent_scope,
1489                             ribs,
1490                             ignore_binding,
1491                             module,
1492                             i,
1493                             ident,
1494                         )
1495                     });
1496                 }
1497             }
1498         }
1499
1500         self.lint_if_path_starts_with_module(finalize, path, second_binding);
1501
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),
1506         })
1507     }
1508 }