]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_resolve/src/ident.rs
Rollup merge of #99581 - nnethercote:improve-derive-packed-errors, r=estebank
[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     pub(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).ext;
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     pub(crate) fn resolve_ident_in_lexical_scope(
278         &mut self,
279         mut ident: Ident,
280         ns: Namespace,
281         parent_scope: &ParentScope<'a>,
282         finalize: Option<Finalize>,
283         ribs: &[Rib<'a>],
284         ignore_binding: Option<&'a NameBinding<'a>>,
285     ) -> Option<LexicalScopeBinding<'a>> {
286         assert!(ns == TypeNS || ns == ValueNS);
287         let orig_ident = ident;
288         if ident.name == kw::Empty {
289             return Some(LexicalScopeBinding::Res(Res::Err));
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 mut module = self.graph_root;
306         for i in (0..ribs.len()).rev() {
307             debug!("walk rib\n{:?}", ribs[i].bindings);
308             // Use the rib kind to determine whether we are resolving parameters
309             // (macro 2.0 hygiene) or local variables (`macro_rules` hygiene).
310             let rib_ident = if ribs[i].kind.contains_params() { normalized_ident } else { ident };
311             if let Some((original_rib_ident_def, res)) = ribs[i].bindings.get_key_value(&rib_ident)
312             {
313                 // The ident resolves to a type parameter or local variable.
314                 return Some(LexicalScopeBinding::Res(self.validate_res_from_ribs(
315                     i,
316                     rib_ident,
317                     *res,
318                     finalize.map(|finalize| finalize.path_span),
319                     *original_rib_ident_def,
320                     ribs,
321                 )));
322             }
323
324             module = match ribs[i].kind {
325                 ModuleRibKind(module) => module,
326                 MacroDefinition(def) if def == self.macro_def(ident.span.ctxt()) => {
327                     // If an invocation of this macro created `ident`, give up on `ident`
328                     // and switch to `ident`'s source from the macro definition.
329                     ident.span.remove_mark();
330                     continue;
331                 }
332                 _ => continue,
333             };
334
335             match module.kind {
336                 ModuleKind::Block(..) => {} // We can see through blocks
337                 _ => break,
338             }
339
340             let item = self.resolve_ident_in_module_unadjusted(
341                 ModuleOrUniformRoot::Module(module),
342                 ident,
343                 ns,
344                 parent_scope,
345                 finalize,
346                 ignore_binding,
347             );
348             if let Ok(binding) = item {
349                 // The ident resolves to an item.
350                 return Some(LexicalScopeBinding::Item(binding));
351             }
352         }
353         self.early_resolve_ident_in_lexical_scope(
354             orig_ident,
355             ScopeSet::Late(ns, module, finalize.map(|finalize| finalize.node_id)),
356             parent_scope,
357             finalize,
358             finalize.is_some(),
359             ignore_binding,
360         )
361         .ok()
362         .map(LexicalScopeBinding::Item)
363     }
364
365     /// Resolve an identifier in lexical scope.
366     /// This is a variation of `fn resolve_ident_in_lexical_scope` that can be run during
367     /// expansion and import resolution (perhaps they can be merged in the future).
368     /// The function is used for resolving initial segments of macro paths (e.g., `foo` in
369     /// `foo::bar!(); or `foo!();`) and also for import paths on 2018 edition.
370     #[tracing::instrument(level = "debug", skip(self, scope_set))]
371     pub(crate) fn early_resolve_ident_in_lexical_scope(
372         &mut self,
373         orig_ident: Ident,
374         scope_set: ScopeSet<'a>,
375         parent_scope: &ParentScope<'a>,
376         finalize: Option<Finalize>,
377         force: bool,
378         ignore_binding: Option<&'a NameBinding<'a>>,
379     ) -> Result<&'a NameBinding<'a>, Determinacy> {
380         bitflags::bitflags! {
381             struct Flags: u8 {
382                 const MACRO_RULES          = 1 << 0;
383                 const MODULE               = 1 << 1;
384                 const MISC_SUGGEST_CRATE   = 1 << 2;
385                 const MISC_SUGGEST_SELF    = 1 << 3;
386                 const MISC_FROM_PRELUDE    = 1 << 4;
387             }
388         }
389
390         assert!(force || !finalize.is_some()); // `finalize` implies `force`
391
392         // Make sure `self`, `super` etc produce an error when passed to here.
393         if orig_ident.is_path_segment_keyword() {
394             return Err(Determinacy::Determined);
395         }
396
397         let (ns, macro_kind, is_import) = match scope_set {
398             ScopeSet::All(ns, is_import) => (ns, None, is_import),
399             ScopeSet::AbsolutePath(ns) => (ns, None, false),
400             ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind), false),
401             ScopeSet::Late(ns, ..) => (ns, None, false),
402         };
403
404         // This is *the* result, resolution from the scope closest to the resolved identifier.
405         // However, sometimes this result is "weak" because it comes from a glob import or
406         // a macro expansion, and in this case it cannot shadow names from outer scopes, e.g.
407         // mod m { ... } // solution in outer scope
408         // {
409         //     use prefix::*; // imports another `m` - innermost solution
410         //                    // weak, cannot shadow the outer `m`, need to report ambiguity error
411         //     m::mac!();
412         // }
413         // So we have to save the innermost solution and continue searching in outer scopes
414         // to detect potential ambiguities.
415         let mut innermost_result: Option<(&NameBinding<'_>, Flags)> = None;
416         let mut determinacy = Determinacy::Determined;
417
418         // Go through all the scopes and try to resolve the name.
419         let break_result = self.visit_scopes(
420             scope_set,
421             parent_scope,
422             orig_ident.span.ctxt(),
423             |this, scope, use_prelude, ctxt| {
424                 let ident = Ident::new(orig_ident.name, orig_ident.span.with_ctxt(ctxt));
425                 let ok = |res, span, arenas| {
426                     Ok((
427                         (res, ty::Visibility::Public, span, LocalExpnId::ROOT)
428                             .to_name_binding(arenas),
429                         Flags::empty(),
430                     ))
431                 };
432                 let result = match scope {
433                     Scope::DeriveHelpers(expn_id) => {
434                         if let Some(attr) = this
435                             .helper_attrs
436                             .get(&expn_id)
437                             .and_then(|attrs| attrs.iter().rfind(|i| ident == **i))
438                         {
439                             let binding = (
440                                 Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper),
441                                 ty::Visibility::Public,
442                                 attr.span,
443                                 expn_id,
444                             )
445                                 .to_name_binding(this.arenas);
446                             Ok((binding, Flags::empty()))
447                         } else {
448                             Err(Determinacy::Determined)
449                         }
450                     }
451                     Scope::DeriveHelpersCompat => {
452                         let mut result = Err(Determinacy::Determined);
453                         for derive in parent_scope.derives {
454                             let parent_scope = &ParentScope { derives: &[], ..*parent_scope };
455                             match this.resolve_macro_path(
456                                 derive,
457                                 Some(MacroKind::Derive),
458                                 parent_scope,
459                                 true,
460                                 force,
461                             ) {
462                                 Ok((Some(ext), _)) => {
463                                     if ext.helper_attrs.contains(&ident.name) {
464                                         result = ok(
465                                             Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat),
466                                             derive.span,
467                                             this.arenas,
468                                         );
469                                         break;
470                                     }
471                                 }
472                                 Ok(_) | Err(Determinacy::Determined) => {}
473                                 Err(Determinacy::Undetermined) => {
474                                     result = Err(Determinacy::Undetermined)
475                                 }
476                             }
477                         }
478                         result
479                     }
480                     Scope::MacroRules(macro_rules_scope) => match macro_rules_scope.get() {
481                         MacroRulesScope::Binding(macro_rules_binding)
482                             if ident == macro_rules_binding.ident =>
483                         {
484                             Ok((macro_rules_binding.binding, Flags::MACRO_RULES))
485                         }
486                         MacroRulesScope::Invocation(_) => Err(Determinacy::Undetermined),
487                         _ => Err(Determinacy::Determined),
488                     },
489                     Scope::CrateRoot => {
490                         let root_ident = Ident::new(kw::PathRoot, ident.span);
491                         let root_module = this.resolve_crate_root(root_ident);
492                         let binding = this.resolve_ident_in_module_ext(
493                             ModuleOrUniformRoot::Module(root_module),
494                             ident,
495                             ns,
496                             parent_scope,
497                             finalize,
498                             ignore_binding,
499                         );
500                         match binding {
501                             Ok(binding) => Ok((binding, Flags::MODULE | Flags::MISC_SUGGEST_CRATE)),
502                             Err((Determinacy::Undetermined, Weak::No)) => {
503                                 return Some(Err(Determinacy::determined(force)));
504                             }
505                             Err((Determinacy::Undetermined, Weak::Yes)) => {
506                                 Err(Determinacy::Undetermined)
507                             }
508                             Err((Determinacy::Determined, _)) => Err(Determinacy::Determined),
509                         }
510                     }
511                     Scope::Module(module, derive_fallback_lint_id) => {
512                         let adjusted_parent_scope = &ParentScope { module, ..*parent_scope };
513                         let binding = this.resolve_ident_in_module_unadjusted_ext(
514                             ModuleOrUniformRoot::Module(module),
515                             ident,
516                             ns,
517                             adjusted_parent_scope,
518                             !matches!(scope_set, ScopeSet::Late(..)),
519                             finalize,
520                             ignore_binding,
521                         );
522                         match binding {
523                             Ok(binding) => {
524                                 if let Some(lint_id) = derive_fallback_lint_id {
525                                     this.lint_buffer.buffer_lint_with_diagnostic(
526                                         PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
527                                         lint_id,
528                                         orig_ident.span,
529                                         &format!(
530                                             "cannot find {} `{}` in this scope",
531                                             ns.descr(),
532                                             ident
533                                         ),
534                                         BuiltinLintDiagnostics::ProcMacroDeriveResolutionFallback(
535                                             orig_ident.span,
536                                         ),
537                                     );
538                                 }
539                                 let misc_flags = if ptr::eq(module, this.graph_root) {
540                                     Flags::MISC_SUGGEST_CRATE
541                                 } else if module.is_normal() {
542                                     Flags::MISC_SUGGEST_SELF
543                                 } else {
544                                     Flags::empty()
545                                 };
546                                 Ok((binding, Flags::MODULE | misc_flags))
547                             }
548                             Err((Determinacy::Undetermined, Weak::No)) => {
549                                 return Some(Err(Determinacy::determined(force)));
550                             }
551                             Err((Determinacy::Undetermined, Weak::Yes)) => {
552                                 Err(Determinacy::Undetermined)
553                             }
554                             Err((Determinacy::Determined, _)) => Err(Determinacy::Determined),
555                         }
556                     }
557                     Scope::RegisteredAttrs => match this.registered_attrs.get(&ident).cloned() {
558                         Some(ident) => ok(
559                             Res::NonMacroAttr(NonMacroAttrKind::Registered),
560                             ident.span,
561                             this.arenas,
562                         ),
563                         None => Err(Determinacy::Determined),
564                     },
565                     Scope::MacroUsePrelude => {
566                         match this.macro_use_prelude.get(&ident.name).cloned() {
567                             Some(binding) => Ok((binding, Flags::MISC_FROM_PRELUDE)),
568                             None => Err(Determinacy::determined(
569                                 this.graph_root.unexpanded_invocations.borrow().is_empty(),
570                             )),
571                         }
572                     }
573                     Scope::BuiltinAttrs => {
574                         if is_builtin_attr_name(ident.name) {
575                             ok(
576                                 Res::NonMacroAttr(NonMacroAttrKind::Builtin(ident.name)),
577                                 DUMMY_SP,
578                                 this.arenas,
579                             )
580                         } else {
581                             Err(Determinacy::Determined)
582                         }
583                     }
584                     Scope::ExternPrelude => {
585                         match this.extern_prelude_get(ident, finalize.is_some()) {
586                             Some(binding) => Ok((binding, Flags::empty())),
587                             None => Err(Determinacy::determined(
588                                 this.graph_root.unexpanded_invocations.borrow().is_empty(),
589                             )),
590                         }
591                     }
592                     Scope::ToolPrelude => match this.registered_tools.get(&ident).cloned() {
593                         Some(ident) => ok(Res::ToolMod, ident.span, this.arenas),
594                         None => Err(Determinacy::Determined),
595                     },
596                     Scope::StdLibPrelude => {
597                         let mut result = Err(Determinacy::Determined);
598                         if let Some(prelude) = this.prelude {
599                             if let Ok(binding) = this.resolve_ident_in_module_unadjusted(
600                                 ModuleOrUniformRoot::Module(prelude),
601                                 ident,
602                                 ns,
603                                 parent_scope,
604                                 None,
605                                 ignore_binding,
606                             ) {
607                                 if use_prelude || this.is_builtin_macro(binding.res()) {
608                                     result = Ok((binding, Flags::MISC_FROM_PRELUDE));
609                                 }
610                             }
611                         }
612                         result
613                     }
614                     Scope::BuiltinTypes => match PrimTy::from_name(ident.name) {
615                         Some(prim_ty) => ok(Res::PrimTy(prim_ty), DUMMY_SP, this.arenas),
616                         None => Err(Determinacy::Determined),
617                     },
618                 };
619
620                 match result {
621                     Ok((binding, flags))
622                         if sub_namespace_match(binding.macro_kind(), macro_kind) =>
623                     {
624                         if finalize.is_none() || matches!(scope_set, ScopeSet::Late(..)) {
625                             return Some(Ok(binding));
626                         }
627
628                         if let Some((innermost_binding, innermost_flags)) = innermost_result {
629                             // Found another solution, if the first one was "weak", report an error.
630                             let (res, innermost_res) = (binding.res(), innermost_binding.res());
631                             if res != innermost_res {
632                                 let is_builtin = |res| {
633                                     matches!(res, Res::NonMacroAttr(NonMacroAttrKind::Builtin(..)))
634                                 };
635                                 let derive_helper =
636                                     Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper);
637                                 let derive_helper_compat =
638                                     Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat);
639
640                                 let ambiguity_error_kind = if is_import {
641                                     Some(AmbiguityKind::Import)
642                                 } else if is_builtin(innermost_res) || is_builtin(res) {
643                                     Some(AmbiguityKind::BuiltinAttr)
644                                 } else if innermost_res == derive_helper_compat
645                                     || res == derive_helper_compat && innermost_res != derive_helper
646                                 {
647                                     Some(AmbiguityKind::DeriveHelper)
648                                 } else if innermost_flags.contains(Flags::MACRO_RULES)
649                                     && flags.contains(Flags::MODULE)
650                                     && !this.disambiguate_macro_rules_vs_modularized(
651                                         innermost_binding,
652                                         binding,
653                                     )
654                                     || flags.contains(Flags::MACRO_RULES)
655                                         && innermost_flags.contains(Flags::MODULE)
656                                         && !this.disambiguate_macro_rules_vs_modularized(
657                                             binding,
658                                             innermost_binding,
659                                         )
660                                 {
661                                     Some(AmbiguityKind::MacroRulesVsModularized)
662                                 } else if innermost_binding.is_glob_import() {
663                                     Some(AmbiguityKind::GlobVsOuter)
664                                 } else if innermost_binding
665                                     .may_appear_after(parent_scope.expansion, binding)
666                                 {
667                                     Some(AmbiguityKind::MoreExpandedVsOuter)
668                                 } else {
669                                     None
670                                 };
671                                 if let Some(kind) = ambiguity_error_kind {
672                                     let misc = |f: Flags| {
673                                         if f.contains(Flags::MISC_SUGGEST_CRATE) {
674                                             AmbiguityErrorMisc::SuggestCrate
675                                         } else if f.contains(Flags::MISC_SUGGEST_SELF) {
676                                             AmbiguityErrorMisc::SuggestSelf
677                                         } else if f.contains(Flags::MISC_FROM_PRELUDE) {
678                                             AmbiguityErrorMisc::FromPrelude
679                                         } else {
680                                             AmbiguityErrorMisc::None
681                                         }
682                                     };
683                                     this.ambiguity_errors.push(AmbiguityError {
684                                         kind,
685                                         ident: orig_ident,
686                                         b1: innermost_binding,
687                                         b2: binding,
688                                         misc1: misc(innermost_flags),
689                                         misc2: misc(flags),
690                                     });
691                                     return Some(Ok(innermost_binding));
692                                 }
693                             }
694                         } else {
695                             // Found the first solution.
696                             innermost_result = Some((binding, flags));
697                         }
698                     }
699                     Ok(..) | Err(Determinacy::Determined) => {}
700                     Err(Determinacy::Undetermined) => determinacy = Determinacy::Undetermined,
701                 }
702
703                 None
704             },
705         );
706
707         if let Some(break_result) = break_result {
708             return break_result;
709         }
710
711         // The first found solution was the only one, return it.
712         if let Some((binding, _)) = innermost_result {
713             return Ok(binding);
714         }
715
716         Err(Determinacy::determined(determinacy == Determinacy::Determined || force))
717     }
718
719     #[tracing::instrument(level = "debug", skip(self))]
720     pub(crate) fn maybe_resolve_ident_in_module(
721         &mut self,
722         module: ModuleOrUniformRoot<'a>,
723         ident: Ident,
724         ns: Namespace,
725         parent_scope: &ParentScope<'a>,
726     ) -> Result<&'a NameBinding<'a>, Determinacy> {
727         self.resolve_ident_in_module_ext(module, ident, ns, parent_scope, None, None)
728             .map_err(|(determinacy, _)| determinacy)
729     }
730
731     #[tracing::instrument(level = "debug", skip(self))]
732     pub(crate) fn resolve_ident_in_module(
733         &mut self,
734         module: ModuleOrUniformRoot<'a>,
735         ident: Ident,
736         ns: Namespace,
737         parent_scope: &ParentScope<'a>,
738         finalize: Option<Finalize>,
739         ignore_binding: Option<&'a NameBinding<'a>>,
740     ) -> Result<&'a NameBinding<'a>, Determinacy> {
741         self.resolve_ident_in_module_ext(module, ident, ns, parent_scope, finalize, ignore_binding)
742             .map_err(|(determinacy, _)| determinacy)
743     }
744
745     #[tracing::instrument(level = "debug", skip(self))]
746     fn resolve_ident_in_module_ext(
747         &mut self,
748         module: ModuleOrUniformRoot<'a>,
749         mut ident: Ident,
750         ns: Namespace,
751         parent_scope: &ParentScope<'a>,
752         finalize: Option<Finalize>,
753         ignore_binding: Option<&'a NameBinding<'a>>,
754     ) -> Result<&'a NameBinding<'a>, (Determinacy, Weak)> {
755         let tmp_parent_scope;
756         let mut adjusted_parent_scope = parent_scope;
757         match module {
758             ModuleOrUniformRoot::Module(m) => {
759                 if let Some(def) = ident.span.normalize_to_macros_2_0_and_adjust(m.expansion) {
760                     tmp_parent_scope =
761                         ParentScope { module: self.expn_def_scope(def), ..*parent_scope };
762                     adjusted_parent_scope = &tmp_parent_scope;
763                 }
764             }
765             ModuleOrUniformRoot::ExternPrelude => {
766                 ident.span.normalize_to_macros_2_0_and_adjust(ExpnId::root());
767             }
768             ModuleOrUniformRoot::CrateRootAndExternPrelude | ModuleOrUniformRoot::CurrentScope => {
769                 // No adjustments
770             }
771         }
772         self.resolve_ident_in_module_unadjusted_ext(
773             module,
774             ident,
775             ns,
776             adjusted_parent_scope,
777             false,
778             finalize,
779             ignore_binding,
780         )
781     }
782
783     #[tracing::instrument(level = "debug", skip(self))]
784     fn resolve_ident_in_module_unadjusted(
785         &mut self,
786         module: ModuleOrUniformRoot<'a>,
787         ident: Ident,
788         ns: Namespace,
789         parent_scope: &ParentScope<'a>,
790         finalize: Option<Finalize>,
791         ignore_binding: Option<&'a NameBinding<'a>>,
792     ) -> Result<&'a NameBinding<'a>, Determinacy> {
793         self.resolve_ident_in_module_unadjusted_ext(
794             module,
795             ident,
796             ns,
797             parent_scope,
798             false,
799             finalize,
800             ignore_binding,
801         )
802         .map_err(|(determinacy, _)| determinacy)
803     }
804
805     /// Attempts to resolve `ident` in namespaces `ns` of `module`.
806     /// Invariant: if `finalize` is `Some`, expansion and import resolution must be complete.
807     #[tracing::instrument(level = "debug", skip(self))]
808     fn resolve_ident_in_module_unadjusted_ext(
809         &mut self,
810         module: ModuleOrUniformRoot<'a>,
811         ident: Ident,
812         ns: Namespace,
813         parent_scope: &ParentScope<'a>,
814         restricted_shadowing: bool,
815         finalize: Option<Finalize>,
816         // This binding should be ignored during in-module resolution, so that we don't get
817         // "self-confirming" import resolutions during import validation and checking.
818         ignore_binding: Option<&'a NameBinding<'a>>,
819     ) -> Result<&'a NameBinding<'a>, (Determinacy, Weak)> {
820         let module = match module {
821             ModuleOrUniformRoot::Module(module) => module,
822             ModuleOrUniformRoot::CrateRootAndExternPrelude => {
823                 assert!(!restricted_shadowing);
824                 let binding = self.early_resolve_ident_in_lexical_scope(
825                     ident,
826                     ScopeSet::AbsolutePath(ns),
827                     parent_scope,
828                     finalize,
829                     finalize.is_some(),
830                     ignore_binding,
831                 );
832                 return binding.map_err(|determinacy| (determinacy, Weak::No));
833             }
834             ModuleOrUniformRoot::ExternPrelude => {
835                 assert!(!restricted_shadowing);
836                 return if ns != TypeNS {
837                     Err((Determined, Weak::No))
838                 } else if let Some(binding) = self.extern_prelude_get(ident, finalize.is_some()) {
839                     Ok(binding)
840                 } else if !self.graph_root.unexpanded_invocations.borrow().is_empty() {
841                     // Macro-expanded `extern crate` items can add names to extern prelude.
842                     Err((Undetermined, Weak::No))
843                 } else {
844                     Err((Determined, Weak::No))
845                 };
846             }
847             ModuleOrUniformRoot::CurrentScope => {
848                 assert!(!restricted_shadowing);
849                 if ns == TypeNS {
850                     if ident.name == kw::Crate || ident.name == kw::DollarCrate {
851                         let module = self.resolve_crate_root(ident);
852                         let binding =
853                             (module, ty::Visibility::Public, module.span, LocalExpnId::ROOT)
854                                 .to_name_binding(self.arenas);
855                         return Ok(binding);
856                     } else if ident.name == kw::Super || ident.name == kw::SelfLower {
857                         // FIXME: Implement these with renaming requirements so that e.g.
858                         // `use super;` doesn't work, but `use super as name;` does.
859                         // Fall through here to get an error from `early_resolve_...`.
860                     }
861                 }
862
863                 let scopes = ScopeSet::All(ns, true);
864                 let binding = self.early_resolve_ident_in_lexical_scope(
865                     ident,
866                     scopes,
867                     parent_scope,
868                     finalize,
869                     finalize.is_some(),
870                     ignore_binding,
871                 );
872                 return binding.map_err(|determinacy| (determinacy, Weak::No));
873             }
874         };
875
876         let key = self.new_key(ident, ns);
877         let resolution =
878             self.resolution(module, key).try_borrow_mut().map_err(|_| (Determined, Weak::No))?; // This happens when there is a cycle of imports.
879
880         if let Some(Finalize { path_span, report_private, .. }) = finalize {
881             // If the primary binding is unusable, search further and return the shadowed glob
882             // binding if it exists. What we really want here is having two separate scopes in
883             // a module - one for non-globs and one for globs, but until that's done use this
884             // hack to avoid inconsistent resolution ICEs during import validation.
885             let binding = [resolution.binding, resolution.shadowed_glob]
886                 .into_iter()
887                 .filter_map(|binding| match (binding, ignore_binding) {
888                     (Some(binding), Some(ignored)) if ptr::eq(binding, ignored) => None,
889                     _ => binding,
890                 })
891                 .next();
892             let Some(binding) = binding else {
893                 return Err((Determined, Weak::No));
894             };
895
896             if !self.is_accessible_from(binding.vis, parent_scope.module) {
897                 if report_private {
898                     self.privacy_errors.push(PrivacyError {
899                         ident,
900                         binding,
901                         dedup_span: path_span,
902                     });
903                 } else {
904                     return Err((Determined, Weak::No));
905                 }
906             }
907
908             // Forbid expanded shadowing to avoid time travel.
909             if let Some(shadowed_glob) = resolution.shadowed_glob
910                 && restricted_shadowing
911                 && binding.expansion != LocalExpnId::ROOT
912                 && binding.res() != shadowed_glob.res()
913             {
914                 self.ambiguity_errors.push(AmbiguityError {
915                     kind: AmbiguityKind::GlobVsExpanded,
916                     ident,
917                     b1: binding,
918                     b2: shadowed_glob,
919                     misc1: AmbiguityErrorMisc::None,
920                     misc2: AmbiguityErrorMisc::None,
921                 });
922             }
923
924             if !restricted_shadowing && binding.expansion != LocalExpnId::ROOT {
925                 if let NameBindingKind::Res(_, true) = binding.kind {
926                     self.macro_expanded_macro_export_errors.insert((path_span, binding.span));
927                 }
928             }
929
930             self.record_use(ident, binding, restricted_shadowing);
931             return Ok(binding);
932         }
933
934         let check_usable = |this: &mut Self, binding: &'a NameBinding<'a>| {
935             if let Some(ignored) = ignore_binding && ptr::eq(binding, ignored) {
936                 return Err((Determined, Weak::No));
937             }
938             let usable = this.is_accessible_from(binding.vis, parent_scope.module);
939             if usable { Ok(binding) } else { Err((Determined, Weak::No)) }
940         };
941
942         // Items and single imports are not shadowable, if we have one, then it's determined.
943         if let Some(binding) = resolution.binding {
944             if !binding.is_glob_import() {
945                 return check_usable(self, binding);
946             }
947         }
948
949         // --- From now on we either have a glob resolution or no resolution. ---
950
951         // Check if one of single imports can still define the name,
952         // if it can then our result is not determined and can be invalidated.
953         for single_import in &resolution.single_imports {
954             if !self.is_accessible_from(single_import.vis.get(), parent_scope.module) {
955                 continue;
956             }
957             let Some(module) = single_import.imported_module.get() else {
958                 return Err((Undetermined, Weak::No));
959             };
960             let ImportKind::Single { source: ident, .. } = single_import.kind else {
961                 unreachable!();
962             };
963             match self.resolve_ident_in_module(
964                 module,
965                 ident,
966                 ns,
967                 &single_import.parent_scope,
968                 None,
969                 ignore_binding,
970             ) {
971                 Err(Determined) => continue,
972                 Ok(binding)
973                     if !self.is_accessible_from(binding.vis, single_import.parent_scope.module) =>
974                 {
975                     continue;
976                 }
977                 Ok(_) | Err(Undetermined) => return Err((Undetermined, Weak::No)),
978             }
979         }
980
981         // So we have a resolution that's from a glob import. This resolution is determined
982         // if it cannot be shadowed by some new item/import expanded from a macro.
983         // This happens either if there are no unexpanded macros, or expanded names cannot
984         // shadow globs (that happens in macro namespace or with restricted shadowing).
985         //
986         // Additionally, any macro in any module can plant names in the root module if it creates
987         // `macro_export` macros, so the root module effectively has unresolved invocations if any
988         // module has unresolved invocations.
989         // However, it causes resolution/expansion to stuck too often (#53144), so, to make
990         // progress, we have to ignore those potential unresolved invocations from other modules
991         // and prohibit access to macro-expanded `macro_export` macros instead (unless restricted
992         // shadowing is enabled, see `macro_expanded_macro_export_errors`).
993         let unexpanded_macros = !module.unexpanded_invocations.borrow().is_empty();
994         if let Some(binding) = resolution.binding {
995             if !unexpanded_macros || ns == MacroNS || restricted_shadowing {
996                 return check_usable(self, binding);
997             } else {
998                 return Err((Undetermined, Weak::No));
999             }
1000         }
1001
1002         // --- From now on we have no resolution. ---
1003
1004         // Now we are in situation when new item/import can appear only from a glob or a macro
1005         // expansion. With restricted shadowing names from globs and macro expansions cannot
1006         // shadow names from outer scopes, so we can freely fallback from module search to search
1007         // in outer scopes. For `early_resolve_ident_in_lexical_scope` to continue search in outer
1008         // scopes we return `Undetermined` with `Weak::Yes`.
1009
1010         // Check if one of unexpanded macros can still define the name,
1011         // if it can then our "no resolution" result is not determined and can be invalidated.
1012         if unexpanded_macros {
1013             return Err((Undetermined, Weak::Yes));
1014         }
1015
1016         // Check if one of glob imports can still define the name,
1017         // if it can then our "no resolution" result is not determined and can be invalidated.
1018         for glob_import in module.globs.borrow().iter() {
1019             if !self.is_accessible_from(glob_import.vis.get(), parent_scope.module) {
1020                 continue;
1021             }
1022             let module = match glob_import.imported_module.get() {
1023                 Some(ModuleOrUniformRoot::Module(module)) => module,
1024                 Some(_) => continue,
1025                 None => return Err((Undetermined, Weak::Yes)),
1026             };
1027             let tmp_parent_scope;
1028             let (mut adjusted_parent_scope, mut ident) =
1029                 (parent_scope, ident.normalize_to_macros_2_0());
1030             match ident.span.glob_adjust(module.expansion, glob_import.span) {
1031                 Some(Some(def)) => {
1032                     tmp_parent_scope =
1033                         ParentScope { module: self.expn_def_scope(def), ..*parent_scope };
1034                     adjusted_parent_scope = &tmp_parent_scope;
1035                 }
1036                 Some(None) => {}
1037                 None => continue,
1038             };
1039             let result = self.resolve_ident_in_module_unadjusted(
1040                 ModuleOrUniformRoot::Module(module),
1041                 ident,
1042                 ns,
1043                 adjusted_parent_scope,
1044                 None,
1045                 ignore_binding,
1046             );
1047
1048             match result {
1049                 Err(Determined) => continue,
1050                 Ok(binding)
1051                     if !self.is_accessible_from(binding.vis, glob_import.parent_scope.module) =>
1052                 {
1053                     continue;
1054                 }
1055                 Ok(_) | Err(Undetermined) => return Err((Undetermined, Weak::Yes)),
1056             }
1057         }
1058
1059         // No resolution and no one else can define the name - determinate error.
1060         Err((Determined, Weak::No))
1061     }
1062
1063     /// Validate a local resolution (from ribs).
1064     #[tracing::instrument(level = "debug", skip(self, all_ribs))]
1065     fn validate_res_from_ribs(
1066         &mut self,
1067         rib_index: usize,
1068         rib_ident: Ident,
1069         mut res: Res,
1070         finalize: Option<Span>,
1071         original_rib_ident_def: Ident,
1072         all_ribs: &[Rib<'a>],
1073     ) -> Res {
1074         const CG_BUG_STR: &str = "min_const_generics resolve check didn't stop compilation";
1075         debug!("validate_res_from_ribs({:?})", res);
1076         let ribs = &all_ribs[rib_index + 1..];
1077
1078         // An invalid forward use of a generic parameter from a previous default.
1079         if let ForwardGenericParamBanRibKind = all_ribs[rib_index].kind {
1080             if let Some(span) = finalize {
1081                 let res_error = if rib_ident.name == kw::SelfUpper {
1082                     ResolutionError::SelfInGenericParamDefault
1083                 } else {
1084                     ResolutionError::ForwardDeclaredGenericParam
1085                 };
1086                 self.report_error(span, res_error);
1087             }
1088             assert_eq!(res, Res::Err);
1089             return Res::Err;
1090         }
1091
1092         match res {
1093             Res::Local(_) => {
1094                 use ResolutionError::*;
1095                 let mut res_err = None;
1096
1097                 for rib in ribs {
1098                     match rib.kind {
1099                         NormalRibKind
1100                         | ClosureOrAsyncRibKind
1101                         | ModuleRibKind(..)
1102                         | MacroDefinition(..)
1103                         | ForwardGenericParamBanRibKind => {
1104                             // Nothing to do. Continue.
1105                         }
1106                         ItemRibKind(_) | FnItemRibKind | AssocItemRibKind => {
1107                             // This was an attempt to access an upvar inside a
1108                             // named function item. This is not allowed, so we
1109                             // report an error.
1110                             if let Some(span) = finalize {
1111                                 // We don't immediately trigger a resolve error, because
1112                                 // we want certain other resolution errors (namely those
1113                                 // emitted for `ConstantItemRibKind` below) to take
1114                                 // precedence.
1115                                 res_err = Some((span, CannotCaptureDynamicEnvironmentInFnItem));
1116                             }
1117                         }
1118                         ConstantItemRibKind(_, item) => {
1119                             // Still doesn't deal with upvars
1120                             if let Some(span) = finalize {
1121                                 let (span, resolution_error) =
1122                                     if let Some((ident, constant_item_kind)) = item {
1123                                         let kind_str = match constant_item_kind {
1124                                             ConstantItemKind::Const => "const",
1125                                             ConstantItemKind::Static => "static",
1126                                         };
1127                                         (
1128                                             span,
1129                                             AttemptToUseNonConstantValueInConstant(
1130                                                 ident, "let", kind_str,
1131                                             ),
1132                                         )
1133                                     } else {
1134                                         (
1135                                             rib_ident.span,
1136                                             AttemptToUseNonConstantValueInConstant(
1137                                                 original_rib_ident_def,
1138                                                 "const",
1139                                                 "let",
1140                                             ),
1141                                         )
1142                                     };
1143                                 self.report_error(span, resolution_error);
1144                             }
1145                             return Res::Err;
1146                         }
1147                         ConstParamTyRibKind => {
1148                             if let Some(span) = finalize {
1149                                 self.report_error(span, ParamInTyOfConstParam(rib_ident.name));
1150                             }
1151                             return Res::Err;
1152                         }
1153                         InlineAsmSymRibKind => {
1154                             if let Some(span) = finalize {
1155                                 self.report_error(span, InvalidAsmSym);
1156                             }
1157                             return Res::Err;
1158                         }
1159                     }
1160                 }
1161                 if let Some((span, res_err)) = res_err {
1162                     self.report_error(span, res_err);
1163                     return Res::Err;
1164                 }
1165             }
1166             Res::Def(DefKind::TyParam, _) | Res::SelfTy { .. } => {
1167                 for rib in ribs {
1168                     let has_generic_params: HasGenericParams = match rib.kind {
1169                         NormalRibKind
1170                         | ClosureOrAsyncRibKind
1171                         | AssocItemRibKind
1172                         | ModuleRibKind(..)
1173                         | MacroDefinition(..)
1174                         | InlineAsmSymRibKind
1175                         | ForwardGenericParamBanRibKind => {
1176                             // Nothing to do. Continue.
1177                             continue;
1178                         }
1179
1180                         ConstantItemRibKind(trivial, _) => {
1181                             let features = self.session.features_untracked();
1182                             // HACK(min_const_generics): We currently only allow `N` or `{ N }`.
1183                             if !(trivial == HasGenericParams::Yes || features.generic_const_exprs) {
1184                                 // HACK(min_const_generics): If we encounter `Self` in an anonymous constant
1185                                 // we can't easily tell if it's generic at this stage, so we instead remember
1186                                 // this and then enforce the self type to be concrete later on.
1187                                 if let Res::SelfTy { trait_, alias_to: Some((def, _)) } = res {
1188                                     res = Res::SelfTy { trait_, alias_to: Some((def, true)) }
1189                                 } else {
1190                                     if let Some(span) = finalize {
1191                                         self.report_error(
1192                                             span,
1193                                             ResolutionError::ParamInNonTrivialAnonConst {
1194                                                 name: rib_ident.name,
1195                                                 is_type: true,
1196                                             },
1197                                         );
1198                                         self.session.delay_span_bug(span, CG_BUG_STR);
1199                                     }
1200
1201                                     return Res::Err;
1202                                 }
1203                             }
1204
1205                             continue;
1206                         }
1207
1208                         // This was an attempt to use a type parameter outside its scope.
1209                         ItemRibKind(has_generic_params) => has_generic_params,
1210                         FnItemRibKind => HasGenericParams::Yes,
1211                         ConstParamTyRibKind => {
1212                             if let Some(span) = finalize {
1213                                 self.report_error(
1214                                     span,
1215                                     ResolutionError::ParamInTyOfConstParam(rib_ident.name),
1216                                 );
1217                             }
1218                             return Res::Err;
1219                         }
1220                     };
1221
1222                     if let Some(span) = finalize {
1223                         self.report_error(
1224                             span,
1225                             ResolutionError::GenericParamsFromOuterFunction(
1226                                 res,
1227                                 has_generic_params,
1228                             ),
1229                         );
1230                     }
1231                     return Res::Err;
1232                 }
1233             }
1234             Res::Def(DefKind::ConstParam, _) => {
1235                 let mut ribs = ribs.iter().peekable();
1236                 if let Some(Rib { kind: FnItemRibKind, .. }) = ribs.peek() {
1237                     // When declaring const parameters inside function signatures, the first rib
1238                     // is always a `FnItemRibKind`. In this case, we can skip it, to avoid it
1239                     // (spuriously) conflicting with the const param.
1240                     ribs.next();
1241                 }
1242
1243                 for rib in ribs {
1244                     let has_generic_params = match rib.kind {
1245                         NormalRibKind
1246                         | ClosureOrAsyncRibKind
1247                         | AssocItemRibKind
1248                         | ModuleRibKind(..)
1249                         | MacroDefinition(..)
1250                         | InlineAsmSymRibKind
1251                         | ForwardGenericParamBanRibKind => continue,
1252
1253                         ConstantItemRibKind(trivial, _) => {
1254                             let features = self.session.features_untracked();
1255                             // HACK(min_const_generics): We currently only allow `N` or `{ N }`.
1256                             if !(trivial == HasGenericParams::Yes || features.generic_const_exprs) {
1257                                 if let Some(span) = finalize {
1258                                     self.report_error(
1259                                         span,
1260                                         ResolutionError::ParamInNonTrivialAnonConst {
1261                                             name: rib_ident.name,
1262                                             is_type: false,
1263                                         },
1264                                     );
1265                                     self.session.delay_span_bug(span, CG_BUG_STR);
1266                                 }
1267
1268                                 return Res::Err;
1269                             }
1270
1271                             continue;
1272                         }
1273
1274                         ItemRibKind(has_generic_params) => has_generic_params,
1275                         FnItemRibKind => HasGenericParams::Yes,
1276                         ConstParamTyRibKind => {
1277                             if let Some(span) = finalize {
1278                                 self.report_error(
1279                                     span,
1280                                     ResolutionError::ParamInTyOfConstParam(rib_ident.name),
1281                                 );
1282                             }
1283                             return Res::Err;
1284                         }
1285                     };
1286
1287                     // This was an attempt to use a const parameter outside its scope.
1288                     if let Some(span) = finalize {
1289                         self.report_error(
1290                             span,
1291                             ResolutionError::GenericParamsFromOuterFunction(
1292                                 res,
1293                                 has_generic_params,
1294                             ),
1295                         );
1296                     }
1297                     return Res::Err;
1298                 }
1299             }
1300             _ => {}
1301         }
1302         res
1303     }
1304
1305     #[tracing::instrument(level = "debug", skip(self))]
1306     pub(crate) fn maybe_resolve_path(
1307         &mut self,
1308         path: &[Segment],
1309         opt_ns: Option<Namespace>, // `None` indicates a module path in import
1310         parent_scope: &ParentScope<'a>,
1311     ) -> PathResult<'a> {
1312         self.resolve_path_with_ribs(path, opt_ns, parent_scope, None, None, None)
1313     }
1314
1315     #[tracing::instrument(level = "debug", skip(self))]
1316     pub(crate) fn resolve_path(
1317         &mut self,
1318         path: &[Segment],
1319         opt_ns: Option<Namespace>, // `None` indicates a module path in import
1320         parent_scope: &ParentScope<'a>,
1321         finalize: Option<Finalize>,
1322         ignore_binding: Option<&'a NameBinding<'a>>,
1323     ) -> PathResult<'a> {
1324         self.resolve_path_with_ribs(path, opt_ns, parent_scope, finalize, None, ignore_binding)
1325     }
1326
1327     pub(crate) fn resolve_path_with_ribs(
1328         &mut self,
1329         path: &[Segment],
1330         opt_ns: Option<Namespace>, // `None` indicates a module path in import
1331         parent_scope: &ParentScope<'a>,
1332         finalize: Option<Finalize>,
1333         ribs: Option<&PerNS<Vec<Rib<'a>>>>,
1334         ignore_binding: Option<&'a NameBinding<'a>>,
1335     ) -> PathResult<'a> {
1336         debug!("resolve_path(path={:?}, opt_ns={:?}, finalize={:?})", path, opt_ns, finalize);
1337
1338         let mut module = None;
1339         let mut allow_super = true;
1340         let mut second_binding = None;
1341
1342         for (i, &Segment { ident, id, .. }) in path.iter().enumerate() {
1343             debug!("resolve_path ident {} {:?} {:?}", i, ident, id);
1344             let record_segment_res = |this: &mut Self, res| {
1345                 if finalize.is_some() {
1346                     if let Some(id) = id {
1347                         if !this.partial_res_map.contains_key(&id) {
1348                             assert!(id != ast::DUMMY_NODE_ID, "Trying to resolve dummy id");
1349                             this.record_partial_res(id, PartialRes::new(res));
1350                         }
1351                     }
1352                 }
1353             };
1354
1355             let is_last = i == path.len() - 1;
1356             let ns = if is_last { opt_ns.unwrap_or(TypeNS) } else { TypeNS };
1357             let name = ident.name;
1358
1359             allow_super &= ns == TypeNS && (name == kw::SelfLower || name == kw::Super);
1360
1361             if ns == TypeNS {
1362                 if allow_super && name == kw::Super {
1363                     let mut ctxt = ident.span.ctxt().normalize_to_macros_2_0();
1364                     let self_module = match i {
1365                         0 => Some(self.resolve_self(&mut ctxt, parent_scope.module)),
1366                         _ => match module {
1367                             Some(ModuleOrUniformRoot::Module(module)) => Some(module),
1368                             _ => None,
1369                         },
1370                     };
1371                     if let Some(self_module) = self_module {
1372                         if let Some(parent) = self_module.parent {
1373                             module = Some(ModuleOrUniformRoot::Module(
1374                                 self.resolve_self(&mut ctxt, parent),
1375                             ));
1376                             continue;
1377                         }
1378                     }
1379                     return PathResult::failed(ident.span, false, finalize.is_some(), || {
1380                         ("there are too many leading `super` keywords".to_string(), None)
1381                     });
1382                 }
1383                 if i == 0 {
1384                     if name == kw::SelfLower {
1385                         let mut ctxt = ident.span.ctxt().normalize_to_macros_2_0();
1386                         module = Some(ModuleOrUniformRoot::Module(
1387                             self.resolve_self(&mut ctxt, parent_scope.module),
1388                         ));
1389                         continue;
1390                     }
1391                     if name == kw::PathRoot && ident.span.rust_2018() {
1392                         module = Some(ModuleOrUniformRoot::ExternPrelude);
1393                         continue;
1394                     }
1395                     if name == kw::PathRoot && ident.span.rust_2015() && self.session.rust_2018() {
1396                         // `::a::b` from 2015 macro on 2018 global edition
1397                         module = Some(ModuleOrUniformRoot::CrateRootAndExternPrelude);
1398                         continue;
1399                     }
1400                     if name == kw::PathRoot || name == kw::Crate || name == kw::DollarCrate {
1401                         // `::a::b`, `crate::a::b` or `$crate::a::b`
1402                         module = Some(ModuleOrUniformRoot::Module(self.resolve_crate_root(ident)));
1403                         continue;
1404                     }
1405                 }
1406             }
1407
1408             // Report special messages for path segment keywords in wrong positions.
1409             if ident.is_path_segment_keyword() && i != 0 {
1410                 return PathResult::failed(ident.span, false, finalize.is_some(), || {
1411                     let name_str = if name == kw::PathRoot {
1412                         "crate root".to_string()
1413                     } else {
1414                         format!("`{}`", name)
1415                     };
1416                     let label = if i == 1 && path[0].ident.name == kw::PathRoot {
1417                         format!("global paths cannot start with {}", name_str)
1418                     } else {
1419                         format!("{} in paths can only be used in start position", name_str)
1420                     };
1421                     (label, None)
1422                 });
1423             }
1424
1425             enum FindBindingResult<'a> {
1426                 Binding(Result<&'a NameBinding<'a>, Determinacy>),
1427                 Res(Res),
1428             }
1429             let find_binding_in_ns = |this: &mut Self, ns| {
1430                 let binding = if let Some(module) = module {
1431                     this.resolve_ident_in_module(
1432                         module,
1433                         ident,
1434                         ns,
1435                         parent_scope,
1436                         finalize,
1437                         ignore_binding,
1438                     )
1439                 } else if let Some(ribs) = ribs
1440                     && let Some(TypeNS | ValueNS) = opt_ns
1441                 {
1442                     match this.resolve_ident_in_lexical_scope(
1443                         ident,
1444                         ns,
1445                         parent_scope,
1446                         finalize,
1447                         &ribs[ns],
1448                         ignore_binding,
1449                     ) {
1450                         // we found a locally-imported or available item/module
1451                         Some(LexicalScopeBinding::Item(binding)) => Ok(binding),
1452                         // we found a local variable or type param
1453                         Some(LexicalScopeBinding::Res(res)) => return FindBindingResult::Res(res),
1454                         _ => Err(Determinacy::determined(finalize.is_some())),
1455                     }
1456                 } else {
1457                     let scopes = ScopeSet::All(ns, opt_ns.is_none());
1458                     this.early_resolve_ident_in_lexical_scope(
1459                         ident,
1460                         scopes,
1461                         parent_scope,
1462                         finalize,
1463                         finalize.is_some(),
1464                         ignore_binding,
1465                     )
1466                 };
1467                 FindBindingResult::Binding(binding)
1468             };
1469             let binding = match find_binding_in_ns(self, ns) {
1470                 FindBindingResult::Res(res) => {
1471                     record_segment_res(self, res);
1472                     return PathResult::NonModule(PartialRes::with_unresolved_segments(
1473                         res,
1474                         path.len() - 1,
1475                     ));
1476                 }
1477                 FindBindingResult::Binding(binding) => binding,
1478             };
1479             match binding {
1480                 Ok(binding) => {
1481                     if i == 1 {
1482                         second_binding = Some(binding);
1483                     }
1484                     let res = binding.res();
1485                     let maybe_assoc = opt_ns != Some(MacroNS) && PathSource::Type.is_expected(res);
1486                     if let Some(next_module) = binding.module() {
1487                         module = Some(ModuleOrUniformRoot::Module(next_module));
1488                         record_segment_res(self, res);
1489                     } else if res == Res::ToolMod && i + 1 != path.len() {
1490                         if binding.is_import() {
1491                             self.session
1492                                 .struct_span_err(
1493                                     ident.span,
1494                                     "cannot use a tool module through an import",
1495                                 )
1496                                 .span_note(binding.span, "the tool module imported here")
1497                                 .emit();
1498                         }
1499                         let res = Res::NonMacroAttr(NonMacroAttrKind::Tool);
1500                         return PathResult::NonModule(PartialRes::new(res));
1501                     } else if res == Res::Err {
1502                         return PathResult::NonModule(PartialRes::new(Res::Err));
1503                     } else if opt_ns.is_some() && (is_last || maybe_assoc) {
1504                         self.lint_if_path_starts_with_module(finalize, path, second_binding);
1505                         record_segment_res(self, res);
1506                         return PathResult::NonModule(PartialRes::with_unresolved_segments(
1507                             res,
1508                             path.len() - i - 1,
1509                         ));
1510                     } else {
1511                         return PathResult::failed(ident.span, is_last, finalize.is_some(), || {
1512                             let label = format!(
1513                                 "`{ident}` is {} {}, not a module",
1514                                 res.article(),
1515                                 res.descr()
1516                             );
1517                             (label, None)
1518                         });
1519                     }
1520                 }
1521                 Err(Undetermined) => return PathResult::Indeterminate,
1522                 Err(Determined) => {
1523                     if let Some(ModuleOrUniformRoot::Module(module)) = module {
1524                         if opt_ns.is_some() && !module.is_normal() {
1525                             return PathResult::NonModule(PartialRes::with_unresolved_segments(
1526                                 module.res().unwrap(),
1527                                 path.len() - i,
1528                             ));
1529                         }
1530                     }
1531
1532                     return PathResult::failed(ident.span, is_last, finalize.is_some(), || {
1533                         self.report_path_resolution_error(
1534                             path,
1535                             opt_ns,
1536                             parent_scope,
1537                             ribs,
1538                             ignore_binding,
1539                             module,
1540                             i,
1541                             ident,
1542                         )
1543                     });
1544                 }
1545             }
1546         }
1547
1548         self.lint_if_path_starts_with_module(finalize, path, second_binding);
1549
1550         PathResult::Module(match module {
1551             Some(module) => module,
1552             None if path.is_empty() => ModuleOrUniformRoot::CurrentScope,
1553             _ => bug!("resolve_path: non-empty path `{:?}` has no module", path),
1554         })
1555     }
1556 }