Skip to content

Commit b72bbf8

Browse files
Rollup merge of rust-lang#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 60481a5 + b69baba commit b72bbf8

7 files changed

Lines changed: 44 additions & 51 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.
@@ -1625,7 +1611,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
16251611

16261612
None => {
16271613
let id = if let Some(LifetimeRes::ElidedAnchor { start, end }) =
1628-
self.resolver.get_lifetime_res(t.id)
1614+
self.owner.get_lifetime_res(t.id)
16291615
{
16301616
assert_eq!(start.plus(1), end);
16311617
start
@@ -1866,7 +1852,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
18661852
_ => hir::ImplicitSelfKind::None,
18671853
}
18681854
}))
1869-
.set_lifetime_elision_allowed(self.resolver.lifetime_elision_allowed(fn_node_id))
1855+
.set_lifetime_elision_allowed(
1856+
self.owner.id == fn_node_id && self.owner.lifetime_elision_allowed,
1857+
)
18701858
.set_c_variadic(c_variadic);
18711859

18721860
self.arena.alloc(hir::FnDecl { inputs, output, fn_decl_kind })
@@ -2032,7 +2020,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
20322020
source: LifetimeSource,
20332021
syntax: LifetimeSyntax,
20342022
) -> &'hir hir::Lifetime {
2035-
let res = if let Some(res) = self.resolver.get_lifetime_res(id) {
2023+
let res = if let Some(res) = self.owner.get_lifetime_res(id) {
20362024
match res {
20372025
LifetimeRes::Param { param, .. } => hir::LifetimeKind::Param(param),
20382026
LifetimeRes::Fresh { param, .. } => {
@@ -2118,13 +2106,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
21182106
// AST resolution emitted an error on those parameters, so we lower them using
21192107
// `ParamName::Error`.
21202108
let ident = self.lower_ident(param.ident);
2121-
let param_name = if let Some(LifetimeRes::Error(..)) =
2122-
self.resolver.get_lifetime_res(param.id)
2123-
{
2124-
ParamName::Error(ident)
2125-
} else {
2126-
ParamName::Plain(ident)
2127-
};
2109+
let param_name =
2110+
if let Some(LifetimeRes::Error(..)) = self.owner.get_lifetime_res(param.id) {
2111+
ParamName::Error(ident)
2112+
} else {
2113+
ParamName::Plain(ident)
2114+
};
21282115
let kind =
21292116
hir::GenericParamKind::Lifetime { kind: hir::LifetimeParamKind::Explicit };
21302117

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;
@@ -206,7 +206,15 @@ pub struct ResolverGlobalCtxt {
206206

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

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

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

@@ -239,8 +253,6 @@ pub struct ResolverAstLowering<'tcx> {
239253
pub owners: NodeMap<PerOwnerResolverData>,
240254

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

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

compiler/rustc_resolve/src/late.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2412,7 +2412,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
24122412
res: LifetimeRes,
24132413
candidate: LifetimeElisionCandidate,
24142414
) {
2415-
if let Some(prev_res) = self.r.lifetimes_res_map.insert(id, res) {
2415+
if let Some(prev_res) = self.r.current_owner.lifetimes_res_map.insert(id, res) {
24162416
panic!("lifetime {id:?} resolved multiple times ({prev_res:?} before, {res:?} now)")
24172417
}
24182418

@@ -2428,7 +2428,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
24282428

24292429
#[instrument(level = "debug", skip(self))]
24302430
fn record_lifetime_param(&mut self, id: NodeId, res: LifetimeRes) {
2431-
if let Some(prev_res) = self.r.lifetimes_res_map.insert(id, res) {
2431+
if let Some(prev_res) = self.r.current_owner.lifetimes_res_map.insert(id, res) {
24322432
panic!(
24332433
"lifetime parameter {id:?} resolved multiple times ({prev_res:?} before, {res:?} now)"
24342434
)
@@ -2456,7 +2456,9 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
24562456

24572457
let outer_failures = take(&mut this.diag_metadata.current_elision_failures);
24582458
let output_rib = if let Ok(res) = elision_lifetime.as_ref() {
2459-
this.r.lifetime_elision_allowed.insert(fn_id);
2459+
if fn_id == this.r.current_owner.id {
2460+
this.r.current_owner.lifetime_elision_allowed = true;
2461+
}
24602462
LifetimeRibKind::Elided(*res)
24612463
} else {
24622464
LifetimeRibKind::ElisionFailure
@@ -2635,11 +2637,11 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
26352637
let lt_id = if let Some(lt) = lt {
26362638
lt.id
26372639
} else {
2638-
let res = self.r.lifetimes_res_map[&ty.id];
2640+
let res = self.r.current_owner.lifetimes_res_map[&ty.id];
26392641
let LifetimeRes::ElidedAnchor { start, .. } = res else { bug!() };
26402642
start
26412643
};
2642-
let lt_res = self.r.lifetimes_res_map[&lt_id];
2644+
let lt_res = self.r.current_owner.lifetimes_res_map[&lt_id];
26432645
trace!("FindReferenceVisitor inserting res={:?}", lt_res);
26442646
self.lifetime.insert(lt_res);
26452647
}
@@ -5224,7 +5226,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
52245226
match self.resolve_label(label.ident) {
52255227
Ok((node_id, _)) => {
52265228
// Since this res is a label, it is never read.
5227-
self.r.label_res_map.insert(expr.id, node_id);
5229+
self.r.current_owner.label_res_map.insert(expr.id, node_id);
52285230
self.diag_metadata.unused_labels.swap_remove(&node_id);
52295231
}
52305232
Err(error) => {

compiler/rustc_resolve/src/lib.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,10 +1380,6 @@ pub struct Resolver<'ra, 'tcx> {
13801380
import_res_map: NodeMap<PerNS<Option<Res>>> = Default::default(),
13811381
/// An import will be inserted into this map if it has been used.
13821382
import_use_map: FxHashMap<Import<'ra>, Used> = default::fx_hash_map(),
1383-
/// Resolutions for labels (node IDs of their corresponding blocks or loops).
1384-
label_res_map: NodeMap<NodeId> = Default::default(),
1385-
/// Resolutions for lifetimes.
1386-
lifetimes_res_map: NodeMap<LifetimeRes> = Default::default(),
13871383
/// Lifetime parameters that lowering will have to introduce.
13881384
extra_lifetime_params_map: NodeMap<Vec<(Ident, NodeId, LifetimeRes)>> = Default::default(),
13891385

@@ -1533,8 +1529,6 @@ pub struct Resolver<'ra, 'tcx> {
15331529
/// they are declared in the static array generated by proc_macro_harness.
15341530
proc_macros: Vec<LocalDefId> = Vec::new(),
15351531
confused_type_with_std_module: FxIndexMap<Span, Span>,
1536-
/// Whether lifetime elision was successful.
1537-
lifetime_elision_allowed: FxHashSet<NodeId> = default::fx_hash_set(),
15381532

15391533
/// Names of items that were stripped out via cfg with their corresponding cfg meta item.
15401534
stripped_cfg_items: Vec<StrippedCfgItem<NodeId>> = Vec::new(),
@@ -2017,13 +2011,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
20172011
let ast_lowering = ty::ResolverAstLowering {
20182012
partial_res_map: self.partial_res_map,
20192013
import_res_map: self.import_res_map,
2020-
label_res_map: self.label_res_map,
2021-
lifetimes_res_map: self.lifetimes_res_map,
20222014
extra_lifetime_params_map: self.extra_lifetime_params_map,
20232015
next_node_id: self.next_node_id,
20242016
owners: self.owners,
20252017
trait_map: self.trait_map,
2026-
lifetime_elision_allowed: self.lifetime_elision_allowed,
20272018
lint_buffer: Steal::new(self.lint_buffer),
20282019
delegation_infos: self.delegation_infos,
20292020
disambiguators,

0 commit comments

Comments
 (0)