Skip to content

Commit c70fa4a

Browse files
committed
Don't overuse the tcx arena
1 parent cf49ec3 commit c70fa4a

File tree

6 files changed

+38
-45
lines changed

6 files changed

+38
-45
lines changed

frontend/exporter/src/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ impl ImplInfos {
357357
.impl_trait_ref(did)
358358
.map(|trait_ref| trait_ref.instantiate_identity())
359359
.sinto(s),
360-
clauses: predicates_defined_on(tcx, did).predicates.sinto(s),
360+
clauses: predicates_defined_on(tcx, did).as_ref().sinto(s),
361361
}
362362
}
363363
}

frontend/exporter/src/traits.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod utils;
77
#[cfg(feature = "rustc")]
88
pub use utils::{
99
erase_and_norm, implied_predicates, predicates_defined_on, required_predicates, self_predicate,
10-
ToPolyTraitRef,
10+
Predicates, ToPolyTraitRef,
1111
};
1212

1313
#[cfg(feature = "rustc")]
@@ -358,12 +358,11 @@ pub fn solve_item_implied_traits<'tcx, S: UnderOwnerState<'tcx>>(
358358
fn solve_item_traits_inner<'tcx, S: UnderOwnerState<'tcx>>(
359359
s: &S,
360360
generics: ty::GenericArgsRef<'tcx>,
361-
predicates: ty::GenericPredicates<'tcx>,
361+
predicates: utils::Predicates<'tcx>,
362362
) -> Vec<ImplExpr> {
363363
let tcx = s.base().tcx;
364364
let typing_env = s.typing_env();
365365
predicates
366-
.predicates
367366
.iter()
368367
.map(|(clause, _span)| *clause)
369368
.filter_map(|clause| clause.as_trait_clause())

frontend/exporter/src/traits/resolution.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ use rustc_middle::traits::CodegenObligationError;
1111
use rustc_middle::ty::{self, *};
1212
use rustc_trait_selection::traits::ImplSource;
1313

14-
use crate::{self_predicate, traits::utils::erase_and_norm};
15-
16-
use super::utils::{implied_predicates, normalize_bound_val, required_predicates, ToPolyTraitRef};
14+
use super::utils::{
15+
self, erase_and_norm, implied_predicates, normalize_bound_val, required_predicates,
16+
self_predicate, ToPolyTraitRef,
17+
};
1718

1819
#[derive(Debug, Clone)]
1920
pub enum PathChunk<'tcx> {
@@ -179,7 +180,6 @@ fn initial_search_predicates<'tcx>(
179180
}
180181
predicates.extend(
181182
required_predicates(tcx, def_id, add_drop)
182-
.predicates
183183
.iter()
184184
.map(|(clause, _span)| *clause)
185185
.filter_map(|clause| {
@@ -204,7 +204,6 @@ fn parents_trait_predicates<'tcx>(
204204
) -> Vec<PolyTraitPredicate<'tcx>> {
205205
let self_trait_ref = pred.to_poly_trait_ref();
206206
implied_predicates(tcx, pred.def_id(), add_drop)
207-
.predicates
208207
.iter()
209208
.map(|(clause, _span)| *clause)
210209
// Substitute with the `self` args so that the clause makes sense in the
@@ -342,8 +341,8 @@ impl<'tcx> PredicateSearcher<'tcx> {
342341
};
343342

344343
// The bounds that hold on the associated type.
345-
let item_bounds = implied_predicates(tcx, alias_ty.def_id, self.add_drop)
346-
.predicates
344+
let item_bounds = implied_predicates(tcx, alias_ty.def_id, self.add_drop);
345+
let item_bounds = item_bounds
347346
.iter()
348347
.map(|(clause, _span)| *clause)
349348
.filter_map(|pred| pred.as_trait_clause())
@@ -642,13 +641,12 @@ impl<'tcx> PredicateSearcher<'tcx> {
642641
pub fn resolve_predicates(
643642
&mut self,
644643
generics: GenericArgsRef<'tcx>,
645-
predicates: GenericPredicates<'tcx>,
644+
predicates: utils::Predicates<'tcx>,
646645
// Call back into hax-related code to display a nice warning.
647646
warn: &impl Fn(&str),
648647
) -> Result<Vec<ImplExpr<'tcx>>, String> {
649648
let tcx = self.tcx;
650649
predicates
651-
.predicates
652650
.iter()
653651
.map(|(clause, _span)| *clause)
654652
.filter_map(|clause| clause.as_trait_clause())

frontend/exporter/src/traits/utils.rs

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,21 @@
2929
use rustc_hir::def::DefKind;
3030
use rustc_middle::ty::*;
3131
use rustc_span::def_id::DefId;
32-
use rustc_span::DUMMY_SP;
32+
use rustc_span::{Span, DUMMY_SP};
33+
use std::borrow::Cow;
34+
35+
pub type Predicates<'tcx> = Cow<'tcx, [(Clause<'tcx>, Span)]>;
3336

3437
/// Returns a list of type predicates for the definition with ID `def_id`, including inferred
3538
/// lifetime constraints. This is the basic list of predicates we use for essentially all items.
36-
pub fn predicates_defined_on(tcx: TyCtxt<'_>, def_id: DefId) -> GenericPredicates<'_> {
37-
let mut result = tcx.explicit_predicates_of(def_id);
39+
pub fn predicates_defined_on(tcx: TyCtxt<'_>, def_id: DefId) -> Predicates<'_> {
40+
let mut result = Cow::Borrowed(tcx.explicit_predicates_of(def_id).predicates);
3841
let inferred_outlives = tcx.inferred_outlives_of(def_id);
3942
if !inferred_outlives.is_empty() {
40-
let inferred_outlives_iter = inferred_outlives
41-
.iter()
42-
.map(|(clause, span)| ((*clause).upcast(tcx), *span));
43-
result.predicates = tcx.arena.alloc_from_iter(
44-
result
45-
.predicates
46-
.into_iter()
47-
.copied()
48-
.chain(inferred_outlives_iter),
43+
result.to_mut().extend(
44+
inferred_outlives
45+
.iter()
46+
.map(|(clause, span)| ((*clause).upcast(tcx), *span)),
4947
);
5048
}
5149
result
@@ -66,7 +64,7 @@ pub fn required_predicates<'tcx>(
6664
tcx: TyCtxt<'tcx>,
6765
def_id: DefId,
6866
add_drop: bool,
69-
) -> GenericPredicates<'tcx> {
67+
) -> Predicates<'tcx> {
7068
use DefKind::*;
7169
let def_kind = tcx.def_kind(def_id);
7270
let mut predicates = match def_kind {
@@ -103,9 +101,7 @@ pub fn required_predicates<'tcx>(
103101
.map(|ty| Binder::dummy(TraitRef::new(tcx, drop_trait, [ty])))
104102
.map(|tref| tref.upcast(tcx))
105103
.map(|clause| (clause, DUMMY_SP));
106-
predicates.predicates = tcx
107-
.arena
108-
.alloc_from_iter(predicates.predicates.iter().copied().chain(extra_bounds));
104+
predicates.to_mut().extend(extra_bounds);
109105
}
110106
}
111107
predicates
@@ -134,36 +130,26 @@ pub fn implied_predicates<'tcx>(
134130
tcx: TyCtxt<'tcx>,
135131
def_id: DefId,
136132
add_drop: bool,
137-
) -> GenericPredicates<'tcx> {
133+
) -> Predicates<'tcx> {
138134
use DefKind::*;
139135
let parent = tcx.opt_parent(def_id);
140136
match tcx.def_kind(def_id) {
141137
// We consider all predicates on traits to be outputs
142138
Trait | TraitAlias => predicates_defined_on(tcx, def_id),
143139
AssocTy if matches!(tcx.def_kind(parent.unwrap()), Trait) => {
144-
let mut predicates = GenericPredicates {
145-
parent,
146-
// `skip_binder` is for the GAT `EarlyBinder`
147-
predicates: tcx.explicit_item_bounds(def_id).skip_binder(),
148-
..GenericPredicates::default()
149-
};
140+
// `skip_binder` is for the GAT `EarlyBinder`
141+
let mut predicates = Cow::Borrowed(tcx.explicit_item_bounds(def_id).skip_binder());
150142
if add_drop {
151143
// Add a `Drop` bound to the assoc item.
152144
let drop_trait = tcx.lang_items().drop_trait().unwrap();
153145
let ty =
154146
Ty::new_projection(tcx, def_id, GenericArgs::identity_for_item(tcx, def_id));
155147
let tref = Binder::dummy(TraitRef::new(tcx, drop_trait, [ty]));
156-
predicates.predicates = tcx.arena.alloc_from_iter(
157-
predicates
158-
.predicates
159-
.iter()
160-
.copied()
161-
.chain([(tref.upcast(tcx), DUMMY_SP)]),
162-
);
148+
predicates.to_mut().push((tref.upcast(tcx), DUMMY_SP));
163149
}
164150
predicates
165151
}
166-
_ => GenericPredicates::default(),
152+
_ => Predicates::default(),
167153
}
168154
}
169155

frontend/exporter/src/types/hir.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,6 @@ fn region_bounds_at_current_owner<'tcx, S: UnderOwnerState<'tcx>>(s: &S) -> Gene
830830
.instantiate_identity()
831831
} else {
832832
predicates_defined_on(tcx, s.owner_id())
833-
.predicates
834833
.iter()
835834
.map(|(x, _span)| x)
836835
.copied()

frontend/exporter/src/types/ty.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,17 @@ pub struct GenericPredicates {
13371337
pub predicates: Vec<(Clause, Span)>,
13381338
}
13391339

1340+
#[cfg(feature = "rustc")]
1341+
impl<'tcx, S: UnderOwnerState<'tcx>> SInto<S, GenericPredicates>
1342+
for crate::traits::Predicates<'tcx>
1343+
{
1344+
fn sinto(&self, s: &S) -> GenericPredicates {
1345+
GenericPredicates {
1346+
predicates: self.as_ref().sinto(s),
1347+
}
1348+
}
1349+
}
1350+
13401351
#[cfg(feature = "rustc")]
13411352
impl<'tcx, S: UnderOwnerState<'tcx>, T1, T2> SInto<S, Binder<T2>> for ty::Binder<'tcx, T1>
13421353
where

0 commit comments

Comments
 (0)