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