Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 43 additions & 36 deletions compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ use crate::imports::{ImportData, ImportKind, OnUnknownData};
use crate::macros::{MacroRulesDecl, MacroRulesScope, MacroRulesScopeRef};
use crate::ref_mut::CmCell;
use crate::{
BindingKey, Decl, DeclData, DeclKind, ExternPreludeEntry, Finalize, IdentKey, MacroData,
Module, ModuleKind, ModuleOrUniformRoot, ParentScope, PathResult, Res, ResolutionError,
Resolver, Segment, Used, VisResolutionError, errors,
BindingKey, Decl, DeclData, DeclKind, ExternModule, ExternPreludeEntry, Finalize, IdentKey,
LocalModule, MacroData, Module, ModuleKind, ModuleOrUniformRoot, ParentScope, PathResult, Res,
ResolutionError, Resolver, Segment, Used, VisResolutionError, errors,
};

impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
Expand All @@ -62,23 +62,24 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
/// Create a name definition from the given components, and put it into the local module.
fn define_local(
&mut self,
parent: Module<'ra>,
parent: LocalModule<'ra>,
orig_ident: Ident,
ns: Namespace,
res: Res,
vis: Visibility,
span: Span,
expn_id: LocalExpnId,
) {
let decl = self.arenas.new_def_decl(res, vis.to_def_id(), span, expn_id, Some(parent));
let decl =
self.arenas.new_def_decl(res, vis.to_def_id(), span, expn_id, Some(parent.to_module()));
let ident = IdentKey::new(orig_ident);
self.plant_decl_into_local_module(ident, orig_ident.span, ns, decl);
}

/// Create a name definition from the given components, and put it into the extern module.
fn define_extern(
&self,
parent: Module<'ra>,
parent: ExternModule<'ra>,
ident: IdentKey,
orig_ident_span: Span,
ns: Namespace,
Expand All @@ -97,15 +98,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
vis: CmCell::new(vis),
span,
expansion,
parent_module: Some(parent),
parent_module: Some(parent.to_module()),
});
// Even if underscore names cannot be looked up, we still need to add them to modules,
// because they can be fetched by glob imports from those modules, and bring traits
// into scope both directly and through glob imports.
let key =
BindingKey::new_disambiguated(ident, ns, || (child_index + 1).try_into().unwrap()); // 0 indicates no underscore
if self
.resolution_or_default(parent, key, orig_ident_span)
.resolution_or_default(parent.to_module(), key, orig_ident_span)
.borrow_mut_unchecked()
.non_glob_decl
.replace(decl)
Expand Down Expand Up @@ -149,30 +150,30 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
/// returns `None`.
pub(crate) fn get_module(&self, def_id: DefId) -> Option<Module<'ra>> {
match def_id.as_local() {
Some(local_def_id) => self.local_module_map.get(&local_def_id).copied(),
Some(local_def_id) => self.local_module_map.get(&local_def_id).map(|m| m.to_module()),
None => {
if let module @ Some(..) = self.extern_module_map.borrow().get(&def_id) {
return module.copied();
return module.map(|m| m.to_module());
}

// Query `def_kind` is not used because query system overhead is too expensive here.
let def_kind = self.cstore().def_kind_untracked(self.tcx, def_id);
if def_kind.is_module_like() {
let parent = self
.tcx
.opt_parent(def_id)
.map(|parent_id| self.get_nearest_non_block_module(parent_id));
let parent = self.tcx.opt_parent(def_id).map(|parent_id| {
self.get_nearest_non_block_module(parent_id).expect_extern()
});
// Query `expn_that_defined` is not used because
// hashing spans in its result is expensive.
let expn_id = self.cstore().expn_that_defined_untracked(self.tcx, def_id);
return Some(self.new_extern_module(
let module = self.new_extern_module(
parent,
ModuleKind::Def(def_kind, def_id, Some(self.tcx.item_name(def_id))),
expn_id,
self.def_span(def_id),
// FIXME: Account for `#[no_implicit_prelude]` attributes.
parent.is_some_and(|module| module.no_implicit_prelude),
));
);
return Some(module.to_module());
}

None
Expand All @@ -186,13 +187,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
None => expn_id
.as_local()
.and_then(|expn_id| self.ast_transform_scopes.get(&expn_id).copied())
.unwrap_or(self.graph_root),
.unwrap_or(self.graph_root)
.to_module(),
}
}

pub(crate) fn macro_def_scope(&self, def_id: DefId) -> Module<'ra> {
if let Some(id) = def_id.as_local() {
self.local_macro_def_scopes[&id]
self.local_macro_def_scopes[&id].to_module()
} else {
self.get_nearest_non_block_module(def_id)
}
Expand Down Expand Up @@ -246,10 +248,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
visitor.parent_scope.macro_rules
}

pub(crate) fn build_reduced_graph_external(&self, module: Module<'ra>) {
pub(crate) fn build_reduced_graph_external(&self, module: ExternModule<'ra>) {
let def_id = module.def_id();
let children = self.tcx.module_children(def_id);
let parent_scope = ParentScope::module(module, self.arenas);
let parent_scope = ParentScope::module(module.to_module(), self.arenas);
for (i, child) in children.iter().enumerate() {
self.build_reduced_graph_for_external_crate_res(child, parent_scope, i, None)
}
Expand All @@ -273,7 +275,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
child_index: usize,
ambig_child: Option<&ModChild>,
) {
let parent = parent_scope.module;
let parent = parent_scope.module.expect_extern();
let child_span = |this: &Self, reexport_chain: &[Reexport], res: def::Res<_>| {
this.def_span(
reexport_chain
Expand All @@ -291,7 +293,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
let ModChild { ident: _, res, vis, ref reexport_chain } = *ambig_child;
let span = child_span(self, reexport_chain, res);
let res = res.expect_non_local();
self.arenas.new_def_decl(res, vis, span, expansion, Some(parent))
self.arenas.new_def_decl(res, vis, span, expansion, Some(parent.to_module()))
});

// Record primary definitions.
Expand Down Expand Up @@ -801,7 +803,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
adt_span: Span,
) {
let parent_scope = &self.parent_scope;
let parent = parent_scope.module;
let parent = parent_scope.module.expect_local();
let expansion = parent_scope.expansion;

// Define a name in the type namespace if it is not anonymous.
Expand All @@ -817,7 +819,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
/// Constructs the reduced graph for one item.
fn build_reduced_graph_for_item(&mut self, item: &'a Item) {
let parent_scope = &self.parent_scope;
let parent = parent_scope.module;
let parent = parent_scope.module.expect_local();
let expansion = parent_scope.expansion;
let sp = item.span;
let vis = self.resolve_visibility(&item.vis);
Expand Down Expand Up @@ -862,14 +864,15 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
{
self.r.mods_with_parse_errors.insert(def_id);
}
self.parent_scope.module = self.r.new_local_module(
let module = self.r.new_local_module(
Some(parent),
ModuleKind::Def(def_kind, def_id, Some(ident.name)),
expansion.to_expn_id(),
item.span,
parent.no_implicit_prelude
|| ast::attr::contains_name(&item.attrs, sym::no_implicit_prelude),
);
self.parent_scope.module = module.to_module();
}

// These items live in the value namespace.
Expand All @@ -895,13 +898,14 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
ItemKind::Enum(ident, _, _) | ItemKind::Trait(box ast::Trait { ident, .. }) => {
self.r.define_local(parent, ident, TypeNS, res, vis, sp, expansion);

self.parent_scope.module = self.r.new_local_module(
let module = self.r.new_local_module(
Some(parent),
ModuleKind::Def(def_kind, def_id, Some(ident.name)),
expansion.to_expn_id(),
item.span,
parent.no_implicit_prelude,
);
self.parent_scope.module = module.to_module();
}

// These items live in both the type and value namespaces.
Expand Down Expand Up @@ -997,7 +1001,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
self.r.dcx().emit_err(errors::ExternCrateSelfRequiresRenaming { span: sp });
return;
} else if orig_name == Some(kw::SelfLower) {
Some(self.r.graph_root)
Some(self.r.graph_root.to_module())
} else {
let tcx = self.r.tcx;
let crate_id = self.r.cstore_mut().process_extern_crate(
Expand Down Expand Up @@ -1038,7 +1042,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
self.r.potentially_unused_imports.push(import);
let import_decl = self.r.new_import_decl(decl, import);
let ident = IdentKey::new(orig_ident);
if ident.name != kw::Underscore && parent == self.r.graph_root {
if ident.name != kw::Underscore && parent == self.r.graph_root.to_module() {
// FIXME: this error is technically unnecessary now when extern prelude is split into
// two scopes, remove it with lang team approval.
if let Some(entry) = self.r.extern_prelude.get(&ident)
Expand Down Expand Up @@ -1083,15 +1087,15 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
ForeignItemKind::TyAlias(..) => TypeNS,
ForeignItemKind::MacCall(..) => unreachable!(),
};
let parent = self.parent_scope.module;
let parent = self.parent_scope.module.expect_local();
let expansion = self.parent_scope.expansion;
let vis = self.resolve_visibility(&item.vis);
self.r.define_local(parent, ident, ns, self.res(def_id), vis, item.span, expansion);
self.r.feed_visibility(feed, vis);
}

fn build_reduced_graph_for_block(&mut self, block: &Block) {
let parent = self.parent_scope.module;
let parent = self.parent_scope.module.expect_local();
let expansion = self.parent_scope.expansion;
if self.block_needs_anonymous_module(block) {
let module = self.r.new_local_module(
Expand All @@ -1102,7 +1106,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
parent.no_implicit_prelude,
);
self.r.block_map.insert(block.id, module);
self.parent_scope.module = module; // Descend into the block.
self.parent_scope.module = module.to_module(); // Descend into the block.
}
}

Expand Down Expand Up @@ -1302,7 +1306,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
_ => unreachable!(),
};

self.r.local_macro_def_scopes.insert(def_id, parent_scope.module);
self.r.local_macro_def_scopes.insert(def_id, parent_scope.module.expect_local());

if macro_rules {
let ident = IdentKey::new(orig_ident);
Expand All @@ -1325,7 +1329,10 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
let import = self.r.arenas.alloc_import(ImportData {
kind: ImportKind::MacroExport,
root_id: item.id,
parent_scope: ParentScope { module: self.r.graph_root, ..parent_scope },
parent_scope: ParentScope {
module: self.r.graph_root.to_module(),
..parent_scope
},
imported_module: CmCell::new(None),
has_attributes: false,
use_span_with_attributes: span,
Expand Down Expand Up @@ -1356,7 +1363,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
self.r.macro_rules_scopes.insert(def_id, scope);
scope
} else {
let module = parent_scope.module;
let module = parent_scope.module.expect_local();
let vis = match item.kind {
// Visibilities must not be resolved non-speculatively twice
// and we already resolved this one as a `fn` item visibility.
Expand Down Expand Up @@ -1504,7 +1511,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
}

if ctxt == AssocCtxt::Trait {
let parent = self.parent_scope.module;
let parent = self.parent_scope.module.expect_local();
let expansion = self.parent_scope.expansion;
self.r.define_local(parent, ident, ns, self.res(def_id), vis, item.span, expansion);
} else if !matches!(&item.kind, AssocItemKind::Delegation(deleg) if deleg.from_glob)
Expand Down Expand Up @@ -1586,7 +1593,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
return;
}

let parent = self.parent_scope.module;
let parent = self.parent_scope.module.expect_local();
let expn_id = self.parent_scope.expansion;
let ident = variant.ident;

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/check_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ impl Resolver<'_, '_> {
let unused_imports = visitor.unused_imports;
let mut check_redundant_imports = FxIndexSet::default();
for module in &self.local_modules {
for (_key, resolution) in self.resolutions(*module).borrow().iter() {
for (_key, resolution) in self.resolutions(module.to_module()).borrow().iter() {
if let Some(decl) = resolution.borrow().best_decl()
&& let DeclKind::Import { import, .. } = decl.kind
&& let ImportKind::Single { id, .. } = import.kind
Expand Down
10 changes: 6 additions & 4 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1585,7 +1585,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
lookup_ident,
namespace,
parent_scope,
self.graph_root,
self.graph_root.to_module(),
crate_path,
&filter_fn,
);
Expand Down Expand Up @@ -2074,7 +2074,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {

if kind != AmbiguityKind::GlobVsGlob {
if let Scope::ModuleNonGlobs(module, _) | Scope::ModuleGlobs(module, _) = scope {
if module == self.graph_root {
if module == self.graph_root.to_module() {
help_msgs.push(format!(
"use `crate::{ident}` to refer to this {thing} unambiguously"
));
Expand Down Expand Up @@ -2452,7 +2452,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
self.local_module_map
.iter()
.filter(|(_, module)| {
current_module.is_ancestor_of(**module) && current_module != **module
let module = module.to_module();
current_module.is_ancestor_of(module) && current_module != module
})
.flat_map(|(_, module)| module.kind.name()),
)
Expand All @@ -2461,7 +2462,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
.borrow()
.iter()
.filter(|(_, module)| {
current_module.is_ancestor_of(**module) && current_module != **module
let module = module.to_module();
current_module.is_ancestor_of(module) && current_module != module
})
.flat_map(|(_, module)| module.kind.name()),
)
Expand Down
16 changes: 8 additions & 8 deletions compiler/rustc_resolve/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ use crate::late::{
use crate::macros::{MacroRulesScope, sub_namespace_match};
use crate::{
AmbiguityError, AmbiguityKind, AmbiguityWarning, BindingKey, CmResolver, Decl, DeclKind,
Determinacy, Finalize, IdentKey, ImportKind, LateDecl, Module, ModuleKind, ModuleOrUniformRoot,
ParentScope, PathResult, PrivacyError, Res, ResolutionError, Resolver, Scope, ScopeSet,
Segment, Stage, Symbol, Used, errors,
Determinacy, Finalize, IdentKey, ImportKind, LateDecl, LocalModule, Module, ModuleKind,
ModuleOrUniformRoot, ParentScope, PathResult, PrivacyError, Res, ResolutionError, Resolver,
Scope, ScopeSet, Segment, Stage, Symbol, Used, errors,
};

#[derive(Copy, Clone)]
Expand Down Expand Up @@ -346,7 +346,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
} else if let RibKind::Block(Some(module)) = rib.kind
&& let Ok(binding) = self.cm().resolve_ident_in_scope_set(
ident,
ScopeSet::Module(ns, module),
ScopeSet::Module(ns, module.to_module()),
parent_scope,
finalize.map(|finalize| Finalize { used: Used::Scope, ..finalize }),
ignore_decl,
Expand All @@ -357,7 +357,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
return Some(LateDecl::Decl(binding));
} else if let RibKind::Module(module) = rib.kind {
// Encountered a module item, abandon ribs and look into that module and preludes.
let parent_scope = &ParentScope { module, ..*parent_scope };
let parent_scope = &ParentScope { module: module.to_module(), ..*parent_scope };
let finalize = finalize.map(|f| Finalize { stage: Stage::Late, ..f });
return self
.cm()
Expand Down Expand Up @@ -658,7 +658,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
)
};
let binding = self.reborrow().resolve_ident_in_module_globs_unadjusted(
module,
module.expect_local(),
ident,
orig_ident_span,
ns,
Expand Down Expand Up @@ -1122,7 +1122,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
/// Attempts to resolve `ident` in namespace `ns` of glob bindings in `module`.
fn resolve_ident_in_module_globs_unadjusted<'r>(
mut self: CmResolver<'r, 'ra, 'tcx>,
module: Module<'ra>,
module: LocalModule<'ra>,
ident: IdentKey,
orig_ident_span: Span,
ns: Namespace,
Expand All @@ -1137,7 +1137,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
// doesn't need to be mutable. It will fail when there is a cycle of imports, and without
// the exclusive access infinite recursion will crash the compiler with stack overflow.
let resolution = &*self
.resolution_or_default(module, key, orig_ident_span)
.resolution_or_default(module.to_module(), key, orig_ident_span)
.try_borrow_mut_unchecked()
.map_err(|_| ControlFlow::Continue(Determined))?;

Expand Down
Loading
Loading