Update GenericPredicates queries

This commit is contained in:
John Kåre Alsaker 2018-12-01 18:13:27 +01:00
parent ba5d9c094d
commit ae8975c812
11 changed files with 64 additions and 55 deletions

View File

@ -68,6 +68,7 @@ macro_rules! arena_types {
>
>,
[few] resolve_lifetimes: rustc::middle::resolve_lifetime::ResolveLifetimes,
[decode] generic_predicates: rustc::ty::GenericPredicates<'tcx>,
], $tcx);
)
}

View File

@ -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(_: DefId) -> Lrc<ty::GenericPredicates<'tcx>> {}
query predicates_of(_: DefId) -> &'tcx ty::GenericPredicates<'tcx> {}
query native_libraries(_: CrateNum) -> Lrc<Vec<NativeLibrary>> {
desc { "looking up the native libraries of a linked crate" }
@ -166,11 +166,11 @@ rustc_queries! {
/// equal to the `explicit_predicates_of` predicates plus the
/// `inferred_outlives_of` predicates.
query predicates_defined_on(_: DefId)
-> Lrc<ty::GenericPredicates<'tcx>> {}
-> &'tcx ty::GenericPredicates<'tcx> {}
/// Returns the predicates written explicitly by the user.
query explicit_predicates_of(_: DefId)
-> Lrc<ty::GenericPredicates<'tcx>> {}
-> &'tcx ty::GenericPredicates<'tcx> {}
/// Returns the inferred outlives predicates (e.g., for `struct
/// Foo<'a, T> { x: &'a T }`, this would return `T: 'a`).
@ -182,14 +182,14 @@ rustc_queries! {
/// 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) -> Lrc<ty::GenericPredicates<'tcx>> {
query super_predicates_of(key: DefId) -> &'tcx 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))
-> Lrc<ty::GenericPredicates<'tcx>> {
-> &'tcx ty::GenericPredicates<'tcx> {
no_force
desc { |tcx| "computing the bounds for type parameter `{}`", {
let id = tcx.hir().as_local_hir_id(key.1).unwrap();

View File

@ -201,6 +201,10 @@ impl<'gcx: 'tcx, '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>,
@ -1045,6 +1049,9 @@ pub struct GlobalCtxt<'tcx> {
pub dep_graph: DepGraph,
/// Common objects.
pub common: Common<'tcx>,
/// Common types, pre-interned for your convenience.
pub types: CommonTypes<'tcx>,
@ -1252,6 +1259,12 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, '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);
@ -1308,6 +1321,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
global_arenas: &arenas.global,
global_interners: interners,
dep_graph,
common,
types: common_types,
lifetimes: common_lifetimes,
consts: common_consts,

View File

@ -2298,7 +2298,7 @@ impl<'a, 'gcx, 'tcx> AdtDef {
}
#[inline]
pub fn predicates(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Lrc<GenericPredicates<'gcx>> {
pub fn predicates(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> &'tcx GenericPredicates<'gcx> {
tcx.predicates_of(self.did)
}

View File

@ -96,9 +96,11 @@ provide! { <'tcx> tcx, def_id, other, cdata,
generics_of => {
tcx.alloc_generics(cdata.get_generics(def_id.index, tcx.sess))
}
predicates_of => { Lrc::new(cdata.get_predicates(def_id.index, tcx)) }
predicates_defined_on => { Lrc::new(cdata.get_predicates_defined_on(def_id.index, tcx)) }
super_predicates_of => { Lrc::new(cdata.get_super_predicates(def_id.index, tcx)) }
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)) }
trait_def => {
tcx.alloc_trait_def(cdata.get_trait_def(def_id.index, tcx.sess))
}

View File

@ -67,7 +67,7 @@ trait DefIdVisitor<'a, 'tcx: 'a> {
fn visit_trait(&mut self, trait_ref: TraitRef<'tcx>) -> bool {
self.skeleton().visit_trait(trait_ref)
}
fn visit_predicates(&mut self, predicates: Lrc<ty::GenericPredicates<'tcx>>) -> bool {
fn visit_predicates(&mut self, predicates: &ty::GenericPredicates<'tcx>) -> bool {
self.skeleton().visit_predicates(predicates)
}
}
@ -89,8 +89,8 @@ impl<'a, 'tcx, V> DefIdVisitorSkeleton<'_, 'a, 'tcx, V>
(!self.def_id_visitor.shallow() && substs.visit_with(self))
}
fn visit_predicates(&mut self, predicates: Lrc<ty::GenericPredicates<'tcx>>) -> bool {
let ty::GenericPredicates { parent: _, predicates } = &*predicates;
fn visit_predicates(&mut self, predicates: &ty::GenericPredicates<'tcx>) -> bool {
let ty::GenericPredicates { parent: _, predicates } = predicates;
for (predicate, _span) in predicates {
match predicate {
ty::Predicate::Trait(poly_predicate) => {

View File

@ -17,7 +17,6 @@ use rustc::ty::{GenericParamDef, GenericParamDefKind};
use rustc::ty::subst::{Kind, Subst, InternalSubsts, SubstsRef};
use rustc::ty::wf::object_region_bounds;
use rustc::mir::interpret::ConstValue;
use rustc_data_structures::sync::Lrc;
use rustc_target::spec::abi;
use crate::require_c_abi_if_c_variadic;
use smallvec::SmallVec;
@ -46,7 +45,7 @@ pub trait AstConv<'gcx, 'tcx> {
/// Returns the set of bounds in scope for the type parameter with
/// the given id.
fn get_type_parameter_bounds(&self, span: Span, def_id: DefId)
-> Lrc<ty::GenericPredicates<'tcx>>;
-> &'tcx ty::GenericPredicates<'tcx>;
/// What lifetime should we use when a lifetime is omitted (and not elided)?
fn re_infer(&self, span: Span, _def: Option<&ty::GenericParamDef>)

View File

@ -97,7 +97,6 @@ use crate::namespace::Namespace;
use rustc::infer::{self, InferCtxt, InferOk, InferResult};
use rustc::infer::canonical::{Canonical, OriginalQueryValues, QueryResponse};
use rustc_data_structures::indexed_vec::Idx;
use rustc_data_structures::sync::Lrc;
use rustc_target::spec::abi::Abi;
use rustc::infer::opaque_types::OpaqueTypeDecl;
use rustc::infer::type_variable::{TypeVariableOrigin};
@ -1907,7 +1906,7 @@ impl<'a, 'gcx, 'tcx> AstConv<'gcx, 'tcx> for FnCtxt<'a, 'gcx, 'tcx> {
fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> { self.tcx }
fn get_type_parameter_bounds(&self, _: Span, def_id: DefId)
-> Lrc<ty::GenericPredicates<'tcx>>
-> &'tcx ty::GenericPredicates<'tcx>
{
let tcx = self.tcx;
let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
@ -1915,7 +1914,7 @@ impl<'a, 'gcx, 'tcx> AstConv<'gcx, 'tcx> for FnCtxt<'a, 'gcx, 'tcx> {
let item_def_id = tcx.hir().local_def_id_from_hir_id(item_id);
let generics = tcx.generics_of(item_def_id);
let index = generics.param_def_id_to_index[&def_id];
Lrc::new(ty::GenericPredicates {
tcx.arena.alloc(ty::GenericPredicates {
parent: None,
predicates: self.param_env.caller_bounds.iter().filter_map(|&predicate| {
match predicate {

View File

@ -31,7 +31,6 @@ use rustc::ty::{self, AdtKind, ToPolyTraitRef, Ty, TyCtxt};
use rustc::ty::{ReprOptions, ToPredicate};
use rustc::util::captures::Captures;
use rustc::util::nodemap::FxHashMap;
use rustc_data_structures::sync::Lrc;
use rustc_target::spec::abi;
use syntax::ast;
@ -178,7 +177,7 @@ impl<'a, 'tcx> AstConv<'tcx, 'tcx> for ItemCtxt<'a, 'tcx> {
}
fn get_type_parameter_bounds(&self, span: Span, def_id: DefId)
-> Lrc<ty::GenericPredicates<'tcx>> {
-> &'tcx ty::GenericPredicates<'tcx> {
self.tcx
.at(span)
.type_param_predicates((self.item_def_id, def_id))
@ -243,7 +242,7 @@ impl<'a, 'tcx> AstConv<'tcx, 'tcx> for ItemCtxt<'a, 'tcx> {
fn type_param_predicates<'a, 'tcx>(
tcx: TyCtxt<'a, 'tcx, 'tcx>,
(item_def_id, def_id): (DefId, DefId),
) -> Lrc<ty::GenericPredicates<'tcx>> {
) -> &'tcx ty::GenericPredicates<'tcx> {
use rustc::hir::*;
// In the AST, bounds can derive from two places. Either
@ -264,16 +263,11 @@ fn type_param_predicates<'a, 'tcx>(
tcx.generics_of(item_def_id).parent
};
let mut result = parent.map_or_else(
|| Lrc::new(ty::GenericPredicates {
parent: None,
predicates: vec![],
}),
|parent| {
let icx = ItemCtxt::new(tcx, parent);
icx.get_type_parameter_bounds(DUMMY_SP, def_id)
},
);
let result = parent.map_or(&tcx.common.empty_predicates, |parent| {
let icx = ItemCtxt::new(tcx, parent);
icx.get_type_parameter_bounds(DUMMY_SP, def_id)
});
let mut extend = None;
let item_hir_id = tcx.hir().as_local_hir_id(item_def_id).unwrap();
let ast_generics = match tcx.hir().get_by_hir_id(item_hir_id) {
@ -298,9 +292,7 @@ fn type_param_predicates<'a, 'tcx>(
// Implied `Self: Trait` and supertrait bounds.
if param_id == item_hir_id {
let identity_trait_ref = ty::TraitRef::identity(tcx, item_def_id);
Lrc::make_mut(&mut result)
.predicates
.push((identity_trait_ref.to_predicate(), item.span));
extend = Some((identity_trait_ref.to_predicate(), item.span));
}
generics
}
@ -317,11 +309,12 @@ fn type_param_predicates<'a, 'tcx>(
};
let icx = ItemCtxt::new(tcx, item_def_id);
Lrc::make_mut(&mut result)
.predicates
.extend(icx.type_parameter_bounds_in_generics(ast_generics, param_id, ty,
OnlySelfBounds(true)));
result
let mut result = (*result).clone();
result.predicates.extend(extend.into_iter());
result.predicates
.extend(icx.type_parameter_bounds_in_generics(ast_generics, param_id, ty,
OnlySelfBounds(true)));
tcx.arena.alloc(result)
}
impl<'a, 'tcx> ItemCtxt<'a, 'tcx> {
@ -690,7 +683,7 @@ fn adt_def<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx ty::Ad
fn super_predicates_of<'a, 'tcx>(
tcx: TyCtxt<'a, 'tcx, 'tcx>,
trait_def_id: DefId,
) -> Lrc<ty::GenericPredicates<'tcx>> {
) -> &'tcx ty::GenericPredicates<'tcx> {
debug!("super_predicates(trait_def_id={:?})", trait_def_id);
let trait_hir_id = tcx.hir().as_local_hir_id(trait_def_id).unwrap();
@ -734,7 +727,7 @@ fn super_predicates_of<'a, 'tcx>(
}
}
Lrc::new(ty::GenericPredicates {
tcx.arena.alloc(ty::GenericPredicates {
parent: None,
predicates: superbounds,
})
@ -1842,7 +1835,7 @@ fn early_bound_lifetimes_from_generics<'a, 'tcx>(
fn predicates_defined_on<'a, 'tcx>(
tcx: TyCtxt<'a, 'tcx, 'tcx>,
def_id: DefId,
) -> Lrc<ty::GenericPredicates<'tcx>> {
) -> &'tcx ty::GenericPredicates<'tcx> {
debug!("predicates_defined_on({:?})", def_id);
let mut result = tcx.explicit_predicates_of(def_id);
debug!(
@ -1858,9 +1851,9 @@ fn predicates_defined_on<'a, 'tcx>(
def_id,
inferred_outlives,
);
Lrc::make_mut(&mut result)
.predicates
.extend(inferred_outlives.iter().map(|&p| (p, span)));
let mut predicates = (*result).clone();
predicates.predicates.extend(inferred_outlives.iter().map(|&p| (p, span)));
result = tcx.arena.alloc(predicates);
}
debug!("predicates_defined_on({:?}) = {:?}", def_id, result);
result
@ -1872,7 +1865,7 @@ fn predicates_defined_on<'a, 'tcx>(
fn predicates_of<'a, 'tcx>(
tcx: TyCtxt<'a, 'tcx, 'tcx>,
def_id: DefId,
) -> Lrc<ty::GenericPredicates<'tcx>> {
) -> &'tcx ty::GenericPredicates<'tcx> {
let mut result = tcx.predicates_defined_on(def_id);
if tcx.is_trait(def_id) {
@ -1889,9 +1882,9 @@ fn predicates_of<'a, 'tcx>(
// used, and adding the predicate into this list ensures
// that this is done.
let span = tcx.def_span(def_id);
Lrc::make_mut(&mut result)
.predicates
.push((ty::TraitRef::identity(tcx, def_id).to_predicate(), span));
let mut predicates = (*result).clone();
predicates.predicates.push((ty::TraitRef::identity(tcx, def_id).to_predicate(), span));
result = tcx.arena.alloc(predicates);
}
debug!("predicates_of(def_id={:?}) = {:?}", def_id, result);
result
@ -1902,7 +1895,7 @@ fn predicates_of<'a, 'tcx>(
fn explicit_predicates_of<'a, 'tcx>(
tcx: TyCtxt<'a, 'tcx, 'tcx>,
def_id: DefId,
) -> Lrc<ty::GenericPredicates<'tcx>> {
) -> &'tcx ty::GenericPredicates<'tcx> {
use rustc::hir::*;
use rustc_data_structures::fx::FxHashSet;
@ -2017,7 +2010,7 @@ fn explicit_predicates_of<'a, 'tcx>(
if impl_trait_fn.is_some() {
// impl Trait
return Lrc::new(ty::GenericPredicates {
return tcx.arena.alloc(ty::GenericPredicates {
parent: None,
predicates: bounds.predicates(tcx, opaque_ty),
});
@ -2228,7 +2221,7 @@ fn explicit_predicates_of<'a, 'tcx>(
);
}
let result = Lrc::new(ty::GenericPredicates {
let result = tcx.arena.alloc(ty::GenericPredicates {
parent: generics.parent,
predicates,
});

View File

@ -104,8 +104,10 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
// Instead, we generate `impl !Send for Foo<T>`, which better
// expresses the fact that `Foo<T>` never implements `Send`,
// regardless of the choice of `T`.
let params = (self.cx.tcx.generics_of(param_env_def_id), &Default::default())
.clean(self.cx).params;
let params = (
self.cx.tcx.generics_of(param_env_def_id),
&&self.cx.tcx.common.empty_predicates,
).clean(self.cx).params;
Generics {
params,

View File

@ -10,7 +10,6 @@ mod auto_trait;
mod blanket_impl;
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
use rustc_data_structures::sync::Lrc;
use rustc_target::spec::abi::Abi;
use rustc_typeck::hir_ty_to_ty;
use rustc::infer::region_constraints::{RegionConstraintData, Constraint};
@ -1687,7 +1686,7 @@ impl Clean<Generics> for hir::Generics {
}
impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics,
&'a Lrc<ty::GenericPredicates<'tcx>>) {
&'a &'tcx ty::GenericPredicates<'tcx>) {
fn clean(&self, cx: &DocContext<'_>) -> Generics {
use self::WherePredicate as WP;