Negative impls are considered safe

This commit is contained in:
Flavio Percoco 2015-01-10 10:40:17 +01:00
parent e644ca0d6a
commit 39fe05f58c
3 changed files with 58 additions and 6 deletions

View 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 */
}
}

View File

@ -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() {}

View File

@ -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() {}