]> git.lizzy.rs Git - rust.git/commitdiff
support `default impl` for specialization
authorGianni Ciccarelli <gianni.ciccarelli@gmail.com>
Tue, 21 Nov 2017 07:27:20 +0000 (07:27 +0000)
committerGianni Ciccarelli <gianni.ciccarelli@gmail.com>
Wed, 7 Feb 2018 18:31:39 +0000 (18:31 +0000)
not skipping any wfchecks on default impls

src/librustc/infer/mod.rs
src/librustc/traits/select.rs
src/librustc/ty/trait_def.rs
src/librustc_typeck/check/mod.rs
src/librustc_typeck/check/wfcheck.rs
src/test/compile-fail/specialization/defaultimpl/specialization-wfcheck.rs [new file with mode: 0644]

index 07c5b319970f8e673500b0bb4ad01f1982aa7b93..a2d5af67516028a7c9e3fbd19b372d9eb609b916 100644 (file)
@@ -181,6 +181,9 @@ pub struct InferCtxt<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
     // obligations within. This is expected to be done 'late enough'
     // that all type inference variables have been bound and so forth.
     region_obligations: RefCell<Vec<(ast::NodeId, RegionObligation<'tcx>)>>,
+
+    // true if trait selection in this context should emit `default impl` candiates
+    pub emit_defaul_impl_candidates: Cell<bool>,
 }
 
 /// A map returned by `skolemize_late_bound_regions()` indicating the skolemized
@@ -452,6 +455,7 @@ pub fn enter<F, R>(&'tcx mut self, f: F) -> R
             err_count_on_creation: tcx.sess.err_count(),
             in_snapshot: Cell::new(false),
             region_obligations: RefCell::new(vec![]),
+            emit_defaul_impl_candidates: Cell::new(false)
         }))
     }
 }
index 4ed25646d436d03671852d1d3a0fcd0f625e9c85..b58a154275ca9eeac4838d2a536bb49eb30a593b 100644 (file)
@@ -1296,6 +1296,12 @@ fn can_use_global_caches(&self, param_env: ty::ParamEnv<'tcx>) -> bool {
             return false;
         }
 
+        // Using local cache if the infcx can emit `default impls`
+        if self.infcx.emit_defaul_impl_candidates.get() {
+            return false;
+        }
+
+
         // Otherwise, we can use the global cache.
         true
     }
@@ -1714,18 +1720,21 @@ fn assemble_candidates_from_impls(&mut self,
             obligation.predicate.def_id(),
             obligation.predicate.0.trait_ref.self_ty(),
             |impl_def_id| {
-                self.probe(|this, snapshot| { /* [1] */
-                    match this.match_impl(impl_def_id, obligation, snapshot) {
-                        Ok(skol_map) => {
-                            candidates.vec.push(ImplCandidate(impl_def_id));
-
-                            // NB: we can safely drop the skol map
-                            // since we are in a probe [1]
-                            mem::drop(skol_map);
+                if self.infcx().emit_defaul_impl_candidates.get() ||
+                   !self.tcx().impl_is_default(impl_def_id) {
+                    self.probe(|this, snapshot| { /* [1] */
+                        match this.match_impl(impl_def_id, obligation, snapshot) {
+                            Ok(skol_map) => {
+                                candidates.vec.push(ImplCandidate(impl_def_id));
+
+                                // NB: we can safely drop the skol map
+                                // since we are in a probe [1]
+                                mem::drop(skol_map);
+                            }
+                            Err(_) => { }
                         }
-                        Err(_) => { }
-                    }
-                });
+                    });
+                }
             }
         );
 
index 33972df97fb68771c5ee3b271408e87682ec5a6d..0fbf9f1bd587bffc070f41544d43532dc455089c 100644 (file)
@@ -92,16 +92,10 @@ pub fn for_each_relevant_impl<F: FnMut(DefId)>(self,
                                                    self_ty: Ty<'tcx>,
                                                    mut f: F)
     {
-        let mut emit_impl = |impl_def_id: DefId| {
-            if !self.impl_is_default(impl_def_id) {
-                f(impl_def_id);
-            }
-        };
-
         let impls = self.trait_impls_of(def_id);
 
         for &impl_def_id in impls.blanket_impls.iter() {
-            emit_impl(impl_def_id);
+            f(impl_def_id);
         }
 
         // simplify_type(.., false) basically replaces type parameters and
@@ -132,13 +126,13 @@ pub fn for_each_relevant_impl<F: FnMut(DefId)>(self,
         if let Some(simp) = fast_reject::simplify_type(self, self_ty, true) {
             if let Some(impls) = impls.non_blanket_impls.get(&simp) {
                 for &impl_def_id in impls {
-                    emit_impl(impl_def_id);
+                    f(impl_def_id);
                 }
             }
         } else {
             for v in impls.non_blanket_impls.values() {
                 for &impl_def_id in v {
-                    emit_impl(impl_def_id);
+                    f(impl_def_id);
                 }
             }
         }
index 4fe2f5b574e67d6c040b183c93cc0b9799754260..a622f0b67322007cda0e7723e1f9e564d97647c3 100644 (file)
@@ -1745,6 +1745,8 @@ pub fn new(inh: &'a Inherited<'a, 'gcx, 'tcx>,
                param_env: ty::ParamEnv<'tcx>,
                body_id: ast::NodeId)
                -> FnCtxt<'a, 'gcx, 'tcx> {
+        FnCtxt::set_emit_default_impl_candidates(inh, body_id);
+
         FnCtxt {
             body_id,
             param_env,
@@ -1763,6 +1765,22 @@ pub fn new(inh: &'a Inherited<'a, 'gcx, 'tcx>,
         }
     }
 
+    fn set_emit_default_impl_candidates(inh: &'a Inherited<'a, 'gcx, 'tcx>,
+                                        body_id: ast::NodeId) {
+        inh.infcx.emit_defaul_impl_candidates.set(
+            match inh.tcx.hir.find(body_id) {
+                Some(Node::NodeItem(..)) => {
+                    if inh.tcx.impl_is_default(inh.tcx.hir.local_def_id(body_id)) {
+                        true
+                    } else {
+                        false
+                    }
+                },
+                _ => false
+            }
+        );
+    }
+
     pub fn sess(&self) -> &Session {
         &self.tcx.sess
     }
index 78113bdcc0e4b48a9e2138e5d86734f2fa741d94..3668fc46ddc27b0e0fbba290fa762cc4f809f757 100644 (file)
@@ -343,18 +343,8 @@ fn check_impl(&mut self,
                                                   fcx.body_id,
                                                   &trait_ref,
                                                   ast_trait_ref.path.span);
-
-                    // not registering predicates associcated with a `default impl`
-                    let impl_is_default = fcx.tcx.impl_is_default(item_def_id);
                     for obligation in obligations {
-                        let register = match obligation.predicate {
-                            ty::Predicate::Trait(..)  => !impl_is_default,
-                            _ => true
-                        };
-
-                        if register {
-                            fcx.register_predicate(obligation);
-                        }
+                        fcx.register_predicate(obligation);
                     }
                 }
                 None => {
diff --git a/src/test/compile-fail/specialization/defaultimpl/specialization-wfcheck.rs b/src/test/compile-fail/specialization/defaultimpl/specialization-wfcheck.rs
new file mode 100644 (file)
index 0000000..5f6844d
--- /dev/null
@@ -0,0 +1,18 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![feature(specialization)]
+
+trait Foo<'a, T: Eq + 'a> { }
+
+default impl<U> Foo<'static, U> for () {}
+//~^ ERROR the trait bound `U: std::cmp::Eq` is not satisfied
+
+fn main(){}
\ No newline at end of file