]> git.lizzy.rs Git - rust.git/commitdiff
Negative impls are considered safe
authorFlavio Percoco <flaper87@gmail.com>
Sat, 10 Jan 2015 09:40:17 +0000 (10:40 +0100)
committerFlavio Percoco <flaper87@gmail.com>
Fri, 16 Jan 2015 07:18:55 +0000 (08:18 +0100)
src/librustc_typeck/coherence/unsafety.rs
src/test/compile-fail/coherence-negative-impls-safe.rs [new file with mode: 0644]
src/test/run-pass/coherence-negative-impls-safe.rs [new file with mode: 0644]

index 1acea6fd58179eac948cd3085b65f8247ffa2981..e30d0a29938aa91090365b87c7cc8fd1c5bbd2c1 100644 (file)
@@ -30,7 +30,7 @@ struct UnsafetyChecker<'cx, 'tcx:'cx> {
 impl<'cx, 'tcx,'v> visit::Visitor<'v> for UnsafetyChecker<'cx, 'tcx> {
     fn visit_item(&mut self, item: &'v ast::Item) {
         match item.node {
-            ast::ItemImpl(unsafety, _, _, _, _, _) => {
+            ast::ItemImpl(unsafety, polarity, _, _, _, _) => {
                 match ty::impl_trait_ref(self.tcx, ast_util::local_def(item.id)) {
                     None => {
                         // Inherent impl.
@@ -46,23 +46,34 @@ fn visit_item(&mut self, item: &'v ast::Item) {
 
                     Some(trait_ref) => {
                         let trait_def = ty::lookup_trait_def(self.tcx, trait_ref.def_id);
-                        match (trait_def.unsafety, unsafety) {
-                            (ast::Unsafety::Normal, ast::Unsafety::Unsafe) => {
+                        match (trait_def.unsafety, unsafety, polarity) {
+                            (ast::Unsafety::Unsafe,
+                             ast::Unsafety::Unsafe, ast::ImplPolarity::Negative) => {
+                                self.tcx.sess.span_err(
+                                    item.span,
+                                    format!("negative implementations are not unsafe").as_slice());
+                            }
+
+                            (ast::Unsafety::Normal, ast::Unsafety::Unsafe, _) => {
                                 self.tcx.sess.span_err(
                                     item.span,
                                     format!("implementing the trait `{}` is not unsafe",
                                             trait_ref.user_string(self.tcx)).as_slice());
                             }
 
-                            (ast::Unsafety::Unsafe, ast::Unsafety::Normal) => {
+                            (ast::Unsafety::Unsafe,
+                             ast::Unsafety::Normal, ast::ImplPolarity::Positive) => {
                                 self.tcx.sess.span_err(
                                     item.span,
                                     format!("the trait `{}` requires an `unsafe impl` declaration",
                                             trait_ref.user_string(self.tcx)).as_slice());
                             }
 
-                            (ast::Unsafety::Unsafe, ast::Unsafety::Unsafe) |
-                            (ast::Unsafety::Normal, ast::Unsafety::Normal) => {
+                            (ast::Unsafety::Unsafe,
+                             ast::Unsafety::Normal, ast::ImplPolarity::Negative) |
+                            (ast::Unsafety::Unsafe,
+                             ast::Unsafety::Unsafe, ast::ImplPolarity::Positive) |
+                            (ast::Unsafety::Normal, ast::Unsafety::Normal, _) => {
                                 /* OK */
                             }
                         }
diff --git a/src/test/compile-fail/coherence-negative-impls-safe.rs b/src/test/compile-fail/coherence-negative-impls-safe.rs
new file mode 100644 (file)
index 0000000..3b335d5
--- /dev/null
@@ -0,0 +1,20 @@
+// 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(optin_builtin_traits)]
+
+use std::marker::Send;
+
+struct TestType;
+
+unsafe impl !Send for TestType {}
+//~^ ERROR negative implementations are not unsafe
+
+fn main() {}
diff --git a/src/test/run-pass/coherence-negative-impls-safe.rs b/src/test/run-pass/coherence-negative-impls-safe.rs
new file mode 100644 (file)
index 0000000..646da74
--- /dev/null
@@ -0,0 +1,21 @@
+// 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(optin_builtin_traits)]
+
+use std::marker::Send;
+
+struct TestType;
+
+unsafe impl Send for TestType {}
+
+impl !Send for TestType {}
+
+fn main() {}