Skip to content

Commit c9b528f

Browse files
committed
Auto merge of #65541 - eddyb:spanned-inferred-outlives, r=<try>
rustc: add `Span`s to `inferred_outlives_of` predicates. *Based on #65535.* This would simplify #59789, and I suspect it has some potential in diagnostics (although we don't seem to use the predicate `Span`s much atm).
2 parents 518deda + 2e387e2 commit c9b528f

File tree

30 files changed

+174
-166
lines changed

30 files changed

+174
-166
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

+6-9
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,31 +184,28 @@ 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`).
196-
query inferred_outlives_of(_: DefId) -> &'tcx [ty::Predicate<'tcx>] {}
194+
query inferred_outlives_of(_: DefId) -> &'tcx [(ty::Predicate<'tcx>, Span)] {}
197195

198196
/// Maps from the `DefId` of a trait to the list of
199197
/// super-predicates. This is a subset of the full list of
200198
/// predicates. We store these in a separate map because we must
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

+5-8
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,
@@ -1139,7 +1136,7 @@ pub struct CratePredicatesMap<'tcx> {
11391136
/// For each struct with outlive bounds, maps to a vector of the
11401137
/// predicate of its outlive bounds. If an item has no outlives
11411138
/// bounds, it will have no entry.
1142-
pub predicates: FxHashMap<DefId, &'tcx [ty::Predicate<'tcx>]>,
1139+
pub predicates: FxHashMap<DefId, &'tcx [(ty::Predicate<'tcx>, Span)]>,
11431140
}
11441141

11451142
impl<'tcx> AsRef<Predicate<'tcx>> for Predicate<'tcx> {
@@ -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

+6-6
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(..) |
@@ -1518,10 +1518,10 @@ declare_lint_pass!(ExplicitOutlivesRequirements => [EXPLICIT_OUTLIVES_REQUIREMEN
15181518

15191519
impl ExplicitOutlivesRequirements {
15201520
fn lifetimes_outliving_lifetime<'tcx>(
1521-
inferred_outlives: &'tcx [ty::Predicate<'tcx>],
1521+
inferred_outlives: &'tcx [(ty::Predicate<'tcx>, Span)],
15221522
index: u32,
15231523
) -> Vec<ty::Region<'tcx>> {
1524-
inferred_outlives.iter().filter_map(|pred| {
1524+
inferred_outlives.iter().filter_map(|(pred, _)| {
15251525
match pred {
15261526
ty::Predicate::RegionOutlives(outlives) => {
15271527
let outlives = outlives.skip_binder();
@@ -1538,10 +1538,10 @@ impl ExplicitOutlivesRequirements {
15381538
}
15391539

15401540
fn lifetimes_outliving_type<'tcx>(
1541-
inferred_outlives: &'tcx [ty::Predicate<'tcx>],
1541+
inferred_outlives: &'tcx [(ty::Predicate<'tcx>, Span)],
15421542
index: u32,
15431543
) -> Vec<ty::Region<'tcx>> {
1544-
inferred_outlives.iter().filter_map(|pred| {
1544+
inferred_outlives.iter().filter_map(|(pred, _)| {
15451545
match pred {
15461546
ty::Predicate::TypeOutlives(outlives) => {
15471547
let outlives = outlives.skip_binder();
@@ -1560,7 +1560,7 @@ impl ExplicitOutlivesRequirements {
15601560
&self,
15611561
param: &'tcx hir::GenericParam,
15621562
tcx: TyCtxt<'tcx>,
1563-
inferred_outlives: &'tcx [ty::Predicate<'tcx>],
1563+
inferred_outlives: &'tcx [(ty::Predicate<'tcx>, Span)],
15641564
ty_generics: &'tcx ty::Generics,
15651565
) -> Vec<ty::Region<'tcx>> {
15661566
let index = ty_generics.param_def_id_to_index[

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
}

0 commit comments

Comments
 (0)