Skip to content

Commit a25cb8a

Browse files
committed
Auto merge of #153494 - aerooneqq:boxed-trait-candidates-to-hir-arena, r=petrochenkov
Replace Box<[TraitCandidate]> with &'hir [TraitCandidate<'hir>] This PR allocates trait candidates on HIR arena and replaces `remove` with `get` in `ResolverAstLowering`. First step for #153489. r? @petrochenkov
2 parents 085c58f + dc31c15 commit a25cb8a

File tree

16 files changed

+58
-67
lines changed

16 files changed

+58
-67
lines changed

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -815,13 +815,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
815815
}
816816
}
817817

818-
struct SelfResolver<'a> {
819-
resolver: &'a mut ResolverAstLowering,
818+
struct SelfResolver<'a, 'tcx> {
819+
resolver: &'a mut ResolverAstLowering<'tcx>,
820820
path_id: NodeId,
821821
self_param_id: NodeId,
822822
}
823823

824-
impl<'a> SelfResolver<'a> {
824+
impl SelfResolver<'_, '_> {
825825
fn try_replace_id(&mut self, id: NodeId) {
826826
if let Some(res) = self.resolver.partial_res_map.get(&id)
827827
&& let Some(Res::Local(sig_id)) = res.full_res()
@@ -833,7 +833,7 @@ impl<'a> SelfResolver<'a> {
833833
}
834834
}
835835

836-
impl<'ast, 'a> Visitor<'ast> for SelfResolver<'a> {
836+
impl<'ast, 'a> Visitor<'ast> for SelfResolver<'a, '_> {
837837
fn visit_id(&mut self, id: NodeId) {
838838
self.try_replace_id(id);
839839
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use super::{
2727

2828
pub(super) struct ItemLowerer<'a, 'hir> {
2929
pub(super) tcx: TyCtxt<'hir>,
30-
pub(super) resolver: &'a mut ResolverAstLowering,
30+
pub(super) resolver: &'a mut ResolverAstLowering<'hir>,
3131
pub(super) ast_index: &'a IndexSlice<LocalDefId, AstOwner<'a>>,
3232
pub(super) owners: &'a mut IndexVec<LocalDefId, hir::MaybeOwner<'hir>>,
3333
}

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ struct LoweringContext<'a, 'hir> {
9999
// will be in AST index.
100100
ast_index: &'a IndexSlice<LocalDefId, AstOwner<'a>>,
101101

102-
resolver: &'a mut ResolverAstLowering,
102+
resolver: &'a mut ResolverAstLowering<'hir>,
103103
disambiguator: DisambiguatorState,
104104

105105
/// Used to allocate HIR nodes.
@@ -133,7 +133,7 @@ struct LoweringContext<'a, 'hir> {
133133

134134
current_hir_id_owner: hir::OwnerId,
135135
item_local_id_counter: hir::ItemLocalId,
136-
trait_map: ItemLocalMap<Box<[TraitCandidate]>>,
136+
trait_map: ItemLocalMap<&'hir [TraitCandidate<'hir>]>,
137137

138138
impl_trait_defs: Vec<hir::GenericParam<'hir>>,
139139
impl_trait_bounds: Vec<hir::WherePredicate<'hir>>,
@@ -162,7 +162,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
162162
fn new(
163163
tcx: TyCtxt<'hir>,
164164
ast_index: &'a IndexSlice<LocalDefId, AstOwner<'a>>,
165-
resolver: &'a mut ResolverAstLowering,
165+
resolver: &'a mut ResolverAstLowering<'hir>,
166166
) -> Self {
167167
let registered_tools = tcx.registered_tools(()).iter().map(|x| x.name).collect();
168168
Self {
@@ -248,7 +248,7 @@ impl SpanLowerer {
248248
}
249249

250250
#[extension(trait ResolverAstLoweringExt)]
251-
impl ResolverAstLowering {
251+
impl ResolverAstLowering<'_> {
252252
fn legacy_const_generic_args(&self, expr: &Expr, tcx: TyCtxt<'_>) -> Option<Vec<usize>> {
253253
let ExprKind::Path(None, path) = &expr.kind else {
254254
return None;
@@ -748,8 +748,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
748748
self.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
749749
}
750750

751-
if let Some(traits) = self.resolver.trait_map.remove(&ast_node_id) {
752-
self.trait_map.insert(hir_id.local_id, traits.into_boxed_slice());
751+
if let Some(&traits) = self.resolver.trait_map.get(&ast_node_id) {
752+
self.trait_map.insert(hir_id.local_id, traits);
753753
}
754754

755755
// Check whether the same `NodeId` is lowered more than once.

compiler/rustc_hir/src/hir.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,7 +1633,7 @@ pub struct OwnerInfo<'hir> {
16331633
pub attrs: AttributeMap<'hir>,
16341634
/// Map indicating what traits are in scope for places where this
16351635
/// is relevant; generated by resolve.
1636-
pub trait_map: ItemLocalMap<Box<[TraitCandidate]>>,
1636+
pub trait_map: ItemLocalMap<&'hir [TraitCandidate<'hir>]>,
16371637

16381638
/// Lints delayed during ast lowering to be emitted
16391639
/// after hir has completely built
@@ -4683,10 +4683,10 @@ pub struct Upvar {
46834683
// The TraitCandidate's import_ids is empty if the trait is defined in the same module, and
46844684
// has length > 0 if the trait is found through an chain of imports, starting with the
46854685
// import/use statement in the scope where the trait is used.
4686-
#[derive(Debug, Clone, HashStable_Generic)]
4687-
pub struct TraitCandidate {
4686+
#[derive(Debug, Clone, Copy, HashStable_Generic)]
4687+
pub struct TraitCandidate<'hir> {
46884688
pub def_id: DefId,
4689-
pub import_ids: SmallVec<[LocalDefId; 1]>,
4689+
pub import_ids: &'hir [LocalDefId],
46904690
// Indicates whether this trait candidate is ambiguously glob imported
46914691
// in it's scope. Related to the AMBIGUOUS_GLOB_IMPORTED_TRAITS lint.
46924692
// If this is set to true and the trait is used as a result of method lookup, this

compiler/rustc_hir_typeck/src/demand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10081008
);
10091009
let container_id = pick.item.container_id(self.tcx);
10101010
let container = with_no_trimmed_paths!(self.tcx.def_path_str(container_id));
1011-
for def_id in pick.import_ids {
1011+
for &def_id in pick.import_ids {
10121012
let hir_id = self.tcx.local_def_id_to_hir_id(def_id);
10131013
path_span
10141014
.push_span_label(self.tcx.hir_span(hir_id), format!("`{container}` imported here"));

compiler/rustc_hir_typeck/src/method/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
196196

197197
// NOTE: on the failure path, we also record the possibly-used trait methods
198198
// since an unused import warning is kinda distracting from the method error.
199-
for &import_id in &pick.import_ids {
199+
for &import_id in pick.import_ids {
200200
debug!("used_trait_import: {:?}", import_id);
201201
self.typeck_results.borrow_mut().used_trait_imports.insert(import_id);
202202
}
@@ -554,7 +554,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
554554
debug!(?pick);
555555
{
556556
let mut typeck_results = self.typeck_results.borrow_mut();
557-
for import_id in pick.import_ids {
557+
for &import_id in pick.import_ids {
558558
debug!(used_trait_import=?import_id);
559559
typeck_results.used_trait_imports.insert(import_id);
560560
}

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use rustc_trait_selection::traits::query::method_autoderef::{
3636
CandidateStep, MethodAutoderefBadTy, MethodAutoderefStepsResult,
3737
};
3838
use rustc_trait_selection::traits::{self, ObligationCause, ObligationCtxt};
39-
use smallvec::{SmallVec, smallvec};
39+
use smallvec::SmallVec;
4040
use tracing::{debug, instrument};
4141

4242
use self::CandidateKind::*;
@@ -99,7 +99,7 @@ impl<'a, 'tcx> Deref for ProbeContext<'a, 'tcx> {
9999
pub(crate) struct Candidate<'tcx> {
100100
pub(crate) item: ty::AssocItem,
101101
pub(crate) kind: CandidateKind<'tcx>,
102-
pub(crate) import_ids: SmallVec<[LocalDefId; 1]>,
102+
pub(crate) import_ids: &'tcx [LocalDefId],
103103
}
104104

105105
#[derive(Debug, Clone)]
@@ -206,7 +206,7 @@ impl PickConstraintsForShadowed {
206206
pub(crate) struct Pick<'tcx> {
207207
pub item: ty::AssocItem,
208208
pub kind: PickKind<'tcx>,
209-
pub import_ids: SmallVec<[LocalDefId; 1]>,
209+
pub import_ids: &'tcx [LocalDefId],
210210

211211
/// Indicates that the source expression should be autoderef'd N times
212212
/// ```ignore (not-rust)
@@ -574,7 +574,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
574574
ty::Binder::dummy(trait_ref),
575575
false,
576576
),
577-
import_ids: smallvec![],
577+
import_ids: &[],
578578
},
579579
false,
580580
);
@@ -946,7 +946,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
946946
Candidate {
947947
item,
948948
kind: InherentImplCandidate { impl_def_id, receiver_steps },
949-
import_ids: smallvec![],
949+
import_ids: &[],
950950
},
951951
true,
952952
);
@@ -979,11 +979,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
979979
traits::supertraits(self.tcx, trait_ref),
980980
|this, new_trait_ref, item| {
981981
this.push_candidate(
982-
Candidate {
983-
item,
984-
kind: ObjectCandidate(new_trait_ref),
985-
import_ids: smallvec![],
986-
},
982+
Candidate { item, kind: ObjectCandidate(new_trait_ref), import_ids: &[] },
987983
true,
988984
);
989985
},
@@ -1018,11 +1014,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
10181014

10191015
self.assemble_candidates_for_bounds(bounds, |this, poly_trait_ref, item| {
10201016
this.push_candidate(
1021-
Candidate {
1022-
item,
1023-
kind: WhereClauseCandidate(poly_trait_ref),
1024-
import_ids: smallvec![],
1025-
},
1017+
Candidate { item, kind: WhereClauseCandidate(poly_trait_ref), import_ids: &[] },
10261018
true,
10271019
);
10281020
});
@@ -1072,11 +1064,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
10721064
let mut duplicates = FxHashSet::default();
10731065
for trait_info in suggest::all_traits(self.tcx) {
10741066
if duplicates.insert(trait_info.def_id) {
1075-
self.assemble_extension_candidates_for_trait(
1076-
&smallvec![],
1077-
trait_info.def_id,
1078-
false,
1079-
);
1067+
self.assemble_extension_candidates_for_trait(&[], trait_info.def_id, false);
10801068
}
10811069
}
10821070
}
@@ -1100,7 +1088,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
11001088
#[instrument(level = "debug", skip(self))]
11011089
fn assemble_extension_candidates_for_trait(
11021090
&mut self,
1103-
import_ids: &SmallVec<[LocalDefId; 1]>,
1091+
import_ids: &'tcx [LocalDefId],
11041092
trait_def_id: DefId,
11051093
lint_ambiguous: bool,
11061094
) {
@@ -1123,7 +1111,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
11231111
self.push_candidate(
11241112
Candidate {
11251113
item,
1126-
import_ids: import_ids.clone(),
1114+
import_ids,
11271115
kind: TraitCandidate(bound_trait_ref, lint_ambiguous),
11281116
},
11291117
false,
@@ -1146,7 +1134,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
11461134
self.push_candidate(
11471135
Candidate {
11481136
item,
1149-
import_ids: import_ids.clone(),
1137+
import_ids,
11501138
kind: TraitCandidate(ty::Binder::dummy(trait_ref), lint_ambiguous),
11511139
},
11521140
false,
@@ -2353,7 +2341,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
23532341
Some(Pick {
23542342
item: probes[0].0.item,
23552343
kind: TraitPick(lint_ambiguous),
2356-
import_ids: probes[0].0.import_ids.clone(),
2344+
import_ids: probes[0].0.import_ids,
23572345
autoderefs: 0,
23582346
autoref_or_ptr_adjustment: None,
23592347
self_ty,
@@ -2430,7 +2418,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
24302418
Some(Pick {
24312419
item: child_candidate.item,
24322420
kind: TraitPick(lint_ambiguous),
2433-
import_ids: child_candidate.import_ids.clone(),
2421+
import_ids: child_candidate.import_ids,
24342422
autoderefs: 0,
24352423
autoref_or_ptr_adjustment: None,
24362424
self_ty,
@@ -2707,7 +2695,7 @@ impl<'tcx> Candidate<'tcx> {
27072695
WhereClausePick(trait_ref)
27082696
}
27092697
},
2710-
import_ids: self.import_ids.clone(),
2698+
import_ids: self.import_ids,
27112699
autoderefs: 0,
27122700
autoref_or_ptr_adjustment: None,
27132701
self_ty,

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
199199
// NOTE: Reporting a method error should also suppress any unused trait errors,
200200
// since the method error is very possibly the reason why the trait wasn't used.
201201
for &import_id in
202-
self.tcx.in_scope_traits(call_id).into_iter().flatten().flat_map(|c| &c.import_ids)
202+
self.tcx.in_scope_traits(call_id).into_iter().flatten().flat_map(|c| c.import_ids)
203203
{
204204
self.typeck_results.borrow_mut().used_trait_imports.insert(import_id);
205205
}

compiler/rustc_interface/src/passes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ fn write_out_deps(tcx: TyCtxt<'_>, outputs: &OutputFilenames, out_filenames: &[P
781781
fn resolver_for_lowering_raw<'tcx>(
782782
tcx: TyCtxt<'tcx>,
783783
(): (),
784-
) -> (&'tcx Steal<(ty::ResolverAstLowering, Arc<ast::Crate>)>, &'tcx ty::ResolverGlobalCtxt) {
784+
) -> (&'tcx Steal<(ty::ResolverAstLowering<'tcx>, Arc<ast::Crate>)>, &'tcx ty::ResolverGlobalCtxt) {
785785
let arenas = Resolver::arenas();
786786
let _ = tcx.registered_tools(()); // Uses `crate_for_resolver`.
787787
let (krate, pre_configured_attrs) = tcx.crate_for_resolver(()).steal();

compiler/rustc_middle/src/arena.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ macro_rules! arena_types {
3232
rustc_middle::ty::DefinitionSiteHiddenType<'tcx>,
3333
>,
3434
[] resolver: rustc_data_structures::steal::Steal<(
35-
rustc_middle::ty::ResolverAstLowering,
35+
rustc_middle::ty::ResolverAstLowering<'tcx>,
3636
std::sync::Arc<rustc_ast::Crate>,
3737
)>,
3838
[] crate_for_resolver: rustc_data_structures::steal::Steal<(rustc_ast::Crate, rustc_ast::AttrVec)>,

0 commit comments

Comments
 (0)