Skip to content

Commit fc36bf2

Browse files
authored
Rollup merge of #157379 - oli-obk:more-per-owner, r=petrochenkov
Some more simple per-owner resolver changes Fairly straight forward ones. The remaining data structures are either more involved or blocked on other changes waiting to happen. I especially like how the import res map stops being a map, as there can only be one import res per owner (each import is its own owner) r? @petrochenkov
2 parents 5478269 + 3fbaefd commit fc36bf2

7 files changed

Lines changed: 26 additions & 36 deletions

File tree

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ struct LoweringContext<'a, 'hir> {
126126
is_in_dyn_type: bool,
127127

128128
current_hir_id_owner: hir::OwnerId,
129-
owner: &'a PerOwnerResolverData,
129+
owner: &'a PerOwnerResolverData<'hir>,
130130
item_local_id_counter: hir::ItemLocalId,
131131
trait_map: ItemLocalMap<&'hir [TraitCandidate<'hir>]>,
132132

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

291-
/// Obtains per-namespace resolutions for `use` statement with the given `NodeId`.
292-
fn get_import_res(&self, id: NodeId) -> PerNS<Option<Res<NodeId>>> {
293-
self.import_res_map.get(&id).copied().unwrap_or_default()
294-
}
295-
296291
/// Obtain the list of lifetimes parameters to add to an item.
297292
///
298293
/// Extra lifetime parameters should only be added in places that can appear
@@ -810,8 +805,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
810805
self.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
811806
}
812807

813-
if let Some(traits) = self.resolver.trait_map.get(&ast_node_id) {
814-
self.trait_map.insert(hir_id.local_id, &traits[..]);
808+
if let Some(traits) = self.owner.trait_map.get(&ast_node_id) {
809+
self.trait_map.insert(hir_id.local_id, *traits);
815810
}
816811

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

858853
fn lower_import_res(&mut self, id: NodeId, span: Span) -> PerNS<Option<Res>> {
859-
let per_ns = self.resolver.get_import_res(id);
860-
let per_ns = per_ns.map(|res| res.map(|res| self.lower_res(res)));
854+
debug_assert_eq!(id, self.owner.id);
855+
let per_ns = self.owner.import_res.map(|res| res.map(|res| self.lower_res(res)));
861856
if per_ns.is_empty() {
862857
// Propagate the error to all namespaces, just to be sure.
863858
self.dcx().span_delayed_bug(span, "no resolution for an import");

compiler/rustc_hir/src/def.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,8 @@ impl IntoDiagArg for Namespace {
717717
}
718718

719719
/// Just a helper ‒ separate structure for each namespace.
720-
#[derive(Copy, Clone, Default, Debug, StableHash)]
720+
#[derive(Copy, Clone, Debug, StableHash)]
721+
#[derive_const(Default)]
721722
pub struct PerNS<T> {
722723
pub value_ns: T,
723724
pub type_ns: T,

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ pub use intrinsic::IntrinsicDef;
2727
use rustc_abi::{
2828
Align, FieldIdx, Integer, IntegerType, ReprFlags, ReprOptions, ScalableElt, VariantIdx,
2929
};
30-
use rustc_ast as ast;
3130
use rustc_ast::expand::typetree::{FncTree, Kind, Type, TypeTree};
3231
use rustc_ast::node_id::NodeMap;
32+
use rustc_ast::{self as ast};
3333
pub use rustc_ast_ir::{Movability, Mutability, try_visit};
3434
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
3535
use rustc_data_structures::intern::Interned;
@@ -204,7 +204,7 @@ pub struct ResolverGlobalCtxt {
204204
}
205205

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

217+
pub trait_map: NodeMap<&'tcx [hir::TraitCandidate<'tcx>]> = Default::default(),
218+
219+
/// Resolution for import nodes, which have multiple resolutions in different namespaces.
220+
pub import_res: hir::def::PerNS<Option<Res<ast::NodeId>>> = Default::default(),
221+
217222
/// The id of the owner
218223
pub id: ast::NodeId,
219224
/// The `DefId` of the owner, can't be found in `node_id_to_def_id`.
220225
pub def_id: LocalDefId,
221226
}
222227

223-
impl PerOwnerResolverData {
224-
pub fn new(id: ast::NodeId, def_id: LocalDefId) -> PerOwnerResolverData {
228+
impl<'tcx> PerOwnerResolverData<'tcx> {
229+
pub fn new(id: ast::NodeId, def_id: LocalDefId) -> PerOwnerResolverData<'tcx> {
225230
PerOwnerResolverData { id, def_id, .. }
226231
}
227232

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

250253
pub next_node_id: ast::NodeId,
251254

252-
pub owners: NodeMap<PerOwnerResolverData>,
253-
254-
pub trait_map: NodeMap<&'tcx [hir::TraitCandidate<'tcx>]>,
255+
pub owners: NodeMap<PerOwnerResolverData<'tcx>>,
255256

256257
/// Lints that were emitted by the resolver and early lints.
257258
pub lint_buffer: Steal<LintBuffer>,

compiler/rustc_resolve/src/check_unused.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,10 @@ impl<'a, 'ra, 'tcx> UnusedImportCheckVisitor<'a, 'ra, 'tcx> {
134134
match item.kind {
135135
ast::UseTreeKind::Simple(Some(ident)) => {
136136
if ident.name == kw::Underscore
137-
&& !self.r.import_res_map.get(&id).is_some_and(|per_ns| {
138-
matches!(
139-
per_ns.type_ns,
140-
Some(Res::Def(DefKind::Trait | DefKind::TraitAlias, _))
141-
)
142-
})
137+
&& !matches!(
138+
self.r.owners[&id].import_res.type_ns,
139+
Some(Res::Def(DefKind::Trait | DefKind::TraitAlias, _))
140+
)
143141
{
144142
self.unused_import(self.base_id).add(id);
145143
}

compiler/rustc_resolve/src/imports.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1642,7 +1642,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16421642
// purposes it's good enough to just favor one over the other.
16431643
self.per_ns(|this, ns| {
16441644
if let Some(binding) = bindings[ns].get().decl().map(|b| b.import_source()) {
1645-
this.import_res_map.entry(import_id).or_default()[ns] = Some(binding.res());
1645+
this.owners.get_mut(&import_id).unwrap().import_res[ns] = Some(binding.res());
16461646
}
16471647
});
16481648

compiler/rustc_resolve/src/late.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5388,7 +5388,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
53885388
ident.span,
53895389
Some((ident.name, ValueNS)),
53905390
);
5391-
self.r.trait_map.insert(node_id, traits);
5391+
self.r.current_owner.trait_map.insert(node_id, traits);
53925392
}
53935393

53945394
fn resolve_and_cache_rustdoc_path(&mut self, path_str: &str, ns: Namespace) -> Option<Res> {

compiler/rustc_resolve/src/lib.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,8 +1364,6 @@ pub struct Resolver<'ra, 'tcx> {
13641364

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

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

14921489
/// Preserves per owner data once the owner is finished resolving.
1493-
owners: NodeMap<PerOwnerResolverData>,
1490+
owners: NodeMap<PerOwnerResolverData<'tcx>>,
14941491

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

14981495
disambiguators: LocalDefIdMap<PerParentDisambiguatorState>,
14991496

@@ -1997,11 +1994,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
19971994
};
19981995
let ast_lowering = ty::ResolverAstLowering {
19991996
partial_res_map: self.partial_res_map,
2000-
import_res_map: self.import_res_map,
20011997
extra_lifetime_params_map: self.extra_lifetime_params_map,
20021998
next_node_id: self.next_node_id,
20031999
owners: self.owners,
2004-
trait_map: self.trait_map,
20052000
lint_buffer: Steal::new(self.lint_buffer),
20062001
delegation_infos: self.delegation_infos,
20072002
disambiguators,
@@ -2658,7 +2653,7 @@ fn with_owner<'ra, 'tcx, R: AsMut<Resolver<'ra, 'tcx>>, T>(
26582653
fn with_owner_tables<'ra, 'tcx, R: AsMut<Resolver<'ra, 'tcx>>, T>(
26592654
this: &mut R,
26602655
owner: NodeId,
2661-
tables: PerOwnerResolverData,
2656+
tables: PerOwnerResolverData<'tcx>,
26622657
work: impl FnOnce(&mut R) -> T,
26632658
) -> T {
26642659
debug_assert!(!this.as_mut().owners.contains_key(&owner));

0 commit comments

Comments
 (0)