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