Skip to content

Commit dbab381

Browse files
committed
Auto merge of #55040 - scalexm:param-env, r=nikomatsakis
Replace `ParamEnv` with a new type in chalk context. I left a few FIXMEs. r? @nikomatsakis
2 parents cb5e1b9 + 55ce7a2 commit dbab381

File tree

16 files changed

+553
-116
lines changed

16 files changed

+553
-116
lines changed

src/librustc/dep_graph/dep_node.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ use rustc_data_structures::stable_hasher::{StableHasher, HashStable};
7070
use std::fmt;
7171
use std::hash::Hash;
7272
use syntax_pos::symbol::InternedString;
73+
use traits;
7374
use traits::query::{
7475
CanonicalProjectionGoal, CanonicalTyGoal, CanonicalTypeOpEqGoal, CanonicalTypeOpSubtypeGoal,
7576
CanonicalPredicateGoal, CanonicalTypeOpProvePredicateGoal, CanonicalTypeOpNormalizeGoal,
@@ -550,6 +551,7 @@ define_dep_nodes!( <'tcx>
550551
[anon] TraitSelect,
551552

552553
[] ParamEnv(DefId),
554+
[] Environment(DefId),
553555
[] DescribeDef(DefId),
554556

555557
// FIXME(mw): DefSpans are not really inputs since they are derived from
@@ -669,7 +671,7 @@ define_dep_nodes!( <'tcx>
669671
[input] Features,
670672

671673
[] ProgramClausesFor(DefId),
672-
[] ProgramClausesForEnv(ParamEnv<'tcx>),
674+
[] ProgramClausesForEnv(traits::Environment<'tcx>),
673675
[] WasmImportModuleMap(CrateNum),
674676
[] ForeignModules(CrateNum),
675677

src/librustc/ich/impls_ty.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1395,10 +1395,16 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for traits::Goal<'tcx> {
13951395

13961396
impl_stable_hash_for!(
13971397
impl<'tcx> for struct traits::ProgramClause<'tcx> {
1398-
goal, hypotheses
1398+
goal, hypotheses, category
13991399
}
14001400
);
14011401

1402+
impl_stable_hash_for!(enum traits::ProgramClauseCategory {
1403+
ImpliedBound,
1404+
WellFormed,
1405+
Other,
1406+
});
1407+
14021408
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for traits::Clause<'tcx> {
14031409
fn hash_stable<W: StableHasherResult>(&self,
14041410
hcx: &mut StableHashingContext<'a>,
@@ -1422,3 +1428,8 @@ impl_stable_hash_for!(struct ty::subst::UserSubsts<'tcx> { substs, user_self_ty
14221428

14231429
impl_stable_hash_for!(struct ty::subst::UserSelfTy<'tcx> { impl_def_id, self_ty });
14241430

1431+
impl_stable_hash_for!(
1432+
impl<'tcx> for struct traits::Environment<'tcx> {
1433+
clauses,
1434+
}
1435+
);

src/librustc/traits/mod.rs

+51
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ pub type TraitObligations<'tcx> = Vec<TraitObligation<'tcx>>;
278278
/// * `DomainGoal`
279279
/// * `Goal`
280280
/// * `Clause`
281+
/// * `Environment`
282+
/// * `InEnvironment`
281283
/// are used for representing the trait system in the form of
282284
/// logic programming clauses. They are part of the interface
283285
/// for the chalk SLG solver.
@@ -335,6 +337,14 @@ impl<'tcx> DomainGoal<'tcx> {
335337
pub fn into_goal(self) -> GoalKind<'tcx> {
336338
GoalKind::DomainGoal(self)
337339
}
340+
341+
pub fn into_program_clause(self) -> ProgramClause<'tcx> {
342+
ProgramClause {
343+
goal: self,
344+
hypotheses: ty::List::empty(),
345+
category: ProgramClauseCategory::Other,
346+
}
347+
}
338348
}
339349

340350
impl<'tcx> GoalKind<'tcx> {
@@ -360,6 +370,15 @@ pub enum Clause<'tcx> {
360370
ForAll(ty::Binder<ProgramClause<'tcx>>),
361371
}
362372

373+
impl Clause<'tcx> {
374+
pub fn category(self) -> ProgramClauseCategory {
375+
match self {
376+
Clause::Implies(clause) => clause.category,
377+
Clause::ForAll(clause) => clause.skip_binder().category,
378+
}
379+
}
380+
}
381+
363382
/// Multiple clauses.
364383
pub type Clauses<'tcx> = &'tcx List<Clause<'tcx>>;
365384

@@ -376,6 +395,38 @@ pub struct ProgramClause<'tcx> {
376395

377396
/// ...if we can prove these hypotheses (there may be no hypotheses at all):
378397
pub hypotheses: Goals<'tcx>,
398+
399+
/// Useful for filtering clauses.
400+
pub category: ProgramClauseCategory,
401+
}
402+
403+
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
404+
pub enum ProgramClauseCategory {
405+
ImpliedBound,
406+
WellFormed,
407+
Other,
408+
}
409+
410+
/// A set of clauses that we assume to be true.
411+
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
412+
pub struct Environment<'tcx> {
413+
pub clauses: Clauses<'tcx>,
414+
}
415+
416+
impl Environment<'tcx> {
417+
pub fn with<G>(self, goal: G) -> InEnvironment<'tcx, G> {
418+
InEnvironment {
419+
environment: self,
420+
goal,
421+
}
422+
}
423+
}
424+
425+
/// Something (usually a goal), along with an environment.
426+
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
427+
pub struct InEnvironment<'tcx, G> {
428+
pub environment: Environment<'tcx>,
429+
pub goal: G,
379430
}
380431

381432
pub type Selection<'tcx> = Vtable<'tcx, PredicateObligation<'tcx>>;

src/librustc/traits/structural_impls.rs

+44-3
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ impl<'tcx> fmt::Display for traits::Goal<'tcx> {
496496

497497
impl<'tcx> fmt::Display for traits::ProgramClause<'tcx> {
498498
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
499-
let traits::ProgramClause { goal, hypotheses } = self;
499+
let traits::ProgramClause { goal, hypotheses, .. } = self;
500500
write!(fmt, "{}", goal)?;
501501
if !hypotheses.is_empty() {
502502
write!(fmt, " :- ")?;
@@ -647,18 +647,59 @@ impl<'tcx> TypeFoldable<'tcx> for traits::Goal<'tcx> {
647647
BraceStructTypeFoldableImpl! {
648648
impl<'tcx> TypeFoldable<'tcx> for traits::ProgramClause<'tcx> {
649649
goal,
650-
hypotheses
650+
hypotheses,
651+
category,
651652
}
652653
}
653654

655+
CloneTypeFoldableAndLiftImpls! {
656+
traits::ProgramClauseCategory,
657+
}
658+
654659
EnumTypeFoldableImpl! {
655660
impl<'tcx> TypeFoldable<'tcx> for traits::Clause<'tcx> {
656661
(traits::Clause::Implies)(clause),
657662
(traits::Clause::ForAll)(clause),
658663
}
659664
}
660665

661-
impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<traits::Clause<'tcx>> {
666+
BraceStructTypeFoldableImpl! {
667+
impl<'tcx> TypeFoldable<'tcx> for traits::Environment<'tcx> { clauses }
668+
}
669+
670+
BraceStructTypeFoldableImpl! {
671+
impl<'tcx, G> TypeFoldable<'tcx> for traits::InEnvironment<'tcx, G> {
672+
environment,
673+
goal
674+
} where G: TypeFoldable<'tcx>
675+
}
676+
677+
impl<'a, 'tcx> Lift<'tcx> for traits::Environment<'a> {
678+
type Lifted = traits::Environment<'tcx>;
679+
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
680+
tcx.lift(&self.clauses).map(|clauses| {
681+
traits::Environment {
682+
clauses,
683+
}
684+
})
685+
}
686+
}
687+
688+
impl<'a, 'tcx, G: Lift<'tcx>> Lift<'tcx> for traits::InEnvironment<'a, G> {
689+
type Lifted = traits::InEnvironment<'tcx, G::Lifted>;
690+
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
691+
tcx.lift(&self.environment).and_then(|environment| {
692+
tcx.lift(&self.goal).map(|goal| {
693+
traits::InEnvironment {
694+
environment,
695+
goal,
696+
}
697+
})
698+
})
699+
}
700+
}
701+
702+
impl<'tcx> TypeFoldable<'tcx> for traits::Clauses<'tcx> {
662703
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
663704
let v = self.iter()
664705
.map(|t| t.fold_with(folder))

src/librustc/ty/query/config.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use dep_graph::SerializedDepNodeIndex;
1212
use dep_graph::DepNode;
1313
use hir::def_id::{CrateNum, DefId, DefIndex};
1414
use mir::interpret::GlobalId;
15+
use traits;
1516
use traits::query::{
1617
CanonicalPredicateGoal, CanonicalProjectionGoal, CanonicalTyGoal, CanonicalTypeOpEqGoal,
1718
CanonicalTypeOpNormalizeGoal, CanonicalTypeOpProvePredicateGoal, CanonicalTypeOpSubtypeGoal,
@@ -826,8 +827,14 @@ impl<'tcx> QueryDescription<'tcx> for queries::program_clauses_for<'tcx> {
826827
}
827828

828829
impl<'tcx> QueryDescription<'tcx> for queries::program_clauses_for_env<'tcx> {
829-
fn describe(_tcx: TyCtxt<'_, '_, '_>, _: ty::ParamEnv<'tcx>) -> Cow<'static, str> {
830-
"generating chalk-style clauses for param env".into()
830+
fn describe(_tcx: TyCtxt<'_, '_, '_>, _: traits::Environment<'tcx>) -> Cow<'static, str> {
831+
"generating chalk-style clauses for environment".into()
832+
}
833+
}
834+
835+
impl<'tcx> QueryDescription<'tcx> for queries::environment<'tcx> {
836+
fn describe(_tcx: TyCtxt<'_, '_, '_>, _: DefId) -> Cow<'static, str> {
837+
"return a chalk-style environment".into()
831838
}
832839
}
833840

src/librustc/ty/query/keys.rs

+10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
1313
use infer::canonical::Canonical;
1414
use hir::def_id::{CrateNum, DefId, LOCAL_CRATE, DefIndex};
15+
use traits;
1516
use ty::{self, Ty, TyCtxt};
1617
use ty::subst::Substs;
1718
use ty::fast_reject::SimplifiedType;
@@ -181,6 +182,15 @@ impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
181182
}
182183
}
183184

185+
impl<'tcx> Key for traits::Environment<'tcx> {
186+
fn query_crate(&self) -> CrateNum {
187+
LOCAL_CRATE
188+
}
189+
fn default_span(&self, _: TyCtxt<'_, '_, '_>) -> Span {
190+
DUMMY_SP
191+
}
192+
}
193+
184194
impl Key for InternedString {
185195
fn query_crate(&self) -> CrateNum {
186196
LOCAL_CRATE

src/librustc/ty/query/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -664,8 +664,11 @@ define_queries! { <'tcx>
664664
[] fn program_clauses_for: ProgramClausesFor(DefId) -> Clauses<'tcx>,
665665

666666
[] fn program_clauses_for_env: ProgramClausesForEnv(
667-
ty::ParamEnv<'tcx>
667+
traits::Environment<'tcx>
668668
) -> Clauses<'tcx>,
669+
670+
// Get the chalk-style environment of the given item.
671+
[] fn environment: Environment(DefId) -> traits::Environment<'tcx>,
669672
},
670673

671674
Linking {

src/librustc/ty/query/plumbing.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,7 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
11561156
DepKind::CheckMatch => { force!(check_match, def_id!()); }
11571157

11581158
DepKind::ParamEnv => { force!(param_env, def_id!()); }
1159+
DepKind::Environment => { force!(environment, def_id!()); }
11591160
DepKind::DescribeDef => { force!(describe_def, def_id!()); }
11601161
DepKind::DefSpan => { force!(def_span, def_id!()); }
11611162
DepKind::LookupStability => { force!(lookup_stability, def_id!()); }

0 commit comments

Comments
 (0)