]>
Commit | Line | Data |
---|---|---|
e1599b0c XL |
1 | //! This crate is responsible for the part of name resolution that doesn't require type checker. |
2 | //! | |
3 | //! Module structure of the crate is built here. | |
4 | //! Paths in macros, imports, expressions, types, patterns are resolved here. | |
dfeec247 | 5 | //! Label and lifetime names are resolved here as well. |
e1599b0c | 6 | //! |
2b03887a | 7 | //! Type-relative name resolution (methods, fields, associated items) happens in `rustc_hir_analysis`. |
e1599b0c | 8 | |
1b1a35ee | 9 | #![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")] |
f2b60f7d | 10 | #![feature(assert_matches)] |
5869c6ff | 11 | #![feature(box_patterns)] |
c295e0f8 | 12 | #![feature(drain_filter)] |
04454e1e | 13 | #![feature(if_let_guard)] |
064997fb | 14 | #![feature(iter_intersperse)] |
5e7ed085 | 15 | #![feature(let_chains)] |
c295e0f8 | 16 | #![feature(never_type)] |
dfeec247 | 17 | #![recursion_limit = "256"] |
cdc7bbd5 | 18 | #![allow(rustdoc::private_intra_doc_links)] |
5e7ed085 | 19 | #![allow(rustc::potential_query_instability)] |
7cac9316 | 20 | |
c295e0f8 XL |
21 | #[macro_use] |
22 | extern crate tracing; | |
23 | ||
dfeec247 | 24 | pub use rustc_hir::def::{Namespace, PerNS}; |
94b46f34 | 25 | |
29967ef6 | 26 | use rustc_arena::{DroplessArena, TypedArena}; |
f9f354fc | 27 | use rustc_ast::node_id::NodeMap; |
04454e1e FG |
28 | use rustc_ast::{self as ast, NodeId, CRATE_NODE_ID}; |
29 | use rustc_ast::{AngleBracketedArg, Crate, Expr, ExprKind, GenericArg, GenericArgs, LitKind, Path}; | |
923072b8 | 30 | use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet}; |
5099ac24 | 31 | use rustc_data_structures::intern::Interned; |
dfeec247 | 32 | use rustc_data_structures::sync::Lrc; |
04454e1e | 33 | use rustc_errors::{Applicability, DiagnosticBuilder, ErrorGuaranteed}; |
cdc7bbd5 | 34 | use rustc_expand::base::{DeriveResolutions, SyntaxExtension, SyntaxExtensionKind}; |
dfeec247 | 35 | use rustc_hir::def::Namespace::*; |
923072b8 FG |
36 | use rustc_hir::def::{self, CtorOf, DefKind, LifetimeRes, PartialRes}; |
37 | use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId}; | |
04454e1e | 38 | use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE}; |
923072b8 | 39 | use rustc_hir::definitions::{DefPathData, Definitions}; |
cdc7bbd5 | 40 | use rustc_hir::TraitCandidate; |
f035d41b | 41 | use rustc_index::vec::IndexVec; |
dfeec247 | 42 | use rustc_metadata::creader::{CStore, CrateLoader}; |
5099ac24 | 43 | use rustc_middle::metadata::ModChild; |
2b03887a | 44 | use rustc_middle::middle::privacy::EffectiveVisibilities; |
04454e1e | 45 | use rustc_middle::span_bug; |
2b03887a FG |
46 | use rustc_middle::ty::{self, DefIdTree, MainDefinition, RegisteredTools}; |
47 | use rustc_middle::ty::{ResolverGlobalCtxt, ResolverOutputs}; | |
3c0e092e | 48 | use rustc_query_system::ich::StableHashingContext; |
2b03887a | 49 | use rustc_session::cstore::{CrateStore, MetadataLoaderDyn}; |
04454e1e | 50 | use rustc_session::lint::LintBuffer; |
dfeec247 | 51 | use rustc_session::Session; |
04454e1e | 52 | use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind, SyntaxContext, Transparency}; |
3c0e092e | 53 | use rustc_span::source_map::Spanned; |
f9f354fc | 54 | use rustc_span::symbol::{kw, sym, Ident, Symbol}; |
dfeec247 | 55 | use rustc_span::{Span, DUMMY_SP}; |
1a4d82fc | 56 | |
3dfed10e | 57 | use smallvec::{smallvec, SmallVec}; |
1a4d82fc | 58 | use std::cell::{Cell, RefCell}; |
5099ac24 | 59 | use std::collections::BTreeSet; |
f2b60f7d | 60 | use std::{fmt, ptr}; |
85aaf69f | 61 | |
f035d41b | 62 | use diagnostics::{ImportSuggestion, LabelSuggestion, Suggestion}; |
74b04a01 | 63 | use imports::{Import, ImportKind, ImportResolver, NameResolution}; |
064997fb | 64 | use late::{HasGenericParams, PathSource, PatternSource}; |
29967ef6 | 65 | use macros::{MacroRulesBinding, MacroRulesScope, MacroRulesScopeRef}; |
c34b1796 | 66 | |
2b03887a | 67 | use crate::effective_visibilities::EffectiveVisibilitiesVisitor; |
5099ac24 | 68 | |
48663c56 XL |
69 | type Res = def::Res<NodeId>; |
70 | ||
dfeec247 XL |
71 | mod build_reduced_graph; |
72 | mod check_unused; | |
60c5eb7d | 73 | mod def_collector; |
54a0048b | 74 | mod diagnostics; |
2b03887a | 75 | mod effective_visibilities; |
04454e1e | 76 | mod ident; |
dfeec247 | 77 | mod imports; |
416331ca | 78 | mod late; |
9e0c209e | 79 | mod macros; |
1a4d82fc | 80 | |
13cf67c4 XL |
81 | enum Weak { |
82 | Yes, | |
83 | No, | |
84 | } | |
85 | ||
416331ca XL |
86 | #[derive(Copy, Clone, PartialEq, Debug)] |
87 | pub enum Determinacy { | |
88 | Determined, | |
89 | Undetermined, | |
90 | } | |
91 | ||
92 | impl Determinacy { | |
93 | fn determined(determined: bool) -> Determinacy { | |
94 | if determined { Determinacy::Determined } else { Determinacy::Undetermined } | |
95 | } | |
96 | } | |
97 | ||
98 | /// A specific scope in which a name can be looked up. | |
99 | /// This enum is currently used only for early resolution (imports and macros), | |
100 | /// but not for late resolution yet. | |
101 | #[derive(Clone, Copy)] | |
102 | enum Scope<'a> { | |
136023e0 | 103 | DeriveHelpers(LocalExpnId), |
60c5eb7d | 104 | DeriveHelpersCompat, |
29967ef6 | 105 | MacroRules(MacroRulesScopeRef<'a>), |
416331ca | 106 | CrateRoot, |
cdc7bbd5 XL |
107 | // The node ID is for reporting the `PROC_MACRO_DERIVE_RESOLUTION_FALLBACK` |
108 | // lint if it should be reported. | |
109 | Module(Module<'a>, Option<NodeId>), | |
416331ca XL |
110 | MacroUsePrelude, |
111 | BuiltinAttrs, | |
416331ca XL |
112 | ExternPrelude, |
113 | ToolPrelude, | |
114 | StdLibPrelude, | |
115 | BuiltinTypes, | |
116 | } | |
117 | ||
118 | /// Names from different contexts may want to visit different subsets of all specific scopes | |
119 | /// with different restrictions when looking up the resolution. | |
120 | /// This enum is currently used only for early resolution (imports and macros), | |
121 | /// but not for late resolution yet. | |
cdc7bbd5 XL |
122 | #[derive(Clone, Copy)] |
123 | enum ScopeSet<'a> { | |
416331ca XL |
124 | /// All scopes with the given namespace. |
125 | All(Namespace, /*is_import*/ bool), | |
126 | /// Crate root, then extern prelude (used for mixed 2015-2018 mode in macros). | |
13cf67c4 | 127 | AbsolutePath(Namespace), |
416331ca | 128 | /// All scopes with macro namespace and the given macro kind restriction. |
13cf67c4 | 129 | Macro(MacroKind), |
cdc7bbd5 XL |
130 | /// All scopes with the given namespace, used for partially performing late resolution. |
131 | /// The node id enables lints and is used for reporting them. | |
132 | Late(Namespace, Module<'a>, Option<NodeId>), | |
13cf67c4 XL |
133 | } |
134 | ||
416331ca XL |
135 | /// Everything you need to know about a name's location to resolve it. |
136 | /// Serves as a starting point for the scope visitor. | |
137 | /// This struct is currently used only for early resolution (imports and macros), | |
138 | /// but not for late resolution yet. | |
e1599b0c | 139 | #[derive(Clone, Copy, Debug)] |
416331ca | 140 | pub struct ParentScope<'a> { |
04454e1e | 141 | pub module: Module<'a>, |
136023e0 | 142 | expansion: LocalExpnId, |
04454e1e | 143 | pub macro_rules: MacroRulesScopeRef<'a>, |
e1599b0c XL |
144 | derives: &'a [ast::Path], |
145 | } | |
146 | ||
147 | impl<'a> ParentScope<'a> { | |
148 | /// Creates a parent scope with the passed argument used as the module scope component, | |
149 | /// and other scope components set to default empty values. | |
29967ef6 | 150 | pub fn module(module: Module<'a>, resolver: &Resolver<'a>) -> ParentScope<'a> { |
ba9703b0 XL |
151 | ParentScope { |
152 | module, | |
136023e0 | 153 | expansion: LocalExpnId::ROOT, |
29967ef6 | 154 | macro_rules: resolver.arenas.alloc_macro_rules_scope(MacroRulesScope::Empty), |
ba9703b0 XL |
155 | derives: &[], |
156 | } | |
e1599b0c | 157 | } |
7453a54e SL |
158 | } |
159 | ||
6a06907d XL |
160 | #[derive(Copy, Debug, Clone)] |
161 | enum ImplTraitContext { | |
162 | Existential, | |
163 | Universal(LocalDefId), | |
164 | } | |
165 | ||
8bb4bdeb | 166 | struct BindingError { |
f9f354fc | 167 | name: Symbol, |
8bb4bdeb XL |
168 | origin: BTreeSet<Span>, |
169 | target: BTreeSet<Span>, | |
dfeec247 | 170 | could_be_path: bool, |
0731742a XL |
171 | } |
172 | ||
54a0048b | 173 | enum ResolutionError<'a> { |
9fa01778 | 174 | /// Error E0401: can't use type or const parameters from outer function. |
e74abb32 | 175 | GenericParamsFromOuterFunction(Res, HasGenericParams), |
9fa01778 XL |
176 | /// Error E0403: the name is already used for a type or const parameter in this generic |
177 | /// parameter list. | |
f9f354fc | 178 | NameAlreadyUsedInParameterList(Symbol, Span), |
9fa01778 | 179 | /// Error E0407: method is not a member of trait. |
04454e1e | 180 | MethodNotMemberOfTrait(Ident, String, Option<Symbol>), |
9fa01778 | 181 | /// Error E0437: type is not a member of trait. |
04454e1e | 182 | TypeNotMemberOfTrait(Ident, String, Option<Symbol>), |
9fa01778 | 183 | /// Error E0438: const is not a member of trait. |
04454e1e | 184 | ConstNotMemberOfTrait(Ident, String, Option<Symbol>), |
9fa01778 | 185 | /// Error E0408: variable `{}` is not bound in all patterns. |
04454e1e | 186 | VariableNotBoundInPattern(BindingError, ParentScope<'a>), |
9fa01778 | 187 | /// Error E0409: variable `{}` is bound in inconsistent ways within the same match arm. |
f9f354fc | 188 | VariableBoundWithDifferentMode(Symbol, Span), |
9fa01778 | 189 | /// Error E0415: identifier is bound more than once in this parameter list. |
3dfed10e | 190 | IdentifierBoundMoreThanOnceInParameterList(Symbol), |
9fa01778 | 191 | /// Error E0416: identifier is bound more than once in the same pattern. |
3dfed10e | 192 | IdentifierBoundMoreThanOnceInSamePattern(Symbol), |
9fa01778 | 193 | /// Error E0426: use of undeclared label. |
3dfed10e | 194 | UndeclaredLabel { name: Symbol, suggestion: Option<LabelSuggestion> }, |
9fa01778 | 195 | /// Error E0429: `self` imports are only allowed within a `{ }` list. |
f9f354fc | 196 | SelfImportsOnlyAllowedWithin { root: bool, span_with_rename: Span }, |
9fa01778 | 197 | /// Error E0430: `self` import can only appear once in the list. |
c1a9b12d | 198 | SelfImportCanOnlyAppearOnceInTheList, |
9fa01778 | 199 | /// Error E0431: `self` import can only appear in an import list with a non-empty prefix. |
c1a9b12d | 200 | SelfImportOnlyInImportListWithNonEmptyPrefix, |
9fa01778 | 201 | /// Error E0433: failed to resolve. |
532ac7d7 | 202 | FailedToResolve { label: String, suggestion: Option<Suggestion> }, |
9fa01778 | 203 | /// Error E0434: can't capture dynamic environment in a fn item. |
c1a9b12d | 204 | CannotCaptureDynamicEnvironmentInFnItem, |
9fa01778 | 205 | /// Error E0435: attempt to use a non-constant value in a constant. |
5869c6ff XL |
206 | AttemptToUseNonConstantValueInConstant( |
207 | Ident, | |
208 | /* suggestion */ &'static str, | |
209 | /* current */ &'static str, | |
210 | ), | |
9fa01778 | 211 | /// Error E0530: `X` bindings cannot shadow `Y`s. |
17df50a5 | 212 | BindingShadowsSomethingUnacceptable { |
064997fb | 213 | shadowing_binding: PatternSource, |
17df50a5 XL |
214 | name: Symbol, |
215 | participle: &'static str, | |
216 | article: &'static str, | |
064997fb | 217 | shadowed_binding: Res, |
17df50a5 XL |
218 | shadowed_binding_span: Span, |
219 | }, | |
cdc7bbd5 | 220 | /// Error E0128: generic parameters with a default cannot use forward-declared identifiers. |
17df50a5 | 221 | ForwardDeclaredGenericParam, |
3dfed10e XL |
222 | /// ERROR E0770: the type of const parameters must not depend on other generic parameters. |
223 | ParamInTyOfConstParam(Symbol), | |
29967ef6 | 224 | /// generic parameters must not be used inside const evaluations. |
3dfed10e XL |
225 | /// |
226 | /// This error is only emitted when using `min_const_generics`. | |
1b1a35ee | 227 | ParamInNonTrivialAnonConst { name: Symbol, is_type: bool }, |
cdc7bbd5 | 228 | /// Error E0735: generic parameters with a default cannot use `Self` |
136023e0 | 229 | SelfInGenericParamDefault, |
f035d41b | 230 | /// Error E0767: use of unreachable label |
3dfed10e | 231 | UnreachableLabel { name: Symbol, definition_span: Span, suggestion: Option<LabelSuggestion> }, |
5099ac24 FG |
232 | /// Error E0323, E0324, E0325: mismatch between trait item and impl item. |
233 | TraitImplMismatch { | |
234 | name: Symbol, | |
235 | kind: &'static str, | |
236 | trait_path: String, | |
237 | trait_item_span: Span, | |
238 | code: rustc_errors::DiagnosticId, | |
239 | }, | |
2b03887a FG |
240 | /// Error E0201: multiple impl items for the same trait item. |
241 | TraitImplDuplicate { name: Symbol, trait_item_span: Span, old_span: Span }, | |
04454e1e FG |
242 | /// Inline asm `sym` operand must refer to a `fn` or `static`. |
243 | InvalidAsmSym, | |
e74abb32 XL |
244 | } |
245 | ||
246 | enum VisResolutionError<'a> { | |
247 | Relative2018(Span, &'a ast::Path), | |
248 | AncestorOnly(Span), | |
249 | FailedToResolve(Span, String, Option<Suggestion>), | |
250 | ExpectedFound(Span, String, Res), | |
251 | Indeterminate(Span), | |
252 | ModuleOnly(Span), | |
9cc50fc6 SL |
253 | } |
254 | ||
f035d41b XL |
255 | /// A minimal representation of a path segment. We use this in resolve because we synthesize 'path |
256 | /// segments' which don't have the rest of an AST or HIR `PathSegment`. | |
13cf67c4 XL |
257 | #[derive(Clone, Copy, Debug)] |
258 | pub struct Segment { | |
259 | ident: Ident, | |
260 | id: Option<NodeId>, | |
f035d41b XL |
261 | /// Signals whether this `PathSegment` has generic arguments. Used to avoid providing |
262 | /// nonsensical suggestions. | |
263 | has_generic_args: bool, | |
04454e1e FG |
264 | /// Signals whether this `PathSegment` has lifetime arguments. |
265 | has_lifetime_args: bool, | |
266 | args_span: Span, | |
13cf67c4 XL |
267 | } |
268 | ||
269 | impl Segment { | |
270 | fn from_path(path: &Path) -> Vec<Segment> { | |
271 | path.segments.iter().map(|s| s.into()).collect() | |
272 | } | |
273 | ||
274 | fn from_ident(ident: Ident) -> Segment { | |
04454e1e FG |
275 | Segment { |
276 | ident, | |
277 | id: None, | |
278 | has_generic_args: false, | |
279 | has_lifetime_args: false, | |
280 | args_span: DUMMY_SP, | |
281 | } | |
282 | } | |
283 | ||
284 | fn from_ident_and_id(ident: Ident, id: NodeId) -> Segment { | |
285 | Segment { | |
286 | ident, | |
287 | id: Some(id), | |
288 | has_generic_args: false, | |
289 | has_lifetime_args: false, | |
290 | args_span: DUMMY_SP, | |
291 | } | |
13cf67c4 XL |
292 | } |
293 | ||
294 | fn names_to_string(segments: &[Segment]) -> String { | |
dfeec247 | 295 | names_to_string(&segments.iter().map(|seg| seg.ident.name).collect::<Vec<_>>()) |
13cf67c4 XL |
296 | } |
297 | } | |
298 | ||
299 | impl<'a> From<&'a ast::PathSegment> for Segment { | |
300 | fn from(seg: &'a ast::PathSegment) -> Segment { | |
04454e1e FG |
301 | let has_generic_args = seg.args.is_some(); |
302 | let (args_span, has_lifetime_args) = if let Some(args) = seg.args.as_deref() { | |
303 | match args { | |
304 | GenericArgs::AngleBracketed(args) => { | |
305 | let found_lifetimes = args | |
306 | .args | |
307 | .iter() | |
308 | .any(|arg| matches!(arg, AngleBracketedArg::Arg(GenericArg::Lifetime(_)))); | |
309 | (args.span, found_lifetimes) | |
5e7ed085 | 310 | } |
04454e1e | 311 | GenericArgs::Parenthesized(args) => (args.span, true), |
6a06907d | 312 | } |
5e7ed085 | 313 | } else { |
04454e1e FG |
314 | (DUMMY_SP, false) |
315 | }; | |
316 | Segment { | |
317 | ident: seg.ident, | |
318 | id: Some(seg.id), | |
319 | has_generic_args, | |
320 | has_lifetime_args, | |
321 | args_span, | |
6a06907d | 322 | } |
3b2f2976 XL |
323 | } |
324 | } | |
325 | ||
0531ce1d XL |
326 | /// An intermediate resolution result. |
327 | /// | |
48663c56 XL |
328 | /// This refers to the thing referred by a name. The difference between `Res` and `Item` is that |
329 | /// items are visible in their whole block, while `Res`es only from the place they are defined | |
0531ce1d | 330 | /// forward. |
416331ca | 331 | #[derive(Debug)] |
54a0048b SL |
332 | enum LexicalScopeBinding<'a> { |
333 | Item(&'a NameBinding<'a>), | |
48663c56 | 334 | Res(Res), |
54a0048b SL |
335 | } |
336 | ||
337 | impl<'a> LexicalScopeBinding<'a> { | |
48663c56 | 338 | fn res(self) -> Res { |
32a655c1 | 339 | match self { |
48663c56 XL |
340 | LexicalScopeBinding::Item(binding) => binding.res(), |
341 | LexicalScopeBinding::Res(res) => res, | |
32a655c1 SL |
342 | } |
343 | } | |
476ff2be SL |
344 | } |
345 | ||
b7449926 | 346 | #[derive(Copy, Clone, Debug)] |
13cf67c4 | 347 | enum ModuleOrUniformRoot<'a> { |
b7449926 XL |
348 | /// Regular module. |
349 | Module(Module<'a>), | |
350 | ||
13cf67c4 XL |
351 | /// Virtual module that denotes resolution in crate root with fallback to extern prelude. |
352 | CrateRootAndExternPrelude, | |
353 | ||
354 | /// Virtual module that denotes resolution in extern prelude. | |
0731742a | 355 | /// Used for paths starting with `::` on 2018 edition. |
13cf67c4 XL |
356 | ExternPrelude, |
357 | ||
358 | /// Virtual module that denotes resolution in current scope. | |
359 | /// Used only for resolving single-segment imports. The reason it exists is that import paths | |
360 | /// are always split into two parts, the first of which should be some kind of module. | |
361 | CurrentScope, | |
362 | } | |
363 | ||
69743fb6 XL |
364 | impl ModuleOrUniformRoot<'_> { |
365 | fn same_def(lhs: Self, rhs: Self) -> bool { | |
366 | match (lhs, rhs) { | |
dfeec247 | 367 | (ModuleOrUniformRoot::Module(lhs), ModuleOrUniformRoot::Module(rhs)) => { |
c295e0f8 | 368 | ptr::eq(lhs, rhs) |
dfeec247 XL |
369 | } |
370 | ( | |
371 | ModuleOrUniformRoot::CrateRootAndExternPrelude, | |
372 | ModuleOrUniformRoot::CrateRootAndExternPrelude, | |
373 | ) | |
374 | | (ModuleOrUniformRoot::ExternPrelude, ModuleOrUniformRoot::ExternPrelude) | |
375 | | (ModuleOrUniformRoot::CurrentScope, ModuleOrUniformRoot::CurrentScope) => true, | |
13cf67c4 XL |
376 | _ => false, |
377 | } | |
378 | } | |
b7449926 XL |
379 | } |
380 | ||
2c00a5a8 | 381 | #[derive(Clone, Debug)] |
476ff2be | 382 | enum PathResult<'a> { |
b7449926 | 383 | Module(ModuleOrUniformRoot<'a>), |
48663c56 | 384 | NonModule(PartialRes), |
476ff2be | 385 | Indeterminate, |
532ac7d7 XL |
386 | Failed { |
387 | span: Span, | |
388 | label: String, | |
389 | suggestion: Option<Suggestion>, | |
390 | is_error_from_last_segment: bool, | |
391 | }, | |
476ff2be SL |
392 | } |
393 | ||
5e7ed085 FG |
394 | impl<'a> PathResult<'a> { |
395 | fn failed( | |
396 | span: Span, | |
397 | is_error_from_last_segment: bool, | |
398 | finalize: bool, | |
399 | label_and_suggestion: impl FnOnce() -> (String, Option<Suggestion>), | |
400 | ) -> PathResult<'a> { | |
401 | let (label, suggestion) = | |
402 | if finalize { label_and_suggestion() } else { (String::new(), None) }; | |
403 | PathResult::Failed { span, label, suggestion, is_error_from_last_segment } | |
404 | } | |
405 | } | |
406 | ||
fc512014 | 407 | #[derive(Debug)] |
9e0c209e | 408 | enum ModuleKind { |
9fa01778 | 409 | /// An anonymous module; e.g., just a block. |
0531ce1d XL |
410 | /// |
411 | /// ``` | |
412 | /// fn main() { | |
413 | /// fn f() {} // (1) | |
414 | /// { // This is an anonymous module | |
415 | /// f(); // This resolves to (2) as we are inside the block. | |
416 | /// fn f() {} // (2) | |
417 | /// } | |
418 | /// f(); // Resolves to (1) | |
419 | /// } | |
420 | /// ``` | |
064997fb | 421 | Block, |
0531ce1d XL |
422 | /// Any module with a name. |
423 | /// | |
424 | /// This could be: | |
425 | /// | |
5869c6ff XL |
426 | /// * A normal module – either `mod from_file;` or `mod from_block { }` – |
427 | /// or the crate root (which is conceptually a top-level module). | |
428 | /// Note that the crate root's [name][Self::name] will be [`kw::Empty`]. | |
0531ce1d XL |
429 | /// * A trait or an enum (it implicitly contains associated types, methods and variant |
430 | /// constructors). | |
f9f354fc | 431 | Def(DefKind, DefId, Symbol), |
48663c56 XL |
432 | } |
433 | ||
434 | impl ModuleKind { | |
435 | /// Get name of the module. | |
f9f354fc | 436 | pub fn name(&self) -> Option<Symbol> { |
48663c56 | 437 | match self { |
064997fb | 438 | ModuleKind::Block => None, |
48663c56 XL |
439 | ModuleKind::Def(.., name) => Some(*name), |
440 | } | |
441 | } | |
1a4d82fc JJ |
442 | } |
443 | ||
e74abb32 XL |
444 | /// A key that identifies a binding in a given `Module`. |
445 | /// | |
446 | /// Multiple bindings in the same module can have the same key (in a valid | |
447 | /// program) if all but one of them come from glob imports. | |
3dfed10e | 448 | #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] |
e74abb32 | 449 | struct BindingKey { |
5e7ed085 | 450 | /// The identifier for the binding, always the `normalize_to_macros_2_0` version of the |
e74abb32 XL |
451 | /// identifier. |
452 | ident: Ident, | |
453 | ns: Namespace, | |
454 | /// 0 if ident is not `_`, otherwise a value that's unique to the specific | |
455 | /// `_` in the expanded AST that introduced this binding. | |
456 | disambiguator: u32, | |
457 | } | |
458 | ||
459 | type Resolutions<'a> = RefCell<FxIndexMap<BindingKey, &'a RefCell<NameResolution<'a>>>>; | |
e1599b0c | 460 | |
1a4d82fc | 461 | /// One node in the tree of modules. |
5869c6ff XL |
462 | /// |
463 | /// Note that a "module" in resolve is broader than a `mod` that you declare in Rust code. It may be one of these: | |
464 | /// | |
465 | /// * `mod` | |
466 | /// * crate root (aka, top-level anonymous module) | |
467 | /// * `enum` | |
468 | /// * `trait` | |
469 | /// * curly-braced block with statements | |
470 | /// | |
471 | /// You can use [`ModuleData::kind`] to determine the kind of module this is. | |
32a655c1 | 472 | pub struct ModuleData<'a> { |
5869c6ff | 473 | /// The direct parent module (it may not be a `mod`, however). |
9e0c209e | 474 | parent: Option<Module<'a>>, |
5869c6ff | 475 | /// What kind of module this is, because this may not be a `mod`. |
9e0c209e SL |
476 | kind: ModuleKind, |
477 | ||
5869c6ff XL |
478 | /// Mapping between names and their (possibly in-progress) resolutions in this module. |
479 | /// Resolutions in modules from other crates are not populated until accessed. | |
e1599b0c | 480 | lazy_resolutions: Resolutions<'a>, |
5869c6ff | 481 | /// True if this is a module from other crate that needs to be populated on access. |
e1599b0c | 482 | populate_on_access: Cell<bool>, |
1a4d82fc | 483 | |
5869c6ff | 484 | /// Macro invocations that can expand into items in this module. |
136023e0 | 485 | unexpanded_invocations: RefCell<FxHashSet<LocalExpnId>>, |
1a4d82fc | 486 | |
5869c6ff | 487 | /// Whether `#[no_implicit_prelude]` is active. |
9e0c209e | 488 | no_implicit_prelude: bool, |
1a4d82fc | 489 | |
74b04a01 XL |
490 | glob_importers: RefCell<Vec<&'a Import<'a>>>, |
491 | globs: RefCell<Vec<&'a Import<'a>>>, | |
e9174d1e | 492 | |
5869c6ff | 493 | /// Used to memoize the traits in this module for faster searches through all traits in scope. |
32a655c1 | 494 | traits: RefCell<Option<Box<[(Ident, &'a NameBinding<'a>)]>>>, |
e9174d1e | 495 | |
7cac9316 XL |
496 | /// Span of the module itself. Used for error reporting. |
497 | span: Span, | |
498 | ||
416331ca | 499 | expansion: ExpnId, |
1a4d82fc JJ |
500 | } |
501 | ||
3b2f2976 | 502 | type Module<'a> = &'a ModuleData<'a>; |
9cc50fc6 | 503 | |
32a655c1 | 504 | impl<'a> ModuleData<'a> { |
dfeec247 XL |
505 | fn new( |
506 | parent: Option<Module<'a>>, | |
507 | kind: ModuleKind, | |
dfeec247 XL |
508 | expansion: ExpnId, |
509 | span: Span, | |
c295e0f8 | 510 | no_implicit_prelude: bool, |
dfeec247 | 511 | ) -> Self { |
c295e0f8 XL |
512 | let is_foreign = match kind { |
513 | ModuleKind::Def(_, def_id, _) => !def_id.is_local(), | |
064997fb | 514 | ModuleKind::Block => false, |
c295e0f8 | 515 | }; |
32a655c1 | 516 | ModuleData { |
3b2f2976 XL |
517 | parent, |
518 | kind, | |
e1599b0c | 519 | lazy_resolutions: Default::default(), |
c295e0f8 | 520 | populate_on_access: Cell::new(is_foreign), |
e1599b0c | 521 | unexpanded_invocations: Default::default(), |
c295e0f8 | 522 | no_implicit_prelude, |
54a0048b | 523 | glob_importers: RefCell::new(Vec::new()), |
2c00a5a8 | 524 | globs: RefCell::new(Vec::new()), |
54a0048b | 525 | traits: RefCell::new(None), |
3b2f2976 XL |
526 | span, |
527 | expansion, | |
7453a54e SL |
528 | } |
529 | } | |
530 | ||
e1599b0c | 531 | fn for_each_child<R, F>(&'a self, resolver: &mut R, mut f: F) |
dfeec247 XL |
532 | where |
533 | R: AsMut<Resolver<'a>>, | |
534 | F: FnMut(&mut R, Ident, Namespace, &'a NameBinding<'a>), | |
e1599b0c | 535 | { |
e74abb32 | 536 | for (key, name_resolution) in resolver.as_mut().resolutions(self).borrow().iter() { |
f9f354fc XL |
537 | if let Some(binding) = name_resolution.borrow().binding { |
538 | f(resolver, key.ident, key.ns, binding); | |
539 | } | |
3b2f2976 XL |
540 | } |
541 | } | |
542 | ||
3dfed10e XL |
543 | /// This modifies `self` in place. The traits will be stored in `self.traits`. |
544 | fn ensure_traits<R>(&'a self, resolver: &mut R) | |
545 | where | |
546 | R: AsMut<Resolver<'a>>, | |
547 | { | |
548 | let mut traits = self.traits.borrow_mut(); | |
549 | if traits.is_none() { | |
550 | let mut collected_traits = Vec::new(); | |
551 | self.for_each_child(resolver, |_, name, ns, binding| { | |
552 | if ns != TypeNS { | |
553 | return; | |
554 | } | |
1b1a35ee XL |
555 | if let Res::Def(DefKind::Trait | DefKind::TraitAlias, _) = binding.res() { |
556 | collected_traits.push((name, binding)) | |
3dfed10e XL |
557 | } |
558 | }); | |
559 | *traits = Some(collected_traits.into_boxed_slice()); | |
560 | } | |
561 | } | |
562 | ||
48663c56 | 563 | fn res(&self) -> Option<Res> { |
9e0c209e | 564 | match self.kind { |
48663c56 XL |
565 | ModuleKind::Def(kind, def_id, _) => Some(Res::Def(kind, def_id)), |
566 | _ => None, | |
567 | } | |
568 | } | |
569 | ||
5099ac24 FG |
570 | // Public for rustdoc. |
571 | pub fn def_id(&self) -> DefId { | |
c295e0f8 XL |
572 | self.opt_def_id().expect("`ModuleData::def_id` is called on a block module") |
573 | } | |
574 | ||
575 | fn opt_def_id(&self) -> Option<DefId> { | |
48663c56 XL |
576 | match self.kind { |
577 | ModuleKind::Def(_, def_id, _) => Some(def_id), | |
578 | _ => None, | |
579 | } | |
92a42be0 SL |
580 | } |
581 | ||
a7813a04 | 582 | // `self` resolves to the first module ancestor that `is_normal`. |
7453a54e | 583 | fn is_normal(&self) -> bool { |
29967ef6 | 584 | matches!(self.kind, ModuleKind::Def(DefKind::Mod, _, _)) |
1a4d82fc JJ |
585 | } |
586 | ||
7453a54e | 587 | fn is_trait(&self) -> bool { |
29967ef6 | 588 | matches!(self.kind, ModuleKind::Def(DefKind::Trait, _, _)) |
e9174d1e | 589 | } |
c30ab7b3 | 590 | |
8bb4bdeb | 591 | fn nearest_item_scope(&'a self) -> Module<'a> { |
e1599b0c | 592 | match self.kind { |
ba9703b0 | 593 | ModuleKind::Def(DefKind::Enum | DefKind::Trait, ..) => { |
dfeec247 XL |
594 | self.parent.expect("enum or trait module without a parent") |
595 | } | |
e1599b0c XL |
596 | _ => self, |
597 | } | |
8bb4bdeb | 598 | } |
4462d4a0 | 599 | |
c295e0f8 XL |
600 | /// The [`DefId`] of the nearest `mod` item ancestor (which may be this module). |
601 | /// This may be the crate root. | |
602 | fn nearest_parent_mod(&self) -> DefId { | |
603 | match self.kind { | |
604 | ModuleKind::Def(DefKind::Mod, def_id, _) => def_id, | |
605 | _ => self.parent.expect("non-root module without parent").nearest_parent_mod(), | |
606 | } | |
607 | } | |
608 | ||
4462d4a0 XL |
609 | fn is_ancestor_of(&self, mut other: &Self) -> bool { |
610 | while !ptr::eq(self, other) { | |
611 | if let Some(parent) = other.parent { | |
612 | other = parent; | |
613 | } else { | |
614 | return false; | |
615 | } | |
616 | } | |
617 | true | |
618 | } | |
1a4d82fc JJ |
619 | } |
620 | ||
32a655c1 | 621 | impl<'a> fmt::Debug for ModuleData<'a> { |
9fa01778 | 622 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
48663c56 | 623 | write!(f, "{:?}", self.res()) |
1a4d82fc JJ |
624 | } |
625 | } | |
626 | ||
83c7162d | 627 | /// Records a possibly-private value, type, or module definition. |
54a0048b | 628 | #[derive(Clone, Debug)] |
7453a54e | 629 | pub struct NameBinding<'a> { |
7453a54e | 630 | kind: NameBindingKind<'a>, |
0731742a | 631 | ambiguity: Option<(&'a NameBinding<'a>, AmbiguityKind)>, |
136023e0 | 632 | expansion: LocalExpnId, |
a7813a04 | 633 | span: Span, |
f2b60f7d | 634 | vis: ty::Visibility<DefId>, |
1a4d82fc JJ |
635 | } |
636 | ||
5bcae85e | 637 | pub trait ToNameBinding<'a> { |
32a655c1 | 638 | fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a>; |
5bcae85e SL |
639 | } |
640 | ||
32a655c1 SL |
641 | impl<'a> ToNameBinding<'a> for &'a NameBinding<'a> { |
642 | fn to_name_binding(self, _: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> { | |
5bcae85e SL |
643 | self |
644 | } | |
645 | } | |
646 | ||
54a0048b | 647 | #[derive(Clone, Debug)] |
7453a54e | 648 | enum NameBindingKind<'a> { |
48663c56 | 649 | Res(Res, /* is_macro_export */ bool), |
9cc50fc6 | 650 | Module(Module<'a>), |
74b04a01 | 651 | Import { binding: &'a NameBinding<'a>, import: &'a Import<'a>, used: Cell<bool> }, |
1a4d82fc JJ |
652 | } |
653 | ||
9fa01778 | 654 | impl<'a> NameBindingKind<'a> { |
94222f64 | 655 | /// Is this a name binding of an import? |
9fa01778 | 656 | fn is_import(&self) -> bool { |
29967ef6 | 657 | matches!(*self, NameBindingKind::Import { .. }) |
9fa01778 XL |
658 | } |
659 | } | |
660 | ||
dfeec247 XL |
661 | struct PrivacyError<'a> { |
662 | ident: Ident, | |
663 | binding: &'a NameBinding<'a>, | |
664 | dedup_span: Span, | |
665 | } | |
54a0048b | 666 | |
3b2f2976 | 667 | struct UseError<'a> { |
5e7ed085 | 668 | err: DiagnosticBuilder<'a, ErrorGuaranteed>, |
f035d41b | 669 | /// Candidates which user could `use` to access the missing type. |
3b2f2976 | 670 | candidates: Vec<ImportSuggestion>, |
f035d41b | 671 | /// The `DefId` of the module to place the use-statements in. |
f9f354fc | 672 | def_id: DefId, |
f035d41b XL |
673 | /// Whether the diagnostic should say "instead" (as in `consider importing ... instead`). |
674 | instead: bool, | |
675 | /// Extra free-form suggestion. | |
dfeec247 | 676 | suggestion: Option<(Span, &'static str, String, Applicability)>, |
04454e1e FG |
677 | /// Path `Segment`s at the place of use that failed. Used for accurate suggestion after telling |
678 | /// the user to import the item directly. | |
679 | path: Vec<Segment>, | |
2b03887a FG |
680 | /// Whether the expected source is a call |
681 | is_call: bool, | |
3b2f2976 XL |
682 | } |
683 | ||
13cf67c4 XL |
684 | #[derive(Clone, Copy, PartialEq, Debug)] |
685 | enum AmbiguityKind { | |
686 | Import, | |
13cf67c4 XL |
687 | BuiltinAttr, |
688 | DeriveHelper, | |
ba9703b0 | 689 | MacroRulesVsModularized, |
13cf67c4 XL |
690 | GlobVsOuter, |
691 | GlobVsGlob, | |
692 | GlobVsExpanded, | |
693 | MoreExpandedVsOuter, | |
694 | } | |
695 | ||
696 | impl AmbiguityKind { | |
697 | fn descr(self) -> &'static str { | |
698 | match self { | |
3c0e092e XL |
699 | AmbiguityKind::Import => "multiple potential import sources", |
700 | AmbiguityKind::BuiltinAttr => "a name conflict with a builtin attribute", | |
701 | AmbiguityKind::DeriveHelper => "a name conflict with a derive helper attribute", | |
ba9703b0 | 702 | AmbiguityKind::MacroRulesVsModularized => { |
3c0e092e | 703 | "a conflict between a `macro_rules` name and a non-`macro_rules` name from another module" |
ba9703b0 | 704 | } |
dfeec247 | 705 | AmbiguityKind::GlobVsOuter => { |
3c0e092e | 706 | "a conflict between a name from a glob import and an outer scope during import or macro resolution" |
dfeec247 | 707 | } |
3c0e092e | 708 | AmbiguityKind::GlobVsGlob => "multiple glob imports of a name in the same module", |
dfeec247 | 709 | AmbiguityKind::GlobVsExpanded => { |
3c0e092e | 710 | "a conflict between a name from a glob import and a macro-expanded name in the same module during import or macro resolution" |
dfeec247 XL |
711 | } |
712 | AmbiguityKind::MoreExpandedVsOuter => { | |
3c0e092e | 713 | "a conflict between a macro-expanded name and a less macro-expanded name from outer scope during import or macro resolution" |
dfeec247 | 714 | } |
13cf67c4 XL |
715 | } |
716 | } | |
717 | } | |
718 | ||
719 | /// Miscellaneous bits of metadata for better ambiguity error reporting. | |
720 | #[derive(Clone, Copy, PartialEq)] | |
721 | enum AmbiguityErrorMisc { | |
722 | SuggestCrate, | |
723 | SuggestSelf, | |
724 | FromPrelude, | |
725 | None, | |
726 | } | |
727 | ||
9e0c209e | 728 | struct AmbiguityError<'a> { |
13cf67c4 | 729 | kind: AmbiguityKind, |
b7449926 | 730 | ident: Ident, |
9e0c209e SL |
731 | b1: &'a NameBinding<'a>, |
732 | b2: &'a NameBinding<'a>, | |
13cf67c4 XL |
733 | misc1: AmbiguityErrorMisc, |
734 | misc2: AmbiguityErrorMisc, | |
9e0c209e SL |
735 | } |
736 | ||
7453a54e | 737 | impl<'a> NameBinding<'a> { |
476ff2be | 738 | fn module(&self) -> Option<Module<'a>> { |
7453a54e | 739 | match self.kind { |
476ff2be | 740 | NameBindingKind::Module(module) => Some(module), |
7453a54e | 741 | NameBindingKind::Import { binding, .. } => binding.module(), |
476ff2be | 742 | _ => None, |
1a4d82fc JJ |
743 | } |
744 | } | |
745 | ||
48663c56 | 746 | fn res(&self) -> Res { |
7453a54e | 747 | match self.kind { |
48663c56 XL |
748 | NameBindingKind::Res(res, _) => res, |
749 | NameBindingKind::Module(module) => module.res().unwrap(), | |
750 | NameBindingKind::Import { binding, .. } => binding.res(), | |
1a4d82fc JJ |
751 | } |
752 | } | |
1a4d82fc | 753 | |
0731742a | 754 | fn is_ambiguity(&self) -> bool { |
dfeec247 XL |
755 | self.ambiguity.is_some() |
756 | || match self.kind { | |
757 | NameBindingKind::Import { binding, .. } => binding.is_ambiguity(), | |
758 | _ => false, | |
759 | } | |
476ff2be SL |
760 | } |
761 | ||
f035d41b XL |
762 | fn is_possibly_imported_variant(&self) -> bool { |
763 | match self.kind { | |
764 | NameBindingKind::Import { binding, .. } => binding.is_possibly_imported_variant(), | |
5869c6ff | 765 | NameBindingKind::Res( |
ba9703b0 XL |
766 | Res::Def(DefKind::Variant | DefKind::Ctor(CtorOf::Variant, ..), _), |
767 | _, | |
6a06907d XL |
768 | ) => true, |
769 | NameBindingKind::Res(..) | NameBindingKind::Module(..) => false, | |
770 | } | |
92a42be0 SL |
771 | } |
772 | ||
7453a54e | 773 | fn is_extern_crate(&self) -> bool { |
476ff2be SL |
774 | match self.kind { |
775 | NameBindingKind::Import { | |
74b04a01 | 776 | import: &Import { kind: ImportKind::ExternCrate { .. }, .. }, |
dfeec247 | 777 | .. |
476ff2be | 778 | } => true, |
dfeec247 XL |
779 | NameBindingKind::Module(&ModuleData { |
780 | kind: ModuleKind::Def(DefKind::Mod, def_id, _), | |
781 | .. | |
04454e1e | 782 | }) => def_id.is_crate_root(), |
476ff2be SL |
783 | _ => false, |
784 | } | |
92a42be0 | 785 | } |
92a42be0 | 786 | |
7453a54e | 787 | fn is_import(&self) -> bool { |
29967ef6 | 788 | matches!(self.kind, NameBindingKind::Import { .. }) |
c34b1796 | 789 | } |
a7813a04 XL |
790 | |
791 | fn is_glob_import(&self) -> bool { | |
792 | match self.kind { | |
74b04a01 | 793 | NameBindingKind::Import { import, .. } => import.is_glob(), |
a7813a04 XL |
794 | _ => false, |
795 | } | |
796 | } | |
797 | ||
798 | fn is_importable(&self) -> bool { | |
29967ef6 XL |
799 | !matches!( |
800 | self.res(), | |
801 | Res::Def(DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy, _) | |
802 | ) | |
a7813a04 | 803 | } |
7cac9316 | 804 | |
b7449926 | 805 | fn macro_kind(&self) -> Option<MacroKind> { |
416331ca | 806 | self.res().macro_kind() |
13cf67c4 XL |
807 | } |
808 | ||
b7449926 XL |
809 | // Suppose that we resolved macro invocation with `invoc_parent_expansion` to binding `binding` |
810 | // at some expansion round `max(invoc, binding)` when they both emerged from macros. | |
811 | // Then this function returns `true` if `self` may emerge from a macro *after* that | |
812 | // in some later round and screw up our previously found resolution. | |
813 | // See more detailed explanation in | |
814 | // https://github.com/rust-lang/rust/pull/53778#issuecomment-419224049 | |
136023e0 XL |
815 | fn may_appear_after( |
816 | &self, | |
817 | invoc_parent_expansion: LocalExpnId, | |
818 | binding: &NameBinding<'_>, | |
819 | ) -> bool { | |
b7449926 XL |
820 | // self > max(invoc, binding) => !(self <= invoc || self <= binding) |
821 | // Expansions are partially ordered, so "may appear after" is an inversion of | |
822 | // "certainly appears before or simultaneously" and includes unordered cases. | |
823 | let self_parent_expansion = self.expansion; | |
824 | let other_parent_expansion = binding.expansion; | |
825 | let certainly_before_other_or_simultaneously = | |
826 | other_parent_expansion.is_descendant_of(self_parent_expansion); | |
827 | let certainly_before_invoc_or_simultaneously = | |
828 | invoc_parent_expansion.is_descendant_of(self_parent_expansion); | |
829 | !(certainly_before_other_or_simultaneously || certainly_before_invoc_or_simultaneously) | |
830 | } | |
1a4d82fc JJ |
831 | } |
832 | ||
f2b60f7d | 833 | #[derive(Default, Clone)] |
0bf4aa26 XL |
834 | pub struct ExternPreludeEntry<'a> { |
835 | extern_crate_item: Option<&'a NameBinding<'a>>, | |
836 | pub introduced_by_item: bool, | |
837 | } | |
838 | ||
1b1a35ee XL |
839 | /// Used for better errors for E0773 |
840 | enum BuiltinMacroState { | |
5869c6ff | 841 | NotYetSeen(SyntaxExtensionKind), |
1b1a35ee XL |
842 | AlreadySeen(Span), |
843 | } | |
844 | ||
cdc7bbd5 XL |
845 | struct DeriveData { |
846 | resolutions: DeriveResolutions, | |
847 | helper_attrs: Vec<(usize, Ident)>, | |
848 | has_derive_copy: bool, | |
849 | } | |
850 | ||
923072b8 FG |
851 | #[derive(Clone)] |
852 | struct MacroData { | |
853 | ext: Lrc<SyntaxExtension>, | |
854 | macro_rules: bool, | |
855 | } | |
856 | ||
1a4d82fc | 857 | /// The main resolver class. |
0531ce1d XL |
858 | /// |
859 | /// This is the visitor that walks the whole crate. | |
0731742a | 860 | pub struct Resolver<'a> { |
1a4d82fc JJ |
861 | session: &'a Session, |
862 | ||
e74abb32 | 863 | definitions: Definitions, |
923072b8 FG |
864 | /// Item with a given `LocalDefId` was defined during macro expansion with ID `ExpnId`. |
865 | expn_that_defined: FxHashMap<LocalDefId, ExpnId>, | |
866 | /// Reference span for definitions. | |
867 | source_span: IndexVec<LocalDefId, Span>, | |
1a4d82fc | 868 | |
e74abb32 | 869 | graph_root: Module<'a>, |
1a4d82fc | 870 | |
3157f602 | 871 | prelude: Option<Module<'a>>, |
e74abb32 | 872 | extern_prelude: FxHashMap<Ident, ExternPreludeEntry<'a>>, |
3157f602 | 873 | |
9fa01778 | 874 | /// N.B., this is used only for better diagnostics, not name resolution itself. |
7cac9316 | 875 | has_self: FxHashSet<DefId>, |
1a4d82fc | 876 | |
83c7162d XL |
877 | /// Names of fields of an item `DefId` accessible with dot syntax. |
878 | /// Used for hints during error reporting. | |
f9f354fc | 879 | field_names: FxHashMap<DefId, Vec<Spanned<Symbol>>>, |
1a4d82fc | 880 | |
83c7162d | 881 | /// All imports known to succeed or fail. |
74b04a01 | 882 | determined_imports: Vec<&'a Import<'a>>, |
9e0c209e | 883 | |
83c7162d | 884 | /// All non-determined imports. |
74b04a01 | 885 | indeterminate_imports: Vec<&'a Import<'a>>, |
1a4d82fc | 886 | |
cdc7bbd5 XL |
887 | // Spans for local variables found during pattern resolution. |
888 | // Used for suggestions during error reporting. | |
889 | pat_span_map: NodeMap<Span>, | |
890 | ||
48663c56 XL |
891 | /// Resolutions for nodes that have a single resolution. |
892 | partial_res_map: NodeMap<PartialRes>, | |
893 | /// Resolutions for import nodes, which have multiple resolutions in different namespaces. | |
894 | import_res_map: NodeMap<PerNS<Option<Res>>>, | |
895 | /// Resolutions for labels (node IDs of their corresponding blocks or loops). | |
896 | label_res_map: NodeMap<NodeId>, | |
04454e1e FG |
897 | /// Resolutions for lifetimes. |
898 | lifetimes_res_map: NodeMap<LifetimeRes>, | |
899 | /// Lifetime parameters that lowering will have to introduce. | |
900 | extra_lifetime_params_map: NodeMap<Vec<(Ident, NodeId, LifetimeRes)>>, | |
48663c56 | 901 | |
e74abb32 | 902 | /// `CrateNum` resolutions of `extern crate` items. |
f9f354fc | 903 | extern_crate_map: FxHashMap<LocalDefId, CrateNum>, |
5099ac24 | 904 | reexport_map: FxHashMap<LocalDefId, Vec<ModChild>>, |
3c0e092e | 905 | trait_map: NodeMap<Vec<TraitCandidate>>, |
a7813a04 | 906 | |
83c7162d XL |
907 | /// A map from nodes to anonymous modules. |
908 | /// Anonymous modules are pseudo-modules that are implicitly created around items | |
909 | /// contained within blocks. | |
910 | /// | |
911 | /// For example, if we have this: | |
912 | /// | |
913 | /// fn f() { | |
914 | /// fn g() { | |
915 | /// ... | |
916 | /// } | |
917 | /// } | |
918 | /// | |
919 | /// There will be an anonymous module created around `g` with the ID of the | |
920 | /// entry block for `f`. | |
32a655c1 | 921 | block_map: NodeMap<Module<'a>>, |
e1599b0c XL |
922 | /// A fake module that contains no definition and no prelude. Used so that |
923 | /// some AST passes can generate identifiers that only resolve to local or | |
924 | /// language items. | |
925 | empty_module: Module<'a>, | |
c295e0f8 | 926 | module_map: FxHashMap<DefId, Module<'a>>, |
5099ac24 | 927 | binding_parent_modules: FxHashMap<Interned<'a, NameBinding<'a>>, Module<'a>>, |
e74abb32 | 928 | underscore_disambiguator: u32, |
1a4d82fc | 929 | |
9fa01778 | 930 | /// Maps glob imports to the names of items actually imported. |
f9f354fc | 931 | glob_map: FxHashMap<LocalDefId, FxHashSet<Symbol>>, |
29967ef6 XL |
932 | /// Visibilities in "lowered" form, for all entities that have them. |
933 | visibilities: FxHashMap<LocalDefId, ty::Visibility>, | |
04454e1e | 934 | has_pub_restricted: bool, |
94222f64 | 935 | used_imports: FxHashSet<NodeId>, |
923072b8 | 936 | maybe_unused_trait_imports: FxIndexSet<LocalDefId>, |
f9f354fc | 937 | maybe_unused_extern_crates: Vec<(LocalDefId, Span)>, |
e9174d1e | 938 | |
9fa01778 | 939 | /// Privacy errors are delayed until the end in order to deduplicate them. |
54a0048b | 940 | privacy_errors: Vec<PrivacyError<'a>>, |
9fa01778 | 941 | /// Ambiguity errors are delayed for deduplication. |
9e0c209e | 942 | ambiguity_errors: Vec<AmbiguityError<'a>>, |
9fa01778 | 943 | /// `use` injections are delayed for better placement and deduplication. |
3b2f2976 | 944 | use_injections: Vec<UseError<'a>>, |
9fa01778 | 945 | /// Crate-local macro expanded `macro_export` referred to by a module-relative path. |
b7449926 | 946 | macro_expanded_macro_export_errors: BTreeSet<(Span, Span)>, |
9cc50fc6 SL |
947 | |
948 | arenas: &'a ResolverArenas<'a>, | |
9e0c209e | 949 | dummy_binding: &'a NameBinding<'a>, |
9e0c209e | 950 | |
e74abb32 | 951 | crate_loader: CrateLoader<'a>, |
7cac9316 | 952 | macro_names: FxHashSet<Ident>, |
1b1a35ee | 953 | builtin_macros: FxHashMap<Symbol, BuiltinMacroState>, |
5e7ed085 FG |
954 | /// A small map keeping true kinds of built-in macros that appear to be fn-like on |
955 | /// the surface (`macro` items in libcore), but are actually attributes or derives. | |
956 | builtin_macro_kinds: FxHashMap<LocalDefId, MacroKind>, | |
5099ac24 | 957 | registered_tools: RegisteredTools, |
f9f354fc | 958 | macro_use_prelude: FxHashMap<Symbol, &'a NameBinding<'a>>, |
923072b8 | 959 | macro_map: FxHashMap<DefId, MacroData>, |
416331ca XL |
960 | dummy_ext_bang: Lrc<SyntaxExtension>, |
961 | dummy_ext_derive: Lrc<SyntaxExtension>, | |
94222f64 | 962 | non_macro_attr: Lrc<SyntaxExtension>, |
f9f354fc | 963 | local_macro_def_scopes: FxHashMap<LocalDefId, Module<'a>>, |
136023e0 | 964 | ast_transform_scopes: FxHashMap<LocalExpnId, Module<'a>>, |
3c0e092e | 965 | unused_macros: FxHashMap<LocalDefId, (NodeId, Ident)>, |
04454e1e | 966 | unused_macro_rules: FxHashMap<(LocalDefId, usize), (Ident, Span)>, |
f9f354fc | 967 | proc_macro_stubs: FxHashSet<LocalDefId>, |
e1599b0c | 968 | /// Traces collected during macro resolution and validated when it's complete. |
dfeec247 XL |
969 | single_segment_macro_resolutions: |
970 | Vec<(Ident, MacroKind, ParentScope<'a>, Option<&'a NameBinding<'a>>)>, | |
971 | multi_segment_macro_resolutions: | |
972 | Vec<(Vec<Segment>, Span, MacroKind, ParentScope<'a>, Option<Res>)>, | |
e1599b0c | 973 | builtin_attrs: Vec<(Ident, ParentScope<'a>)>, |
60c5eb7d | 974 | /// `derive(Copy)` marks items they are applied to so they are treated specially later. |
416331ca XL |
975 | /// Derive macros cannot modify the item themselves and have to store the markers in the global |
976 | /// context, so they attach the markers to derive container IDs using this resolver table. | |
136023e0 | 977 | containers_deriving_copy: FxHashSet<LocalExpnId>, |
e1599b0c XL |
978 | /// Parent scopes in which the macros were invoked. |
979 | /// FIXME: `derives` are missing in these parent scopes and need to be taken from elsewhere. | |
136023e0 | 980 | invocation_parent_scopes: FxHashMap<LocalExpnId, ParentScope<'a>>, |
ba9703b0 | 981 | /// `macro_rules` scopes *produced* by expanding the macro invocations, |
e1599b0c | 982 | /// include all the `macro_rules` items and other invocations generated by them. |
136023e0 | 983 | output_macro_rules_scopes: FxHashMap<LocalExpnId, MacroRulesScopeRef<'a>>, |
04454e1e FG |
984 | /// `macro_rules` scopes produced by `macro_rules` item definitions. |
985 | macro_rules_scopes: FxHashMap<LocalDefId, MacroRulesScopeRef<'a>>, | |
60c5eb7d | 986 | /// Helper attributes that are in scope for the given expansion. |
136023e0 | 987 | helper_attrs: FxHashMap<LocalExpnId, Vec<Ident>>, |
cdc7bbd5 XL |
988 | /// Ready or in-progress results of resolving paths inside the `#[derive(...)]` attribute |
989 | /// with the given `ExpnId`. | |
136023e0 | 990 | derive_data: FxHashMap<LocalExpnId, DeriveData>, |
476ff2be | 991 | |
83c7162d | 992 | /// Avoid duplicated errors for "name already defined". |
f9f354fc | 993 | name_already_seen: FxHashMap<Symbol, Span>, |
32a655c1 | 994 | |
74b04a01 | 995 | potentially_unused_imports: Vec<&'a Import<'a>>, |
8bb4bdeb | 996 | |
9fa01778 | 997 | /// Table for mapping struct IDs into struct constructor IDs, |
83c7162d | 998 | /// it's not used during normal resolution, only for better error reporting. |
1b1a35ee | 999 | /// Also includes of list of each fields visibility |
f2b60f7d | 1000 | struct_constructors: DefIdMap<(Res, ty::Visibility<DefId>, Vec<ty::Visibility<DefId>>)>, |
3b2f2976 | 1001 | |
416331ca | 1002 | /// Features enabled for this crate. |
f9f354fc | 1003 | active_features: FxHashSet<Symbol>, |
e1599b0c | 1004 | |
dfeec247 | 1005 | lint_buffer: LintBuffer, |
60c5eb7d XL |
1006 | |
1007 | next_node_id: NodeId, | |
f035d41b | 1008 | |
f035d41b XL |
1009 | node_id_to_def_id: FxHashMap<ast::NodeId, LocalDefId>, |
1010 | def_id_to_node_id: IndexVec<LocalDefId, ast::NodeId>, | |
1011 | ||
1012 | /// Indices of unnamed struct or variant fields with unresolved attributes. | |
1013 | placeholder_field_indices: FxHashMap<NodeId, usize>, | |
1014 | /// When collecting definitions from an AST fragment produced by a macro invocation `ExpnId` | |
6a06907d XL |
1015 | /// we know what parent node that fragment should be attached to thanks to this table, |
1016 | /// and how the `impl Trait` fragments were introduced. | |
136023e0 | 1017 | invocation_parents: FxHashMap<LocalExpnId, (LocalDefId, ImplTraitContext)>, |
f035d41b | 1018 | |
29967ef6 XL |
1019 | /// Some way to know that we are in a *trait* impl in `visit_assoc_item`. |
1020 | /// FIXME: Replace with a more general AST map (together with some other fields). | |
1021 | trait_impl_items: FxHashSet<LocalDefId>, | |
6a06907d XL |
1022 | |
1023 | legacy_const_generic_args: FxHashMap<DefId, Option<Vec<usize>>>, | |
94222f64 XL |
1024 | /// Amount of lifetime parameters for each item in the crate. |
1025 | item_generics_num_lifetimes: FxHashMap<LocalDefId, usize>, | |
cdc7bbd5 XL |
1026 | |
1027 | main_def: Option<MainDefinition>, | |
5099ac24 | 1028 | trait_impls: FxIndexMap<DefId, Vec<LocalDefId>>, |
94222f64 XL |
1029 | /// A list of proc macro LocalDefIds, written out in the order in which |
1030 | /// they are declared in the static array generated by proc_macro_harness. | |
1031 | proc_macros: Vec<NodeId>, | |
c295e0f8 | 1032 | confused_type_with_std_module: FxHashMap<Span, Span>, |
5099ac24 | 1033 | |
2b03887a | 1034 | effective_visibilities: EffectiveVisibilities, |
9cc50fc6 SL |
1035 | } |
1036 | ||
9fa01778 | 1037 | /// Nothing really interesting here; it just provides memory for the rest of the crate. |
0bf4aa26 | 1038 | #[derive(Default)] |
3157f602 | 1039 | pub struct ResolverArenas<'a> { |
f035d41b | 1040 | modules: TypedArena<ModuleData<'a>>, |
a7813a04 | 1041 | local_modules: RefCell<Vec<Module<'a>>>, |
f035d41b XL |
1042 | imports: TypedArena<Import<'a>>, |
1043 | name_resolutions: TypedArena<RefCell<NameResolution<'a>>>, | |
f035d41b | 1044 | ast_paths: TypedArena<ast::Path>, |
29967ef6 | 1045 | dropless: DroplessArena, |
54a0048b SL |
1046 | } |
1047 | ||
1048 | impl<'a> ResolverArenas<'a> { | |
c295e0f8 XL |
1049 | fn new_module( |
1050 | &'a self, | |
1051 | parent: Option<Module<'a>>, | |
1052 | kind: ModuleKind, | |
1053 | expn_id: ExpnId, | |
1054 | span: Span, | |
1055 | no_implicit_prelude: bool, | |
1056 | module_map: &mut FxHashMap<DefId, Module<'a>>, | |
1057 | ) -> Module<'a> { | |
1058 | let module = | |
1059 | self.modules.alloc(ModuleData::new(parent, kind, expn_id, span, no_implicit_prelude)); | |
1060 | let def_id = module.opt_def_id(); | |
1061 | if def_id.map_or(true, |def_id| def_id.is_local()) { | |
a7813a04 XL |
1062 | self.local_modules.borrow_mut().push(module); |
1063 | } | |
c295e0f8 XL |
1064 | if let Some(def_id) = def_id { |
1065 | module_map.insert(def_id, module); | |
1066 | } | |
a7813a04 XL |
1067 | module |
1068 | } | |
9fa01778 | 1069 | fn local_modules(&'a self) -> std::cell::Ref<'a, Vec<Module<'a>>> { |
a7813a04 | 1070 | self.local_modules.borrow() |
54a0048b SL |
1071 | } |
1072 | fn alloc_name_binding(&'a self, name_binding: NameBinding<'a>) -> &'a NameBinding<'a> { | |
29967ef6 | 1073 | self.dropless.alloc(name_binding) |
54a0048b | 1074 | } |
74b04a01 XL |
1075 | fn alloc_import(&'a self, import: Import<'a>) -> &'a Import<'_> { |
1076 | self.imports.alloc(import) | |
54a0048b SL |
1077 | } |
1078 | fn alloc_name_resolution(&'a self) -> &'a RefCell<NameResolution<'a>> { | |
1079 | self.name_resolutions.alloc(Default::default()) | |
1080 | } | |
29967ef6 | 1081 | fn alloc_macro_rules_scope(&'a self, scope: MacroRulesScope<'a>) -> MacroRulesScopeRef<'a> { |
5099ac24 | 1082 | Interned::new_unchecked(self.dropless.alloc(Cell::new(scope))) |
29967ef6 | 1083 | } |
ba9703b0 XL |
1084 | fn alloc_macro_rules_binding( |
1085 | &'a self, | |
1086 | binding: MacroRulesBinding<'a>, | |
1087 | ) -> &'a MacroRulesBinding<'a> { | |
29967ef6 | 1088 | self.dropless.alloc(binding) |
c30ab7b3 | 1089 | } |
e1599b0c XL |
1090 | fn alloc_ast_paths(&'a self, paths: &[ast::Path]) -> &'a [ast::Path] { |
1091 | self.ast_paths.alloc_from_iter(paths.iter().cloned()) | |
1092 | } | |
1b1a35ee | 1093 | fn alloc_pattern_spans(&'a self, spans: impl Iterator<Item = Span>) -> &'a [Span] { |
29967ef6 | 1094 | self.dropless.alloc_from_iter(spans) |
1b1a35ee | 1095 | } |
e1599b0c XL |
1096 | } |
1097 | ||
1098 | impl<'a> AsMut<Resolver<'a>> for Resolver<'a> { | |
dfeec247 XL |
1099 | fn as_mut(&mut self) -> &mut Resolver<'a> { |
1100 | self | |
1101 | } | |
1a4d82fc JJ |
1102 | } |
1103 | ||
e74abb32 | 1104 | impl<'a, 'b> DefIdTree for &'a Resolver<'b> { |
04454e1e FG |
1105 | #[inline] |
1106 | fn opt_parent(self, id: DefId) -> Option<DefId> { | |
ba9703b0 XL |
1107 | match id.as_local() { |
1108 | Some(id) => self.definitions.def_key(id).parent, | |
1109 | None => self.cstore().def_key(id).parent, | |
dfeec247 XL |
1110 | } |
1111 | .map(|index| DefId { index, ..id }) | |
a7813a04 | 1112 | } |
1a4d82fc JJ |
1113 | } |
1114 | ||
923072b8 | 1115 | impl Resolver<'_> { |
f035d41b XL |
1116 | fn opt_local_def_id(&self, node: NodeId) -> Option<LocalDefId> { |
1117 | self.node_id_to_def_id.get(&node).copied() | |
1118 | } | |
1119 | ||
923072b8 | 1120 | pub fn local_def_id(&self, node: NodeId) -> LocalDefId { |
f035d41b XL |
1121 | self.opt_local_def_id(node).unwrap_or_else(|| panic!("no entry for node id: `{:?}`", node)) |
1122 | } | |
1123 | ||
1124 | /// Adds a definition with a parent definition. | |
1125 | fn create_def( | |
1126 | &mut self, | |
1127 | parent: LocalDefId, | |
1128 | node_id: ast::NodeId, | |
1129 | data: DefPathData, | |
1130 | expn_id: ExpnId, | |
1131 | span: Span, | |
1132 | ) -> LocalDefId { | |
1133 | assert!( | |
1134 | !self.node_id_to_def_id.contains_key(&node_id), | |
1135 | "adding a def'n for node-id {:?} and data {:?} but a previous def'n exists: {:?}", | |
1136 | node_id, | |
1137 | data, | |
1138 | self.definitions.def_key(self.node_id_to_def_id[&node_id]), | |
1139 | ); | |
1140 | ||
923072b8 FG |
1141 | let def_id = self.definitions.create_def(parent, data); |
1142 | ||
1143 | // Create the definition. | |
1144 | if expn_id != ExpnId::root() { | |
1145 | self.expn_that_defined.insert(def_id, expn_id); | |
1146 | } | |
1147 | ||
1148 | // A relative span's parent must be an absolute span. | |
1149 | debug_assert_eq!(span.data_untracked().parent, None); | |
1150 | let _id = self.source_span.push(span); | |
1151 | debug_assert_eq!(_id, def_id); | |
f035d41b XL |
1152 | |
1153 | // Some things for which we allocate `LocalDefId`s don't correspond to | |
1154 | // anything in the AST, so they don't have a `NodeId`. For these cases | |
1155 | // we don't need a mapping from `NodeId` to `LocalDefId`. | |
1156 | if node_id != ast::DUMMY_NODE_ID { | |
1157 | debug!("create_def: def_id_to_node_id[{:?}] <-> {:?}", def_id, node_id); | |
1158 | self.node_id_to_def_id.insert(node_id, def_id); | |
1159 | } | |
1160 | assert_eq!(self.def_id_to_node_id.push(node_id), def_id); | |
1161 | ||
1162 | def_id | |
1163 | } | |
5e7ed085 | 1164 | |
923072b8 FG |
1165 | fn item_generics_num_lifetimes(&self, def_id: DefId) -> usize { |
1166 | if let Some(def_id) = def_id.as_local() { | |
1167 | self.item_generics_num_lifetimes[&def_id] | |
1168 | } else { | |
1169 | self.cstore().item_generics_num_lifetimes(def_id, self.session) | |
1170 | } | |
5e7ed085 | 1171 | } |
a7813a04 XL |
1172 | } |
1173 | ||
0731742a | 1174 | impl<'a> Resolver<'a> { |
dfeec247 XL |
1175 | pub fn new( |
1176 | session: &'a Session, | |
1177 | krate: &Crate, | |
1178 | crate_name: &str, | |
17df50a5 | 1179 | metadata_loader: Box<MetadataLoaderDyn>, |
dfeec247 XL |
1180 | arenas: &'a ResolverArenas<'a>, |
1181 | ) -> Resolver<'a> { | |
c295e0f8 | 1182 | let root_def_id = CRATE_DEF_ID.to_def_id(); |
0bf4aa26 | 1183 | let mut module_map = FxHashMap::default(); |
c295e0f8 XL |
1184 | let graph_root = arenas.new_module( |
1185 | None, | |
1186 | ModuleKind::Def(DefKind::Mod, root_def_id, kw::Empty), | |
1187 | ExpnId::root(), | |
5e7ed085 | 1188 | krate.spans.inner_span, |
c295e0f8 XL |
1189 | session.contains_name(&krate.attrs, sym::no_implicit_prelude), |
1190 | &mut module_map, | |
1191 | ); | |
1192 | let empty_module = arenas.new_module( | |
1193 | None, | |
1194 | ModuleKind::Def(DefKind::Mod, root_def_id, kw::Empty), | |
1195 | ExpnId::root(), | |
1196 | DUMMY_SP, | |
1197 | true, | |
1198 | &mut FxHashMap::default(), | |
1199 | ); | |
1a4d82fc | 1200 | |
923072b8 | 1201 | let definitions = Definitions::new(session.local_stable_crate_id()); |
f035d41b | 1202 | |
29967ef6 | 1203 | let mut visibilities = FxHashMap::default(); |
c295e0f8 | 1204 | visibilities.insert(CRATE_DEF_ID, ty::Visibility::Public); |
29967ef6 | 1205 | |
f035d41b | 1206 | let mut def_id_to_node_id = IndexVec::default(); |
04454e1e | 1207 | assert_eq!(def_id_to_node_id.push(CRATE_NODE_ID), CRATE_DEF_ID); |
f035d41b | 1208 | let mut node_id_to_def_id = FxHashMap::default(); |
04454e1e | 1209 | node_id_to_def_id.insert(CRATE_NODE_ID, CRATE_DEF_ID); |
f035d41b XL |
1210 | |
1211 | let mut invocation_parents = FxHashMap::default(); | |
04454e1e | 1212 | invocation_parents.insert(LocalExpnId::ROOT, (CRATE_DEF_ID, ImplTraitContext::Existential)); |
c30ab7b3 | 1213 | |
923072b8 FG |
1214 | let mut source_span = IndexVec::default(); |
1215 | let _id = source_span.push(krate.spans.inner_span); | |
1216 | debug_assert_eq!(_id, CRATE_DEF_ID); | |
1217 | ||
dfeec247 XL |
1218 | let mut extern_prelude: FxHashMap<Ident, ExternPreludeEntry<'_>> = session |
1219 | .opts | |
1220 | .externs | |
1221 | .iter() | |
1222 | .filter(|(_, entry)| entry.add_prelude) | |
1223 | .map(|(name, _)| (Ident::from_str(name), Default::default())) | |
1224 | .collect(); | |
b7449926 | 1225 | |
3dfed10e | 1226 | if !session.contains_name(&krate.attrs, sym::no_core) { |
e1599b0c | 1227 | extern_prelude.insert(Ident::with_dummy_span(sym::core), Default::default()); |
3dfed10e | 1228 | if !session.contains_name(&krate.attrs, sym::no_std) { |
e1599b0c | 1229 | extern_prelude.insert(Ident::with_dummy_span(sym::std), Default::default()); |
4462d4a0 XL |
1230 | } |
1231 | } | |
94b46f34 | 1232 | |
f2b60f7d | 1233 | let registered_tools = macros::registered_tools(session, &krate.attrs); |
60c5eb7d | 1234 | |
416331ca | 1235 | let features = session.features_untracked(); |
dc9dc135 | 1236 | |
29967ef6 | 1237 | let mut resolver = Resolver { |
3b2f2976 | 1238 | session, |
1a4d82fc | 1239 | |
3b2f2976 | 1240 | definitions, |
923072b8 FG |
1241 | expn_that_defined: Default::default(), |
1242 | source_span, | |
1a4d82fc JJ |
1243 | |
1244 | // The outermost module has def ID 0; this is not reflected in the | |
1245 | // AST. | |
3b2f2976 | 1246 | graph_root, |
3157f602 | 1247 | prelude: None, |
94b46f34 | 1248 | extern_prelude, |
1a4d82fc | 1249 | |
0bf4aa26 XL |
1250 | has_self: FxHashSet::default(), |
1251 | field_names: FxHashMap::default(), | |
1a4d82fc | 1252 | |
9e0c209e SL |
1253 | determined_imports: Vec::new(), |
1254 | indeterminate_imports: Vec::new(), | |
1a4d82fc | 1255 | |
cdc7bbd5 | 1256 | pat_span_map: Default::default(), |
48663c56 XL |
1257 | partial_res_map: Default::default(), |
1258 | import_res_map: Default::default(), | |
1259 | label_res_map: Default::default(), | |
04454e1e FG |
1260 | lifetimes_res_map: Default::default(), |
1261 | extra_lifetime_params_map: Default::default(), | |
e74abb32 | 1262 | extern_crate_map: Default::default(), |
5099ac24 | 1263 | reexport_map: FxHashMap::default(), |
3c0e092e | 1264 | trait_map: NodeMap::default(), |
e74abb32 | 1265 | underscore_disambiguator: 0, |
e1599b0c | 1266 | empty_module, |
3b2f2976 | 1267 | module_map, |
a1dfa0c6 | 1268 | block_map: Default::default(), |
0bf4aa26 | 1269 | binding_parent_modules: FxHashMap::default(), |
e1599b0c | 1270 | ast_transform_scopes: FxHashMap::default(), |
1a4d82fc | 1271 | |
a1dfa0c6 | 1272 | glob_map: Default::default(), |
29967ef6 | 1273 | visibilities, |
04454e1e | 1274 | has_pub_restricted: false, |
0bf4aa26 | 1275 | used_imports: FxHashSet::default(), |
a1dfa0c6 | 1276 | maybe_unused_trait_imports: Default::default(), |
3b2f2976 | 1277 | maybe_unused_extern_crates: Vec::new(), |
a7813a04 | 1278 | |
54a0048b | 1279 | privacy_errors: Vec::new(), |
9e0c209e | 1280 | ambiguity_errors: Vec::new(), |
3b2f2976 | 1281 | use_injections: Vec::new(), |
b7449926 | 1282 | macro_expanded_macro_export_errors: BTreeSet::new(), |
9cc50fc6 | 1283 | |
3b2f2976 | 1284 | arenas, |
9e0c209e | 1285 | dummy_binding: arenas.alloc_name_binding(NameBinding { |
48663c56 | 1286 | kind: NameBindingKind::Res(Res::Err, false), |
0731742a | 1287 | ambiguity: None, |
136023e0 | 1288 | expansion: LocalExpnId::ROOT, |
9e0c209e SL |
1289 | span: DUMMY_SP, |
1290 | vis: ty::Visibility::Public, | |
1291 | }), | |
32a655c1 | 1292 | |
e74abb32 | 1293 | crate_loader: CrateLoader::new(session, metadata_loader, crate_name), |
0bf4aa26 | 1294 | macro_names: FxHashSet::default(), |
416331ca | 1295 | builtin_macros: Default::default(), |
5e7ed085 | 1296 | builtin_macro_kinds: Default::default(), |
60c5eb7d | 1297 | registered_tools, |
0bf4aa26 | 1298 | macro_use_prelude: FxHashMap::default(), |
0bf4aa26 | 1299 | macro_map: FxHashMap::default(), |
416331ca XL |
1300 | dummy_ext_bang: Lrc::new(SyntaxExtension::dummy_bang(session.edition())), |
1301 | dummy_ext_derive: Lrc::new(SyntaxExtension::dummy_derive(session.edition())), | |
94222f64 | 1302 | non_macro_attr: Lrc::new(SyntaxExtension::non_macro_attr(session.edition())), |
29967ef6 | 1303 | invocation_parent_scopes: Default::default(), |
ba9703b0 | 1304 | output_macro_rules_scopes: Default::default(), |
04454e1e | 1305 | macro_rules_scopes: Default::default(), |
60c5eb7d | 1306 | helper_attrs: Default::default(), |
cdc7bbd5 | 1307 | derive_data: Default::default(), |
0bf4aa26 XL |
1308 | local_macro_def_scopes: FxHashMap::default(), |
1309 | name_already_seen: FxHashMap::default(), | |
32a655c1 | 1310 | potentially_unused_imports: Vec::new(), |
a1dfa0c6 | 1311 | struct_constructors: Default::default(), |
416331ca | 1312 | unused_macros: Default::default(), |
04454e1e | 1313 | unused_macro_rules: Default::default(), |
416331ca | 1314 | proc_macro_stubs: Default::default(), |
e1599b0c XL |
1315 | single_segment_macro_resolutions: Default::default(), |
1316 | multi_segment_macro_resolutions: Default::default(), | |
1317 | builtin_attrs: Default::default(), | |
60c5eb7d | 1318 | containers_deriving_copy: Default::default(), |
dfeec247 XL |
1319 | active_features: features |
1320 | .declared_lib_features | |
1321 | .iter() | |
1322 | .map(|(feat, ..)| *feat) | |
1323 | .chain(features.declared_lang_features.iter().map(|(feat, ..)| *feat)) | |
1324 | .collect(), | |
dfeec247 | 1325 | lint_buffer: LintBuffer::default(), |
a2a8927a | 1326 | next_node_id: CRATE_NODE_ID, |
f035d41b XL |
1327 | node_id_to_def_id, |
1328 | def_id_to_node_id, | |
1329 | placeholder_field_indices: Default::default(), | |
1330 | invocation_parents, | |
29967ef6 | 1331 | trait_impl_items: Default::default(), |
6a06907d | 1332 | legacy_const_generic_args: Default::default(), |
94222f64 | 1333 | item_generics_num_lifetimes: Default::default(), |
cdc7bbd5 | 1334 | main_def: Default::default(), |
94222f64 XL |
1335 | trait_impls: Default::default(), |
1336 | proc_macros: Default::default(), | |
c295e0f8 | 1337 | confused_type_with_std_module: Default::default(), |
2b03887a | 1338 | effective_visibilities: Default::default(), |
29967ef6 XL |
1339 | }; |
1340 | ||
1341 | let root_parent_scope = ParentScope::module(graph_root, &resolver); | |
136023e0 | 1342 | resolver.invocation_parent_scopes.insert(LocalExpnId::ROOT, root_parent_scope); |
29967ef6 XL |
1343 | |
1344 | resolver | |
9cc50fc6 SL |
1345 | } |
1346 | ||
c295e0f8 XL |
1347 | fn new_module( |
1348 | &mut self, | |
1349 | parent: Option<Module<'a>>, | |
1350 | kind: ModuleKind, | |
1351 | expn_id: ExpnId, | |
1352 | span: Span, | |
1353 | no_implicit_prelude: bool, | |
1354 | ) -> Module<'a> { | |
1355 | let module_map = &mut self.module_map; | |
1356 | self.arenas.new_module(parent, kind, expn_id, span, no_implicit_prelude, module_map) | |
1357 | } | |
1358 | ||
60c5eb7d | 1359 | pub fn next_node_id(&mut self) -> NodeId { |
04454e1e FG |
1360 | let start = self.next_node_id; |
1361 | let next = start.as_u32().checked_add(1).expect("input too large; ran out of NodeIds"); | |
1362 | self.next_node_id = ast::NodeId::from_u32(next); | |
1363 | start | |
1364 | } | |
1365 | ||
1366 | pub fn next_node_ids(&mut self, count: usize) -> std::ops::Range<NodeId> { | |
1367 | let start = self.next_node_id; | |
1368 | let end = start.as_usize().checked_add(count).expect("input too large; ran out of NodeIds"); | |
1369 | self.next_node_id = ast::NodeId::from_usize(end); | |
1370 | start..self.next_node_id | |
60c5eb7d XL |
1371 | } |
1372 | ||
dfeec247 | 1373 | pub fn lint_buffer(&mut self) -> &mut LintBuffer { |
e74abb32 XL |
1374 | &mut self.lint_buffer |
1375 | } | |
1376 | ||
3157f602 | 1377 | pub fn arenas() -> ResolverArenas<'a> { |
0bf4aa26 | 1378 | Default::default() |
1a4d82fc JJ |
1379 | } |
1380 | ||
2b03887a | 1381 | pub fn into_outputs(self) -> ResolverOutputs { |
94222f64 | 1382 | let proc_macros = self.proc_macros.iter().map(|id| self.local_def_id(*id)).collect(); |
f9f354fc | 1383 | let definitions = self.definitions; |
923072b8 FG |
1384 | let cstore = Box::new(self.crate_loader.into_cstore()); |
1385 | let source_span = self.source_span; | |
1386 | let expn_that_defined = self.expn_that_defined; | |
29967ef6 | 1387 | let visibilities = self.visibilities; |
04454e1e | 1388 | let has_pub_restricted = self.has_pub_restricted; |
f9f354fc | 1389 | let extern_crate_map = self.extern_crate_map; |
5099ac24 | 1390 | let reexport_map = self.reexport_map; |
f9f354fc XL |
1391 | let maybe_unused_trait_imports = self.maybe_unused_trait_imports; |
1392 | let maybe_unused_extern_crates = self.maybe_unused_extern_crates; | |
1393 | let glob_map = self.glob_map; | |
cdc7bbd5 | 1394 | let main_def = self.main_def; |
c295e0f8 | 1395 | let confused_type_with_std_module = self.confused_type_with_std_module; |
2b03887a FG |
1396 | let effective_visibilities = self.effective_visibilities; |
1397 | let global_ctxt = ResolverGlobalCtxt { | |
1398 | cstore, | |
923072b8 FG |
1399 | source_span, |
1400 | expn_that_defined, | |
29967ef6 | 1401 | visibilities, |
04454e1e | 1402 | has_pub_restricted, |
2b03887a | 1403 | effective_visibilities, |
f9f354fc | 1404 | extern_crate_map, |
5099ac24 | 1405 | reexport_map, |
f9f354fc XL |
1406 | glob_map, |
1407 | maybe_unused_trait_imports, | |
1408 | maybe_unused_extern_crates, | |
dfeec247 XL |
1409 | extern_prelude: self |
1410 | .extern_prelude | |
1411 | .iter() | |
1412 | .map(|(ident, entry)| (ident.name, entry.introduced_by_item)) | |
1413 | .collect(), | |
cdc7bbd5 | 1414 | main_def, |
94222f64 XL |
1415 | trait_impls: self.trait_impls, |
1416 | proc_macros, | |
c295e0f8 | 1417 | confused_type_with_std_module, |
5099ac24 | 1418 | registered_tools: self.registered_tools, |
923072b8 | 1419 | }; |
2b03887a | 1420 | let ast_lowering = ty::ResolverAstLowering { |
923072b8 FG |
1421 | legacy_const_generic_args: self.legacy_const_generic_args, |
1422 | partial_res_map: self.partial_res_map, | |
1423 | import_res_map: self.import_res_map, | |
1424 | label_res_map: self.label_res_map, | |
1425 | lifetimes_res_map: self.lifetimes_res_map, | |
1426 | extra_lifetime_params_map: self.extra_lifetime_params_map, | |
1427 | next_node_id: self.next_node_id, | |
1428 | node_id_to_def_id: self.node_id_to_def_id, | |
1429 | def_id_to_node_id: self.def_id_to_node_id, | |
1430 | trait_map: self.trait_map, | |
1431 | builtin_macro_kinds: self.builtin_macro_kinds, | |
1432 | }; | |
2b03887a | 1433 | ResolverOutputs { definitions, global_ctxt, ast_lowering } |
e74abb32 XL |
1434 | } |
1435 | ||
2b03887a | 1436 | pub fn clone_outputs(&self) -> ResolverOutputs { |
94222f64 | 1437 | let proc_macros = self.proc_macros.iter().map(|id| self.local_def_id(*id)).collect(); |
923072b8 FG |
1438 | let definitions = self.definitions.clone(); |
1439 | let cstore = Box::new(self.cstore().clone()); | |
2b03887a FG |
1440 | let global_ctxt = ResolverGlobalCtxt { |
1441 | cstore, | |
923072b8 FG |
1442 | source_span: self.source_span.clone(), |
1443 | expn_that_defined: self.expn_that_defined.clone(), | |
29967ef6 | 1444 | visibilities: self.visibilities.clone(), |
04454e1e | 1445 | has_pub_restricted: self.has_pub_restricted, |
e74abb32 | 1446 | extern_crate_map: self.extern_crate_map.clone(), |
5099ac24 | 1447 | reexport_map: self.reexport_map.clone(), |
e74abb32 XL |
1448 | glob_map: self.glob_map.clone(), |
1449 | maybe_unused_trait_imports: self.maybe_unused_trait_imports.clone(), | |
1450 | maybe_unused_extern_crates: self.maybe_unused_extern_crates.clone(), | |
dfeec247 XL |
1451 | extern_prelude: self |
1452 | .extern_prelude | |
1453 | .iter() | |
1454 | .map(|(ident, entry)| (ident.name, entry.introduced_by_item)) | |
1455 | .collect(), | |
c295e0f8 | 1456 | main_def: self.main_def, |
94222f64 XL |
1457 | trait_impls: self.trait_impls.clone(), |
1458 | proc_macros, | |
c295e0f8 | 1459 | confused_type_with_std_module: self.confused_type_with_std_module.clone(), |
5099ac24 | 1460 | registered_tools: self.registered_tools.clone(), |
2b03887a | 1461 | effective_visibilities: self.effective_visibilities.clone(), |
923072b8 | 1462 | }; |
2b03887a | 1463 | let ast_lowering = ty::ResolverAstLowering { |
923072b8 FG |
1464 | legacy_const_generic_args: self.legacy_const_generic_args.clone(), |
1465 | partial_res_map: self.partial_res_map.clone(), | |
1466 | import_res_map: self.import_res_map.clone(), | |
1467 | label_res_map: self.label_res_map.clone(), | |
1468 | lifetimes_res_map: self.lifetimes_res_map.clone(), | |
1469 | extra_lifetime_params_map: self.extra_lifetime_params_map.clone(), | |
1470 | next_node_id: self.next_node_id.clone(), | |
1471 | node_id_to_def_id: self.node_id_to_def_id.clone(), | |
1472 | def_id_to_node_id: self.def_id_to_node_id.clone(), | |
1473 | trait_map: self.trait_map.clone(), | |
1474 | builtin_macro_kinds: self.builtin_macro_kinds.clone(), | |
1475 | }; | |
2b03887a | 1476 | ResolverOutputs { definitions, global_ctxt, ast_lowering } |
923072b8 FG |
1477 | } |
1478 | ||
1479 | fn create_stable_hashing_context(&self) -> StableHashingContext<'_> { | |
1480 | StableHashingContext::new( | |
1481 | self.session, | |
1482 | &self.definitions, | |
1483 | self.crate_loader.cstore(), | |
1484 | &self.source_span, | |
1485 | ) | |
e74abb32 XL |
1486 | } |
1487 | ||
1488 | pub fn cstore(&self) -> &CStore { | |
1489 | self.crate_loader.cstore() | |
1490 | } | |
1491 | ||
416331ca XL |
1492 | fn dummy_ext(&self, macro_kind: MacroKind) -> Lrc<SyntaxExtension> { |
1493 | match macro_kind { | |
1494 | MacroKind::Bang => self.dummy_ext_bang.clone(), | |
1495 | MacroKind::Derive => self.dummy_ext_derive.clone(), | |
94222f64 | 1496 | MacroKind::Attr => self.non_macro_attr.clone(), |
416331ca XL |
1497 | } |
1498 | } | |
1499 | ||
0531ce1d | 1500 | /// Runs the function on each namespace. |
83c7162d XL |
1501 | fn per_ns<F: FnMut(&mut Self, Namespace)>(&mut self, mut f: F) { |
1502 | f(self, TypeNS); | |
1503 | f(self, ValueNS); | |
b7449926 | 1504 | f(self, MacroNS); |
476ff2be | 1505 | } |
c30ab7b3 | 1506 | |
e1599b0c | 1507 | fn is_builtin_macro(&mut self, res: Res) -> bool { |
923072b8 | 1508 | self.get_macro(res).map_or(false, |macro_data| macro_data.ext.builtin_name.is_some()) |
416331ca XL |
1509 | } |
1510 | ||
ff7c6d11 XL |
1511 | fn macro_def(&self, mut ctxt: SyntaxContext) -> DefId { |
1512 | loop { | |
6a06907d | 1513 | match ctxt.outer_expn_data().macro_def_id { |
f9f354fc | 1514 | Some(def_id) => return def_id, |
ff7c6d11 XL |
1515 | None => ctxt.remove_mark(), |
1516 | }; | |
1517 | } | |
1518 | } | |
1519 | ||
476ff2be SL |
1520 | /// Entry point to crate resolution. |
1521 | pub fn resolve_crate(&mut self, krate: &Crate) { | |
5869c6ff XL |
1522 | self.session.time("resolve_crate", || { |
1523 | self.session.time("finalize_imports", || ImportResolver { r: self }.finalize_imports()); | |
2b03887a FG |
1524 | self.session.time("compute_effective_visibilities", || { |
1525 | EffectiveVisibilitiesVisitor::compute_effective_visibilities(self, krate) | |
5099ac24 | 1526 | }); |
5869c6ff XL |
1527 | self.session.time("finalize_macro_resolutions", || self.finalize_macro_resolutions()); |
1528 | self.session.time("late_resolve_crate", || self.late_resolve_crate(krate)); | |
cdc7bbd5 | 1529 | self.session.time("resolve_main", || self.resolve_main()); |
5869c6ff XL |
1530 | self.session.time("resolve_check_unused", || self.check_unused(krate)); |
1531 | self.session.time("resolve_report_errors", || self.report_errors(krate)); | |
1532 | self.session.time("resolve_postprocess", || self.crate_loader.postprocess(krate)); | |
1533 | }); | |
1534 | } | |
e74abb32 | 1535 | |
5869c6ff XL |
1536 | pub fn traits_in_scope( |
1537 | &mut self, | |
1538 | current_trait: Option<Module<'a>>, | |
1539 | parent_scope: &ParentScope<'a>, | |
1540 | ctxt: SyntaxContext, | |
1541 | assoc_item: Option<(Symbol, Namespace)>, | |
1542 | ) -> Vec<TraitCandidate> { | |
1543 | let mut found_traits = Vec::new(); | |
1544 | ||
1545 | if let Some(module) = current_trait { | |
1546 | if self.trait_may_have_item(Some(module), assoc_item) { | |
c295e0f8 | 1547 | let def_id = module.def_id(); |
5869c6ff XL |
1548 | found_traits.push(TraitCandidate { def_id, import_ids: smallvec![] }); |
1549 | } | |
1550 | } | |
3b2f2976 | 1551 | |
5869c6ff XL |
1552 | self.visit_scopes(ScopeSet::All(TypeNS, false), parent_scope, ctxt, |this, scope, _, _| { |
1553 | match scope { | |
cdc7bbd5 | 1554 | Scope::Module(module, _) => { |
5869c6ff XL |
1555 | this.traits_in_module(module, assoc_item, &mut found_traits); |
1556 | } | |
1557 | Scope::StdLibPrelude => { | |
1558 | if let Some(module) = this.prelude { | |
1559 | this.traits_in_module(module, assoc_item, &mut found_traits); | |
1560 | } | |
1561 | } | |
1562 | Scope::ExternPrelude | Scope::ToolPrelude | Scope::BuiltinTypes => {} | |
1563 | _ => unreachable!(), | |
1564 | } | |
1565 | None::<()> | |
1566 | }); | |
3157f602 | 1567 | |
5869c6ff | 1568 | found_traits |
3157f602 XL |
1569 | } |
1570 | ||
5869c6ff | 1571 | fn traits_in_module( |
3dfed10e | 1572 | &mut self, |
3dfed10e | 1573 | module: Module<'a>, |
5869c6ff | 1574 | assoc_item: Option<(Symbol, Namespace)>, |
3dfed10e | 1575 | found_traits: &mut Vec<TraitCandidate>, |
3dfed10e | 1576 | ) { |
3dfed10e XL |
1577 | module.ensure_traits(self); |
1578 | let traits = module.traits.borrow(); | |
5869c6ff XL |
1579 | for (trait_name, trait_binding) in traits.as_ref().unwrap().iter() { |
1580 | if self.trait_may_have_item(trait_binding.module(), assoc_item) { | |
1581 | let def_id = trait_binding.res().def_id(); | |
1582 | let import_ids = self.find_transitive_imports(&trait_binding.kind, *trait_name); | |
1583 | found_traits.push(TraitCandidate { def_id, import_ids }); | |
1584 | } | |
1585 | } | |
1586 | } | |
3dfed10e | 1587 | |
5869c6ff XL |
1588 | // List of traits in scope is pruned on best effort basis. We reject traits not having an |
1589 | // associated item with the given name and namespace (if specified). This is a conservative | |
1590 | // optimization, proper hygienic type-based resolution of associated items is done in typeck. | |
1591 | // We don't reject trait aliases (`trait_module == None`) because we don't have access to their | |
1592 | // associated items. | |
1593 | fn trait_may_have_item( | |
1594 | &mut self, | |
1595 | trait_module: Option<Module<'a>>, | |
1596 | assoc_item: Option<(Symbol, Namespace)>, | |
1597 | ) -> bool { | |
1598 | match (trait_module, assoc_item) { | |
1599 | (Some(trait_module), Some((name, ns))) => { | |
1600 | self.resolutions(trait_module).borrow().iter().any(|resolution| { | |
1601 | let (&BindingKey { ident: assoc_ident, ns: assoc_ns, .. }, _) = resolution; | |
1602 | assoc_ns == ns && assoc_ident.name == name | |
1603 | }) | |
3dfed10e | 1604 | } |
5869c6ff | 1605 | _ => true, |
3dfed10e XL |
1606 | } |
1607 | } | |
1608 | ||
1609 | fn find_transitive_imports( | |
1610 | &mut self, | |
1611 | mut kind: &NameBindingKind<'_>, | |
1612 | trait_name: Ident, | |
1613 | ) -> SmallVec<[LocalDefId; 1]> { | |
1614 | let mut import_ids = smallvec![]; | |
1615 | while let NameBindingKind::Import { import, binding, .. } = kind { | |
1616 | let id = self.local_def_id(import.id); | |
1617 | self.maybe_unused_trait_imports.insert(id); | |
1618 | self.add_to_glob_map(&import, trait_name); | |
1619 | import_ids.push(id); | |
1620 | kind = &binding.kind; | |
1621 | } | |
1622 | import_ids | |
1623 | } | |
1624 | ||
e74abb32 | 1625 | fn new_key(&mut self, ident: Ident, ns: Namespace) -> BindingKey { |
ba9703b0 | 1626 | let ident = ident.normalize_to_macros_2_0(); |
e74abb32 XL |
1627 | let disambiguator = if ident.name == kw::Underscore { |
1628 | self.underscore_disambiguator += 1; | |
1629 | self.underscore_disambiguator | |
1630 | } else { | |
1631 | 0 | |
1632 | }; | |
1633 | BindingKey { ident, ns, disambiguator } | |
1634 | } | |
1635 | ||
e1599b0c XL |
1636 | fn resolutions(&mut self, module: Module<'a>) -> &'a Resolutions<'a> { |
1637 | if module.populate_on_access.get() { | |
1638 | module.populate_on_access.set(false); | |
1639 | self.build_reduced_graph_external(module); | |
1640 | } | |
1641 | &module.lazy_resolutions | |
1642 | } | |
1643 | ||
dfeec247 XL |
1644 | fn resolution( |
1645 | &mut self, | |
1646 | module: Module<'a>, | |
1647 | key: BindingKey, | |
1648 | ) -> &'a RefCell<NameResolution<'a>> { | |
1649 | *self | |
1650 | .resolutions(module) | |
1651 | .borrow_mut() | |
1652 | .entry(key) | |
1653 | .or_insert_with(|| self.arenas.alloc_name_resolution()) | |
e1599b0c XL |
1654 | } |
1655 | ||
dfeec247 XL |
1656 | fn record_use( |
1657 | &mut self, | |
1658 | ident: Ident, | |
dfeec247 XL |
1659 | used_binding: &'a NameBinding<'a>, |
1660 | is_lexical_scope: bool, | |
1661 | ) { | |
0731742a XL |
1662 | if let Some((b2, kind)) = used_binding.ambiguity { |
1663 | self.ambiguity_errors.push(AmbiguityError { | |
dfeec247 XL |
1664 | kind, |
1665 | ident, | |
1666 | b1: used_binding, | |
1667 | b2, | |
0731742a XL |
1668 | misc1: AmbiguityErrorMisc::None, |
1669 | misc2: AmbiguityErrorMisc::None, | |
1670 | }); | |
1671 | } | |
74b04a01 | 1672 | if let NameBindingKind::Import { import, binding, ref used } = used_binding.kind { |
0731742a XL |
1673 | // Avoid marking `extern crate` items that refer to a name from extern prelude, |
1674 | // but not introduce it, as used if they are accessed from lexical scope. | |
1675 | if is_lexical_scope { | |
ba9703b0 | 1676 | if let Some(entry) = self.extern_prelude.get(&ident.normalize_to_macros_2_0()) { |
0731742a XL |
1677 | if let Some(crate_item) = entry.extern_crate_item { |
1678 | if ptr::eq(used_binding, crate_item) && !entry.introduced_by_item { | |
1679 | return; | |
13cf67c4 XL |
1680 | } |
1681 | } | |
1682 | } | |
9e0c209e | 1683 | } |
0731742a | 1684 | used.set(true); |
74b04a01 | 1685 | import.used.set(true); |
94222f64 | 1686 | self.used_imports.insert(import.id); |
74b04a01 | 1687 | self.add_to_glob_map(&import, ident); |
94222f64 | 1688 | self.record_use(ident, binding, false); |
54a0048b | 1689 | } |
5bcae85e | 1690 | } |
7453a54e | 1691 | |
9fa01778 | 1692 | #[inline] |
74b04a01 XL |
1693 | fn add_to_glob_map(&mut self, import: &Import<'_>, ident: Ident) { |
1694 | if import.is_glob() { | |
f035d41b | 1695 | let def_id = self.local_def_id(import.id); |
f9f354fc | 1696 | self.glob_map.entry(def_id).or_default().insert(ident.name); |
1a4d82fc | 1697 | } |
1a4d82fc JJ |
1698 | } |
1699 | ||
8faf50e0 | 1700 | fn resolve_crate_root(&mut self, ident: Ident) -> Module<'a> { |
3dfed10e | 1701 | debug!("resolve_crate_root({:?})", ident); |
8faf50e0 | 1702 | let mut ctxt = ident.span.ctxt(); |
dc9dc135 | 1703 | let mark = if ident.name == kw::DollarCrate { |
2c00a5a8 XL |
1704 | // When resolving `$crate` from a `macro_rules!` invoked in a `macro`, |
1705 | // we don't want to pretend that the `macro_rules!` definition is in the `macro` | |
ba9703b0 | 1706 | // as described in `SyntaxContext::apply_mark`, so we ignore prepended opaque marks. |
8faf50e0 XL |
1707 | // FIXME: This is only a guess and it doesn't work correctly for `macro_rules!` |
1708 | // definitions actually produced by `macro` and `macro` definitions produced by | |
1709 | // `macro_rules!`, but at least such configurations are not stable yet. | |
ba9703b0 | 1710 | ctxt = ctxt.normalize_to_macro_rules(); |
3dfed10e XL |
1711 | debug!( |
1712 | "resolve_crate_root: marks={:?}", | |
1713 | ctxt.marks().into_iter().map(|(i, t)| (i.expn_data(), t)).collect::<Vec<_>>() | |
1714 | ); | |
8faf50e0 XL |
1715 | let mut iter = ctxt.marks().into_iter().rev().peekable(); |
1716 | let mut result = None; | |
ba9703b0 | 1717 | // Find the last opaque mark from the end if it exists. |
8faf50e0 XL |
1718 | while let Some(&(mark, transparency)) = iter.peek() { |
1719 | if transparency == Transparency::Opaque { | |
1720 | result = Some(mark); | |
1721 | iter.next(); | |
1722 | } else { | |
1723 | break; | |
1724 | } | |
1725 | } | |
3dfed10e XL |
1726 | debug!( |
1727 | "resolve_crate_root: found opaque mark {:?} {:?}", | |
1728 | result, | |
1729 | result.map(|r| r.expn_data()) | |
1730 | ); | |
ba9703b0 | 1731 | // Then find the last semi-transparent mark from the end if it exists. |
8faf50e0 XL |
1732 | for (mark, transparency) in iter { |
1733 | if transparency == Transparency::SemiTransparent { | |
1734 | result = Some(mark); | |
1735 | } else { | |
1736 | break; | |
1737 | } | |
1738 | } | |
3dfed10e XL |
1739 | debug!( |
1740 | "resolve_crate_root: found semi-transparent mark {:?} {:?}", | |
1741 | result, | |
1742 | result.map(|r| r.expn_data()) | |
1743 | ); | |
8faf50e0 | 1744 | result |
2c00a5a8 | 1745 | } else { |
3dfed10e | 1746 | debug!("resolve_crate_root: not DollarCrate"); |
ba9703b0 | 1747 | ctxt = ctxt.normalize_to_macros_2_0(); |
416331ca | 1748 | ctxt.adjust(ExpnId::root()) |
2c00a5a8 XL |
1749 | }; |
1750 | let module = match mark { | |
c295e0f8 | 1751 | Some(def) => self.expn_def_scope(def), |
3dfed10e XL |
1752 | None => { |
1753 | debug!( | |
1754 | "resolve_crate_root({:?}): found no mark (ident.span = {:?})", | |
1755 | ident, ident.span | |
1756 | ); | |
1757 | return self.graph_root; | |
1758 | } | |
7cac9316 | 1759 | }; |
c295e0f8 XL |
1760 | let module = self.expect_module( |
1761 | module.opt_def_id().map_or(LOCAL_CRATE, |def_id| def_id.krate).as_def_id(), | |
1762 | ); | |
3dfed10e XL |
1763 | debug!( |
1764 | "resolve_crate_root({:?}): got module {:?} ({:?}) (ident.span = {:?})", | |
1765 | ident, | |
1766 | module, | |
1767 | module.kind.name(), | |
1768 | ident.span | |
1769 | ); | |
1770 | module | |
7cac9316 XL |
1771 | } |
1772 | ||
1773 | fn resolve_self(&mut self, ctxt: &mut SyntaxContext, module: Module<'a>) -> Module<'a> { | |
c295e0f8 | 1774 | let mut module = self.expect_module(module.nearest_parent_mod()); |
ba9703b0 | 1775 | while module.span.ctxt().normalize_to_macros_2_0() != *ctxt { |
c295e0f8 XL |
1776 | let parent = module.parent.unwrap_or_else(|| self.expn_def_scope(ctxt.remove_mark())); |
1777 | module = self.expect_module(parent.nearest_parent_mod()); | |
c30ab7b3 | 1778 | } |
7cac9316 | 1779 | module |
c30ab7b3 SL |
1780 | } |
1781 | ||
48663c56 XL |
1782 | fn record_partial_res(&mut self, node_id: NodeId, resolution: PartialRes) { |
1783 | debug!("(recording res) recording {:?} for {}", resolution, node_id); | |
1784 | if let Some(prev_res) = self.partial_res_map.insert(node_id, resolution) { | |
a7813a04 | 1785 | panic!("path resolved multiple times ({:?} before, {:?} now)", prev_res, resolution); |
1a4d82fc JJ |
1786 | } |
1787 | } | |
1788 | ||
cdc7bbd5 XL |
1789 | fn record_pat_span(&mut self, node: NodeId, span: Span) { |
1790 | debug!("(recording pat) recording {:?} for {:?}", node, span); | |
1791 | self.pat_span_map.insert(node, span); | |
1792 | } | |
1793 | ||
f2b60f7d FG |
1794 | fn is_accessible_from( |
1795 | &self, | |
1796 | vis: ty::Visibility<impl Into<DefId>>, | |
1797 | module: Module<'a>, | |
1798 | ) -> bool { | |
c295e0f8 | 1799 | vis.is_accessible_from(module.nearest_parent_mod(), self) |
54a0048b SL |
1800 | } |
1801 | ||
4462d4a0 | 1802 | fn set_binding_parent_module(&mut self, binding: &'a NameBinding<'a>, module: Module<'a>) { |
5099ac24 FG |
1803 | if let Some(old_module) = |
1804 | self.binding_parent_modules.insert(Interned::new_unchecked(binding), module) | |
1805 | { | |
4462d4a0 XL |
1806 | if !ptr::eq(module, old_module) { |
1807 | span_bug!(binding.span, "parent module is reset for binding"); | |
1808 | } | |
1809 | } | |
1810 | } | |
1811 | ||
ba9703b0 | 1812 | fn disambiguate_macro_rules_vs_modularized( |
4462d4a0 | 1813 | &self, |
ba9703b0 XL |
1814 | macro_rules: &'a NameBinding<'a>, |
1815 | modularized: &'a NameBinding<'a>, | |
4462d4a0 | 1816 | ) -> bool { |
ba9703b0 | 1817 | // Some non-controversial subset of ambiguities "modularized macro name" vs "macro_rules" |
4462d4a0 XL |
1818 | // is disambiguated to mitigate regressions from macro modularization. |
1819 | // Scoping for `macro_rules` behaves like scoping for `let` at module level, in general. | |
dfeec247 | 1820 | match ( |
5099ac24 FG |
1821 | self.binding_parent_modules.get(&Interned::new_unchecked(macro_rules)), |
1822 | self.binding_parent_modules.get(&Interned::new_unchecked(modularized)), | |
dfeec247 | 1823 | ) { |
ba9703b0 | 1824 | (Some(macro_rules), Some(modularized)) => { |
c295e0f8 | 1825 | macro_rules.nearest_parent_mod() == modularized.nearest_parent_mod() |
ba9703b0 | 1826 | && modularized.is_ancestor_of(macro_rules) |
dfeec247 | 1827 | } |
4462d4a0 XL |
1828 | _ => false, |
1829 | } | |
1830 | } | |
1831 | ||
5e7ed085 | 1832 | fn extern_prelude_get(&mut self, ident: Ident, finalize: bool) -> Option<&'a NameBinding<'a>> { |
13cf67c4 XL |
1833 | if ident.is_path_segment_keyword() { |
1834 | // Make sure `self`, `super` etc produce an error when passed to here. | |
1835 | return None; | |
1836 | } | |
ba9703b0 | 1837 | self.extern_prelude.get(&ident.normalize_to_macros_2_0()).cloned().and_then(|entry| { |
0bf4aa26 | 1838 | if let Some(binding) = entry.extern_crate_item { |
5e7ed085 | 1839 | if finalize && entry.introduced_by_item { |
94222f64 | 1840 | self.record_use(ident, binding, false); |
0731742a | 1841 | } |
0bf4aa26 XL |
1842 | Some(binding) |
1843 | } else { | |
5e7ed085 | 1844 | let crate_id = if finalize { |
a2a8927a XL |
1845 | let Some(crate_id) = |
1846 | self.crate_loader.process_path_extern(ident.name, ident.span) else { return Some(self.dummy_binding); }; | |
1847 | crate_id | |
0bf4aa26 | 1848 | } else { |
3dfed10e | 1849 | self.crate_loader.maybe_process_path_extern(ident.name)? |
0bf4aa26 | 1850 | }; |
c295e0f8 | 1851 | let crate_root = self.expect_module(crate_id.as_def_id()); |
f2b60f7d FG |
1852 | let vis = ty::Visibility::<LocalDefId>::Public; |
1853 | Some((crate_root, vis, DUMMY_SP, LocalExpnId::ROOT).to_name_binding(self.arenas)) | |
0bf4aa26 XL |
1854 | } |
1855 | }) | |
1856 | } | |
c34b1796 | 1857 | |
5e7ed085 | 1858 | /// Rustdoc uses this to resolve doc link paths in a recoverable way. `PathResult<'a>` |
416331ca XL |
1859 | /// isn't something that can be returned because it can't be made to live that long, |
1860 | /// and also it's a private type. Fortunately rustdoc doesn't need to know the error, | |
1861 | /// just that an error occurred. | |
5e7ed085 | 1862 | pub fn resolve_rustdoc_path( |
dfeec247 | 1863 | &mut self, |
dfeec247 XL |
1864 | path_str: &str, |
1865 | ns: Namespace, | |
04454e1e | 1866 | mut parent_scope: ParentScope<'a>, |
5e7ed085 FG |
1867 | ) -> Option<Res> { |
1868 | let mut segments = | |
1869 | Vec::from_iter(path_str.split("::").map(Ident::from_str).map(Segment::from_ident)); | |
04454e1e FG |
1870 | if let Some(segment) = segments.first_mut() { |
1871 | if segment.ident.name == kw::Crate { | |
1872 | // FIXME: `resolve_path` always resolves `crate` to the current crate root, but | |
1873 | // rustdoc wants it to resolve to the `parent_scope`'s crate root. This trick of | |
1874 | // replacing `crate` with `self` and changing the current module should achieve | |
1875 | // the same effect. | |
1876 | segment.ident.name = kw::SelfLower; | |
1877 | parent_scope.module = | |
1878 | self.expect_module(parent_scope.module.def_id().krate.as_def_id()); | |
1879 | } else if segment.ident.name == kw::Empty { | |
1880 | segment.ident.name = kw::PathRoot; | |
1881 | } | |
5e7ed085 | 1882 | } |
416331ca | 1883 | |
04454e1e | 1884 | match self.maybe_resolve_path(&segments, Some(ns), &parent_scope) { |
5e7ed085 | 1885 | PathResult::Module(ModuleOrUniformRoot::Module(module)) => Some(module.res().unwrap()), |
2b03887a FG |
1886 | PathResult::NonModule(path_res) => path_res.full_res(), |
1887 | PathResult::Module(ModuleOrUniformRoot::ExternPrelude) | PathResult::Failed { .. } => { | |
1888 | None | |
dfeec247 | 1889 | } |
416331ca | 1890 | PathResult::Module(..) | PathResult::Indeterminate => unreachable!(), |
416331ca XL |
1891 | } |
1892 | } | |
32a655c1 | 1893 | |
5099ac24 FG |
1894 | /// For rustdoc. |
1895 | /// For local modules returns only reexports, for external modules returns all children. | |
1896 | pub fn module_children_or_reexports(&self, def_id: DefId) -> Vec<ModChild> { | |
1897 | if let Some(def_id) = def_id.as_local() { | |
1898 | self.reexport_map.get(&def_id).cloned().unwrap_or_default() | |
1899 | } else { | |
1900 | self.cstore().module_children_untracked(def_id, self.session) | |
1901 | } | |
1902 | } | |
1903 | ||
04454e1e FG |
1904 | /// For rustdoc. |
1905 | pub fn macro_rules_scope(&self, def_id: LocalDefId) -> (MacroRulesScopeRef<'a>, Res) { | |
1906 | let scope = *self.macro_rules_scopes.get(&def_id).expect("not a `macro_rules` item"); | |
1907 | match scope.get() { | |
1908 | MacroRulesScope::Binding(mb) => (scope, mb.binding.res()), | |
1909 | _ => unreachable!(), | |
1910 | } | |
1911 | } | |
1912 | ||
2b03887a FG |
1913 | /// For rustdoc. |
1914 | pub fn get_partial_res(&self, node_id: NodeId) -> Option<PartialRes> { | |
1915 | self.partial_res_map.get(&node_id).copied() | |
1916 | } | |
1917 | ||
f035d41b XL |
1918 | /// Retrieves the span of the given `DefId` if `DefId` is in the local crate. |
1919 | #[inline] | |
1920 | pub fn opt_span(&self, def_id: DefId) -> Option<Span> { | |
923072b8 | 1921 | def_id.as_local().map(|def_id| self.source_span[def_id]) |
f035d41b | 1922 | } |
6a06907d | 1923 | |
f2b60f7d FG |
1924 | /// Retrieves the name of the given `DefId`. |
1925 | #[inline] | |
1926 | pub fn opt_name(&self, def_id: DefId) -> Option<Symbol> { | |
1927 | let def_key = match def_id.as_local() { | |
1928 | Some(def_id) => self.definitions.def_key(def_id), | |
1929 | None => self.cstore().def_key(def_id), | |
1930 | }; | |
1931 | def_key.get_opt_name() | |
1932 | } | |
1933 | ||
6a06907d XL |
1934 | /// Checks if an expression refers to a function marked with |
1935 | /// `#[rustc_legacy_const_generics]` and returns the argument index list | |
1936 | /// from the attribute. | |
1937 | pub fn legacy_const_generic_args(&mut self, expr: &Expr) -> Option<Vec<usize>> { | |
1938 | if let ExprKind::Path(None, path) = &expr.kind { | |
1939 | // Don't perform legacy const generics rewriting if the path already | |
1940 | // has generic arguments. | |
1941 | if path.segments.last().unwrap().args.is_some() { | |
1942 | return None; | |
1943 | } | |
1944 | ||
2b03887a FG |
1945 | let res = self.partial_res_map.get(&expr.id)?.full_res()?; |
1946 | if let Res::Def(def::DefKind::Fn, def_id) = res { | |
6a06907d XL |
1947 | // We only support cross-crate argument rewriting. Uses |
1948 | // within the same crate should be updated to use the new | |
1949 | // const generics style. | |
1950 | if def_id.is_local() { | |
1951 | return None; | |
1952 | } | |
1953 | ||
1954 | if let Some(v) = self.legacy_const_generic_args.get(&def_id) { | |
1955 | return v.clone(); | |
1956 | } | |
1957 | ||
a2a8927a XL |
1958 | let attr = self |
1959 | .cstore() | |
1960 | .item_attrs_untracked(def_id, self.session) | |
a2a8927a XL |
1961 | .find(|a| a.has_name(sym::rustc_legacy_const_generics))?; |
1962 | let mut ret = Vec::new(); | |
1963 | for meta in attr.meta_item_list()? { | |
1964 | match meta.literal()?.kind { | |
1965 | LitKind::Int(a, _) => ret.push(a as usize), | |
1966 | _ => panic!("invalid arg index"), | |
6a06907d | 1967 | } |
a2a8927a | 1968 | } |
f2b60f7d | 1969 | // Cache the lookup to avoid parsing attributes for an item multiple times. |
a2a8927a XL |
1970 | self.legacy_const_generic_args.insert(def_id, Some(ret.clone())); |
1971 | return Some(ret); | |
6a06907d XL |
1972 | } |
1973 | } | |
1974 | None | |
1975 | } | |
cdc7bbd5 XL |
1976 | |
1977 | fn resolve_main(&mut self) { | |
1978 | let module = self.graph_root; | |
1979 | let ident = Ident::with_dummy_span(sym::main); | |
1980 | let parent_scope = &ParentScope::module(module, self); | |
1981 | ||
04454e1e | 1982 | let Ok(name_binding) = self.maybe_resolve_ident_in_module( |
cdc7bbd5 XL |
1983 | ModuleOrUniformRoot::Module(module), |
1984 | ident, | |
1985 | ValueNS, | |
1986 | parent_scope, | |
5e7ed085 FG |
1987 | ) else { |
1988 | return; | |
cdc7bbd5 XL |
1989 | }; |
1990 | ||
1991 | let res = name_binding.res(); | |
1992 | let is_import = name_binding.is_import(); | |
1993 | let span = name_binding.span; | |
1994 | if let Res::Def(DefKind::Fn, _) = res { | |
94222f64 | 1995 | self.record_use(ident, name_binding, false); |
cdc7bbd5 XL |
1996 | } |
1997 | self.main_def = Some(MainDefinition { res, is_import, span }); | |
1998 | } | |
f2b60f7d FG |
1999 | |
2000 | // Items that go to reexport table encoded to metadata and visible through it to other crates. | |
2001 | fn is_reexport(&self, binding: &NameBinding<'a>) -> Option<def::Res<!>> { | |
2002 | // FIXME: Consider changing the binding inserted by `#[macro_export] macro_rules` | |
2003 | // into the crate root to actual `NameBindingKind::Import`. | |
2004 | if binding.is_import() | |
2005 | || matches!(binding.kind, NameBindingKind::Res(_, _is_macro_export @ true)) | |
2006 | { | |
2007 | let res = binding.res().expect_non_local(); | |
2008 | // Ambiguous imports are treated as errors at this point and are | |
2009 | // not exposed to other crates (see #36837 for more details). | |
2010 | if res != def::Res::Err && !binding.is_ambiguity() { | |
2011 | return Some(res); | |
2012 | } | |
2013 | } | |
2014 | ||
2015 | return None; | |
2016 | } | |
32a655c1 SL |
2017 | } |
2018 | ||
f9f354fc | 2019 | fn names_to_string(names: &[Symbol]) -> String { |
c34b1796 | 2020 | let mut result = String::new(); |
dfeec247 | 2021 | for (i, name) in names.iter().filter(|name| **name != kw::PathRoot).enumerate() { |
32a655c1 SL |
2022 | if i > 0 { |
2023 | result.push_str("::"); | |
c34b1796 | 2024 | } |
60c5eb7d XL |
2025 | if Ident::with_dummy_span(*name).is_raw_guess() { |
2026 | result.push_str("r#"); | |
2027 | } | |
a2a8927a | 2028 | result.push_str(name.as_str()); |
92a42be0 | 2029 | } |
c34b1796 AL |
2030 | result |
2031 | } | |
2032 | ||
32a655c1 | 2033 | fn path_names_to_string(path: &Path) -> String { |
60c5eb7d | 2034 | names_to_string(&path.segments.iter().map(|seg| seg.ident.name).collect::<Vec<_>>()) |
c34b1796 AL |
2035 | } |
2036 | ||
2037 | /// A somewhat inefficient routine to obtain the name of a module. | |
9fa01778 | 2038 | fn module_to_string(module: Module<'_>) -> Option<String> { |
c34b1796 AL |
2039 | let mut names = Vec::new(); |
2040 | ||
f9f354fc | 2041 | fn collect_mod(names: &mut Vec<Symbol>, module: Module<'_>) { |
48663c56 | 2042 | if let ModuleKind::Def(.., name) = module.kind { |
9e0c209e | 2043 | if let Some(parent) = module.parent { |
e1599b0c | 2044 | names.push(name); |
9e0c209e | 2045 | collect_mod(names, parent); |
c34b1796 | 2046 | } |
9e0c209e | 2047 | } else { |
f9f354fc | 2048 | names.push(Symbol::intern("<opaque>")); |
c30ab7b3 | 2049 | collect_mod(names, module.parent.unwrap()); |
c34b1796 AL |
2050 | } |
2051 | } | |
2052 | collect_mod(&mut names, module); | |
2053 | ||
9346a6ac | 2054 | if names.is_empty() { |
2c00a5a8 | 2055 | return None; |
c34b1796 | 2056 | } |
e1599b0c XL |
2057 | names.reverse(); |
2058 | Some(names_to_string(&names)) | |
c34b1796 AL |
2059 | } |
2060 | ||
94b46f34 | 2061 | #[derive(Copy, Clone, Debug)] |
04454e1e FG |
2062 | struct Finalize { |
2063 | /// Node ID for linting. | |
2064 | node_id: NodeId, | |
2065 | /// Span of the whole path or some its characteristic fragment. | |
2066 | /// E.g. span of `b` in `foo::{a, b, c}`, or full span for regular paths. | |
2067 | path_span: Span, | |
2b03887a | 2068 | /// Span of the path start, suitable for prepending something to it. |
04454e1e FG |
2069 | /// E.g. span of `foo` in `foo::{a, b, c}`, or full span for regular paths. |
2070 | root_span: Span, | |
2071 | /// Whether to report privacy errors or silently return "no resolution" for them, | |
2072 | /// similarly to speculative resolution. | |
2073 | report_private: bool, | |
94b46f34 XL |
2074 | } |
2075 | ||
5e7ed085 | 2076 | impl Finalize { |
04454e1e FG |
2077 | fn new(node_id: NodeId, path_span: Span) -> Finalize { |
2078 | Finalize::with_root_span(node_id, path_span, path_span) | |
5e7ed085 FG |
2079 | } |
2080 | ||
04454e1e FG |
2081 | fn with_root_span(node_id: NodeId, path_span: Span, root_span: Span) -> Finalize { |
2082 | Finalize { node_id, path_span, root_span, report_private: true } | |
5e7ed085 | 2083 | } |
8faf50e0 | 2084 | } |