blob: 945c7ce3a9b3276e73888f2f694acbedb4c09c14 [file] [log] [blame]
Charisee9cf67802022-06-30 20:04:09 +00001use rustc_ast::{self as ast, NodeId};
2use rustc_feature::is_builtin_attr_name;
3use rustc_hir::def::{DefKind, Namespace, NonMacroAttrKind, PartialRes, PerNS};
4use rustc_hir::PrimTy;
5use rustc_middle::bug;
6use rustc_middle::ty;
7use rustc_session::lint::builtin::PROC_MACRO_DERIVE_RESOLUTION_FALLBACK;
8use rustc_session::lint::BuiltinLintDiagnostics;
Chris Wailes2f380c12022-11-09 13:04:22 -08009use rustc_span::def_id::LocalDefId;
Charisee9cf67802022-06-30 20:04:09 +000010use rustc_span::hygiene::{ExpnId, ExpnKind, LocalExpnId, MacroKind, SyntaxContext};
11use rustc_span::symbol::{kw, Ident};
12use rustc_span::{Span, DUMMY_SP};
13
14use std::ptr;
15
Chris Wailescd1aefd2023-07-13 13:36:21 -070016use crate::errors::{ParamKindInEnumDiscriminant, ParamKindInNonTrivialAnonConst};
Chris Wailes2f380c12022-11-09 13:04:22 -080017use crate::late::{
Chris Wailescd1aefd2023-07-13 13:36:21 -070018 ConstantHasGenerics, HasGenericParams, NoConstantGenericsReason, PathSource, Rib, RibKind,
Chris Wailes2f380c12022-11-09 13:04:22 -080019};
Charisee9cf67802022-06-30 20:04:09 +000020use crate::macros::{sub_namespace_match, MacroRulesScope};
Chris Wailescd1aefd2023-07-13 13:36:21 -070021use crate::BindingKey;
Charisee635618d2023-06-01 20:46:00 +000022use crate::{errors, AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, Determinacy, Finalize};
Chris Wailes977026a2023-02-13 09:13:10 -080023use crate::{Import, ImportKind, LexicalScopeBinding, Module, ModuleKind, ModuleOrUniformRoot};
Charisee9cf67802022-06-30 20:04:09 +000024use crate::{NameBinding, NameBindingKind, ParentScope, PathResult, PrivacyError, Res};
25use crate::{ResolutionError, Resolver, Scope, ScopeSet, Segment, ToNameBinding, Weak};
26
27use Determinacy::*;
28use Namespace::*;
Charisee9cf67802022-06-30 20:04:09 +000029
Chris Wailes2f380c12022-11-09 13:04:22 -080030type Visibility = ty::Visibility<LocalDefId>;
31
Chris Wailes5c0824a2023-04-24 16:30:59 -070032impl<'a, 'tcx> Resolver<'a, 'tcx> {
Charisee9cf67802022-06-30 20:04:09 +000033 /// A generic scope visitor.
34 /// Visits scopes in order to resolve some identifier in them or perform other actions.
35 /// If the callback returns `Some` result, we stop visiting scopes and return it.
Chris Wailes65720582022-08-11 09:53:28 -070036 pub(crate) fn visit_scopes<T>(
Charisee9cf67802022-06-30 20:04:09 +000037 &mut self,
38 scope_set: ScopeSet<'a>,
39 parent_scope: &ParentScope<'a>,
40 ctxt: SyntaxContext,
41 mut visitor: impl FnMut(
42 &mut Self,
43 Scope<'a>,
44 /*use_prelude*/ bool,
45 SyntaxContext,
46 ) -> Option<T>,
47 ) -> Option<T> {
48 // General principles:
49 // 1. Not controlled (user-defined) names should have higher priority than controlled names
50 // built into the language or standard library. This way we can add new names into the
51 // language or standard library without breaking user code.
52 // 2. "Closed set" below means new names cannot appear after the current resolution attempt.
53 // Places to search (in order of decreasing priority):
54 // (Type NS)
55 // 1. FIXME: Ribs (type parameters), there's no necessary infrastructure yet
56 // (open set, not controlled).
57 // 2. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
58 // (open, not controlled).
59 // 3. Extern prelude (open, the open part is from macro expansions, not controlled).
60 // 4. Tool modules (closed, controlled right now, but not in the future).
61 // 5. Standard library prelude (de-facto closed, controlled).
62 // 6. Language prelude (closed, controlled).
63 // (Value NS)
64 // 1. FIXME: Ribs (local variables), 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. Standard library prelude (de-facto closed, controlled).
69 // (Macro NS)
70 // 1-3. Derive helpers (open, not controlled). All ambiguities with other names
71 // are currently reported as errors. They should be higher in priority than preludes
72 // and probably even names in modules according to the "general principles" above. They
73 // also should be subject to restricted shadowing because are effectively produced by
74 // derives (you need to resolve the derive first to add helpers into scope), but they
75 // should be available before the derive is expanded for compatibility.
76 // It's mess in general, so we are being conservative for now.
77 // 1-3. `macro_rules` (open, not controlled), loop through `macro_rules` scopes. Have higher
78 // priority than prelude macros, but create ambiguities with macros in modules.
79 // 1-3. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
80 // (open, not controlled). Have higher priority than prelude macros, but create
81 // ambiguities with `macro_rules`.
82 // 4. `macro_use` prelude (open, the open part is from macro expansions, not controlled).
83 // 4a. User-defined prelude from macro-use
84 // (open, the open part is from macro expansions, not controlled).
85 // 4b. "Standard library prelude" part implemented through `macro-use` (closed, controlled).
86 // 4c. Standard library prelude (de-facto closed, controlled).
87 // 6. Language prelude: builtin attributes (closed, controlled).
88
Chris Wailes5c0824a2023-04-24 16:30:59 -070089 let rust_2015 = ctxt.edition().is_rust_2015();
Charisee9cf67802022-06-30 20:04:09 +000090 let (ns, macro_kind, is_absolute_path) = match scope_set {
91 ScopeSet::All(ns, _) => (ns, None, false),
92 ScopeSet::AbsolutePath(ns) => (ns, None, true),
93 ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind), false),
94 ScopeSet::Late(ns, ..) => (ns, None, false),
95 };
96 let module = match scope_set {
97 // Start with the specified module.
98 ScopeSet::Late(_, module, _) => module,
99 // Jump out of trait or enum modules, they do not act as scopes.
100 _ => parent_scope.module.nearest_item_scope(),
101 };
102 let mut scope = match ns {
103 _ if is_absolute_path => Scope::CrateRoot,
104 TypeNS | ValueNS => Scope::Module(module, None),
105 MacroNS => Scope::DeriveHelpers(parent_scope.expansion),
106 };
107 let mut ctxt = ctxt.normalize_to_macros_2_0();
108 let mut use_prelude = !module.no_implicit_prelude;
109
110 loop {
111 let visit = match scope {
112 // Derive helpers are not in scope when resolving derives in the same container.
113 Scope::DeriveHelpers(expn_id) => {
114 !(expn_id == parent_scope.expansion && macro_kind == Some(MacroKind::Derive))
115 }
116 Scope::DeriveHelpersCompat => true,
117 Scope::MacroRules(macro_rules_scope) => {
118 // Use "path compression" on `macro_rules` scope chains. This is an optimization
119 // used to avoid long scope chains, see the comments on `MacroRulesScopeRef`.
120 // As another consequence of this optimization visitors never observe invocation
121 // scopes for macros that were already expanded.
122 while let MacroRulesScope::Invocation(invoc_id) = macro_rules_scope.get() {
123 if let Some(next_scope) = self.output_macro_rules_scopes.get(&invoc_id) {
124 macro_rules_scope.set(next_scope.get());
125 } else {
126 break;
127 }
128 }
129 true
130 }
131 Scope::CrateRoot => true,
132 Scope::Module(..) => true,
Charisee9cf67802022-06-30 20:04:09 +0000133 Scope::MacroUsePrelude => use_prelude || rust_2015,
134 Scope::BuiltinAttrs => true,
135 Scope::ExternPrelude => use_prelude || is_absolute_path,
136 Scope::ToolPrelude => use_prelude,
137 Scope::StdLibPrelude => use_prelude || ns == MacroNS,
138 Scope::BuiltinTypes => true,
139 };
140
141 if visit {
142 if let break_result @ Some(..) = visitor(self, scope, use_prelude, ctxt) {
143 return break_result;
144 }
145 }
146
147 scope = match scope {
148 Scope::DeriveHelpers(LocalExpnId::ROOT) => Scope::DeriveHelpersCompat,
149 Scope::DeriveHelpers(expn_id) => {
150 // Derive helpers are not visible to code generated by bang or derive macros.
151 let expn_data = expn_id.expn_data();
152 match expn_data.kind {
153 ExpnKind::Root
154 | ExpnKind::Macro(MacroKind::Bang | MacroKind::Derive, _) => {
155 Scope::DeriveHelpersCompat
156 }
157 _ => Scope::DeriveHelpers(expn_data.parent.expect_local()),
158 }
159 }
160 Scope::DeriveHelpersCompat => Scope::MacroRules(parent_scope.macro_rules),
161 Scope::MacroRules(macro_rules_scope) => match macro_rules_scope.get() {
162 MacroRulesScope::Binding(binding) => {
163 Scope::MacroRules(binding.parent_macro_rules_scope)
164 }
165 MacroRulesScope::Invocation(invoc_id) => {
166 Scope::MacroRules(self.invocation_parent_scopes[&invoc_id].macro_rules)
167 }
168 MacroRulesScope::Empty => Scope::Module(module, None),
169 },
170 Scope::CrateRoot => match ns {
171 TypeNS => {
172 ctxt.adjust(ExpnId::root());
173 Scope::ExternPrelude
174 }
175 ValueNS | MacroNS => break,
176 },
177 Scope::Module(module, prev_lint_id) => {
178 use_prelude = !module.no_implicit_prelude;
179 let derive_fallback_lint_id = match scope_set {
180 ScopeSet::Late(.., lint_id) => lint_id,
181 _ => None,
182 };
183 match self.hygienic_lexical_parent(module, &mut ctxt, derive_fallback_lint_id) {
184 Some((parent_module, lint_id)) => {
185 Scope::Module(parent_module, lint_id.or(prev_lint_id))
186 }
187 None => {
188 ctxt.adjust(ExpnId::root());
189 match ns {
190 TypeNS => Scope::ExternPrelude,
191 ValueNS => Scope::StdLibPrelude,
Chris Wailes2f380c12022-11-09 13:04:22 -0800192 MacroNS => Scope::MacroUsePrelude,
Charisee9cf67802022-06-30 20:04:09 +0000193 }
194 }
195 }
196 }
Charisee9cf67802022-06-30 20:04:09 +0000197 Scope::MacroUsePrelude => Scope::StdLibPrelude,
198 Scope::BuiltinAttrs => break, // nowhere else to search
199 Scope::ExternPrelude if is_absolute_path => break,
200 Scope::ExternPrelude => Scope::ToolPrelude,
201 Scope::ToolPrelude => Scope::StdLibPrelude,
202 Scope::StdLibPrelude => match ns {
203 TypeNS => Scope::BuiltinTypes,
204 ValueNS => break, // nowhere else to search
205 MacroNS => Scope::BuiltinAttrs,
206 },
207 Scope::BuiltinTypes => break, // nowhere else to search
208 };
209 }
210
211 None
212 }
213
214 fn hygienic_lexical_parent(
215 &mut self,
216 module: Module<'a>,
217 ctxt: &mut SyntaxContext,
218 derive_fallback_lint_id: Option<NodeId>,
219 ) -> Option<(Module<'a>, Option<NodeId>)> {
220 if !module.expansion.outer_expn_is_descendant_of(*ctxt) {
221 return Some((self.expn_def_scope(ctxt.remove_mark()), None));
222 }
223
Chariseeb1d32802022-09-22 15:38:41 +0000224 if let ModuleKind::Block = module.kind {
Charisee9cf67802022-06-30 20:04:09 +0000225 return Some((module.parent.unwrap().nearest_item_scope(), None));
226 }
227
228 // We need to support the next case under a deprecation warning
229 // ```
230 // struct MyStruct;
231 // ---- begin: this comes from a proc macro derive
232 // mod implementation_details {
233 // // Note that `MyStruct` is not in scope here.
234 // impl SomeTrait for MyStruct { ... }
235 // }
236 // ---- end
237 // ```
238 // So we have to fall back to the module's parent during lexical resolution in this case.
239 if derive_fallback_lint_id.is_some() {
240 if let Some(parent) = module.parent {
241 // Inner module is inside the macro, parent module is outside of the macro.
242 if module.expansion != parent.expansion
243 && module.expansion.is_descendant_of(parent.expansion)
244 {
245 // The macro is a proc macro derive
246 if let Some(def_id) = module.expansion.expn_data().macro_def_id {
Chris Wailes65720582022-08-11 09:53:28 -0700247 let ext = self.get_macro_by_def_id(def_id).ext;
Charisee9cf67802022-06-30 20:04:09 +0000248 if ext.builtin_name.is_none()
249 && ext.macro_kind() == MacroKind::Derive
250 && parent.expansion.outer_expn_is_descendant_of(*ctxt)
251 {
252 return Some((parent, derive_fallback_lint_id));
253 }
254 }
255 }
256 }
257 }
258
259 None
260 }
261
262 /// This resolves the identifier `ident` in the namespace `ns` in the current lexical scope.
263 /// More specifically, we proceed up the hierarchy of scopes and return the binding for
264 /// `ident` in the first scope that defines it (or None if no scopes define it).
265 ///
266 /// A block's items are above its local variables in the scope hierarchy, regardless of where
267 /// the items are defined in the block. For example,
268 /// ```rust
269 /// fn f() {
270 /// g(); // Since there are no local variables in scope yet, this resolves to the item.
271 /// let g = || {};
272 /// fn g() {}
273 /// g(); // This resolves to the local variable `g` since it shadows the item.
274 /// }
275 /// ```
276 ///
277 /// Invariant: This must only be called during main resolution, not during
278 /// import resolution.
Chris Wailes2f380c12022-11-09 13:04:22 -0800279 #[instrument(level = "debug", skip(self, ribs))]
Chris Wailes65720582022-08-11 09:53:28 -0700280 pub(crate) fn resolve_ident_in_lexical_scope(
Charisee9cf67802022-06-30 20:04:09 +0000281 &mut self,
282 mut ident: Ident,
283 ns: Namespace,
284 parent_scope: &ParentScope<'a>,
285 finalize: Option<Finalize>,
286 ribs: &[Rib<'a>],
287 ignore_binding: Option<&'a NameBinding<'a>>,
288 ) -> Option<LexicalScopeBinding<'a>> {
289 assert!(ns == TypeNS || ns == ValueNS);
290 let orig_ident = ident;
291 if ident.name == kw::Empty {
292 return Some(LexicalScopeBinding::Res(Res::Err));
293 }
294 let (general_span, normalized_span) = if ident.name == kw::SelfUpper {
295 // FIXME(jseyfried) improve `Self` hygiene
296 let empty_span = ident.span.with_ctxt(SyntaxContext::root());
297 (empty_span, empty_span)
298 } else if ns == TypeNS {
299 let normalized_span = ident.span.normalize_to_macros_2_0();
300 (normalized_span, normalized_span)
301 } else {
302 (ident.span.normalize_to_macro_rules(), ident.span.normalize_to_macros_2_0())
303 };
304 ident.span = general_span;
305 let normalized_ident = Ident { span: normalized_span, ..ident };
306
307 // Walk backwards up the ribs in scope.
308 let mut module = self.graph_root;
309 for i in (0..ribs.len()).rev() {
310 debug!("walk rib\n{:?}", ribs[i].bindings);
311 // Use the rib kind to determine whether we are resolving parameters
312 // (macro 2.0 hygiene) or local variables (`macro_rules` hygiene).
313 let rib_ident = if ribs[i].kind.contains_params() { normalized_ident } else { ident };
314 if let Some((original_rib_ident_def, res)) = ribs[i].bindings.get_key_value(&rib_ident)
315 {
316 // The ident resolves to a type parameter or local variable.
317 return Some(LexicalScopeBinding::Res(self.validate_res_from_ribs(
318 i,
319 rib_ident,
320 *res,
321 finalize.map(|finalize| finalize.path_span),
322 *original_rib_ident_def,
323 ribs,
324 )));
325 }
326
327 module = match ribs[i].kind {
Chris Wailescd1aefd2023-07-13 13:36:21 -0700328 RibKind::Module(module) => module,
329 RibKind::MacroDefinition(def) if def == self.macro_def(ident.span.ctxt()) => {
Charisee9cf67802022-06-30 20:04:09 +0000330 // If an invocation of this macro created `ident`, give up on `ident`
331 // and switch to `ident`'s source from the macro definition.
332 ident.span.remove_mark();
333 continue;
334 }
335 _ => continue,
336 };
337
338 match module.kind {
Chariseeb1d32802022-09-22 15:38:41 +0000339 ModuleKind::Block => {} // We can see through blocks
Charisee9cf67802022-06-30 20:04:09 +0000340 _ => break,
341 }
342
343 let item = self.resolve_ident_in_module_unadjusted(
344 ModuleOrUniformRoot::Module(module),
345 ident,
346 ns,
347 parent_scope,
348 finalize,
349 ignore_binding,
350 );
351 if let Ok(binding) = item {
352 // The ident resolves to an item.
353 return Some(LexicalScopeBinding::Item(binding));
354 }
355 }
356 self.early_resolve_ident_in_lexical_scope(
357 orig_ident,
358 ScopeSet::Late(ns, module, finalize.map(|finalize| finalize.node_id)),
359 parent_scope,
360 finalize,
361 finalize.is_some(),
362 ignore_binding,
363 )
364 .ok()
365 .map(LexicalScopeBinding::Item)
366 }
367
368 /// Resolve an identifier in lexical scope.
369 /// This is a variation of `fn resolve_ident_in_lexical_scope` that can be run during
370 /// expansion and import resolution (perhaps they can be merged in the future).
371 /// The function is used for resolving initial segments of macro paths (e.g., `foo` in
Chris Wailes5c0824a2023-04-24 16:30:59 -0700372 /// `foo::bar!();` or `foo!();`) and also for import paths on 2018 edition.
Chris Wailes2f380c12022-11-09 13:04:22 -0800373 #[instrument(level = "debug", skip(self, scope_set))]
Chris Wailes65720582022-08-11 09:53:28 -0700374 pub(crate) fn early_resolve_ident_in_lexical_scope(
Charisee9cf67802022-06-30 20:04:09 +0000375 &mut self,
376 orig_ident: Ident,
377 scope_set: ScopeSet<'a>,
378 parent_scope: &ParentScope<'a>,
379 finalize: Option<Finalize>,
380 force: bool,
381 ignore_binding: Option<&'a NameBinding<'a>>,
382 ) -> Result<&'a NameBinding<'a>, Determinacy> {
383 bitflags::bitflags! {
384 struct Flags: u8 {
385 const MACRO_RULES = 1 << 0;
386 const MODULE = 1 << 1;
387 const MISC_SUGGEST_CRATE = 1 << 2;
388 const MISC_SUGGEST_SELF = 1 << 3;
389 const MISC_FROM_PRELUDE = 1 << 4;
390 }
391 }
392
Charisee635618d2023-06-01 20:46:00 +0000393 assert!(force || finalize.is_none()); // `finalize` implies `force`
Charisee9cf67802022-06-30 20:04:09 +0000394
395 // Make sure `self`, `super` etc produce an error when passed to here.
396 if orig_ident.is_path_segment_keyword() {
397 return Err(Determinacy::Determined);
398 }
399
400 let (ns, macro_kind, is_import) = match scope_set {
401 ScopeSet::All(ns, is_import) => (ns, None, is_import),
402 ScopeSet::AbsolutePath(ns) => (ns, None, false),
403 ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind), false),
404 ScopeSet::Late(ns, ..) => (ns, None, false),
405 };
406
407 // This is *the* result, resolution from the scope closest to the resolved identifier.
408 // However, sometimes this result is "weak" because it comes from a glob import or
409 // a macro expansion, and in this case it cannot shadow names from outer scopes, e.g.
410 // mod m { ... } // solution in outer scope
411 // {
412 // use prefix::*; // imports another `m` - innermost solution
413 // // weak, cannot shadow the outer `m`, need to report ambiguity error
414 // m::mac!();
415 // }
416 // So we have to save the innermost solution and continue searching in outer scopes
417 // to detect potential ambiguities.
418 let mut innermost_result: Option<(&NameBinding<'_>, Flags)> = None;
419 let mut determinacy = Determinacy::Determined;
420
421 // Go through all the scopes and try to resolve the name.
422 let break_result = self.visit_scopes(
423 scope_set,
424 parent_scope,
425 orig_ident.span.ctxt(),
426 |this, scope, use_prelude, ctxt| {
427 let ident = Ident::new(orig_ident.name, orig_ident.span.with_ctxt(ctxt));
428 let ok = |res, span, arenas| {
429 Ok((
Chris Wailes2f380c12022-11-09 13:04:22 -0800430 (res, Visibility::Public, span, LocalExpnId::ROOT).to_name_binding(arenas),
Charisee9cf67802022-06-30 20:04:09 +0000431 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),
Chris Wailes2f380c12022-11-09 13:04:22 -0800443 Visibility::Public,
Charisee9cf67802022-06-30 20:04:09 +0000444 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,
Chris Wailescd1aefd2023-07-13 13:36:21 -0700531 format!(
Charisee9cf67802022-06-30 20:04:09 +0000532 "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 }
Charisee9cf67802022-06-30 20:04:09 +0000559 Scope::MacroUsePrelude => {
560 match this.macro_use_prelude.get(&ident.name).cloned() {
561 Some(binding) => Ok((binding, Flags::MISC_FROM_PRELUDE)),
562 None => Err(Determinacy::determined(
563 this.graph_root.unexpanded_invocations.borrow().is_empty(),
564 )),
565 }
566 }
567 Scope::BuiltinAttrs => {
568 if is_builtin_attr_name(ident.name) {
569 ok(
570 Res::NonMacroAttr(NonMacroAttrKind::Builtin(ident.name)),
571 DUMMY_SP,
572 this.arenas,
573 )
574 } else {
575 Err(Determinacy::Determined)
576 }
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_tools.get(&ident).cloned() {
587 Some(ident) => ok(Res::ToolMod, ident.span, this.arenas),
588 None => Err(Determinacy::Determined),
589 },
590 Scope::StdLibPrelude => {
591 let mut result = Err(Determinacy::Determined);
592 if let Some(prelude) = this.prelude {
593 if let Ok(binding) = this.resolve_ident_in_module_unadjusted(
594 ModuleOrUniformRoot::Module(prelude),
595 ident,
596 ns,
597 parent_scope,
598 None,
599 ignore_binding,
600 ) {
601 if use_prelude || this.is_builtin_macro(binding.res()) {
602 result = Ok((binding, Flags::MISC_FROM_PRELUDE));
603 }
604 }
605 }
606 result
607 }
608 Scope::BuiltinTypes => match PrimTy::from_name(ident.name) {
609 Some(prim_ty) => ok(Res::PrimTy(prim_ty), DUMMY_SP, this.arenas),
610 None => Err(Determinacy::Determined),
611 },
612 };
613
614 match result {
615 Ok((binding, flags))
616 if sub_namespace_match(binding.macro_kind(), macro_kind) =>
617 {
618 if finalize.is_none() || matches!(scope_set, ScopeSet::Late(..)) {
619 return Some(Ok(binding));
620 }
621
622 if let Some((innermost_binding, innermost_flags)) = innermost_result {
623 // Found another solution, if the first one was "weak", report an error.
624 let (res, innermost_res) = (binding.res(), innermost_binding.res());
625 if res != innermost_res {
626 let is_builtin = |res| {
627 matches!(res, Res::NonMacroAttr(NonMacroAttrKind::Builtin(..)))
628 };
629 let derive_helper =
630 Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper);
631 let derive_helper_compat =
632 Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat);
633
634 let ambiguity_error_kind = if is_import {
635 Some(AmbiguityKind::Import)
636 } else if is_builtin(innermost_res) || is_builtin(res) {
637 Some(AmbiguityKind::BuiltinAttr)
638 } else if innermost_res == derive_helper_compat
639 || res == derive_helper_compat && innermost_res != derive_helper
640 {
641 Some(AmbiguityKind::DeriveHelper)
642 } else if innermost_flags.contains(Flags::MACRO_RULES)
643 && flags.contains(Flags::MODULE)
644 && !this.disambiguate_macro_rules_vs_modularized(
645 innermost_binding,
646 binding,
647 )
648 || flags.contains(Flags::MACRO_RULES)
649 && innermost_flags.contains(Flags::MODULE)
650 && !this.disambiguate_macro_rules_vs_modularized(
651 binding,
652 innermost_binding,
653 )
654 {
655 Some(AmbiguityKind::MacroRulesVsModularized)
656 } else if innermost_binding.is_glob_import() {
657 Some(AmbiguityKind::GlobVsOuter)
658 } else if innermost_binding
659 .may_appear_after(parent_scope.expansion, binding)
660 {
661 Some(AmbiguityKind::MoreExpandedVsOuter)
662 } else {
663 None
664 };
665 if let Some(kind) = ambiguity_error_kind {
666 let misc = |f: Flags| {
667 if f.contains(Flags::MISC_SUGGEST_CRATE) {
668 AmbiguityErrorMisc::SuggestCrate
669 } else if f.contains(Flags::MISC_SUGGEST_SELF) {
670 AmbiguityErrorMisc::SuggestSelf
671 } else if f.contains(Flags::MISC_FROM_PRELUDE) {
672 AmbiguityErrorMisc::FromPrelude
673 } else {
674 AmbiguityErrorMisc::None
675 }
676 };
677 this.ambiguity_errors.push(AmbiguityError {
678 kind,
679 ident: orig_ident,
680 b1: innermost_binding,
681 b2: binding,
682 misc1: misc(innermost_flags),
683 misc2: misc(flags),
684 });
685 return Some(Ok(innermost_binding));
686 }
687 }
688 } else {
689 // Found the first solution.
690 innermost_result = Some((binding, flags));
691 }
692 }
693 Ok(..) | Err(Determinacy::Determined) => {}
694 Err(Determinacy::Undetermined) => determinacy = Determinacy::Undetermined,
695 }
696
697 None
698 },
699 );
700
701 if let Some(break_result) = break_result {
702 return break_result;
703 }
704
705 // The first found solution was the only one, return it.
706 if let Some((binding, _)) = innermost_result {
707 return Ok(binding);
708 }
709
710 Err(Determinacy::determined(determinacy == Determinacy::Determined || force))
711 }
712
Chris Wailes2f380c12022-11-09 13:04:22 -0800713 #[instrument(level = "debug", skip(self))]
Chris Wailes65720582022-08-11 09:53:28 -0700714 pub(crate) fn maybe_resolve_ident_in_module(
Charisee9cf67802022-06-30 20:04:09 +0000715 &mut self,
716 module: ModuleOrUniformRoot<'a>,
717 ident: Ident,
718 ns: Namespace,
719 parent_scope: &ParentScope<'a>,
720 ) -> Result<&'a NameBinding<'a>, Determinacy> {
721 self.resolve_ident_in_module_ext(module, ident, ns, parent_scope, None, None)
722 .map_err(|(determinacy, _)| determinacy)
723 }
724
Chris Wailes2f380c12022-11-09 13:04:22 -0800725 #[instrument(level = "debug", skip(self))]
Chris Wailes65720582022-08-11 09:53:28 -0700726 pub(crate) fn resolve_ident_in_module(
Charisee9cf67802022-06-30 20:04:09 +0000727 &mut self,
728 module: ModuleOrUniformRoot<'a>,
729 ident: Ident,
730 ns: Namespace,
731 parent_scope: &ParentScope<'a>,
732 finalize: Option<Finalize>,
733 ignore_binding: Option<&'a NameBinding<'a>>,
734 ) -> Result<&'a NameBinding<'a>, Determinacy> {
735 self.resolve_ident_in_module_ext(module, ident, ns, parent_scope, finalize, ignore_binding)
736 .map_err(|(determinacy, _)| determinacy)
737 }
738
Chris Wailes2f380c12022-11-09 13:04:22 -0800739 #[instrument(level = "debug", skip(self))]
Charisee9cf67802022-06-30 20:04:09 +0000740 fn resolve_ident_in_module_ext(
741 &mut self,
742 module: ModuleOrUniformRoot<'a>,
743 mut ident: Ident,
744 ns: Namespace,
745 parent_scope: &ParentScope<'a>,
746 finalize: Option<Finalize>,
747 ignore_binding: Option<&'a NameBinding<'a>>,
748 ) -> Result<&'a NameBinding<'a>, (Determinacy, Weak)> {
749 let tmp_parent_scope;
750 let mut adjusted_parent_scope = parent_scope;
751 match module {
752 ModuleOrUniformRoot::Module(m) => {
753 if let Some(def) = ident.span.normalize_to_macros_2_0_and_adjust(m.expansion) {
754 tmp_parent_scope =
755 ParentScope { module: self.expn_def_scope(def), ..*parent_scope };
756 adjusted_parent_scope = &tmp_parent_scope;
757 }
758 }
759 ModuleOrUniformRoot::ExternPrelude => {
760 ident.span.normalize_to_macros_2_0_and_adjust(ExpnId::root());
761 }
762 ModuleOrUniformRoot::CrateRootAndExternPrelude | ModuleOrUniformRoot::CurrentScope => {
763 // No adjustments
764 }
765 }
766 self.resolve_ident_in_module_unadjusted_ext(
767 module,
768 ident,
769 ns,
770 adjusted_parent_scope,
771 false,
772 finalize,
773 ignore_binding,
774 )
775 }
776
Chris Wailes2f380c12022-11-09 13:04:22 -0800777 #[instrument(level = "debug", skip(self))]
Charisee9cf67802022-06-30 20:04:09 +0000778 fn resolve_ident_in_module_unadjusted(
779 &mut self,
780 module: ModuleOrUniformRoot<'a>,
781 ident: Ident,
782 ns: Namespace,
783 parent_scope: &ParentScope<'a>,
784 finalize: Option<Finalize>,
785 ignore_binding: Option<&'a NameBinding<'a>>,
786 ) -> Result<&'a NameBinding<'a>, Determinacy> {
787 self.resolve_ident_in_module_unadjusted_ext(
788 module,
789 ident,
790 ns,
791 parent_scope,
792 false,
793 finalize,
794 ignore_binding,
795 )
796 .map_err(|(determinacy, _)| determinacy)
797 }
798
799 /// Attempts to resolve `ident` in namespaces `ns` of `module`.
800 /// Invariant: if `finalize` is `Some`, expansion and import resolution must be complete.
Chris Wailes2f380c12022-11-09 13:04:22 -0800801 #[instrument(level = "debug", skip(self))]
Charisee9cf67802022-06-30 20:04:09 +0000802 fn resolve_ident_in_module_unadjusted_ext(
803 &mut self,
804 module: ModuleOrUniformRoot<'a>,
805 ident: Ident,
806 ns: Namespace,
807 parent_scope: &ParentScope<'a>,
808 restricted_shadowing: bool,
809 finalize: Option<Finalize>,
810 // This binding should be ignored during in-module resolution, so that we don't get
811 // "self-confirming" import resolutions during import validation and checking.
812 ignore_binding: Option<&'a NameBinding<'a>>,
813 ) -> Result<&'a NameBinding<'a>, (Determinacy, Weak)> {
814 let module = match module {
815 ModuleOrUniformRoot::Module(module) => module,
816 ModuleOrUniformRoot::CrateRootAndExternPrelude => {
817 assert!(!restricted_shadowing);
818 let binding = self.early_resolve_ident_in_lexical_scope(
819 ident,
820 ScopeSet::AbsolutePath(ns),
821 parent_scope,
822 finalize,
823 finalize.is_some(),
824 ignore_binding,
825 );
826 return binding.map_err(|determinacy| (determinacy, Weak::No));
827 }
828 ModuleOrUniformRoot::ExternPrelude => {
829 assert!(!restricted_shadowing);
830 return if ns != TypeNS {
831 Err((Determined, Weak::No))
832 } else if let Some(binding) = self.extern_prelude_get(ident, finalize.is_some()) {
833 Ok(binding)
834 } else if !self.graph_root.unexpanded_invocations.borrow().is_empty() {
835 // Macro-expanded `extern crate` items can add names to extern prelude.
836 Err((Undetermined, Weak::No))
837 } else {
838 Err((Determined, Weak::No))
839 };
840 }
841 ModuleOrUniformRoot::CurrentScope => {
842 assert!(!restricted_shadowing);
843 if ns == TypeNS {
844 if ident.name == kw::Crate || ident.name == kw::DollarCrate {
845 let module = self.resolve_crate_root(ident);
Chris Wailes2f380c12022-11-09 13:04:22 -0800846 let binding = (module, Visibility::Public, module.span, LocalExpnId::ROOT)
847 .to_name_binding(self.arenas);
Charisee9cf67802022-06-30 20:04:09 +0000848 return Ok(binding);
849 } else if ident.name == kw::Super || ident.name == kw::SelfLower {
850 // FIXME: Implement these with renaming requirements so that e.g.
851 // `use super;` doesn't work, but `use super as name;` does.
852 // Fall through here to get an error from `early_resolve_...`.
853 }
854 }
855
856 let scopes = ScopeSet::All(ns, true);
857 let binding = self.early_resolve_ident_in_lexical_scope(
858 ident,
859 scopes,
860 parent_scope,
861 finalize,
862 finalize.is_some(),
863 ignore_binding,
864 );
865 return binding.map_err(|determinacy| (determinacy, Weak::No));
866 }
867 };
868
Chris Wailescd1aefd2023-07-13 13:36:21 -0700869 let key = BindingKey::new(ident, ns);
Charisee9cf67802022-06-30 20:04:09 +0000870 let resolution =
871 self.resolution(module, key).try_borrow_mut().map_err(|_| (Determined, Weak::No))?; // This happens when there is a cycle of imports.
872
Charisee635618d2023-06-01 20:46:00 +0000873 // If the primary binding is unusable, search further and return the shadowed glob
874 // binding if it exists. What we really want here is having two separate scopes in
875 // a module - one for non-globs and one for globs, but until that's done use this
876 // hack to avoid inconsistent resolution ICEs during import validation.
877 let binding =
878 [resolution.binding, resolution.shadowed_glob].into_iter().find_map(|binding| {
879 match (binding, ignore_binding) {
Charisee9cf67802022-06-30 20:04:09 +0000880 (Some(binding), Some(ignored)) if ptr::eq(binding, ignored) => None,
881 _ => binding,
Charisee635618d2023-06-01 20:46:00 +0000882 }
883 });
884
885 if let Some(Finalize { path_span, report_private, .. }) = finalize {
Charisee9cf67802022-06-30 20:04:09 +0000886 let Some(binding) = binding else {
887 return Err((Determined, Weak::No));
888 };
889
890 if !self.is_accessible_from(binding.vis, parent_scope.module) {
891 if report_private {
892 self.privacy_errors.push(PrivacyError {
893 ident,
894 binding,
895 dedup_span: path_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 && restricted_shadowing
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 misc1: AmbiguityErrorMisc::None,
914 misc2: AmbiguityErrorMisc::None,
915 });
916 }
917
918 if !restricted_shadowing && binding.expansion != LocalExpnId::ROOT {
Chris Wailes977026a2023-02-13 09:13:10 -0800919 if let NameBindingKind::Import {
920 import: Import { kind: ImportKind::MacroExport, .. },
921 ..
922 } = binding.kind
923 {
Charisee9cf67802022-06-30 20:04:09 +0000924 self.macro_expanded_macro_export_errors.insert((path_span, binding.span));
925 }
926 }
927
928 self.record_use(ident, binding, restricted_shadowing);
929 return Ok(binding);
930 }
931
932 let check_usable = |this: &mut Self, binding: &'a NameBinding<'a>| {
Charisee9cf67802022-06-30 20:04:09 +0000933 let usable = this.is_accessible_from(binding.vis, parent_scope.module);
934 if usable { Ok(binding) } else { Err((Determined, Weak::No)) }
935 };
936
937 // Items and single imports are not shadowable, if we have one, then it's determined.
Charisee635618d2023-06-01 20:46:00 +0000938 if let Some(binding) = binding {
Charisee9cf67802022-06-30 20:04:09 +0000939 if !binding.is_glob_import() {
940 return check_usable(self, binding);
941 }
942 }
943
944 // --- From now on we either have a glob resolution or no resolution. ---
945
946 // Check if one of single imports can still define the name,
947 // if it can then our result is not determined and can be invalidated.
948 for single_import in &resolution.single_imports {
Chris Wailes2f380c12022-11-09 13:04:22 -0800949 let Some(import_vis) = single_import.vis.get() else {
950 continue;
951 };
952 if !self.is_accessible_from(import_vis, parent_scope.module) {
Charisee9cf67802022-06-30 20:04:09 +0000953 continue;
954 }
Charisee635618d2023-06-01 20:46:00 +0000955 if let Some(ignored) = ignore_binding &&
956 let NameBindingKind::Import { import, .. } = ignored.kind &&
957 ptr::eq(import, &**single_import) {
958 // Ignore not just the binding itself, but if it has a shadowed_glob,
959 // ignore that, too, because this loop is supposed to only process
960 // named imports.
961 continue;
962 }
Charisee9cf67802022-06-30 20:04:09 +0000963 let Some(module) = single_import.imported_module.get() else {
964 return Err((Undetermined, Weak::No));
965 };
966 let ImportKind::Single { source: ident, .. } = single_import.kind else {
967 unreachable!();
968 };
969 match self.resolve_ident_in_module(
970 module,
971 ident,
972 ns,
973 &single_import.parent_scope,
974 None,
975 ignore_binding,
976 ) {
977 Err(Determined) => continue,
978 Ok(binding)
979 if !self.is_accessible_from(binding.vis, single_import.parent_scope.module) =>
980 {
981 continue;
982 }
983 Ok(_) | Err(Undetermined) => return Err((Undetermined, Weak::No)),
984 }
985 }
986
987 // So we have a resolution that's from a glob import. This resolution is determined
988 // if it cannot be shadowed by some new item/import expanded from a macro.
989 // This happens either if there are no unexpanded macros, or expanded names cannot
990 // shadow globs (that happens in macro namespace or with restricted shadowing).
991 //
992 // Additionally, any macro in any module can plant names in the root module if it creates
993 // `macro_export` macros, so the root module effectively has unresolved invocations if any
994 // module has unresolved invocations.
995 // However, it causes resolution/expansion to stuck too often (#53144), so, to make
996 // progress, we have to ignore those potential unresolved invocations from other modules
997 // and prohibit access to macro-expanded `macro_export` macros instead (unless restricted
998 // shadowing is enabled, see `macro_expanded_macro_export_errors`).
999 let unexpanded_macros = !module.unexpanded_invocations.borrow().is_empty();
Charisee635618d2023-06-01 20:46:00 +00001000 if let Some(binding) = binding {
Charisee9cf67802022-06-30 20:04:09 +00001001 if !unexpanded_macros || ns == MacroNS || restricted_shadowing {
1002 return check_usable(self, binding);
1003 } else {
1004 return Err((Undetermined, Weak::No));
1005 }
1006 }
1007
1008 // --- From now on we have no resolution. ---
1009
1010 // Now we are in situation when new item/import can appear only from a glob or a macro
1011 // expansion. With restricted shadowing names from globs and macro expansions cannot
1012 // shadow names from outer scopes, so we can freely fallback from module search to search
1013 // in outer scopes. For `early_resolve_ident_in_lexical_scope` to continue search in outer
1014 // scopes we return `Undetermined` with `Weak::Yes`.
1015
1016 // Check if one of unexpanded macros can still define the name,
1017 // if it can then our "no resolution" result is not determined and can be invalidated.
1018 if unexpanded_macros {
1019 return Err((Undetermined, Weak::Yes));
1020 }
1021
1022 // Check if one of glob imports can still define the name,
1023 // if it can then our "no resolution" result is not determined and can be invalidated.
1024 for glob_import in module.globs.borrow().iter() {
Chris Wailes2f380c12022-11-09 13:04:22 -08001025 let Some(import_vis) = glob_import.vis.get() else {
1026 continue;
1027 };
1028 if !self.is_accessible_from(import_vis, parent_scope.module) {
Charisee9cf67802022-06-30 20:04:09 +00001029 continue;
1030 }
1031 let module = match glob_import.imported_module.get() {
1032 Some(ModuleOrUniformRoot::Module(module)) => module,
1033 Some(_) => continue,
1034 None => return Err((Undetermined, Weak::Yes)),
1035 };
1036 let tmp_parent_scope;
1037 let (mut adjusted_parent_scope, mut ident) =
1038 (parent_scope, ident.normalize_to_macros_2_0());
1039 match ident.span.glob_adjust(module.expansion, glob_import.span) {
1040 Some(Some(def)) => {
1041 tmp_parent_scope =
1042 ParentScope { module: self.expn_def_scope(def), ..*parent_scope };
1043 adjusted_parent_scope = &tmp_parent_scope;
1044 }
1045 Some(None) => {}
1046 None => continue,
1047 };
1048 let result = self.resolve_ident_in_module_unadjusted(
1049 ModuleOrUniformRoot::Module(module),
1050 ident,
1051 ns,
1052 adjusted_parent_scope,
1053 None,
1054 ignore_binding,
1055 );
1056
1057 match result {
1058 Err(Determined) => continue,
1059 Ok(binding)
1060 if !self.is_accessible_from(binding.vis, glob_import.parent_scope.module) =>
1061 {
1062 continue;
1063 }
1064 Ok(_) | Err(Undetermined) => return Err((Undetermined, Weak::Yes)),
1065 }
1066 }
1067
1068 // No resolution and no one else can define the name - determinate error.
1069 Err((Determined, Weak::No))
1070 }
1071
1072 /// Validate a local resolution (from ribs).
Chris Wailes2f380c12022-11-09 13:04:22 -08001073 #[instrument(level = "debug", skip(self, all_ribs))]
Charisee9cf67802022-06-30 20:04:09 +00001074 fn validate_res_from_ribs(
1075 &mut self,
1076 rib_index: usize,
1077 rib_ident: Ident,
1078 mut res: Res,
1079 finalize: Option<Span>,
1080 original_rib_ident_def: Ident,
1081 all_ribs: &[Rib<'a>],
1082 ) -> Res {
1083 const CG_BUG_STR: &str = "min_const_generics resolve check didn't stop compilation";
1084 debug!("validate_res_from_ribs({:?})", res);
1085 let ribs = &all_ribs[rib_index + 1..];
1086
1087 // An invalid forward use of a generic parameter from a previous default.
Chris Wailescd1aefd2023-07-13 13:36:21 -07001088 if let RibKind::ForwardGenericParamBan = all_ribs[rib_index].kind {
Charisee9cf67802022-06-30 20:04:09 +00001089 if let Some(span) = finalize {
1090 let res_error = if rib_ident.name == kw::SelfUpper {
1091 ResolutionError::SelfInGenericParamDefault
1092 } else {
1093 ResolutionError::ForwardDeclaredGenericParam
1094 };
1095 self.report_error(span, res_error);
1096 }
1097 assert_eq!(res, Res::Err);
1098 return Res::Err;
1099 }
1100
1101 match res {
1102 Res::Local(_) => {
1103 use ResolutionError::*;
1104 let mut res_err = None;
1105
1106 for rib in ribs {
1107 match rib.kind {
Chris Wailescd1aefd2023-07-13 13:36:21 -07001108 RibKind::Normal
1109 | RibKind::ClosureOrAsync
1110 | RibKind::Module(..)
1111 | RibKind::MacroDefinition(..)
1112 | RibKind::ForwardGenericParamBan => {
Charisee9cf67802022-06-30 20:04:09 +00001113 // Nothing to do. Continue.
1114 }
Chris Wailescd1aefd2023-07-13 13:36:21 -07001115 RibKind::Item(_) | RibKind::AssocItem => {
Charisee9cf67802022-06-30 20:04:09 +00001116 // This was an attempt to access an upvar inside a
1117 // named function item. This is not allowed, so we
1118 // report an error.
1119 if let Some(span) = finalize {
1120 // We don't immediately trigger a resolve error, because
1121 // we want certain other resolution errors (namely those
1122 // emitted for `ConstantItemRibKind` below) to take
1123 // precedence.
1124 res_err = Some((span, CannotCaptureDynamicEnvironmentInFnItem));
1125 }
1126 }
Chris Wailescd1aefd2023-07-13 13:36:21 -07001127 RibKind::ConstantItem(_, item) => {
Charisee9cf67802022-06-30 20:04:09 +00001128 // Still doesn't deal with upvars
1129 if let Some(span) = finalize {
Chris Wailescd1aefd2023-07-13 13:36:21 -07001130 let (span, resolution_error) = match item {
1131 None if rib_ident.as_str() == "self" => (span, LowercaseSelf),
1132 None => (
1133 rib_ident.span,
1134 AttemptToUseNonConstantValueInConstant(
1135 original_rib_ident_def,
1136 "const",
1137 "let",
1138 ),
1139 ),
1140 Some((ident, kind)) => (
1141 span,
1142 AttemptToUseNonConstantValueInConstant(
1143 ident,
1144 "let",
1145 kind.as_str(),
1146 ),
1147 ),
1148 };
Charisee9cf67802022-06-30 20:04:09 +00001149 self.report_error(span, resolution_error);
1150 }
1151 return Res::Err;
1152 }
Chris Wailescd1aefd2023-07-13 13:36:21 -07001153 RibKind::ConstParamTy => {
Charisee9cf67802022-06-30 20:04:09 +00001154 if let Some(span) = finalize {
Chris Wailescd1aefd2023-07-13 13:36:21 -07001155 self.report_error(
1156 span,
1157 ParamInTyOfConstParam {
1158 name: rib_ident.name,
1159 param_kind: None,
1160 },
1161 );
Charisee9cf67802022-06-30 20:04:09 +00001162 }
1163 return Res::Err;
1164 }
Chris Wailescd1aefd2023-07-13 13:36:21 -07001165 RibKind::InlineAsmSym => {
Charisee9cf67802022-06-30 20:04:09 +00001166 if let Some(span) = finalize {
1167 self.report_error(span, InvalidAsmSym);
1168 }
1169 return Res::Err;
1170 }
1171 }
1172 }
1173 if let Some((span, res_err)) = res_err {
1174 self.report_error(span, res_err);
1175 return Res::Err;
1176 }
1177 }
Chariseef7ad1c42023-01-30 22:46:42 +00001178 Res::Def(DefKind::TyParam, _) | Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } => {
Charisee9cf67802022-06-30 20:04:09 +00001179 for rib in ribs {
1180 let has_generic_params: HasGenericParams = match rib.kind {
Chris Wailescd1aefd2023-07-13 13:36:21 -07001181 RibKind::Normal
1182 | RibKind::ClosureOrAsync
1183 | RibKind::Module(..)
1184 | RibKind::MacroDefinition(..)
1185 | RibKind::InlineAsmSym
1186 | RibKind::AssocItem
1187 | RibKind::ForwardGenericParamBan => {
Charisee9cf67802022-06-30 20:04:09 +00001188 // Nothing to do. Continue.
1189 continue;
1190 }
1191
Chris Wailescd1aefd2023-07-13 13:36:21 -07001192 RibKind::ConstantItem(trivial, _) => {
1193 if let ConstantHasGenerics::No(cause) = trivial {
Chariseef7ad1c42023-01-30 22:46:42 +00001194 // HACK(min_const_generics): If we encounter `Self` in an anonymous
1195 // constant we can't easily tell if it's generic at this stage, so
1196 // we instead remember this and then enforce the self type to be
1197 // concrete later on.
1198 if let Res::SelfTyAlias {
1199 alias_to: def,
1200 forbid_generic: _,
1201 is_trait_impl,
1202 } = res
1203 {
1204 res = Res::SelfTyAlias {
1205 alias_to: def,
1206 forbid_generic: true,
1207 is_trait_impl,
1208 }
Charisee9cf67802022-06-30 20:04:09 +00001209 } else {
1210 if let Some(span) = finalize {
Chris Wailescd1aefd2023-07-13 13:36:21 -07001211 let error = match cause {
1212 NoConstantGenericsReason::IsEnumDiscriminant => {
1213 ResolutionError::ParamInEnumDiscriminant {
1214 name: rib_ident.name,
1215 param_kind: ParamKindInEnumDiscriminant::Type,
1216 }
1217 }
1218 NoConstantGenericsReason::NonTrivialConstArg => {
1219 ResolutionError::ParamInNonTrivialAnonConst {
1220 name: rib_ident.name,
1221 param_kind:
1222 ParamKindInNonTrivialAnonConst::Type,
1223 }
1224 }
1225 };
1226 self.report_error(span, error);
Chris Wailes5c0824a2023-04-24 16:30:59 -07001227 self.tcx.sess.delay_span_bug(span, CG_BUG_STR);
Charisee9cf67802022-06-30 20:04:09 +00001228 }
1229
1230 return Res::Err;
1231 }
1232 }
1233
1234 continue;
1235 }
1236
1237 // This was an attempt to use a type parameter outside its scope.
Chris Wailescd1aefd2023-07-13 13:36:21 -07001238 RibKind::Item(has_generic_params) => has_generic_params,
1239 RibKind::ConstParamTy => {
Charisee9cf67802022-06-30 20:04:09 +00001240 if let Some(span) = finalize {
1241 self.report_error(
1242 span,
Chris Wailescd1aefd2023-07-13 13:36:21 -07001243 ResolutionError::ParamInTyOfConstParam {
1244 name: rib_ident.name,
1245 param_kind: Some(errors::ParamKindInTyOfConstParam::Type),
1246 },
Charisee9cf67802022-06-30 20:04:09 +00001247 );
1248 }
1249 return Res::Err;
1250 }
Charisee9cf67802022-06-30 20:04:09 +00001251 };
1252
1253 if let Some(span) = finalize {
1254 self.report_error(
1255 span,
1256 ResolutionError::GenericParamsFromOuterFunction(
1257 res,
1258 has_generic_params,
1259 ),
1260 );
1261 }
1262 return Res::Err;
1263 }
1264 }
1265 Res::Def(DefKind::ConstParam, _) => {
Charisee9cf67802022-06-30 20:04:09 +00001266 for rib in ribs {
1267 let has_generic_params = match rib.kind {
Chris Wailescd1aefd2023-07-13 13:36:21 -07001268 RibKind::Normal
1269 | RibKind::ClosureOrAsync
1270 | RibKind::Module(..)
1271 | RibKind::MacroDefinition(..)
1272 | RibKind::InlineAsmSym
1273 | RibKind::AssocItem
1274 | RibKind::ForwardGenericParamBan => continue,
Charisee9cf67802022-06-30 20:04:09 +00001275
Chris Wailescd1aefd2023-07-13 13:36:21 -07001276 RibKind::ConstantItem(trivial, _) => {
1277 if let ConstantHasGenerics::No(cause) = trivial {
Charisee9cf67802022-06-30 20:04:09 +00001278 if let Some(span) = finalize {
Chris Wailescd1aefd2023-07-13 13:36:21 -07001279 let error = match cause {
1280 NoConstantGenericsReason::IsEnumDiscriminant => {
1281 ResolutionError::ParamInEnumDiscriminant {
1282 name: rib_ident.name,
1283 param_kind: ParamKindInEnumDiscriminant::Const,
1284 }
1285 }
1286 NoConstantGenericsReason::NonTrivialConstArg => {
1287 ResolutionError::ParamInNonTrivialAnonConst {
1288 name: rib_ident.name,
1289 param_kind: ParamKindInNonTrivialAnonConst::Const {
1290 name: rib_ident.name,
1291 },
1292 }
1293 }
1294 };
1295 self.report_error(span, error);
Charisee9cf67802022-06-30 20:04:09 +00001296 }
1297
1298 return Res::Err;
1299 }
1300
1301 continue;
1302 }
1303
Chris Wailescd1aefd2023-07-13 13:36:21 -07001304 RibKind::Item(has_generic_params) => has_generic_params,
1305 RibKind::ConstParamTy => {
Charisee9cf67802022-06-30 20:04:09 +00001306 if let Some(span) = finalize {
1307 self.report_error(
1308 span,
Chris Wailescd1aefd2023-07-13 13:36:21 -07001309 ResolutionError::ParamInTyOfConstParam {
1310 name: rib_ident.name,
1311 param_kind: Some(errors::ParamKindInTyOfConstParam::Const),
1312 },
Charisee9cf67802022-06-30 20:04:09 +00001313 );
1314 }
1315 return Res::Err;
1316 }
Charisee9cf67802022-06-30 20:04:09 +00001317 };
1318
1319 // This was an attempt to use a const parameter outside its scope.
1320 if let Some(span) = finalize {
1321 self.report_error(
1322 span,
1323 ResolutionError::GenericParamsFromOuterFunction(
1324 res,
1325 has_generic_params,
1326 ),
1327 );
1328 }
1329 return Res::Err;
1330 }
1331 }
1332 _ => {}
1333 }
1334 res
1335 }
1336
Chris Wailes2f380c12022-11-09 13:04:22 -08001337 #[instrument(level = "debug", skip(self))]
Chris Wailes65720582022-08-11 09:53:28 -07001338 pub(crate) fn maybe_resolve_path(
Charisee9cf67802022-06-30 20:04:09 +00001339 &mut self,
1340 path: &[Segment],
1341 opt_ns: Option<Namespace>, // `None` indicates a module path in import
1342 parent_scope: &ParentScope<'a>,
1343 ) -> PathResult<'a> {
1344 self.resolve_path_with_ribs(path, opt_ns, parent_scope, None, None, None)
1345 }
1346
Chris Wailes2f380c12022-11-09 13:04:22 -08001347 #[instrument(level = "debug", skip(self))]
Chris Wailes65720582022-08-11 09:53:28 -07001348 pub(crate) fn resolve_path(
Charisee9cf67802022-06-30 20:04:09 +00001349 &mut self,
1350 path: &[Segment],
1351 opt_ns: Option<Namespace>, // `None` indicates a module path in import
1352 parent_scope: &ParentScope<'a>,
1353 finalize: Option<Finalize>,
1354 ignore_binding: Option<&'a NameBinding<'a>>,
1355 ) -> PathResult<'a> {
1356 self.resolve_path_with_ribs(path, opt_ns, parent_scope, finalize, None, ignore_binding)
1357 }
1358
Chris Wailes65720582022-08-11 09:53:28 -07001359 pub(crate) fn resolve_path_with_ribs(
Charisee9cf67802022-06-30 20:04:09 +00001360 &mut self,
1361 path: &[Segment],
1362 opt_ns: Option<Namespace>, // `None` indicates a module path in import
1363 parent_scope: &ParentScope<'a>,
1364 finalize: Option<Finalize>,
1365 ribs: Option<&PerNS<Vec<Rib<'a>>>>,
1366 ignore_binding: Option<&'a NameBinding<'a>>,
1367 ) -> PathResult<'a> {
Chris Wailescd1aefd2023-07-13 13:36:21 -07001368 debug!(
1369 "resolve_path(path={:?}, opt_ns={:?}, finalize={:?}) path_len: {}",
1370 path,
1371 opt_ns,
1372 finalize,
1373 path.len()
1374 );
Charisee9cf67802022-06-30 20:04:09 +00001375
1376 let mut module = None;
1377 let mut allow_super = true;
1378 let mut second_binding = None;
1379
1380 for (i, &Segment { ident, id, .. }) in path.iter().enumerate() {
1381 debug!("resolve_path ident {} {:?} {:?}", i, ident, id);
1382 let record_segment_res = |this: &mut Self, res| {
1383 if finalize.is_some() {
1384 if let Some(id) = id {
1385 if !this.partial_res_map.contains_key(&id) {
1386 assert!(id != ast::DUMMY_NODE_ID, "Trying to resolve dummy id");
1387 this.record_partial_res(id, PartialRes::new(res));
1388 }
1389 }
1390 }
1391 };
1392
Charisee635618d2023-06-01 20:46:00 +00001393 let is_last = i + 1 == path.len();
Charisee9cf67802022-06-30 20:04:09 +00001394 let ns = if is_last { opt_ns.unwrap_or(TypeNS) } else { TypeNS };
1395 let name = ident.name;
1396
1397 allow_super &= ns == TypeNS && (name == kw::SelfLower || name == kw::Super);
1398
1399 if ns == TypeNS {
1400 if allow_super && name == kw::Super {
1401 let mut ctxt = ident.span.ctxt().normalize_to_macros_2_0();
1402 let self_module = match i {
1403 0 => Some(self.resolve_self(&mut ctxt, parent_scope.module)),
1404 _ => match module {
1405 Some(ModuleOrUniformRoot::Module(module)) => Some(module),
1406 _ => None,
1407 },
1408 };
1409 if let Some(self_module) = self_module {
1410 if let Some(parent) = self_module.parent {
1411 module = Some(ModuleOrUniformRoot::Module(
1412 self.resolve_self(&mut ctxt, parent),
1413 ));
1414 continue;
1415 }
1416 }
1417 return PathResult::failed(ident.span, false, finalize.is_some(), || {
1418 ("there are too many leading `super` keywords".to_string(), None)
1419 });
1420 }
1421 if i == 0 {
1422 if name == kw::SelfLower {
1423 let mut ctxt = ident.span.ctxt().normalize_to_macros_2_0();
1424 module = Some(ModuleOrUniformRoot::Module(
1425 self.resolve_self(&mut ctxt, parent_scope.module),
1426 ));
1427 continue;
1428 }
1429 if name == kw::PathRoot && ident.span.rust_2018() {
1430 module = Some(ModuleOrUniformRoot::ExternPrelude);
1431 continue;
1432 }
Chris Wailes5c0824a2023-04-24 16:30:59 -07001433 if name == kw::PathRoot
1434 && ident.span.is_rust_2015()
1435 && self.tcx.sess.rust_2018()
1436 {
Charisee9cf67802022-06-30 20:04:09 +00001437 // `::a::b` from 2015 macro on 2018 global edition
1438 module = Some(ModuleOrUniformRoot::CrateRootAndExternPrelude);
1439 continue;
1440 }
1441 if name == kw::PathRoot || name == kw::Crate || name == kw::DollarCrate {
1442 // `::a::b`, `crate::a::b` or `$crate::a::b`
1443 module = Some(ModuleOrUniformRoot::Module(self.resolve_crate_root(ident)));
1444 continue;
1445 }
1446 }
1447 }
1448
1449 // Report special messages for path segment keywords in wrong positions.
1450 if ident.is_path_segment_keyword() && i != 0 {
1451 return PathResult::failed(ident.span, false, finalize.is_some(), || {
1452 let name_str = if name == kw::PathRoot {
1453 "crate root".to_string()
1454 } else {
1455 format!("`{}`", name)
1456 };
1457 let label = if i == 1 && path[0].ident.name == kw::PathRoot {
1458 format!("global paths cannot start with {}", name_str)
1459 } else {
1460 format!("{} in paths can only be used in start position", name_str)
1461 };
1462 (label, None)
1463 });
1464 }
1465
1466 enum FindBindingResult<'a> {
1467 Binding(Result<&'a NameBinding<'a>, Determinacy>),
1468 Res(Res),
1469 }
1470 let find_binding_in_ns = |this: &mut Self, ns| {
1471 let binding = if let Some(module) = module {
1472 this.resolve_ident_in_module(
1473 module,
1474 ident,
1475 ns,
1476 parent_scope,
1477 finalize,
1478 ignore_binding,
1479 )
1480 } else if let Some(ribs) = ribs
1481 && let Some(TypeNS | ValueNS) = opt_ns
1482 {
1483 match this.resolve_ident_in_lexical_scope(
1484 ident,
1485 ns,
1486 parent_scope,
1487 finalize,
1488 &ribs[ns],
1489 ignore_binding,
1490 ) {
1491 // we found a locally-imported or available item/module
1492 Some(LexicalScopeBinding::Item(binding)) => Ok(binding),
1493 // we found a local variable or type param
1494 Some(LexicalScopeBinding::Res(res)) => return FindBindingResult::Res(res),
1495 _ => Err(Determinacy::determined(finalize.is_some())),
1496 }
1497 } else {
1498 let scopes = ScopeSet::All(ns, opt_ns.is_none());
1499 this.early_resolve_ident_in_lexical_scope(
1500 ident,
1501 scopes,
1502 parent_scope,
1503 finalize,
1504 finalize.is_some(),
1505 ignore_binding,
1506 )
1507 };
1508 FindBindingResult::Binding(binding)
1509 };
1510 let binding = match find_binding_in_ns(self, ns) {
1511 FindBindingResult::Res(res) => {
1512 record_segment_res(self, res);
1513 return PathResult::NonModule(PartialRes::with_unresolved_segments(
1514 res,
1515 path.len() - 1,
1516 ));
1517 }
1518 FindBindingResult::Binding(binding) => binding,
1519 };
1520 match binding {
1521 Ok(binding) => {
1522 if i == 1 {
1523 second_binding = Some(binding);
1524 }
1525 let res = binding.res();
1526 let maybe_assoc = opt_ns != Some(MacroNS) && PathSource::Type.is_expected(res);
1527 if let Some(next_module) = binding.module() {
1528 module = Some(ModuleOrUniformRoot::Module(next_module));
1529 record_segment_res(self, res);
Charisee635618d2023-06-01 20:46:00 +00001530 } else if res == Res::ToolMod && !is_last && opt_ns.is_some() {
Charisee9cf67802022-06-30 20:04:09 +00001531 if binding.is_import() {
Charisee635618d2023-06-01 20:46:00 +00001532 self.tcx.sess.emit_err(errors::ToolModuleImported {
1533 span: ident.span,
1534 import: binding.span,
1535 });
Charisee9cf67802022-06-30 20:04:09 +00001536 }
1537 let res = Res::NonMacroAttr(NonMacroAttrKind::Tool);
1538 return PathResult::NonModule(PartialRes::new(res));
1539 } else if res == Res::Err {
1540 return PathResult::NonModule(PartialRes::new(Res::Err));
1541 } else if opt_ns.is_some() && (is_last || maybe_assoc) {
1542 self.lint_if_path_starts_with_module(finalize, path, second_binding);
Chris Wailes65720582022-08-11 09:53:28 -07001543 record_segment_res(self, res);
Charisee9cf67802022-06-30 20:04:09 +00001544 return PathResult::NonModule(PartialRes::with_unresolved_segments(
1545 res,
1546 path.len() - i - 1,
1547 ));
1548 } else {
1549 return PathResult::failed(ident.span, is_last, finalize.is_some(), || {
1550 let label = format!(
1551 "`{ident}` is {} {}, not a module",
1552 res.article(),
1553 res.descr()
1554 );
1555 (label, None)
1556 });
1557 }
1558 }
1559 Err(Undetermined) => return PathResult::Indeterminate,
1560 Err(Determined) => {
1561 if let Some(ModuleOrUniformRoot::Module(module)) = module {
1562 if opt_ns.is_some() && !module.is_normal() {
1563 return PathResult::NonModule(PartialRes::with_unresolved_segments(
1564 module.res().unwrap(),
1565 path.len() - i,
1566 ));
1567 }
1568 }
1569
1570 return PathResult::failed(ident.span, is_last, finalize.is_some(), || {
1571 self.report_path_resolution_error(
1572 path,
1573 opt_ns,
1574 parent_scope,
1575 ribs,
1576 ignore_binding,
1577 module,
1578 i,
1579 ident,
1580 )
1581 });
1582 }
1583 }
1584 }
1585
1586 self.lint_if_path_starts_with_module(finalize, path, second_binding);
1587
1588 PathResult::Module(match module {
1589 Some(module) => module,
1590 None if path.is_empty() => ModuleOrUniformRoot::CurrentScope,
1591 _ => bug!("resolve_path: non-empty path `{:?}` has no module", path),
1592 })
1593 }
1594}