Skip to content

Commit 64f130b

Browse files
committed
Negative impls are considered safe
1 parent 667e058 commit 64f130b

File tree

3 files changed

+58
-6
lines changed

3 files changed

+58
-6
lines changed

src/librustc_typeck/coherence/unsafety.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct UnsafetyChecker<'cx, 'tcx:'cx> {
3030
impl<'cx, 'tcx,'v> visit::Visitor<'v> for UnsafetyChecker<'cx, 'tcx> {
3131
fn visit_item(&mut self, item: &'v ast::Item) {
3232
match item.node {
33-
ast::ItemImpl(unsafety, _, _, _, _, _) => {
33+
ast::ItemImpl(unsafety, polarity, _, _, _, _) => {
3434
match ty::impl_trait_ref(self.tcx, ast_util::local_def(item.id)) {
3535
None => {
3636
// Inherent impl.
@@ -46,23 +46,34 @@ impl<'cx, 'tcx,'v> visit::Visitor<'v> for UnsafetyChecker<'cx, 'tcx> {
4646

4747
Some(trait_ref) => {
4848
let trait_def = ty::lookup_trait_def(self.tcx, trait_ref.def_id);
49-
match (trait_def.unsafety, unsafety) {
50-
(ast::Unsafety::Normal, ast::Unsafety::Unsafe) => {
49+
match (trait_def.unsafety, unsafety, polarity) {
50+
(ast::Unsafety::Unsafe,
51+
ast::Unsafety::Unsafe, ast::ImplPolarity::Negative) => {
52+
self.tcx.sess.span_err(
53+
item.span,
54+
format!("negative implementations are not unsafe").as_slice());
55+
}
56+
57+
(ast::Unsafety::Normal, ast::Unsafety::Unsafe, _) => {
5158
self.tcx.sess.span_err(
5259
item.span,
5360
format!("implementing the trait `{}` is not unsafe",
5461
trait_ref.user_string(self.tcx)).as_slice());
5562
}
5663

57-
(ast::Unsafety::Unsafe, ast::Unsafety::Normal) => {
64+
(ast::Unsafety::Unsafe,
65+
ast::Unsafety::Normal, ast::ImplPolarity::Positive) => {
5866
self.tcx.sess.span_err(
5967
item.span,
6068
format!("the trait `{}` requires an `unsafe impl` declaration",
6169
trait_ref.user_string(self.tcx)).as_slice());
6270
}
6371

64-
(ast::Unsafety::Unsafe, ast::Unsafety::Unsafe) |
65-
(ast::Unsafety::Normal, ast::Unsafety::Normal) => {
72+
(ast::Unsafety::Unsafe,
73+
ast::Unsafety::Normal, ast::ImplPolarity::Negative) |
74+
(ast::Unsafety::Unsafe,
75+
ast::Unsafety::Unsafe, ast::ImplPolarity::Positive) |
76+
(ast::Unsafety::Normal, ast::Unsafety::Normal, _) => {
6677
/* OK */
6778
}
6879
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(optin_builtin_traits)]
12+
13+
use std::marker::Send;
14+
15+
struct TestType;
16+
17+
unsafe impl !Send for TestType {}
18+
//~^ ERROR negative implementations are not unsafe
19+
20+
fn main() {}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(optin_builtin_traits)]
12+
13+
use std::marker::Send;
14+
15+
struct TestType;
16+
17+
unsafe impl Send for TestType {}
18+
19+
impl !Send for TestType {}
20+
21+
fn main() {}

0 commit comments

Comments
 (0)