Skip to content

Commit 079b97c

Browse files
committed
Implement the environment query
1 parent 8113180 commit 079b97c

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

src/librustc/traits/mod.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,13 @@ impl<'tcx> DomainGoal<'tcx> {
337337
pub fn into_goal(self) -> GoalKind<'tcx> {
338338
GoalKind::DomainGoal(self)
339339
}
340+
341+
pub fn into_program_clause(self) -> ProgramClause<'tcx> {
342+
ProgramClause {
343+
goal: self,
344+
hypotheses: &ty::List::empty(),
345+
}
346+
}
340347
}
341348

342349
impl<'tcx> GoalKind<'tcx> {
@@ -402,11 +409,6 @@ pub struct InEnvironment<'tcx, G> {
402409
pub goal: G,
403410
}
404411

405-
/// Compute the environment of the given item.
406-
fn environment<'a, 'tcx>(_tcx: TyCtxt<'a, 'tcx, 'tcx>, _def_id: DefId) -> Environment<'tcx> {
407-
panic!()
408-
}
409-
410412
pub type Selection<'tcx> = Vtable<'tcx, PredicateObligation<'tcx>>;
411413

412414
#[derive(Clone,Debug)]
@@ -1109,7 +1111,6 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) {
11091111
codegen_fulfill_obligation: codegen::codegen_fulfill_obligation,
11101112
vtable_methods,
11111113
substitute_normalize_and_test_predicates,
1112-
environment,
11131114
..*providers
11141115
};
11151116
}

src/librustc/ty/query/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ define_queries! { <'tcx>
377377
// might want to use `reveal_all()` method to change modes.
378378
[] fn param_env: ParamEnv(DefId) -> ty::ParamEnv<'tcx>,
379379

380+
// Get the chalk-style environment of the given item.
380381
[] fn environment: Environment(DefId) -> traits::Environment<'tcx>,
381382

382383
// Trait selection queries. These are best used by invoking `ty.moves_by_default()`,

src/librustc_traits/lowering/environment.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc::traits::{
1717
Environment,
1818
};
1919
use rustc::ty::{self, TyCtxt, Ty};
20+
use rustc::hir::def_id::DefId;
2021
use rustc_data_structures::fx::FxHashSet;
2122

2223
struct ClauseVisitor<'set, 'a, 'tcx: 'a> {
@@ -162,3 +163,26 @@ crate fn program_clauses_for_env<'a, 'tcx>(
162163
closure.into_iter()
163164
);
164165
}
166+
167+
crate fn environment<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Environment<'tcx> {
168+
use super::{Lower, IntoFromEnvGoal};
169+
170+
// The environment of an impl Trait type is its defining function's environment
171+
if let Some(parent) = ty::is_impl_trait_defn(tcx, def_id) {
172+
return environment(tcx, parent);
173+
}
174+
175+
// Compute the bounds on `Self` and the type parameters.
176+
let ty::InstantiatedPredicates { predicates } =
177+
tcx.predicates_of(def_id).instantiate_identity(tcx);
178+
179+
let clauses = predicates.into_iter()
180+
.map(|predicate| predicate.lower())
181+
.map(|domain_goal| domain_goal.map_bound(|dg| dg.into_from_env_goal()))
182+
.map(|domain_goal| domain_goal.map_bound(|dg| dg.into_program_clause()))
183+
.map(Clause::ForAll);
184+
185+
Environment {
186+
clauses: tcx.mk_clauses(clauses),
187+
}
188+
}

src/librustc_traits/lowering/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ crate fn provide(p: &mut Providers) {
3535
*p = Providers {
3636
program_clauses_for,
3737
program_clauses_for_env: environment::program_clauses_for_env,
38+
environment: environment::environment,
3839
..*p
3940
};
4041
}

0 commit comments

Comments
 (0)