]> git.lizzy.rs Git - rust.git/commitdiff
Implement the `environment` query
authorscalexm <alexandre@scalexm.fr>
Thu, 11 Oct 2018 14:26:43 +0000 (16:26 +0200)
committerscalexm <alexandre@scalexm.fr>
Wed, 17 Oct 2018 12:09:38 +0000 (14:09 +0200)
src/librustc/traits/mod.rs
src/librustc/ty/query/mod.rs
src/librustc_traits/lowering/environment.rs
src/librustc_traits/lowering/mod.rs

index d95b0844e87f588ef12eafb9e87d479f34de849e..7bfb6f060cd4a49bb05bd2481eb3160e6827c607 100644 (file)
@@ -337,6 +337,13 @@ impl<'tcx> DomainGoal<'tcx> {
     pub fn into_goal(self) -> GoalKind<'tcx> {
         GoalKind::DomainGoal(self)
     }
+
+    pub fn into_program_clause(self) -> ProgramClause<'tcx> {
+        ProgramClause {
+            goal: self,
+            hypotheses: &ty::List::empty(),
+        }
+    }
 }
 
 impl<'tcx> GoalKind<'tcx> {
@@ -402,11 +409,6 @@ pub struct InEnvironment<'tcx, G> {
     pub goal: G,
 }
 
-/// Compute the environment of the given item.
-fn environment<'a, 'tcx>(_tcx: TyCtxt<'a, 'tcx, 'tcx>, _def_id: DefId) -> Environment<'tcx> {
-    panic!()
-}
-
 pub type Selection<'tcx> = Vtable<'tcx, PredicateObligation<'tcx>>;
 
 #[derive(Clone,Debug)]
@@ -1109,7 +1111,6 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) {
         codegen_fulfill_obligation: codegen::codegen_fulfill_obligation,
         vtable_methods,
         substitute_normalize_and_test_predicates,
-        environment,
         ..*providers
     };
 }
index 83c8eab0f39ac25874a0cfd8f3b41163c80174c5..adb5883fd5e26eb13f602caf8a3331f94c22756b 100644 (file)
         // might want to use `reveal_all()` method to change modes.
         [] fn param_env: ParamEnv(DefId) -> ty::ParamEnv<'tcx>,
 
+        // Get the chalk-style environment of the given item.
         [] fn environment: Environment(DefId) -> traits::Environment<'tcx>,
 
         // Trait selection queries. These are best used by invoking `ty.moves_by_default()`,
index 338cad8154ff0f423ba63bcf629e9b60e6a9b5c3..1774db3d85a8722f8f397b90fa292aedceae1cc4 100644 (file)
@@ -17,6 +17,7 @@
     Environment,
 };
 use rustc::ty::{self, TyCtxt, Ty};
+use rustc::hir::def_id::DefId;
 use rustc_data_structures::fx::FxHashSet;
 
 struct ClauseVisitor<'set, 'a, 'tcx: 'a> {
@@ -162,3 +163,26 @@ fn visit_clause(&mut self, clause: Clause<'tcx>) {
         closure.into_iter()
     );
 }
+
+crate fn environment<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Environment<'tcx> {
+    use super::{Lower, IntoFromEnvGoal};
+
+    // The environment of an impl Trait type is its defining function's environment
+    if let Some(parent) = ty::is_impl_trait_defn(tcx, def_id) {
+        return environment(tcx, parent);
+    }
+
+    // Compute the bounds on `Self` and the type parameters.
+    let ty::InstantiatedPredicates { predicates } =
+        tcx.predicates_of(def_id).instantiate_identity(tcx);
+    
+    let clauses = predicates.into_iter()
+        .map(|predicate| predicate.lower())
+        .map(|domain_goal| domain_goal.map_bound(|dg| dg.into_from_env_goal()))
+        .map(|domain_goal| domain_goal.map_bound(|dg| dg.into_program_clause()))
+        .map(Clause::ForAll);
+    
+    Environment {
+        clauses: tcx.mk_clauses(clauses),
+    }
+}
index 07a5d6a31dd0a9349efa4eff68a25d95201c36f2..a6bf28b6ad8ac11232d16e0e5ca981affe6937e4 100644 (file)
@@ -35,6 +35,7 @@
     *p = Providers {
         program_clauses_for,
         program_clauses_for_env: environment::program_clauses_for_env,
+        environment: environment::environment,
         ..*p
     };
 }