Skip to content

Commit

Permalink
eliminate the Lrc of a slice and just return the slice
Browse files Browse the repository at this point in the history
Also, introduce `Clauses` and `Goals` type alises for readability.
  • Loading branch information
nikomatsakis committed Apr 23, 2018
1 parent 294cae2 commit 7fa3c8f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 34 deletions.
9 changes: 7 additions & 2 deletions src/librustc/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,16 @@ pub enum QuantifierKind {

#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub enum Goal<'tcx> {
Implies(&'tcx Slice<Clause<'tcx>>, &'tcx Goal<'tcx>),
Implies(Clauses<'tcx>, &'tcx Goal<'tcx>),
And(&'tcx Goal<'tcx>, &'tcx Goal<'tcx>),
Not(&'tcx Goal<'tcx>),
DomainGoal(DomainGoal<'tcx>),
Quantified(QuantifierKind, ty::Binder<&'tcx Goal<'tcx>>),
CannotProve,
}

pub type Goals<'tcx> = &'tcx Slice<Goal<'tcx>>;

impl<'tcx> Goal<'tcx> {
pub fn from_poly_domain_goal<'a>(
domain_goal: PolyDomainGoal<'tcx>,
Expand Down Expand Up @@ -319,6 +321,9 @@ pub enum Clause<'tcx> {
ForAll(ty::Binder<ProgramClause<'tcx>>),
}

/// Multiple clauses.
pub type Clauses<'tcx> = &'tcx Slice<Clause<'tcx>>;

/// A "program clause" has the form `D :- G1, ..., Gn`. It is saying
/// that the domain goal `D` is true if `G1...Gn` are provable. This
/// is equivalent to the implication `G1..Gn => D`; we usually write
Expand All @@ -331,7 +336,7 @@ pub struct ProgramClause<'tcx> {
pub goal: DomainGoal<'tcx>,

/// ...if we can prove these hypotheses (there may be no hypotheses at all):
pub hypotheses: &'tcx Slice<Goal<'tcx>>,
pub hypotheses: Goals<'tcx>,
}

pub type Selection<'tcx> = Vtable<'tcx, PredicateObligation<'tcx>>;
Expand Down
12 changes: 5 additions & 7 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use ty::subst::{Kind, Substs};
use ty::ReprOptions;
use ty::Instance;
use traits;
use traits::{Clause, Goal};
use traits::{Clause, Clauses, Goal, Goals};
use ty::{self, Ty, TypeAndMut};
use ty::{TyS, TypeVariants, Slice};
use ty::{AdtKind, AdtDef, ClosureSubsts, GeneratorInterior, Region, Const};
Expand Down Expand Up @@ -2517,15 +2517,15 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}
}

pub fn intern_clauses(self, ts: &[Clause<'tcx>]) -> &'tcx Slice<Clause<'tcx>> {
pub fn intern_clauses(self, ts: &[Clause<'tcx>]) -> Clauses<'tcx> {
if ts.len() == 0 {
Slice::empty()
} else {
self._intern_clauses(ts)
}
}

pub fn intern_goals(self, ts: &[Goal<'tcx>]) -> &'tcx Slice<Goal<'tcx>> {
pub fn intern_goals(self, ts: &[Goal<'tcx>]) -> Goals<'tcx> {
if ts.len() == 0 {
Slice::empty()
} else {
Expand Down Expand Up @@ -2579,13 +2579,11 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
self.mk_substs(iter::once(s).chain(t.into_iter().cloned()).map(Kind::from))
}

pub fn mk_clauses<I: InternAs<[Clause<'tcx>],
&'tcx Slice<Clause<'tcx>>>>(self, iter: I) -> I::Output {
pub fn mk_clauses<I: InternAs<[Clause<'tcx>], Clauses<'tcx>>>(self, iter: I) -> I::Output {
iter.intern_with(|xs| self.intern_clauses(xs))
}

pub fn mk_goals<I: InternAs<[Goal<'tcx>],
&'tcx Slice<Goal<'tcx>>>>(self, iter: I) -> I::Output {
pub fn mk_goals<I: InternAs<[Goal<'tcx>], Goals<'tcx>>>(self, iter: I) -> I::Output {
iter.intern_with(|xs| self.intern_goals(xs))
}

Expand Down
8 changes: 4 additions & 4 deletions src/librustc/ty/maps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ use traits::query::{CanonicalProjectionGoal, CanonicalTyGoal, NoSolution};
use traits::query::dropck_outlives::{DtorckConstraint, DropckOutlivesResult};
use traits::query::normalize::NormalizationResult;
use traits::specialization_graph;
use traits::Clause;
use ty::{self, CrateInherentImpls, ParamEnvAnd, Slice, Ty, TyCtxt};
use traits::Clauses;
use ty::{self, CrateInherentImpls, ParamEnvAnd, Ty, TyCtxt};
use ty::steal::Steal;
use ty::subst::Substs;
use util::nodemap::{DefIdSet, DefIdMap, ItemLocalSet};
Expand Down Expand Up @@ -445,11 +445,11 @@ define_maps! { <'tcx>

[] fn features_query: features_node(CrateNum) -> Lrc<feature_gate::Features>,

[] fn program_clauses_for: ProgramClausesFor(DefId) -> Lrc<&'tcx Slice<Clause<'tcx>>>,
[] fn program_clauses_for: ProgramClausesFor(DefId) -> Clauses<'tcx>,

[] fn program_clauses_for_env: ProgramClausesForEnv(
ty::ParamEnv<'tcx>
) -> Lrc<&'tcx Slice<Clause<'tcx>>>,
) -> Clauses<'tcx>,

[] fn wasm_custom_sections: WasmCustomSections(CrateNum) -> Lrc<Vec<DefId>>,
[] fn wasm_import_module_map: WasmImportModuleMap(CrateNum)
Expand Down
37 changes: 16 additions & 21 deletions src/librustc_traits/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ use rustc::hir::def_id::DefId;
use rustc::hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc::hir::map::definitions::DefPathData;
use rustc::hir::{self, ImplPolarity};
use rustc::traits::{Clause, DomainGoal, Goal, PolyDomainGoal, ProgramClause, WhereClauseAtom};
use rustc::traits::{Clause, Clauses, DomainGoal, Goal, PolyDomainGoal, ProgramClause,
WhereClauseAtom};
use rustc::ty::subst::Substs;
use rustc::ty::{self, Slice, TyCtxt};
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::sync::Lrc;
use std::mem;
use syntax::ast;

Expand Down Expand Up @@ -122,19 +122,19 @@ impl<'tcx> IntoFromEnvGoal for DomainGoal<'tcx> {
crate fn program_clauses_for<'a, 'tcx>(
tcx: TyCtxt<'a, 'tcx, 'tcx>,
def_id: DefId,
) -> Lrc<&'tcx Slice<Clause<'tcx>>> {
) -> Clauses<'tcx> {
match tcx.def_key(def_id).disambiguated_data.data {
DefPathData::Trait(_) => program_clauses_for_trait(tcx, def_id),
DefPathData::Impl => program_clauses_for_impl(tcx, def_id),
DefPathData::AssocTypeInImpl(..) => program_clauses_for_associated_type_value(tcx, def_id),
_ => Lrc::new(Slice::empty()),
_ => Slice::empty(),
}
}

crate fn program_clauses_for_env<'a, 'tcx>(
tcx: TyCtxt<'a, 'tcx, 'tcx>,
param_env: ty::ParamEnv<'tcx>,
) -> Lrc<&'tcx Slice<Clause<'tcx>>> {
) -> Clauses<'tcx> {
debug!("program_clauses_for_env(param_env={:?})", param_env);

let mut last_round = FxHashSet();
Expand Down Expand Up @@ -164,12 +164,10 @@ crate fn program_clauses_for_env<'a, 'tcx>(

debug!("program_clauses_for_env: closure = {:#?}", closure);

return Lrc::new(
tcx.mk_clauses(
closure
.into_iter()
.flat_map(|def_id| tcx.program_clauses_for(def_id).iter().cloned()),
),
return tcx.mk_clauses(
closure
.into_iter()
.flat_map(|def_id| tcx.program_clauses_for(def_id).iter().cloned()),
);

/// Given that `predicate` is in the environment, returns the
Expand All @@ -196,7 +194,7 @@ crate fn program_clauses_for_env<'a, 'tcx>(
fn program_clauses_for_trait<'a, 'tcx>(
tcx: TyCtxt<'a, 'tcx, 'tcx>,
def_id: DefId,
) -> Lrc<&'tcx Slice<Clause<'tcx>>> {
) -> Clauses<'tcx> {
// `trait Trait<P1..Pn> where WC { .. } // P0 == Self`

// Rule Implemented-From-Env (see rustc guide)
Expand Down Expand Up @@ -243,7 +241,7 @@ fn program_clauses_for_trait<'a, 'tcx>(
.into_iter()
.map(|wc| implied_bound_from_trait(tcx, trait_pred, wc));

Lrc::new(tcx.mk_clauses(clauses.chain(implied_bound_clauses)))
tcx.mk_clauses(clauses.chain(implied_bound_clauses))
}

/// For a given `where_clause`, returns a clause `FromEnv(WC) :- FromEnv(Self: Trait<P1..Pn>)`.
Expand All @@ -262,12 +260,9 @@ fn implied_bound_from_trait<'a, 'tcx>(
}))
}

fn program_clauses_for_impl<'a, 'tcx>(
tcx: TyCtxt<'a, 'tcx, 'tcx>,
def_id: DefId,
) -> Lrc<&'tcx Slice<Clause<'tcx>>> {
fn program_clauses_for_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Clauses<'tcx> {
if let ImplPolarity::Negative = tcx.impl_polarity(def_id) {
return Lrc::new(tcx.mk_clauses(iter::empty::<Clause>()));
return Slice::empty();
}

// Rule Implemented-From-Impl (see rustc guide)
Expand Down Expand Up @@ -295,13 +290,13 @@ fn program_clauses_for_impl<'a, 'tcx>(
.map(|wc| Goal::from_poly_domain_goal(wc, tcx)),
),
};
Lrc::new(tcx.mk_clauses(iter::once(Clause::ForAll(ty::Binder::dummy(clause)))))
tcx.mk_clauses(iter::once(Clause::ForAll(ty::Binder::dummy(clause))))
}

pub fn program_clauses_for_associated_type_value<'a, 'tcx>(
tcx: TyCtxt<'a, 'tcx, 'tcx>,
item_id: DefId,
) -> Lrc<&'tcx Slice<Clause<'tcx>>> {
) -> Clauses<'tcx> {
// Rule Normalize-From-Impl (see rustc guide)
//
// ```impl<P0..Pn> Trait<A1..An> for A0
Expand Down Expand Up @@ -349,7 +344,7 @@ pub fn program_clauses_for_associated_type_value<'a, 'tcx>(
.map(|wc| Goal::from_poly_domain_goal(wc, tcx)),
),
};
Lrc::new(tcx.mk_clauses(iter::once(Clause::ForAll(ty::Binder::dummy(clause)))))
tcx.mk_clauses(iter::once(Clause::ForAll(ty::Binder::dummy(clause))))
}

pub fn dump_program_clauses<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
Expand Down

0 comments on commit 7fa3c8f

Please sign in to comment.