Skip to content

Commit bc85061

Browse files
authored
Rollup merge of rust-lang#65535 - eddyb:sliced-predicates, r=nikomatsakis
rustc: arena-allocate the slice in `ty::GenericsPredicate`, not the whole struct. While rebasing rust-lang#59789 I noticed we can do this now. However, it doesn't help much without changing `inferred_outlives_of` to the same type, which I might try next.
2 parents 138ff4a + cd9e444 commit bc85061

File tree

27 files changed

+120
-138
lines changed

27 files changed

+120
-138
lines changed

src/librustc/arena.rs

-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ macro_rules! arena_types {
9898
rustc::hir::def_id::DefId,
9999
>,
100100
[few] resolve_lifetimes: rustc::middle::resolve_lifetime::ResolveLifetimes,
101-
[decode] generic_predicates: rustc::ty::GenericPredicates<'tcx>,
102101
[few] lint_levels: rustc::lint::LintLevelMap,
103102
[few] stability_index: rustc::middle::stability::Index<'tcx>,
104103
[few] features: syntax::feature_gate::Features,

src/librustc/query/mod.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ rustc_queries! {
6161
/// predicate gets in the way of some checks, which are intended
6262
/// to operate over only the actual where-clauses written by the
6363
/// user.)
64-
query predicates_of(key: DefId) -> &'tcx ty::GenericPredicates<'tcx> {
64+
query predicates_of(key: DefId) -> ty::GenericPredicates<'tcx> {
6565
cache_on_disk_if { key.is_local() }
6666
}
6767

@@ -184,12 +184,10 @@ rustc_queries! {
184184
/// predicates (where-clauses) directly defined on it. This is
185185
/// equal to the `explicit_predicates_of` predicates plus the
186186
/// `inferred_outlives_of` predicates.
187-
query predicates_defined_on(_: DefId)
188-
-> &'tcx ty::GenericPredicates<'tcx> {}
187+
query predicates_defined_on(_: DefId) -> ty::GenericPredicates<'tcx> {}
189188

190189
/// Returns the predicates written explicitly by the user.
191-
query explicit_predicates_of(_: DefId)
192-
-> &'tcx ty::GenericPredicates<'tcx> {}
190+
query explicit_predicates_of(_: DefId) -> ty::GenericPredicates<'tcx> {}
193191

194192
/// Returns the inferred outlives predicates (e.g., for `struct
195193
/// Foo<'a, T> { x: &'a T }`, this would return `T: 'a`).
@@ -201,14 +199,13 @@ rustc_queries! {
201199
/// evaluate them even during type conversion, often before the
202200
/// full predicates are available (note that supertraits have
203201
/// additional acyclicity requirements).
204-
query super_predicates_of(key: DefId) -> &'tcx ty::GenericPredicates<'tcx> {
202+
query super_predicates_of(key: DefId) -> ty::GenericPredicates<'tcx> {
205203
desc { |tcx| "computing the supertraits of `{}`", tcx.def_path_str(key) }
206204
}
207205

208206
/// To avoid cycles within the predicates of a single item we compute
209207
/// per-type-parameter predicates for resolving `T::AssocTy`.
210-
query type_param_predicates(key: (DefId, DefId))
211-
-> &'tcx ty::GenericPredicates<'tcx> {
208+
query type_param_predicates(key: (DefId, DefId)) -> ty::GenericPredicates<'tcx> {
212209
no_force
213210
desc { |tcx| "computing the bounds for type parameter `{}`", {
214211
let id = tcx.hir().as_local_hir_id(key.1).unwrap();

src/librustc/traits/specialize/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ fn to_pretty_impl_header(tcx: TyCtxt<'_>, impl_def_id: DefId) -> Option<String>
419419

420420
// The predicates will contain default bounds like `T: Sized`. We need to
421421
// remove these bounds, and add `T: ?Sized` to any untouched type parameters.
422-
let predicates = &tcx.predicates_of(impl_def_id).predicates;
422+
let predicates = tcx.predicates_of(impl_def_id).predicates;
423423
let mut pretty_predicates = Vec::with_capacity(
424424
predicates.len() + types_without_default_bounds.len());
425425

src/librustc/ty/codec.rs

+20-15
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use std::intrinsics;
1616
use crate::ty::{self, Ty, TyCtxt};
1717
use crate::ty::subst::SubstsRef;
1818
use crate::mir::interpret::Allocation;
19+
use syntax_pos::Span;
1920

2021
/// The shorthand encoding uses an enum's variant index `usize`
2122
/// and is offset by this value so it never matches a real variant.
@@ -92,16 +93,16 @@ pub fn encode_with_shorthand<E, T, M>(encoder: &mut E,
9293
Ok(())
9394
}
9495

95-
pub fn encode_predicates<'tcx, E, C>(encoder: &mut E,
96-
predicates: &ty::GenericPredicates<'tcx>,
97-
cache: C)
98-
-> Result<(), E::Error>
96+
pub fn encode_spanned_predicates<'tcx, E, C>(
97+
encoder: &mut E,
98+
predicates: &'tcx [(ty::Predicate<'tcx>, Span)],
99+
cache: C,
100+
) -> Result<(), E::Error>
99101
where E: TyEncoder,
100102
C: for<'b> Fn(&'b mut E) -> &'b mut FxHashMap<ty::Predicate<'tcx>, usize>,
101103
{
102-
predicates.parent.encode(encoder)?;
103-
predicates.predicates.len().encode(encoder)?;
104-
for (predicate, span) in &predicates.predicates {
104+
predicates.len().encode(encoder)?;
105+
for (predicate, span) in predicates {
105106
encode_with_shorthand(encoder, predicate, &cache)?;
106107
span.encode(encoder)?;
107108
}
@@ -182,13 +183,15 @@ where
182183
}
183184

184185
#[inline]
185-
pub fn decode_predicates<D>(decoder: &mut D) -> Result<ty::GenericPredicates<'tcx>, D::Error>
186+
pub fn decode_spanned_predicates<D>(
187+
decoder: &mut D,
188+
) -> Result<&'tcx [(ty::Predicate<'tcx>, Span)], D::Error>
186189
where
187190
D: TyDecoder<'tcx>,
188191
{
189-
Ok(ty::GenericPredicates {
190-
parent: Decodable::decode(decoder)?,
191-
predicates: (0..decoder.read_usize()?).map(|_| {
192+
let tcx = decoder.tcx();
193+
Ok(tcx.arena.alloc_from_iter(
194+
(0..decoder.read_usize()?).map(|_| {
192195
// Handle shorthands first, if we have an usize > 0x80.
193196
let predicate = if decoder.positioned_at_shorthand() {
194197
let pos = decoder.read_usize()?;
@@ -202,7 +205,7 @@ where
202205
Ok((predicate, Decodable::decode(decoder)?))
203206
})
204207
.collect::<Result<Vec<_>, _>>()?,
205-
})
208+
))
206209
}
207210

208211
#[inline]
@@ -339,6 +342,8 @@ macro_rules! implement_ty_decoder {
339342
use $crate::ty::subst::SubstsRef;
340343
use $crate::hir::def_id::{CrateNum};
341344

345+
use syntax_pos::Span;
346+
342347
use super::$DecoderName;
343348

344349
impl<$($typaram ),*> Decoder for $DecoderName<$($typaram),*> {
@@ -393,11 +398,11 @@ macro_rules! implement_ty_decoder {
393398
}
394399
}
395400

396-
impl<$($typaram),*> SpecializedDecoder<ty::GenericPredicates<'tcx>>
401+
impl<$($typaram),*> SpecializedDecoder<&'tcx [(ty::Predicate<'tcx>, Span)]>
397402
for $DecoderName<$($typaram),*> {
398403
fn specialized_decode(&mut self)
399-
-> Result<ty::GenericPredicates<'tcx>, Self::Error> {
400-
decode_predicates(self)
404+
-> Result<&'tcx [(ty::Predicate<'tcx>, Span)], Self::Error> {
405+
decode_spanned_predicates(self)
401406
}
402407
}
403408

src/librustc/ty/context.rs

-14
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,6 @@ impl<'tcx> CtxtInterners<'tcx> {
148148
}
149149
}
150150

151-
pub struct Common<'tcx> {
152-
pub empty_predicates: ty::GenericPredicates<'tcx>,
153-
}
154-
155151
pub struct CommonTypes<'tcx> {
156152
pub unit: Ty<'tcx>,
157153
pub bool: Ty<'tcx>,
@@ -1039,9 +1035,6 @@ pub struct GlobalCtxt<'tcx> {
10391035

10401036
pub prof: SelfProfilerRef,
10411037

1042-
/// Common objects.
1043-
pub common: Common<'tcx>,
1044-
10451038
/// Common types, pre-interned for your convenience.
10461039
pub types: CommonTypes<'tcx>,
10471040

@@ -1213,12 +1206,6 @@ impl<'tcx> TyCtxt<'tcx> {
12131206
s.fatal(&err);
12141207
});
12151208
let interners = CtxtInterners::new(&arenas.interner);
1216-
let common = Common {
1217-
empty_predicates: ty::GenericPredicates {
1218-
parent: None,
1219-
predicates: vec![],
1220-
},
1221-
};
12221209
let common_types = CommonTypes::new(&interners);
12231210
let common_lifetimes = CommonLifetimes::new(&interners);
12241211
let common_consts = CommonConsts::new(&interners, &common_types);
@@ -1273,7 +1260,6 @@ impl<'tcx> TyCtxt<'tcx> {
12731260
interners,
12741261
dep_graph,
12751262
prof: s.prof.clone(),
1276-
common,
12771263
types: common_types,
12781264
lifetimes: common_lifetimes,
12791265
consts: common_consts,

src/librustc/ty/mod.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -1018,15 +1018,12 @@ impl<'tcx> Generics {
10181018
}
10191019

10201020
/// Bounds on generics.
1021-
#[derive(Clone, Default, Debug, HashStable)]
1021+
#[derive(Copy, Clone, Default, Debug, RustcEncodable, RustcDecodable, HashStable)]
10221022
pub struct GenericPredicates<'tcx> {
10231023
pub parent: Option<DefId>,
1024-
pub predicates: Vec<(Predicate<'tcx>, Span)>,
1024+
pub predicates: &'tcx [(Predicate<'tcx>, Span)],
10251025
}
10261026

1027-
impl<'tcx> rustc_serialize::UseSpecializedEncodable for GenericPredicates<'tcx> {}
1028-
impl<'tcx> rustc_serialize::UseSpecializedDecodable for GenericPredicates<'tcx> {}
1029-
10301027
impl<'tcx> GenericPredicates<'tcx> {
10311028
pub fn instantiate(
10321029
&self,
@@ -2321,7 +2318,7 @@ impl<'tcx> AdtDef {
23212318
}
23222319

23232320
#[inline]
2324-
pub fn predicates(&self, tcx: TyCtxt<'tcx>) -> &'tcx GenericPredicates<'tcx> {
2321+
pub fn predicates(&self, tcx: TyCtxt<'tcx>) -> GenericPredicates<'tcx> {
23252322
tcx.predicates_of(self.did)
23262323
}
23272324

@@ -2561,7 +2558,7 @@ impl<'tcx> AdtDef {
25612558
def_id: sized_trait,
25622559
substs: tcx.mk_substs_trait(ty, &[])
25632560
}).to_predicate();
2564-
let predicates = &tcx.predicates_of(self.did).predicates;
2561+
let predicates = tcx.predicates_of(self.did).predicates;
25652562
if predicates.iter().any(|(p, _)| *p == sized_predicate) {
25662563
vec![]
25672564
} else {

src/librustc/ty/query/on_disk_cache.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -882,15 +882,16 @@ where
882882
}
883883
}
884884

885-
impl<'a, 'tcx, E> SpecializedEncoder<ty::GenericPredicates<'tcx>> for CacheEncoder<'a, 'tcx, E>
885+
impl<'a, 'tcx, E> SpecializedEncoder<&'tcx [(ty::Predicate<'tcx>, Span)]>
886+
for CacheEncoder<'a, 'tcx, E>
886887
where
887888
E: 'a + TyEncoder,
888889
{
889890
#[inline]
890891
fn specialized_encode(&mut self,
891-
predicates: &ty::GenericPredicates<'tcx>)
892+
predicates: &&'tcx [(ty::Predicate<'tcx>, Span)])
892893
-> Result<(), Self::Error> {
893-
ty_codec::encode_predicates(self, predicates,
894+
ty_codec::encode_spanned_predicates(self, predicates,
894895
|encoder| &mut encoder.predicate_shorthands)
895896
}
896897
}

src/librustc/ty/structural_impls.rs

-6
Original file line numberDiff line numberDiff line change
@@ -1218,12 +1218,6 @@ EnumTypeFoldableImpl! {
12181218
}
12191219
}
12201220

1221-
BraceStructTypeFoldableImpl! {
1222-
impl<'tcx> TypeFoldable<'tcx> for ty::GenericPredicates<'tcx> {
1223-
parent, predicates
1224-
}
1225-
}
1226-
12271221
impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
12281222
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
12291223
// This code is hot enough that it's worth specializing for a list of

src/librustc_lint/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TrivialConstraints {
12411241
if cx.tcx.features().trivial_bounds {
12421242
let def_id = cx.tcx.hir().local_def_id(item.hir_id);
12431243
let predicates = cx.tcx.predicates_of(def_id);
1244-
for &(predicate, span) in &predicates.predicates {
1244+
for &(predicate, span) in predicates.predicates {
12451245
let predicate_kind_name = match predicate {
12461246
Trait(..) => "Trait",
12471247
TypeOutlives(..) |

src/librustc_lint/unused.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
156156
}
157157
ty::Opaque(def, _) => {
158158
let mut has_emitted = false;
159-
for (predicate, _) in &cx.tcx.predicates_of(def).predicates {
159+
for (predicate, _) in cx.tcx.predicates_of(def).predicates {
160160
if let ty::Predicate::Trait(ref poly_trait_predicate) = predicate {
161161
let trait_ref = poly_trait_predicate.skip_binder().trait_ref;
162162
let def_id = trait_ref.def_id;

src/librustc_metadata/cstore_impl.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,9 @@ provide! { <'tcx> tcx, def_id, other, cdata,
9797
generics_of => {
9898
tcx.arena.alloc(cdata.get_generics(def_id.index, tcx.sess))
9999
}
100-
predicates_of => { tcx.arena.alloc(cdata.get_predicates(def_id.index, tcx)) }
101-
predicates_defined_on => {
102-
tcx.arena.alloc(cdata.get_predicates_defined_on(def_id.index, tcx))
103-
}
104-
super_predicates_of => { tcx.arena.alloc(cdata.get_super_predicates(def_id.index, tcx)) }
100+
predicates_of => { cdata.get_predicates(def_id.index, tcx) }
101+
predicates_defined_on => { cdata.get_predicates_defined_on(def_id.index, tcx) }
102+
super_predicates_of => { cdata.get_super_predicates(def_id.index, tcx) }
105103
trait_def => {
106104
tcx.arena.alloc(cdata.get_trait_def(def_id.index, tcx.sess))
107105
}

src/librustc_metadata/encoder.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,11 @@ impl<'tcx> SpecializedEncoder<interpret::AllocId> for EncodeContext<'tcx> {
243243
}
244244
}
245245

246-
impl<'tcx> SpecializedEncoder<ty::GenericPredicates<'tcx>> for EncodeContext<'tcx> {
246+
impl<'tcx> SpecializedEncoder<&'tcx [(ty::Predicate<'tcx>, Span)]> for EncodeContext<'tcx> {
247247
fn specialized_encode(&mut self,
248-
predicates: &ty::GenericPredicates<'tcx>)
248+
predicates: &&'tcx [(ty::Predicate<'tcx>, Span)])
249249
-> Result<(), Self::Error> {
250-
ty_codec::encode_predicates(self, predicates, |ecx| &mut ecx.predicate_shorthands)
250+
ty_codec::encode_spanned_predicates(self, predicates, |ecx| &mut ecx.predicate_shorthands)
251251
}
252252
}
253253

@@ -826,13 +826,13 @@ impl EncodeContext<'tcx> {
826826

827827
fn encode_predicates(&mut self, def_id: DefId) {
828828
debug!("EncodeContext::encode_predicates({:?})", def_id);
829-
record!(self.per_def.predicates[def_id] <- &*self.tcx.predicates_of(def_id));
829+
record!(self.per_def.predicates[def_id] <- self.tcx.predicates_of(def_id));
830830
}
831831

832832
fn encode_predicates_defined_on(&mut self, def_id: DefId) {
833833
debug!("EncodeContext::encode_predicates_defined_on({:?})", def_id);
834834
record!(self.per_def.predicates_defined_on[def_id] <-
835-
&*self.tcx.predicates_defined_on(def_id))
835+
self.tcx.predicates_defined_on(def_id))
836836
}
837837

838838
fn encode_info_for_trait_item(&mut self, def_id: DefId) {
@@ -1166,14 +1166,14 @@ impl EncodeContext<'tcx> {
11661166
paren_sugar: trait_def.paren_sugar,
11671167
has_auto_impl: self.tcx.trait_is_auto(def_id),
11681168
is_marker: trait_def.is_marker,
1169-
super_predicates: self.lazy(&*tcx.super_predicates_of(def_id)),
1169+
super_predicates: self.lazy(tcx.super_predicates_of(def_id)),
11701170
};
11711171

11721172
EntryKind::Trait(self.lazy(data))
11731173
}
11741174
hir::ItemKind::TraitAlias(..) => {
11751175
let data = TraitAliasData {
1176-
super_predicates: self.lazy(&*tcx.super_predicates_of(def_id)),
1176+
super_predicates: self.lazy(tcx.super_predicates_of(def_id)),
11771177
};
11781178

11791179
EntryKind::TraitAlias(self.lazy(data))

src/librustc_mir/transform/qualify_min_const_fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn is_min_const_fn(tcx: TyCtxt<'tcx>, def_id: DefId, body: &'a Body<'tcx>) -
1414
let mut current = def_id;
1515
loop {
1616
let predicates = tcx.predicates_of(current);
17-
for (predicate, _) in &predicates.predicates {
17+
for (predicate, _) in predicates.predicates {
1818
match predicate {
1919
| Predicate::RegionOutlives(_)
2020
| Predicate::TypeOutlives(_)

src/librustc_privacy/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ trait DefIdVisitor<'tcx> {
6464
fn visit_trait(&mut self, trait_ref: TraitRef<'tcx>) -> bool {
6565
self.skeleton().visit_trait(trait_ref)
6666
}
67-
fn visit_predicates(&mut self, predicates: &ty::GenericPredicates<'tcx>) -> bool {
67+
fn visit_predicates(&mut self, predicates: ty::GenericPredicates<'tcx>) -> bool {
6868
self.skeleton().visit_predicates(predicates)
6969
}
7070
}
@@ -88,7 +88,7 @@ where
8888
(!self.def_id_visitor.shallow() && substs.visit_with(self))
8989
}
9090

91-
fn visit_predicates(&mut self, predicates: &ty::GenericPredicates<'tcx>) -> bool {
91+
fn visit_predicates(&mut self, predicates: ty::GenericPredicates<'tcx>) -> bool {
9292
let ty::GenericPredicates { parent: _, predicates } = predicates;
9393
for (predicate, _span) in predicates {
9494
match predicate {

src/librustc_traits/lowering/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ fn program_clauses_for_trait(tcx: TyCtxt<'_>, def_id: DefId) -> Clauses<'_> {
218218

219219
let implemented_from_env = Clause::ForAll(ty::Binder::bind(implemented_from_env));
220220

221-
let predicates = &tcx.predicates_defined_on(def_id).predicates;
221+
let predicates = tcx.predicates_defined_on(def_id).predicates;
222222

223223
// Warning: these where clauses are not substituted for bound vars yet,
224224
// so that we don't need to adjust binders in the `FromEnv` rules below
@@ -319,7 +319,7 @@ fn program_clauses_for_impl(tcx: TyCtxt<'tcx>, def_id: DefId) -> Clauses<'tcx> {
319319
let trait_pred = ty::TraitPredicate { trait_ref }.lower();
320320

321321
// `WC`
322-
let predicates = &tcx.predicates_of(def_id).predicates;
322+
let predicates = tcx.predicates_of(def_id).predicates;
323323
let where_clauses = predicates
324324
.iter()
325325
.map(|(wc, _)| wc.lower())

src/librustc_typeck/astconv.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ pub trait AstConv<'tcx> {
5454
/// but this can lead to cycle errors. The problem is that we have
5555
/// to do this resolution *in order to create the predicates in
5656
/// the first place*. Hence, we have this "special pass".
57-
fn get_type_parameter_bounds(&self, span: Span, def_id: DefId)
58-
-> &'tcx ty::GenericPredicates<'tcx>;
57+
fn get_type_parameter_bounds(&self, span: Span, def_id: DefId) -> ty::GenericPredicates<'tcx>;
5958

6059
/// Returns the lifetime to use when a lifetime is omitted (and not elided).
6160
fn re_infer(

0 commit comments

Comments
 (0)