Auto merge of #88872 - petrochenkov:getmod2, r=cjgillot
resolve: Some module-related refactorings See the individual commits.
This commit is contained in:
commit
73422130ee
@ -9,11 +9,9 @@
|
||||
use crate::imports::{Import, ImportKind};
|
||||
use crate::macros::{MacroRulesBinding, MacroRulesScope, MacroRulesScopeRef};
|
||||
use crate::Namespace::{self, MacroNS, TypeNS, ValueNS};
|
||||
use crate::{CrateLint, Determinacy, PathResult, ResolutionError, VisResolutionError};
|
||||
use crate::{
|
||||
ExternPreludeEntry, ModuleOrUniformRoot, ParentScope, PerNS, Resolver, ResolverArenas,
|
||||
};
|
||||
use crate::{Module, ModuleData, ModuleKind, NameBinding, NameBindingKind, Segment, ToNameBinding};
|
||||
use crate::{CrateLint, Determinacy, ExternPreludeEntry, Module, ModuleKind, ModuleOrUniformRoot};
|
||||
use crate::{NameBinding, NameBindingKind, ParentScope, PathResult, PerNS, ResolutionError};
|
||||
use crate::{Resolver, ResolverArenas, Segment, ToNameBinding, VisResolutionError};
|
||||
|
||||
use rustc_ast::visit::{self, AssocCtxt, Visitor};
|
||||
use rustc_ast::{self as ast, AssocItem, AssocItemKind, MetaItemKind, StmtKind};
|
||||
@ -95,100 +93,93 @@ impl<'a> Resolver<'a> {
|
||||
}
|
||||
|
||||
/// Walks up the tree of definitions starting at `def_id`,
|
||||
/// stopping at the first `DefKind::Mod` encountered
|
||||
fn nearest_parent_mod(&mut self, def_id: DefId) -> Module<'a> {
|
||||
let def_key = self.cstore().def_key(def_id);
|
||||
|
||||
let mut parent_id = DefId {
|
||||
krate: def_id.krate,
|
||||
index: def_key.parent.expect("failed to get parent for module"),
|
||||
};
|
||||
// The immediate parent may not be a module
|
||||
// (e.g. `const _: () = { #[path = "foo.rs"] mod foo; };`)
|
||||
// Walk up the tree until we hit a module or the crate root.
|
||||
while parent_id.index != CRATE_DEF_INDEX
|
||||
&& self.cstore().def_kind(parent_id) != DefKind::Mod
|
||||
{
|
||||
let parent_def_key = self.cstore().def_key(parent_id);
|
||||
parent_id.index = parent_def_key.parent.expect("failed to get parent for module");
|
||||
}
|
||||
self.get_module(parent_id)
|
||||
}
|
||||
|
||||
pub fn get_module(&mut self, def_id: DefId) -> Module<'a> {
|
||||
// If this is a local module, it will be in `module_map`, no need to recalculate it.
|
||||
if let Some(def_id) = def_id.as_local() {
|
||||
return self.module_map[&def_id];
|
||||
}
|
||||
|
||||
// Cache module resolution
|
||||
if let Some(&module) = self.extern_module_map.get(&def_id) {
|
||||
return module;
|
||||
}
|
||||
|
||||
let (name, parent) = if def_id.index == CRATE_DEF_INDEX {
|
||||
// This is the crate root
|
||||
(self.cstore().crate_name(def_id.krate), None)
|
||||
} else {
|
||||
let def_key = self.cstore().def_key(def_id);
|
||||
let name = def_key
|
||||
.disambiguated_data
|
||||
.data
|
||||
.get_opt_name()
|
||||
.expect("given a DefId that wasn't a module");
|
||||
|
||||
let parent = Some(self.nearest_parent_mod(def_id));
|
||||
(name, parent)
|
||||
};
|
||||
|
||||
// Allocate and return a new module with the information we found
|
||||
let kind = ModuleKind::Def(DefKind::Mod, def_id, name);
|
||||
let module = self.arenas.alloc_module(ModuleData::new(
|
||||
parent,
|
||||
kind,
|
||||
def_id,
|
||||
self.cstore().module_expansion_untracked(def_id, &self.session),
|
||||
self.cstore().get_span_untracked(def_id, &self.session),
|
||||
));
|
||||
self.extern_module_map.insert(def_id, module);
|
||||
module
|
||||
}
|
||||
|
||||
crate fn macro_def_scope(&mut self, expn_id: ExpnId) -> Module<'a> {
|
||||
let def_id = match expn_id.expn_data().macro_def_id {
|
||||
Some(def_id) => def_id,
|
||||
None => {
|
||||
return expn_id
|
||||
.as_local()
|
||||
.and_then(|expn_id| self.ast_transform_scopes.get(&expn_id))
|
||||
.unwrap_or(&self.graph_root);
|
||||
/// stopping at the first encountered module.
|
||||
/// Parent block modules for arbitrary def-ids are not recorded for the local crate,
|
||||
/// and are not preserved in metadata for foreign crates, so block modules are never
|
||||
/// returned by this function.
|
||||
///
|
||||
/// For the local crate ignoring block modules may be incorrect, so use this method with care.
|
||||
///
|
||||
/// For foreign crates block modules can be ignored without introducing observable differences,
|
||||
/// moreover they has to be ignored right now because they are not kept in metadata.
|
||||
/// Foreign parent modules are used for resolving names used by foreign macros with def-site
|
||||
/// hygiene, therefore block module ignorability relies on macros with def-site hygiene and
|
||||
/// block module parents being unreachable from other crates.
|
||||
/// Reachable macros with block module parents exist due to `#[macro_export] macro_rules!`,
|
||||
/// but they cannot use def-site hygiene, so the assumption holds
|
||||
/// (<https://github.com/rust-lang/rust/pull/77984#issuecomment-712445508>).
|
||||
fn get_nearest_non_block_module(&mut self, mut def_id: DefId) -> Module<'a> {
|
||||
loop {
|
||||
match self.get_module(def_id) {
|
||||
Some(module) => return module,
|
||||
None => {
|
||||
def_id.index =
|
||||
self.def_key(def_id).parent.expect("non-root `DefId` without parent")
|
||||
}
|
||||
}
|
||||
};
|
||||
self.macro_def_scope_from_def_id(def_id)
|
||||
}
|
||||
}
|
||||
|
||||
crate fn macro_def_scope_from_def_id(&mut self, def_id: DefId) -> Module<'a> {
|
||||
pub fn expect_module(&mut self, def_id: DefId) -> Module<'a> {
|
||||
self.get_module(def_id).expect("argument `DefId` is not a module")
|
||||
}
|
||||
|
||||
/// If `def_id` refers to a module (in resolver's sense, i.e. a module item, crate root, enum,
|
||||
/// or trait), then this function returns that module's resolver representation, otherwise it
|
||||
/// returns `None`.
|
||||
/// FIXME: `Module`s for local enums and traits are not currently found.
|
||||
crate fn get_module(&mut self, def_id: DefId) -> Option<Module<'a>> {
|
||||
if let module @ Some(..) = self.module_map.get(&def_id) {
|
||||
return module.copied();
|
||||
}
|
||||
|
||||
if !def_id.is_local() {
|
||||
let def_kind = self.cstore().def_kind(def_id);
|
||||
match def_kind {
|
||||
DefKind::Mod | DefKind::Enum | DefKind::Trait => {
|
||||
let def_key = self.cstore().def_key(def_id);
|
||||
let parent = def_key.parent.map(|index| {
|
||||
self.get_nearest_non_block_module(DefId { index, krate: def_id.krate })
|
||||
});
|
||||
let name = if def_id.index == CRATE_DEF_INDEX {
|
||||
self.cstore().crate_name(def_id.krate)
|
||||
} else {
|
||||
def_key.disambiguated_data.data.get_opt_name().expect("module without name")
|
||||
};
|
||||
|
||||
let module = self.arenas.new_module(
|
||||
parent,
|
||||
ModuleKind::Def(def_kind, def_id, name),
|
||||
self.cstore().module_expansion_untracked(def_id, &self.session),
|
||||
self.cstore().get_span_untracked(def_id, &self.session),
|
||||
// FIXME: Account for `#[no_implicit_prelude]` attributes.
|
||||
parent.map_or(false, |module| module.no_implicit_prelude),
|
||||
);
|
||||
self.module_map.insert(def_id, module);
|
||||
Some(module)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
crate fn expn_def_scope(&mut self, expn_id: ExpnId) -> Module<'a> {
|
||||
match expn_id.expn_data().macro_def_id {
|
||||
Some(def_id) => self.macro_def_scope(def_id),
|
||||
None => expn_id
|
||||
.as_local()
|
||||
.and_then(|expn_id| self.ast_transform_scopes.get(&expn_id))
|
||||
.unwrap_or(&self.graph_root),
|
||||
}
|
||||
}
|
||||
|
||||
crate fn macro_def_scope(&mut self, def_id: DefId) -> Module<'a> {
|
||||
if let Some(id) = def_id.as_local() {
|
||||
self.local_macro_def_scopes[&id]
|
||||
} else {
|
||||
// This is not entirely correct - a `macro_rules!` macro may occur
|
||||
// inside a 'block' module:
|
||||
//
|
||||
// ```rust
|
||||
// const _: () = {
|
||||
// #[macro_export]
|
||||
// macro_rules! my_macro {
|
||||
// () => {};
|
||||
// }
|
||||
// `
|
||||
// We don't record this information for external crates, so
|
||||
// the module we compute here will be the closest 'mod' item
|
||||
// (not necesssarily the actual parent of the `macro_rules!`
|
||||
// macro). `macro_rules!` macros can't use def-site hygiene,
|
||||
// so this hopefully won't be a problem.
|
||||
//
|
||||
// See https://github.com/rust-lang/rust/pull/77984#issuecomment-712445508
|
||||
self.nearest_parent_mod(def_id)
|
||||
self.get_nearest_non_block_module(def_id)
|
||||
}
|
||||
}
|
||||
|
||||
@ -274,7 +265,7 @@ fn resolve_visibility_speculative<'ast>(
|
||||
self.r.visibilities[&def_id.expect_local()]
|
||||
}
|
||||
// Otherwise, the visibility is restricted to the nearest parent `mod` item.
|
||||
_ => ty::Visibility::Restricted(self.parent_scope.module.nearest_parent_mod),
|
||||
_ => ty::Visibility::Restricted(self.parent_scope.module.nearest_parent_mod()),
|
||||
})
|
||||
}
|
||||
ast::VisibilityKind::Restricted { ref path, id, .. } => {
|
||||
@ -717,7 +708,7 @@ fn build_reduced_graph_for_item(&mut self, item: &'b Item) {
|
||||
local_def_id,
|
||||
);
|
||||
self.r.extern_crate_map.insert(local_def_id, crate_id);
|
||||
self.r.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX })
|
||||
self.r.expect_module(crate_id.as_def_id())
|
||||
};
|
||||
|
||||
let used = self.process_macro_use_imports(item, module);
|
||||
@ -768,21 +759,16 @@ fn build_reduced_graph_for_item(&mut self, item: &'b Item) {
|
||||
}
|
||||
|
||||
ItemKind::Mod(..) => {
|
||||
let module_kind = ModuleKind::Def(DefKind::Mod, def_id, ident.name);
|
||||
let module = self.r.arenas.alloc_module(ModuleData {
|
||||
no_implicit_prelude: parent.no_implicit_prelude || {
|
||||
self.r.session.contains_name(&item.attrs, sym::no_implicit_prelude)
|
||||
},
|
||||
..ModuleData::new(
|
||||
Some(parent),
|
||||
module_kind,
|
||||
def_id,
|
||||
expansion.to_expn_id(),
|
||||
item.span,
|
||||
)
|
||||
});
|
||||
let module = self.r.arenas.new_module(
|
||||
Some(parent),
|
||||
ModuleKind::Def(DefKind::Mod, def_id, ident.name),
|
||||
expansion.to_expn_id(),
|
||||
item.span,
|
||||
parent.no_implicit_prelude
|
||||
|| self.r.session.contains_name(&item.attrs, sym::no_implicit_prelude),
|
||||
);
|
||||
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
|
||||
self.r.module_map.insert(local_def_id, module);
|
||||
self.r.module_map.insert(def_id, module);
|
||||
|
||||
// Descend into the module.
|
||||
self.parent_scope.module = module;
|
||||
@ -813,13 +799,12 @@ fn build_reduced_graph_for_item(&mut self, item: &'b Item) {
|
||||
}
|
||||
|
||||
ItemKind::Enum(_, _) => {
|
||||
let module_kind = ModuleKind::Def(DefKind::Enum, def_id, ident.name);
|
||||
let module = self.r.new_module(
|
||||
parent,
|
||||
module_kind,
|
||||
parent.nearest_parent_mod,
|
||||
let module = self.r.arenas.new_module(
|
||||
Some(parent),
|
||||
ModuleKind::Def(DefKind::Enum, def_id, ident.name),
|
||||
expansion.to_expn_id(),
|
||||
item.span,
|
||||
parent.no_implicit_prelude,
|
||||
);
|
||||
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
|
||||
self.parent_scope.module = module;
|
||||
@ -888,13 +873,12 @@ fn build_reduced_graph_for_item(&mut self, item: &'b Item) {
|
||||
|
||||
ItemKind::Trait(..) => {
|
||||
// Add all the items within to a new module.
|
||||
let module_kind = ModuleKind::Def(DefKind::Trait, def_id, ident.name);
|
||||
let module = self.r.new_module(
|
||||
parent,
|
||||
module_kind,
|
||||
parent.nearest_parent_mod,
|
||||
let module = self.r.arenas.new_module(
|
||||
Some(parent),
|
||||
ModuleKind::Def(DefKind::Trait, def_id, ident.name),
|
||||
expansion.to_expn_id(),
|
||||
item.span,
|
||||
parent.no_implicit_prelude,
|
||||
);
|
||||
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
|
||||
self.parent_scope.module = module;
|
||||
@ -932,12 +916,12 @@ fn build_reduced_graph_for_block(&mut self, block: &Block) {
|
||||
let parent = self.parent_scope.module;
|
||||
let expansion = self.parent_scope.expansion;
|
||||
if self.block_needs_anonymous_module(block) {
|
||||
let module = self.r.new_module(
|
||||
parent,
|
||||
let module = self.r.arenas.new_module(
|
||||
Some(parent),
|
||||
ModuleKind::Block(block.id),
|
||||
parent.nearest_parent_mod,
|
||||
expansion.to_expn_id(),
|
||||
block.span,
|
||||
parent.no_implicit_prelude,
|
||||
);
|
||||
self.r.block_map.insert(block.id, module);
|
||||
self.parent_scope.module = module; // Descend into the block.
|
||||
@ -953,12 +937,13 @@ fn build_reduced_graph_for_external_crate_res(&mut self, child: Export) {
|
||||
// Record primary definitions.
|
||||
match res {
|
||||
Res::Def(kind @ (DefKind::Mod | DefKind::Enum | DefKind::Trait), def_id) => {
|
||||
let module = self.r.new_module(
|
||||
parent,
|
||||
let module = self.r.arenas.new_module(
|
||||
Some(parent),
|
||||
ModuleKind::Def(kind, def_id, ident.name),
|
||||
def_id,
|
||||
expansion.to_expn_id(),
|
||||
span,
|
||||
// FIXME: Account for `#[no_implicit_prelude]` attributes.
|
||||
parent.no_implicit_prelude,
|
||||
);
|
||||
self.r.define(parent, ident, TypeNS, (module, vis, span, expansion));
|
||||
}
|
||||
|
@ -915,8 +915,7 @@ fn lookup_import_candidates_from_module<FilterFn>(
|
||||
continue;
|
||||
}
|
||||
if let Some(crate_id) = self.crate_loader.maybe_process_path_extern(ident.name) {
|
||||
let crate_root =
|
||||
self.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX });
|
||||
let crate_root = self.expect_module(crate_id.as_def_id());
|
||||
suggestions.extend(self.lookup_import_candidates_from_module(
|
||||
lookup_ident,
|
||||
namespace,
|
||||
|
@ -427,7 +427,7 @@ impl<'a> Resolver<'a> {
|
||||
match ident.span.glob_adjust(module.expansion, glob_import.span) {
|
||||
Some(Some(def)) => {
|
||||
tmp_parent_scope =
|
||||
ParentScope { module: self.macro_def_scope(def), ..*parent_scope };
|
||||
ParentScope { module: self.expn_def_scope(def), ..*parent_scope };
|
||||
adjusted_parent_scope = &tmp_parent_scope;
|
||||
}
|
||||
Some(None) => {}
|
||||
@ -585,7 +585,7 @@ fn update_resolution<T, F>(&mut self, module: Module<'a>, key: BindingKey, f: F)
|
||||
for import in module.glob_importers.borrow_mut().iter() {
|
||||
let mut ident = key.ident;
|
||||
let scope = match ident.span.reverse_glob_adjust(module.expansion, import.span) {
|
||||
Some(Some(def)) => self.macro_def_scope(def),
|
||||
Some(Some(def)) => self.expn_def_scope(def),
|
||||
Some(None) => import.parent_scope.module,
|
||||
None => continue,
|
||||
};
|
||||
@ -1364,7 +1364,7 @@ fn resolve_glob_import(&mut self, import: &'b Import<'b>) {
|
||||
.collect::<Vec<_>>();
|
||||
for (mut key, binding) in bindings {
|
||||
let scope = match key.ident.span.reverse_glob_adjust(module.expansion, import.span) {
|
||||
Some(Some(def)) => self.r.macro_def_scope(def),
|
||||
Some(Some(def)) => self.r.expn_def_scope(def),
|
||||
Some(None) => import.parent_scope.module,
|
||||
None => continue,
|
||||
};
|
||||
|
@ -799,9 +799,7 @@ fn with_rib<T>(
|
||||
}
|
||||
|
||||
fn with_scope<T>(&mut self, id: NodeId, f: impl FnOnce(&mut Self) -> T) -> T {
|
||||
let id = self.r.local_def_id(id);
|
||||
let module = self.r.module_map.get(&id).cloned(); // clones a reference
|
||||
if let Some(module) = module {
|
||||
if let Some(module) = self.r.get_module(self.r.local_def_id(id).to_def_id()) {
|
||||
// Move down in the graph.
|
||||
let orig_module = replace(&mut self.parent_scope.module, module);
|
||||
self.with_rib(ValueNS, ModuleRibKind(module), |this| {
|
||||
@ -1872,7 +1870,7 @@ fn smart_resolve_path_fragment(
|
||||
if this.should_report_errs() {
|
||||
let (err, candidates) = this.smart_resolve_report_errors(path, span, source, res);
|
||||
|
||||
let def_id = this.parent_scope.module.nearest_parent_mod;
|
||||
let def_id = this.parent_scope.module.nearest_parent_mod();
|
||||
let instead = res.is_some();
|
||||
let suggestion =
|
||||
if res.is_none() { this.report_missing_type_error(path) } else { None };
|
||||
@ -1940,7 +1938,7 @@ fn smart_resolve_path_fragment(
|
||||
|
||||
drop(parent_err);
|
||||
|
||||
let def_id = this.parent_scope.module.nearest_parent_mod;
|
||||
let def_id = this.parent_scope.module.nearest_parent_mod();
|
||||
|
||||
if this.should_report_errs() {
|
||||
this.r.use_injections.push(UseError {
|
||||
|
@ -40,7 +40,8 @@
|
||||
use rustc_expand::base::{DeriveResolutions, SyntaxExtension, SyntaxExtensionKind};
|
||||
use rustc_hir::def::Namespace::*;
|
||||
use rustc_hir::def::{self, CtorOf, DefKind, NonMacroAttrKind, PartialRes};
|
||||
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefPathHash, LocalDefId, CRATE_DEF_INDEX};
|
||||
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefPathHash, LocalDefId};
|
||||
use rustc_hir::def_id::{CRATE_DEF_ID, CRATE_DEF_INDEX, LOCAL_CRATE};
|
||||
use rustc_hir::definitions::{DefKey, DefPathData, Definitions};
|
||||
use rustc_hir::TraitCandidate;
|
||||
use rustc_index::vec::IndexVec;
|
||||
@ -505,10 +506,6 @@ pub struct ModuleData<'a> {
|
||||
/// What kind of module this is, because this may not be a `mod`.
|
||||
kind: ModuleKind,
|
||||
|
||||
/// The [`DefId`] of the nearest `mod` item ancestor (which may be this module).
|
||||
/// This may be the crate root.
|
||||
nearest_parent_mod: DefId,
|
||||
|
||||
/// Mapping between names and their (possibly in-progress) resolutions in this module.
|
||||
/// Resolutions in modules from other crates are not populated until accessed.
|
||||
lazy_resolutions: Resolutions<'a>,
|
||||
@ -539,18 +536,21 @@ impl<'a> ModuleData<'a> {
|
||||
fn new(
|
||||
parent: Option<Module<'a>>,
|
||||
kind: ModuleKind,
|
||||
nearest_parent_mod: DefId,
|
||||
expansion: ExpnId,
|
||||
span: Span,
|
||||
no_implicit_prelude: bool,
|
||||
) -> Self {
|
||||
let is_foreign = match kind {
|
||||
ModuleKind::Def(_, def_id, _) => !def_id.is_local(),
|
||||
ModuleKind::Block(_) => false,
|
||||
};
|
||||
ModuleData {
|
||||
parent,
|
||||
kind,
|
||||
nearest_parent_mod,
|
||||
lazy_resolutions: Default::default(),
|
||||
populate_on_access: Cell::new(!nearest_parent_mod.is_local()),
|
||||
populate_on_access: Cell::new(is_foreign),
|
||||
unexpanded_invocations: Default::default(),
|
||||
no_implicit_prelude: false,
|
||||
no_implicit_prelude,
|
||||
glob_importers: RefCell::new(Vec::new()),
|
||||
globs: RefCell::new(Vec::new()),
|
||||
traits: RefCell::new(None),
|
||||
@ -623,6 +623,15 @@ fn nearest_item_scope(&'a self) -> Module<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The [`DefId`] of the nearest `mod` item ancestor (which may be this module).
|
||||
/// This may be the crate root.
|
||||
fn nearest_parent_mod(&self) -> DefId {
|
||||
match self.kind {
|
||||
ModuleKind::Def(DefKind::Mod, def_id, _) => def_id,
|
||||
_ => self.parent.expect("non-root module without parent").nearest_parent_mod(),
|
||||
}
|
||||
}
|
||||
|
||||
fn is_ancestor_of(&self, mut other: &Self) -> bool {
|
||||
while !ptr::eq(self, other) {
|
||||
if let Some(parent) = other.parent {
|
||||
@ -934,8 +943,7 @@ pub struct Resolver<'a> {
|
||||
/// some AST passes can generate identifiers that only resolve to local or
|
||||
/// language items.
|
||||
empty_module: Module<'a>,
|
||||
module_map: FxHashMap<LocalDefId, Module<'a>>,
|
||||
extern_module_map: FxHashMap<DefId, Module<'a>>,
|
||||
module_map: FxHashMap<DefId, Module<'a>>,
|
||||
binding_parent_modules: FxHashMap<PtrKey<'a, NameBinding<'a>>, Module<'a>>,
|
||||
underscore_disambiguator: u32,
|
||||
|
||||
@ -1052,8 +1060,16 @@ pub struct ResolverArenas<'a> {
|
||||
}
|
||||
|
||||
impl<'a> ResolverArenas<'a> {
|
||||
fn alloc_module(&'a self, module: ModuleData<'a>) -> Module<'a> {
|
||||
let module = self.modules.alloc(module);
|
||||
fn new_module(
|
||||
&'a self,
|
||||
parent: Option<Module<'a>>,
|
||||
kind: ModuleKind,
|
||||
expn_id: ExpnId,
|
||||
span: Span,
|
||||
no_implicit_prelude: bool,
|
||||
) -> Module<'a> {
|
||||
let module =
|
||||
self.modules.alloc(ModuleData::new(parent, kind, expn_id, span, no_implicit_prelude));
|
||||
if module.def_id().map_or(true, |def_id| def_id.is_local()) {
|
||||
self.local_modules.borrow_mut().push(module);
|
||||
}
|
||||
@ -1255,32 +1271,29 @@ pub fn new(
|
||||
metadata_loader: Box<MetadataLoaderDyn>,
|
||||
arenas: &'a ResolverArenas<'a>,
|
||||
) -> Resolver<'a> {
|
||||
let root_local_def_id = LocalDefId { local_def_index: CRATE_DEF_INDEX };
|
||||
let root_def_id = root_local_def_id.to_def_id();
|
||||
let root_module_kind = ModuleKind::Def(DefKind::Mod, root_def_id, kw::Empty);
|
||||
let graph_root = arenas.alloc_module(ModuleData {
|
||||
no_implicit_prelude: session.contains_name(&krate.attrs, sym::no_implicit_prelude),
|
||||
..ModuleData::new(None, root_module_kind, root_def_id, ExpnId::root(), krate.span)
|
||||
});
|
||||
let empty_module_kind = ModuleKind::Def(DefKind::Mod, root_def_id, kw::Empty);
|
||||
let empty_module = arenas.alloc_module(ModuleData {
|
||||
no_implicit_prelude: true,
|
||||
..ModuleData::new(
|
||||
Some(graph_root),
|
||||
empty_module_kind,
|
||||
root_def_id,
|
||||
ExpnId::root(),
|
||||
DUMMY_SP,
|
||||
)
|
||||
});
|
||||
let root_def_id = CRATE_DEF_ID.to_def_id();
|
||||
let graph_root = arenas.new_module(
|
||||
None,
|
||||
ModuleKind::Def(DefKind::Mod, root_def_id, kw::Empty),
|
||||
ExpnId::root(),
|
||||
krate.span,
|
||||
session.contains_name(&krate.attrs, sym::no_implicit_prelude),
|
||||
);
|
||||
let empty_module = arenas.new_module(
|
||||
None,
|
||||
ModuleKind::Def(DefKind::Mod, root_def_id, kw::Empty),
|
||||
ExpnId::root(),
|
||||
DUMMY_SP,
|
||||
true,
|
||||
);
|
||||
let mut module_map = FxHashMap::default();
|
||||
module_map.insert(root_local_def_id, graph_root);
|
||||
module_map.insert(root_def_id, graph_root);
|
||||
|
||||
let definitions = Definitions::new(session.local_stable_crate_id(), krate.span);
|
||||
let root = definitions.get_root_def();
|
||||
|
||||
let mut visibilities = FxHashMap::default();
|
||||
visibilities.insert(root_local_def_id, ty::Visibility::Public);
|
||||
visibilities.insert(CRATE_DEF_ID, ty::Visibility::Public);
|
||||
|
||||
let mut def_id_to_node_id = IndexVec::default();
|
||||
assert_eq!(def_id_to_node_id.push(CRATE_NODE_ID), root);
|
||||
@ -1341,7 +1354,6 @@ pub fn new(
|
||||
empty_module,
|
||||
module_map,
|
||||
block_map: Default::default(),
|
||||
extern_module_map: FxHashMap::default(),
|
||||
binding_parent_modules: FxHashMap::default(),
|
||||
ast_transform_scopes: FxHashMap::default(),
|
||||
|
||||
@ -1632,18 +1644,6 @@ fn find_transitive_imports(
|
||||
import_ids
|
||||
}
|
||||
|
||||
fn new_module(
|
||||
&self,
|
||||
parent: Module<'a>,
|
||||
kind: ModuleKind,
|
||||
nearest_parent_mod: DefId,
|
||||
expn_id: ExpnId,
|
||||
span: Span,
|
||||
) -> Module<'a> {
|
||||
let module = ModuleData::new(Some(parent), kind, nearest_parent_mod, expn_id, span);
|
||||
self.arenas.alloc_module(module)
|
||||
}
|
||||
|
||||
fn new_key(&mut self, ident: Ident, ns: Namespace) -> BindingKey {
|
||||
let ident = ident.normalize_to_macros_2_0();
|
||||
let disambiguator = if ident.name == kw::Underscore {
|
||||
@ -2016,7 +2016,7 @@ fn hygienic_lexical_parent(
|
||||
derive_fallback_lint_id: Option<NodeId>,
|
||||
) -> Option<(Module<'a>, Option<NodeId>)> {
|
||||
if !module.expansion.outer_expn_is_descendant_of(*ctxt) {
|
||||
return Some((self.macro_def_scope(ctxt.remove_mark()), None));
|
||||
return Some((self.expn_def_scope(ctxt.remove_mark()), None));
|
||||
}
|
||||
|
||||
if let ModuleKind::Block(..) = module.kind {
|
||||
@ -2085,7 +2085,7 @@ fn resolve_ident_in_module_ext(
|
||||
ModuleOrUniformRoot::Module(m) => {
|
||||
if let Some(def) = ident.span.normalize_to_macros_2_0_and_adjust(m.expansion) {
|
||||
tmp_parent_scope =
|
||||
ParentScope { module: self.macro_def_scope(def), ..*parent_scope };
|
||||
ParentScope { module: self.expn_def_scope(def), ..*parent_scope };
|
||||
adjusted_parent_scope = &tmp_parent_scope;
|
||||
}
|
||||
}
|
||||
@ -2158,7 +2158,7 @@ fn resolve_crate_root(&mut self, ident: Ident) -> Module<'a> {
|
||||
ctxt.adjust(ExpnId::root())
|
||||
};
|
||||
let module = match mark {
|
||||
Some(def) => self.macro_def_scope(def),
|
||||
Some(def) => self.expn_def_scope(def),
|
||||
None => {
|
||||
debug!(
|
||||
"resolve_crate_root({:?}): found no mark (ident.span = {:?})",
|
||||
@ -2167,7 +2167,8 @@ fn resolve_crate_root(&mut self, ident: Ident) -> Module<'a> {
|
||||
return self.graph_root;
|
||||
}
|
||||
};
|
||||
let module = self.get_module(DefId { index: CRATE_DEF_INDEX, ..module.nearest_parent_mod });
|
||||
let module = self
|
||||
.expect_module(module.def_id().map_or(LOCAL_CRATE, |def_id| def_id.krate).as_def_id());
|
||||
debug!(
|
||||
"resolve_crate_root({:?}): got module {:?} ({:?}) (ident.span = {:?})",
|
||||
ident,
|
||||
@ -2179,10 +2180,10 @@ fn resolve_crate_root(&mut self, ident: Ident) -> Module<'a> {
|
||||
}
|
||||
|
||||
fn resolve_self(&mut self, ctxt: &mut SyntaxContext, module: Module<'a>) -> Module<'a> {
|
||||
let mut module = self.get_module(module.nearest_parent_mod);
|
||||
let mut module = self.expect_module(module.nearest_parent_mod());
|
||||
while module.span.ctxt().normalize_to_macros_2_0() != *ctxt {
|
||||
let parent = module.parent.unwrap_or_else(|| self.macro_def_scope(ctxt.remove_mark()));
|
||||
module = self.get_module(parent.nearest_parent_mod);
|
||||
let parent = module.parent.unwrap_or_else(|| self.expn_def_scope(ctxt.remove_mark()));
|
||||
module = self.expect_module(parent.nearest_parent_mod());
|
||||
}
|
||||
module
|
||||
}
|
||||
@ -2896,7 +2897,7 @@ fn record_pat_span(&mut self, node: NodeId, span: Span) {
|
||||
}
|
||||
|
||||
fn is_accessible_from(&self, vis: ty::Visibility, module: Module<'a>) -> bool {
|
||||
vis.is_accessible_from(module.nearest_parent_mod, self)
|
||||
vis.is_accessible_from(module.nearest_parent_mod(), self)
|
||||
}
|
||||
|
||||
fn set_binding_parent_module(&mut self, binding: &'a NameBinding<'a>, module: Module<'a>) {
|
||||
@ -2920,7 +2921,7 @@ fn disambiguate_macro_rules_vs_modularized(
|
||||
self.binding_parent_modules.get(&PtrKey(modularized)),
|
||||
) {
|
||||
(Some(macro_rules), Some(modularized)) => {
|
||||
macro_rules.nearest_parent_mod == modularized.nearest_parent_mod
|
||||
macro_rules.nearest_parent_mod() == modularized.nearest_parent_mod()
|
||||
&& modularized.is_ancestor_of(macro_rules)
|
||||
}
|
||||
_ => false,
|
||||
@ -3265,7 +3266,7 @@ fn extern_prelude_get(
|
||||
} else {
|
||||
self.crate_loader.maybe_process_path_extern(ident.name)?
|
||||
};
|
||||
let crate_root = self.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX });
|
||||
let crate_root = self.expect_module(crate_id.as_def_id());
|
||||
Some(
|
||||
(crate_root, ty::Visibility::Public, DUMMY_SP, LocalExpnId::ROOT)
|
||||
.to_name_binding(self.arenas),
|
||||
@ -3306,7 +3307,7 @@ pub fn resolve_str_path_error(
|
||||
tokens: None,
|
||||
}
|
||||
};
|
||||
let module = self.get_module(module_id);
|
||||
let module = self.expect_module(module_id);
|
||||
let parent_scope = &ParentScope::module(module, self);
|
||||
let res = self.resolve_ast_path(&path, ns, parent_scope).map_err(|_| ())?;
|
||||
Ok((path, res))
|
||||
|
@ -225,7 +225,8 @@ fn expansion_for_ast_pass(
|
||||
features: &[Symbol],
|
||||
parent_module_id: Option<NodeId>,
|
||||
) -> LocalExpnId {
|
||||
let parent_module = parent_module_id.map(|module_id| self.local_def_id(module_id));
|
||||
let parent_module =
|
||||
parent_module_id.map(|module_id| self.local_def_id(module_id).to_def_id());
|
||||
let expn_id = LocalExpnId::fresh(
|
||||
ExpnData::allow_unstable(
|
||||
ExpnKind::AstPass(pass),
|
||||
@ -233,13 +234,13 @@ fn expansion_for_ast_pass(
|
||||
self.session.edition(),
|
||||
features.into(),
|
||||
None,
|
||||
parent_module.map(LocalDefId::to_def_id),
|
||||
parent_module,
|
||||
),
|
||||
self.create_stable_hashing_context(),
|
||||
);
|
||||
|
||||
let parent_scope = parent_module
|
||||
.map_or(self.empty_module, |parent_def_id| self.module_map[&parent_def_id]);
|
||||
let parent_scope =
|
||||
parent_module.map_or(self.empty_module, |def_id| self.expect_module(def_id));
|
||||
self.ast_transform_scopes.insert(expn_id, parent_scope);
|
||||
|
||||
expn_id
|
||||
@ -298,15 +299,14 @@ fn resolve_macro_invocation(
|
||||
)?;
|
||||
|
||||
let span = invoc.span();
|
||||
let def_id = res.opt_def_id();
|
||||
invoc_id.set_expn_data(
|
||||
ext.expn_data(
|
||||
parent_scope.expansion,
|
||||
span,
|
||||
fast_print_path(path),
|
||||
res.opt_def_id(),
|
||||
res.opt_def_id().map(|macro_def_id| {
|
||||
self.macro_def_scope_from_def_id(macro_def_id).nearest_parent_mod
|
||||
}),
|
||||
def_id,
|
||||
def_id.map(|def_id| self.macro_def_scope(def_id).nearest_parent_mod()),
|
||||
),
|
||||
self.create_stable_hashing_context(),
|
||||
);
|
||||
|
@ -759,7 +759,7 @@ fn traits_implemented_by(cx: &mut DocContext<'_>, type_: DefId, module: DefId) -
|
||||
let mut resolver = cx.resolver.borrow_mut();
|
||||
let in_scope_traits = cx.module_trait_cache.entry(module).or_insert_with(|| {
|
||||
resolver.access(|resolver| {
|
||||
let parent_scope = &ParentScope::module(resolver.get_module(module), resolver);
|
||||
let parent_scope = &ParentScope::module(resolver.expect_module(module), resolver);
|
||||
resolver
|
||||
.traits_in_scope(None, parent_scope, SyntaxContext::root(), None)
|
||||
.into_iter()
|
||||
|
@ -1,3 +1,5 @@
|
||||
// ignore-test (fails spuriously, see issue #89228)
|
||||
|
||||
// FIXME: If two macros in the same module have the same name
|
||||
// (yes, that's a thing), rustdoc lists both of them on the index page,
|
||||
// but only documents the first one on the page for the macro.
|
||||
|
Loading…
Reference in New Issue
Block a user