Skip to content

Commit b7856de

Browse files
Rollup merge of #157100 - oli-obk:some_more_per_owner_things, r=petrochenkov
Some more per owner things A few easy ones to turn from a global hash table to a hash table (or even a bool) per owner, but still have to check perf first. r? @petrochenkov
2 parents b8c03ee + b7ea5d8 commit b7856de

7 files changed

Lines changed: 44 additions & 52 deletions

File tree

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1549,7 +1549,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
15491549
fn lower_loop_destination(&mut self, destination: Option<(NodeId, Label)>) -> hir::Destination {
15501550
let target_id = match destination {
15511551
Some((id, _)) => {
1552-
if let Some(loop_id) = self.resolver.get_label_res(id) {
1552+
if let Some(loop_id) = self.owner.get_label_res(id) {
15531553
let local_id = self.ident_and_label_to_local_id[&loop_id];
15541554
let loop_hir_id = HirId { owner: self.current_hir_id_owner, local_id };
15551555
Ok(loop_hir_id)

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -293,16 +293,6 @@ impl<'tcx> ResolverAstLowering<'tcx> {
293293
self.import_res_map.get(&id).copied().unwrap_or_default()
294294
}
295295

296-
/// Obtains resolution for a label with the given `NodeId`.
297-
fn get_label_res(&self, id: NodeId) -> Option<NodeId> {
298-
self.label_res_map.get(&id).copied()
299-
}
300-
301-
/// Obtains resolution for a lifetime with the given `NodeId`.
302-
fn get_lifetime_res(&self, id: NodeId) -> Option<LifetimeRes> {
303-
self.lifetimes_res_map.get(&id).copied()
304-
}
305-
306296
/// Obtain the list of lifetimes parameters to add to an item.
307297
///
308298
/// Extra lifetime parameters should only be added in places that can appear
@@ -321,10 +311,6 @@ impl<'tcx> ResolverAstLowering<'tcx> {
321311
fn owner_def_id(&self, id: NodeId) -> LocalDefId {
322312
self.owners[&id].def_id
323313
}
324-
325-
fn lifetime_elision_allowed(&self, id: NodeId) -> bool {
326-
self.lifetime_elision_allowed.contains(&id)
327-
}
328314
}
329315

330316
/// How relaxed bounds `?Trait` should be treated.
@@ -1612,7 +1598,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
16121598

16131599
None => {
16141600
let id = if let Some(LifetimeRes::ElidedAnchor { start, end }) =
1615-
self.resolver.get_lifetime_res(t.id)
1601+
self.owner.get_lifetime_res(t.id)
16161602
{
16171603
assert_eq!(start.plus(1), end);
16181604
start
@@ -1853,7 +1839,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
18531839
_ => hir::ImplicitSelfKind::None,
18541840
}
18551841
}))
1856-
.set_lifetime_elision_allowed(self.resolver.lifetime_elision_allowed(fn_node_id))
1842+
.set_lifetime_elision_allowed(
1843+
self.owner.id == fn_node_id && self.owner.lifetime_elision_allowed,
1844+
)
18571845
.set_c_variadic(c_variadic);
18581846

18591847
self.arena.alloc(hir::FnDecl { inputs, output, fn_decl_kind })
@@ -2019,7 +2007,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
20192007
source: LifetimeSource,
20202008
syntax: LifetimeSyntax,
20212009
) -> &'hir hir::Lifetime {
2022-
let res = if let Some(res) = self.resolver.get_lifetime_res(id) {
2010+
let res = if let Some(res) = self.owner.get_lifetime_res(id) {
20232011
match res {
20242012
LifetimeRes::Param { param, .. } => hir::LifetimeKind::Param(param),
20252013
LifetimeRes::Fresh { param, .. } => {
@@ -2105,13 +2093,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
21052093
// AST resolution emitted an error on those parameters, so we lower them using
21062094
// `ParamName::Error`.
21072095
let ident = self.lower_ident(param.ident);
2108-
let param_name = if let Some(LifetimeRes::Error(..)) =
2109-
self.resolver.get_lifetime_res(param.id)
2110-
{
2111-
ParamName::Error(ident)
2112-
} else {
2113-
ParamName::Plain(ident)
2114-
};
2096+
let param_name =
2097+
if let Some(LifetimeRes::Error(..)) = self.owner.get_lifetime_res(param.id) {
2098+
ParamName::Error(ident)
2099+
} else {
2100+
ParamName::Plain(ident)
2101+
};
21152102
let kind =
21162103
hir::GenericParamKind::Lifetime { kind: hir::LifetimeParamKind::Explicit };
21172104

compiler/rustc_ast_lowering/src/path.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use super::errors::{
1717
};
1818
use super::{
1919
AllowReturnTypeNotation, GenericArgsCtor, GenericArgsMode, ImplTraitContext, ImplTraitPosition,
20-
LifetimeRes, LoweringContext, ParamMode, ResolverAstLoweringExt,
20+
LifetimeRes, LoweringContext, ParamMode,
2121
};
2222

2323
impl<'hir> LoweringContext<'_, 'hir> {
@@ -422,7 +422,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
422422
segment_ident_span: Span,
423423
generic_args: &mut GenericArgsCtor<'hir>,
424424
) {
425-
let (start, end) = match self.resolver.get_lifetime_res(segment_id) {
425+
let (start, end) = match self.owner.get_lifetime_res(segment_id) {
426426
Some(LifetimeRes::ElidedAnchor { start, end }) => (start, end),
427427
None => return,
428428
Some(res) => {

compiler/rustc_middle/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#![feature(core_intrinsics)]
3838
#![feature(debug_closure_helpers)]
3939
#![feature(decl_macro)]
40+
#![feature(default_field_values)]
4041
#![feature(deref_patterns)]
4142
#![feature(discriminant_kind)]
4243
#![feature(extern_types)]

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use rustc_ast as ast;
3131
use rustc_ast::expand::typetree::{FncTree, Kind, Type, TypeTree};
3232
use rustc_ast::node_id::NodeMap;
3333
pub use rustc_ast_ir::{Movability, Mutability, try_visit};
34-
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
34+
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
3535
use rustc_data_structures::intern::Interned;
3636
use rustc_data_structures::stable_hash::{StableHash, StableHashCtxt, StableHasher};
3737
use rustc_data_structures::steal::Steal;
@@ -205,7 +205,15 @@ pub struct ResolverGlobalCtxt {
205205

206206
#[derive(Debug)]
207207
pub struct PerOwnerResolverData {
208-
pub node_id_to_def_id: NodeMap<LocalDefId>,
208+
pub node_id_to_def_id: NodeMap<LocalDefId> = Default::default(),
209+
/// Whether lifetime elision was successful.
210+
pub lifetime_elision_allowed: bool = false,
211+
/// Resolutions for labels.
212+
/// Maps from NodeId of the break/continue expression to the NodeId of their corresponding blocks or loops.
213+
pub label_res_map: NodeMap<ast::NodeId> = Default::default(),
214+
/// Resolutions for lifetimes.
215+
pub lifetimes_res_map: NodeMap<LifetimeRes> = Default::default(),
216+
209217
/// The id of the owner
210218
pub id: ast::NodeId,
211219
/// The `DefId` of the owner, can't be found in `node_id_to_def_id`.
@@ -214,7 +222,17 @@ pub struct PerOwnerResolverData {
214222

215223
impl PerOwnerResolverData {
216224
pub fn new(id: ast::NodeId, def_id: LocalDefId) -> PerOwnerResolverData {
217-
PerOwnerResolverData { node_id_to_def_id: Default::default(), id, def_id }
225+
PerOwnerResolverData { id, def_id, .. }
226+
}
227+
228+
/// Obtains resolution for a label with the given `NodeId`.
229+
pub fn get_label_res(&self, id: ast::NodeId) -> Option<ast::NodeId> {
230+
self.label_res_map.get(&id).copied()
231+
}
232+
233+
/// Obtains resolution for a lifetime with the given `NodeId`.
234+
pub fn get_lifetime_res(&self, id: ast::NodeId) -> Option<LifetimeRes> {
235+
self.lifetimes_res_map.get(&id).copied()
218236
}
219237
}
220238

@@ -226,10 +244,6 @@ pub struct ResolverAstLowering<'tcx> {
226244
pub partial_res_map: NodeMap<hir::def::PartialRes>,
227245
/// Resolutions for import nodes, which have multiple resolutions in different namespaces.
228246
pub import_res_map: NodeMap<hir::def::PerNS<Option<Res<ast::NodeId>>>>,
229-
/// Resolutions for labels (node IDs of their corresponding blocks or loops).
230-
pub label_res_map: NodeMap<ast::NodeId>,
231-
/// Resolutions for lifetimes.
232-
pub lifetimes_res_map: NodeMap<LifetimeRes>,
233247
/// Lifetime parameters that lowering will have to introduce.
234248
pub extra_lifetime_params_map: NodeMap<Vec<(Ident, ast::NodeId, MissingLifetimeKind)>>,
235249

@@ -238,8 +252,6 @@ pub struct ResolverAstLowering<'tcx> {
238252
pub owners: NodeMap<PerOwnerResolverData>,
239253

240254
pub trait_map: NodeMap<&'tcx [hir::TraitCandidate<'tcx>]>,
241-
/// List functions and methods for which lifetime elision was successful.
242-
pub lifetime_elision_allowed: FxHashSet<ast::NodeId>,
243255

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

compiler/rustc_resolve/src/late.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2406,7 +2406,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
24062406
/// Define a new lifetime (e.g. in generics)
24072407
#[instrument(level = "debug", skip(self))]
24082408
fn record_lifetime_def(&mut self, id: NodeId, res: LifetimeRes) {
2409-
if let Some(prev_res) = self.r.lifetimes_res_map.insert(id, res) {
2409+
if let Some(prev_res) = self.r.current_owner.lifetimes_res_map.insert(id, res) {
24102410
panic!(
24112411
"lifetime parameter {id:?} resolved multiple times ({prev_res:?} before, {res:?} now)"
24122412
)
@@ -2434,7 +2434,9 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
24342434

24352435
let outer_failures = take(&mut this.diag_metadata.current_elision_failures);
24362436
let output_rib = if let Ok(res) = elision_lifetime.as_ref() {
2437-
this.r.lifetime_elision_allowed.insert(fn_id);
2437+
if fn_id == this.r.current_owner.id {
2438+
this.r.current_owner.lifetime_elision_allowed = true;
2439+
}
24382440
LifetimeRibKind::Elided(*res)
24392441
} else {
24402442
LifetimeRibKind::ElisionFailure
@@ -2609,11 +2611,11 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
26092611
let lt_id = if let Some(lt) = lt {
26102612
lt.id
26112613
} else {
2612-
let res = self.r.lifetimes_res_map[&ty.id];
2614+
let res = self.r.current_owner.lifetimes_res_map[&ty.id];
26132615
let LifetimeRes::ElidedAnchor { start, .. } = res else { bug!() };
26142616
start
26152617
};
2616-
let lt_res = self.r.lifetimes_res_map[&lt_id];
2618+
let lt_res = self.r.current_owner.lifetimes_res_map[&lt_id];
26172619
trace!("FindReferenceVisitor inserting res={:?}", lt_res);
26182620
self.lifetime.insert(lt_res);
26192621
}
@@ -5197,7 +5199,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
51975199
match self.resolve_label(label.ident) {
51985200
Ok((node_id, _)) => {
51995201
// Since this res is a label, it is never read.
5200-
self.r.label_res_map.insert(expr.id, node_id);
5202+
self.r.current_owner.label_res_map.insert(expr.id, node_id);
52015203
self.diag_metadata.unused_labels.swap_remove(&node_id);
52025204
}
52035205
Err(error) => {

compiler/rustc_resolve/src/lib.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ use rustc_feature::BUILTIN_ATTRIBUTES;
5353
use rustc_hir::attrs::StrippedCfgItem;
5454
use rustc_hir::def::Namespace::{self, *};
5555
use rustc_hir::def::{
56-
self, CtorOf, DefKind, DocLinkResMap, LifetimeRes, MacroKinds, NonMacroAttrKind, PartialRes,
57-
PerNS,
56+
self, CtorOf, DefKind, DocLinkResMap, MacroKinds, NonMacroAttrKind, PartialRes, PerNS,
5857
};
5958
use rustc_hir::def_id::{CRATE_DEF_ID, CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalDefIdMap};
6059
use rustc_hir::definitions::{PerParentDisambiguatorState, PerParentDisambiguatorsMap};
@@ -1369,10 +1368,6 @@ pub struct Resolver<'ra, 'tcx> {
13691368
import_res_map: NodeMap<PerNS<Option<Res>>> = Default::default(),
13701369
/// An import will be inserted into this map if it has been used.
13711370
import_use_map: FxHashMap<Import<'ra>, Used> = default::fx_hash_map(),
1372-
/// Resolutions for labels (node IDs of their corresponding blocks or loops).
1373-
label_res_map: NodeMap<NodeId> = Default::default(),
1374-
/// Resolutions for lifetimes.
1375-
lifetimes_res_map: NodeMap<LifetimeRes> = Default::default(),
13761371
/// Lifetime parameters that lowering will have to introduce.
13771372
extra_lifetime_params_map: NodeMap<Vec<(Ident, NodeId, MissingLifetimeKind)>> = Default::default(),
13781373

@@ -1522,8 +1517,6 @@ pub struct Resolver<'ra, 'tcx> {
15221517
/// they are declared in the static array generated by proc_macro_harness.
15231518
proc_macros: Vec<LocalDefId> = Vec::new(),
15241519
confused_type_with_std_module: FxIndexMap<Span, Span>,
1525-
/// Whether lifetime elision was successful.
1526-
lifetime_elision_allowed: FxHashSet<NodeId> = default::fx_hash_set(),
15271520

15281521
/// Names of items that were stripped out via cfg with their corresponding cfg meta item.
15291522
stripped_cfg_items: Vec<StrippedCfgItem<NodeId>> = Vec::new(),
@@ -2005,13 +1998,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
20051998
let ast_lowering = ty::ResolverAstLowering {
20061999
partial_res_map: self.partial_res_map,
20072000
import_res_map: self.import_res_map,
2008-
label_res_map: self.label_res_map,
2009-
lifetimes_res_map: self.lifetimes_res_map,
20102001
extra_lifetime_params_map: self.extra_lifetime_params_map,
20112002
next_node_id: self.next_node_id,
20122003
owners: self.owners,
20132004
trait_map: self.trait_map,
2014-
lifetime_elision_allowed: self.lifetime_elision_allowed,
20152005
lint_buffer: Steal::new(self.lint_buffer),
20162006
delegation_infos: self.delegation_infos,
20172007
disambiguators,

0 commit comments

Comments
 (0)