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