Skip to content
Merged
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
15 changes: 5 additions & 10 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ struct LoweringContext<'a, 'hir> {
is_in_dyn_type: bool,

current_hir_id_owner: hir::OwnerId,
owner: &'a PerOwnerResolverData,
owner: &'a PerOwnerResolverData<'hir>,
item_local_id_counter: hir::ItemLocalId,
trait_map: ItemLocalMap<&'hir [TraitCandidate<'hir>]>,

Expand Down Expand Up @@ -288,11 +288,6 @@ impl<'tcx> ResolverAstLowering<'tcx> {
.map(|fn_indexes| fn_indexes.iter().map(|(num, _)| *num).collect())
}

/// Obtains per-namespace resolutions for `use` statement with the given `NodeId`.
fn get_import_res(&self, id: NodeId) -> PerNS<Option<Res<NodeId>>> {
self.import_res_map.get(&id).copied().unwrap_or_default()
}

/// Obtain the list of lifetimes parameters to add to an item.
///
/// Extra lifetime parameters should only be added in places that can appear
Expand Down Expand Up @@ -810,8 +805,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
self.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
}

if let Some(traits) = self.resolver.trait_map.get(&ast_node_id) {
self.trait_map.insert(hir_id.local_id, &traits[..]);
if let Some(traits) = self.owner.trait_map.get(&ast_node_id) {
self.trait_map.insert(hir_id.local_id, *traits);
}

// Check whether the same `NodeId` is lowered more than once.
Expand Down Expand Up @@ -856,8 +851,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
}

fn lower_import_res(&mut self, id: NodeId, span: Span) -> PerNS<Option<Res>> {
let per_ns = self.resolver.get_import_res(id);
let per_ns = per_ns.map(|res| res.map(|res| self.lower_res(res)));
debug_assert_eq!(id, self.owner.id);
let per_ns = self.owner.import_res.map(|res| res.map(|res| self.lower_res(res)));
if per_ns.is_empty() {
// Propagate the error to all namespaces, just to be sure.
self.dcx().span_delayed_bug(span, "no resolution for an import");
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_hir/src/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,8 @@ impl IntoDiagArg for Namespace {
}

/// Just a helper ‒ separate structure for each namespace.
#[derive(Copy, Clone, Default, Debug, StableHash)]
#[derive(Copy, Clone, Debug, StableHash)]
#[derive_const(Default)]
pub struct PerNS<T> {
pub value_ns: T,
pub type_ns: T,
Expand Down
19 changes: 10 additions & 9 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ pub use intrinsic::IntrinsicDef;
use rustc_abi::{
Align, FieldIdx, Integer, IntegerType, ReprFlags, ReprOptions, ScalableElt, VariantIdx,
};
use rustc_ast as ast;
use rustc_ast::expand::typetree::{FncTree, Kind, Type, TypeTree};
use rustc_ast::node_id::NodeMap;
use rustc_ast::{self as ast};
pub use rustc_ast_ir::{Movability, Mutability, try_visit};
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
use rustc_data_structures::intern::Interned;
Expand Down Expand Up @@ -204,7 +204,7 @@ pub struct ResolverGlobalCtxt {
}

#[derive(Debug)]
pub struct PerOwnerResolverData {
pub struct PerOwnerResolverData<'tcx> {
pub node_id_to_def_id: NodeMap<LocalDefId> = Default::default(),
/// Whether lifetime elision was successful.
pub lifetime_elision_allowed: bool = false,
Expand All @@ -214,14 +214,19 @@ pub struct PerOwnerResolverData {
/// Resolutions for lifetimes.
pub lifetimes_res_map: NodeMap<LifetimeRes> = Default::default(),

pub trait_map: NodeMap<&'tcx [hir::TraitCandidate<'tcx>]> = Default::default(),

/// Resolution for import nodes, which have multiple resolutions in different namespaces.
pub import_res: hir::def::PerNS<Option<Res<ast::NodeId>>> = Default::default(),

/// The id of the owner
pub id: ast::NodeId,
/// The `DefId` of the owner, can't be found in `node_id_to_def_id`.
pub def_id: LocalDefId,
}

impl PerOwnerResolverData {
pub fn new(id: ast::NodeId, def_id: LocalDefId) -> PerOwnerResolverData {
impl<'tcx> PerOwnerResolverData<'tcx> {
pub fn new(id: ast::NodeId, def_id: LocalDefId) -> PerOwnerResolverData<'tcx> {
PerOwnerResolverData { id, def_id, .. }
}

Expand All @@ -242,16 +247,12 @@ impl PerOwnerResolverData {
pub struct ResolverAstLowering<'tcx> {
/// Resolutions for nodes that have a single resolution.
pub partial_res_map: NodeMap<hir::def::PartialRes>,
/// Resolutions for import nodes, which have multiple resolutions in different namespaces.
pub import_res_map: NodeMap<hir::def::PerNS<Option<Res<ast::NodeId>>>>,
/// Lifetime parameters that lowering will have to introduce.
pub extra_lifetime_params_map: NodeMap<Vec<(Ident, ast::NodeId, MissingLifetimeKind)>>,

pub next_node_id: ast::NodeId,

pub owners: NodeMap<PerOwnerResolverData>,

pub trait_map: NodeMap<&'tcx [hir::TraitCandidate<'tcx>]>,
pub owners: NodeMap<PerOwnerResolverData<'tcx>>,

/// Lints that were emitted by the resolver and early lints.
pub lint_buffer: Steal<LintBuffer>,
Expand Down
10 changes: 4 additions & 6 deletions compiler/rustc_resolve/src/check_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,10 @@ impl<'a, 'ra, 'tcx> UnusedImportCheckVisitor<'a, 'ra, 'tcx> {
match item.kind {
ast::UseTreeKind::Simple(Some(ident)) => {
if ident.name == kw::Underscore
&& !self.r.import_res_map.get(&id).is_some_and(|per_ns| {
matches!(
per_ns.type_ns,
Some(Res::Def(DefKind::Trait | DefKind::TraitAlias, _))
)
})
&& !matches!(
self.r.owners[&id].import_res.type_ns,
Some(Res::Def(DefKind::Trait | DefKind::TraitAlias, _))
)
{
self.unused_import(self.base_id).add(id);
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1642,7 +1642,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
// purposes it's good enough to just favor one over the other.
self.per_ns(|this, ns| {
if let Some(binding) = bindings[ns].get().decl().map(|b| b.import_source()) {
this.import_res_map.entry(import_id).or_default()[ns] = Some(binding.res());
this.owners.get_mut(&import_id).unwrap().import_res[ns] = Some(binding.res());
}
});

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5388,7 +5388,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
ident.span,
Some((ident.name, ValueNS)),
);
self.r.trait_map.insert(node_id, traits);
self.r.current_owner.trait_map.insert(node_id, traits);
}

fn resolve_and_cache_rustdoc_path(&mut self, path_str: &str, ns: Namespace) -> Option<Res> {
Expand Down
11 changes: 3 additions & 8 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1364,8 +1364,6 @@ pub struct Resolver<'ra, 'tcx> {

/// Resolutions for nodes that have a single resolution.
partial_res_map: NodeMap<PartialRes> = Default::default(),
/// Resolutions for import nodes, which have multiple resolutions in different namespaces.
import_res_map: NodeMap<PerNS<Option<Res>>> = Default::default(),
/// An import will be inserted into this map if it has been used.
import_use_map: FxHashMap<Import<'ra>, Used> = default::fx_hash_map(),
/// Lifetime parameters that lowering will have to introduce.
Expand All @@ -1375,7 +1373,6 @@ pub struct Resolver<'ra, 'tcx> {
extern_crate_map: UnordMap<LocalDefId, CrateNum> = Default::default(),
module_children: LocalDefIdMap<Vec<ModChild>> = Default::default(),
ambig_module_children: LocalDefIdMap<Vec<AmbigModChild>> = Default::default(),
trait_map: NodeMap<&'tcx [TraitCandidate<'tcx>]> = Default::default(),

/// A map from nodes to anonymous modules.
/// Anonymous modules are pseudo-modules that are implicitly created around items
Expand Down Expand Up @@ -1490,10 +1487,10 @@ pub struct Resolver<'ra, 'tcx> {
next_node_id: NodeId = CRATE_NODE_ID,

/// Preserves per owner data once the owner is finished resolving.
owners: NodeMap<PerOwnerResolverData>,
owners: NodeMap<PerOwnerResolverData<'tcx>>,

/// An entry of `owners` that gets taken out and reinserted whenever an owner is handled.
current_owner: PerOwnerResolverData,
current_owner: PerOwnerResolverData<'tcx>,

disambiguators: LocalDefIdMap<PerParentDisambiguatorState>,

Expand Down Expand Up @@ -1997,11 +1994,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
};
let ast_lowering = ty::ResolverAstLowering {
partial_res_map: self.partial_res_map,
import_res_map: self.import_res_map,
extra_lifetime_params_map: self.extra_lifetime_params_map,
next_node_id: self.next_node_id,
owners: self.owners,
trait_map: self.trait_map,
lint_buffer: Steal::new(self.lint_buffer),
delegation_infos: self.delegation_infos,
disambiguators,
Expand Down Expand Up @@ -2658,7 +2653,7 @@ fn with_owner<'ra, 'tcx, R: AsMut<Resolver<'ra, 'tcx>>, T>(
fn with_owner_tables<'ra, 'tcx, R: AsMut<Resolver<'ra, 'tcx>>, T>(
this: &mut R,
owner: NodeId,
tables: PerOwnerResolverData,
tables: PerOwnerResolverData<'tcx>,
work: impl FnOnce(&mut R) -> T,
) -> T {
debug_assert!(!this.as_mut().owners.contains_key(&owner));
Expand Down
Loading