Skip to content

Commit

Permalink
Auto merge of #65541 - eddyb:spanned-inferred-outlives, r=<try>
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
bors committed Oct 19, 2019
2 parents 518deda + 2e387e2 commit c9b528f
Show file tree
Hide file tree
Showing 30 changed files with 174 additions and 166 deletions.
1 change: 0 additions & 1 deletion src/librustc/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ macro_rules! arena_types {
rustc::hir::def_id::DefId,
>,
[few] resolve_lifetimes: rustc::middle::resolve_lifetime::ResolveLifetimes,
[decode] generic_predicates: rustc::ty::GenericPredicates<'tcx>,
[few] lint_levels: rustc::lint::LintLevelMap,
[few] stability_index: rustc::middle::stability::Index<'tcx>,
[few] features: syntax::feature_gate::Features,
Expand Down
15 changes: 6 additions & 9 deletions src/librustc/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ rustc_queries! {
/// predicate gets in the way of some checks, which are intended
/// to operate over only the actual where-clauses written by the
/// user.)
query predicates_of(key: DefId) -> &'tcx ty::GenericPredicates<'tcx> {
query predicates_of(key: DefId) -> ty::GenericPredicates<'tcx> {
cache_on_disk_if { key.is_local() }
}

Expand Down Expand Up @@ -184,31 +184,28 @@ rustc_queries! {
/// predicates (where-clauses) directly defined on it. This is
/// equal to the `explicit_predicates_of` predicates plus the
/// `inferred_outlives_of` predicates.
query predicates_defined_on(_: DefId)
-> &'tcx ty::GenericPredicates<'tcx> {}
query predicates_defined_on(_: DefId) -> ty::GenericPredicates<'tcx> {}

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

/// Returns the inferred outlives predicates (e.g., for `struct
/// Foo<'a, T> { x: &'a T }`, this would return `T: 'a`).
query inferred_outlives_of(_: DefId) -> &'tcx [ty::Predicate<'tcx>] {}
query inferred_outlives_of(_: DefId) -> &'tcx [(ty::Predicate<'tcx>, Span)] {}

/// Maps from the `DefId` of a trait to the list of
/// super-predicates. This is a subset of the full list of
/// predicates. We store these in a separate map because we must
/// evaluate them even during type conversion, often before the
/// full predicates are available (note that supertraits have
/// additional acyclicity requirements).
query super_predicates_of(key: DefId) -> &'tcx ty::GenericPredicates<'tcx> {
query super_predicates_of(key: DefId) -> ty::GenericPredicates<'tcx> {
desc { |tcx| "computing the supertraits of `{}`", tcx.def_path_str(key) }
}

/// To avoid cycles within the predicates of a single item we compute
/// per-type-parameter predicates for resolving `T::AssocTy`.
query type_param_predicates(key: (DefId, DefId))
-> &'tcx ty::GenericPredicates<'tcx> {
query type_param_predicates(key: (DefId, DefId)) -> ty::GenericPredicates<'tcx> {
no_force
desc { |tcx| "computing the bounds for type parameter `{}`", {
let id = tcx.hir().as_local_hir_id(key.1).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/traits/specialize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ fn to_pretty_impl_header(tcx: TyCtxt<'_>, impl_def_id: DefId) -> Option<String>

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

Expand Down
35 changes: 20 additions & 15 deletions src/librustc/ty/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::intrinsics;
use crate::ty::{self, Ty, TyCtxt};
use crate::ty::subst::SubstsRef;
use crate::mir::interpret::Allocation;
use syntax_pos::Span;

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

pub fn encode_predicates<'tcx, E, C>(encoder: &mut E,
predicates: &ty::GenericPredicates<'tcx>,
cache: C)
-> Result<(), E::Error>
pub fn encode_spanned_predicates<'tcx, E, C>(
encoder: &mut E,
predicates: &'tcx [(ty::Predicate<'tcx>, Span)],
cache: C,
) -> Result<(), E::Error>
where E: TyEncoder,
C: for<'b> Fn(&'b mut E) -> &'b mut FxHashMap<ty::Predicate<'tcx>, usize>,
{
predicates.parent.encode(encoder)?;
predicates.predicates.len().encode(encoder)?;
for (predicate, span) in &predicates.predicates {
predicates.len().encode(encoder)?;
for (predicate, span) in predicates {
encode_with_shorthand(encoder, predicate, &cache)?;
span.encode(encoder)?;
}
Expand Down Expand Up @@ -182,13 +183,15 @@ where
}

#[inline]
pub fn decode_predicates<D>(decoder: &mut D) -> Result<ty::GenericPredicates<'tcx>, D::Error>
pub fn decode_spanned_predicates<D>(
decoder: &mut D,
) -> Result<&'tcx [(ty::Predicate<'tcx>, Span)], D::Error>
where
D: TyDecoder<'tcx>,
{
Ok(ty::GenericPredicates {
parent: Decodable::decode(decoder)?,
predicates: (0..decoder.read_usize()?).map(|_| {
let tcx = decoder.tcx();
Ok(tcx.arena.alloc_from_iter(
(0..decoder.read_usize()?).map(|_| {
// Handle shorthands first, if we have an usize > 0x80.
let predicate = if decoder.positioned_at_shorthand() {
let pos = decoder.read_usize()?;
Expand All @@ -202,7 +205,7 @@ where
Ok((predicate, Decodable::decode(decoder)?))
})
.collect::<Result<Vec<_>, _>>()?,
})
))
}

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

use syntax_pos::Span;

use super::$DecoderName;

impl<$($typaram ),*> Decoder for $DecoderName<$($typaram),*> {
Expand Down Expand Up @@ -393,11 +398,11 @@ macro_rules! implement_ty_decoder {
}
}

impl<$($typaram),*> SpecializedDecoder<ty::GenericPredicates<'tcx>>
impl<$($typaram),*> SpecializedDecoder<&'tcx [(ty::Predicate<'tcx>, Span)]>
for $DecoderName<$($typaram),*> {
fn specialized_decode(&mut self)
-> Result<ty::GenericPredicates<'tcx>, Self::Error> {
decode_predicates(self)
-> Result<&'tcx [(ty::Predicate<'tcx>, Span)], Self::Error> {
decode_spanned_predicates(self)
}
}

Expand Down
14 changes: 0 additions & 14 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,6 @@ impl<'tcx> CtxtInterners<'tcx> {
}
}

pub struct Common<'tcx> {
pub empty_predicates: ty::GenericPredicates<'tcx>,
}

pub struct CommonTypes<'tcx> {
pub unit: Ty<'tcx>,
pub bool: Ty<'tcx>,
Expand Down Expand Up @@ -1039,9 +1035,6 @@ pub struct GlobalCtxt<'tcx> {

pub prof: SelfProfilerRef,

/// Common objects.
pub common: Common<'tcx>,

/// Common types, pre-interned for your convenience.
pub types: CommonTypes<'tcx>,

Expand Down Expand Up @@ -1213,12 +1206,6 @@ impl<'tcx> TyCtxt<'tcx> {
s.fatal(&err);
});
let interners = CtxtInterners::new(&arenas.interner);
let common = Common {
empty_predicates: ty::GenericPredicates {
parent: None,
predicates: vec![],
},
};
let common_types = CommonTypes::new(&interners);
let common_lifetimes = CommonLifetimes::new(&interners);
let common_consts = CommonConsts::new(&interners, &common_types);
Expand Down Expand Up @@ -1273,7 +1260,6 @@ impl<'tcx> TyCtxt<'tcx> {
interners,
dep_graph,
prof: s.prof.clone(),
common,
types: common_types,
lifetimes: common_lifetimes,
consts: common_consts,
Expand Down
13 changes: 5 additions & 8 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1018,15 +1018,12 @@ impl<'tcx> Generics {
}

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

impl<'tcx> rustc_serialize::UseSpecializedEncodable for GenericPredicates<'tcx> {}
impl<'tcx> rustc_serialize::UseSpecializedDecodable for GenericPredicates<'tcx> {}

impl<'tcx> GenericPredicates<'tcx> {
pub fn instantiate(
&self,
Expand Down Expand Up @@ -1139,7 +1136,7 @@ pub struct CratePredicatesMap<'tcx> {
/// For each struct with outlive bounds, maps to a vector of the
/// predicate of its outlive bounds. If an item has no outlives
/// bounds, it will have no entry.
pub predicates: FxHashMap<DefId, &'tcx [ty::Predicate<'tcx>]>,
pub predicates: FxHashMap<DefId, &'tcx [(ty::Predicate<'tcx>, Span)]>,
}

impl<'tcx> AsRef<Predicate<'tcx>> for Predicate<'tcx> {
Expand Down Expand Up @@ -2321,7 +2318,7 @@ impl<'tcx> AdtDef {
}

#[inline]
pub fn predicates(&self, tcx: TyCtxt<'tcx>) -> &'tcx GenericPredicates<'tcx> {
pub fn predicates(&self, tcx: TyCtxt<'tcx>) -> GenericPredicates<'tcx> {
tcx.predicates_of(self.did)
}

Expand Down Expand Up @@ -2561,7 +2558,7 @@ impl<'tcx> AdtDef {
def_id: sized_trait,
substs: tcx.mk_substs_trait(ty, &[])
}).to_predicate();
let predicates = &tcx.predicates_of(self.did).predicates;
let predicates = tcx.predicates_of(self.did).predicates;
if predicates.iter().any(|(p, _)| *p == sized_predicate) {
vec![]
} else {
Expand Down
7 changes: 4 additions & 3 deletions src/librustc/ty/query/on_disk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -882,15 +882,16 @@ where
}
}

impl<'a, 'tcx, E> SpecializedEncoder<ty::GenericPredicates<'tcx>> for CacheEncoder<'a, 'tcx, E>
impl<'a, 'tcx, E> SpecializedEncoder<&'tcx [(ty::Predicate<'tcx>, Span)]>
for CacheEncoder<'a, 'tcx, E>
where
E: 'a + TyEncoder,
{
#[inline]
fn specialized_encode(&mut self,
predicates: &ty::GenericPredicates<'tcx>)
predicates: &&'tcx [(ty::Predicate<'tcx>, Span)])
-> Result<(), Self::Error> {
ty_codec::encode_predicates(self, predicates,
ty_codec::encode_spanned_predicates(self, predicates,
|encoder| &mut encoder.predicate_shorthands)
}
}
Expand Down
6 changes: 0 additions & 6 deletions src/librustc/ty/structural_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1218,12 +1218,6 @@ EnumTypeFoldableImpl! {
}
}

BraceStructTypeFoldableImpl! {
impl<'tcx> TypeFoldable<'tcx> for ty::GenericPredicates<'tcx> {
parent, predicates
}
}

impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
// This code is hot enough that it's worth specializing for a list of
Expand Down
12 changes: 6 additions & 6 deletions src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1241,7 +1241,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TrivialConstraints {
if cx.tcx.features().trivial_bounds {
let def_id = cx.tcx.hir().local_def_id(item.hir_id);
let predicates = cx.tcx.predicates_of(def_id);
for &(predicate, span) in &predicates.predicates {
for &(predicate, span) in predicates.predicates {
let predicate_kind_name = match predicate {
Trait(..) => "Trait",
TypeOutlives(..) |
Expand Down Expand Up @@ -1518,10 +1518,10 @@ declare_lint_pass!(ExplicitOutlivesRequirements => [EXPLICIT_OUTLIVES_REQUIREMEN

impl ExplicitOutlivesRequirements {
fn lifetimes_outliving_lifetime<'tcx>(
inferred_outlives: &'tcx [ty::Predicate<'tcx>],
inferred_outlives: &'tcx [(ty::Predicate<'tcx>, Span)],
index: u32,
) -> Vec<ty::Region<'tcx>> {
inferred_outlives.iter().filter_map(|pred| {
inferred_outlives.iter().filter_map(|(pred, _)| {
match pred {
ty::Predicate::RegionOutlives(outlives) => {
let outlives = outlives.skip_binder();
Expand All @@ -1538,10 +1538,10 @@ impl ExplicitOutlivesRequirements {
}

fn lifetimes_outliving_type<'tcx>(
inferred_outlives: &'tcx [ty::Predicate<'tcx>],
inferred_outlives: &'tcx [(ty::Predicate<'tcx>, Span)],
index: u32,
) -> Vec<ty::Region<'tcx>> {
inferred_outlives.iter().filter_map(|pred| {
inferred_outlives.iter().filter_map(|(pred, _)| {
match pred {
ty::Predicate::TypeOutlives(outlives) => {
let outlives = outlives.skip_binder();
Expand All @@ -1560,7 +1560,7 @@ impl ExplicitOutlivesRequirements {
&self,
param: &'tcx hir::GenericParam,
tcx: TyCtxt<'tcx>,
inferred_outlives: &'tcx [ty::Predicate<'tcx>],
inferred_outlives: &'tcx [(ty::Predicate<'tcx>, Span)],
ty_generics: &'tcx ty::Generics,
) -> Vec<ty::Region<'tcx>> {
let index = ty_generics.param_def_id_to_index[
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_lint/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
}
ty::Opaque(def, _) => {
let mut has_emitted = false;
for (predicate, _) in &cx.tcx.predicates_of(def).predicates {
for (predicate, _) in cx.tcx.predicates_of(def).predicates {
if let ty::Predicate::Trait(ref poly_trait_predicate) = predicate {
let trait_ref = poly_trait_predicate.skip_binder().trait_ref;
let def_id = trait_ref.def_id;
Expand Down
8 changes: 3 additions & 5 deletions src/librustc_metadata/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,9 @@ provide! { <'tcx> tcx, def_id, other, cdata,
generics_of => {
tcx.arena.alloc(cdata.get_generics(def_id.index, tcx.sess))
}
predicates_of => { tcx.arena.alloc(cdata.get_predicates(def_id.index, tcx)) }
predicates_defined_on => {
tcx.arena.alloc(cdata.get_predicates_defined_on(def_id.index, tcx))
}
super_predicates_of => { tcx.arena.alloc(cdata.get_super_predicates(def_id.index, tcx)) }
predicates_of => { cdata.get_predicates(def_id.index, tcx) }
predicates_defined_on => { cdata.get_predicates_defined_on(def_id.index, tcx) }
super_predicates_of => { cdata.get_super_predicates(def_id.index, tcx) }
trait_def => {
tcx.arena.alloc(cdata.get_trait_def(def_id.index, tcx.sess))
}
Expand Down
Loading

0 comments on commit c9b528f

Please sign in to comment.