Skip to content

Commit

Permalink
Specialize kinds inference for Unsafe<T>
Browse files Browse the repository at this point in the history
This patch adds a special rule for `Unsafe<T>` and makes it `Share`
regardless of whether T is `Share`.

[breaking-change]

Closes #13125
  • Loading branch information
flaper87 committed Apr 22, 2014
1 parent ef1b929 commit 5b4d54e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/librustc/middle/ty.rs
Expand Up @@ -2214,7 +2214,9 @@ pub fn type_contents(cx: &ctxt, ty: t) -> TypeContents {
} else if Some(did) == cx.lang_items.no_share_bound() {
tc | TC::ReachesNoShare
} else if Some(did) == cx.lang_items.unsafe_type() {
tc | TC::InteriorUnsafe
// FIXME(#13231): This shouldn't be needed after
// opt-in built-in bounds are implemented.
(tc | TC::InteriorUnsafe) - TC::Nonsharable
} else {
tc
}
Expand Down
43 changes: 43 additions & 0 deletions src/test/compile-fail/typeck-unsafe-always-share.rs
@@ -0,0 +1,43 @@
// Copyright 2014 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.

// Verify that Unsafe is *always* share regardles `T` is share.

// ignore-tidy-linelength

use std::ty::Unsafe;
use std::kinds::marker;

struct MyShare<T> {
u: Unsafe<T>
}

struct NoShare {
m: marker::NoShare
}

fn test<T: Share>(s: T){

}

fn main() {
let us = Unsafe::new(MyShare{u: Unsafe::new(0)});
test(us);

let uns = Unsafe::new(NoShare{m: marker::NoShare});
test(uns);

let ms = MyShare{u: uns};
test(ms);

let ns = NoShare{m: marker::NoShare};
test(ns);
//~^ ERROR instantiating a type parameter with an incompatible type `NoShare`, which does not fulfill `Share`
}

5 comments on commit 5b4d54e

@bors
Copy link
Contributor

@bors bors commented on 5b4d54e Apr 22, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from nikomatsakis
at flaper87@5b4d54e

@bors
Copy link
Contributor

@bors bors commented on 5b4d54e Apr 22, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging FlaPer87/rust/special-unsafe = 5b4d54e into auto

@bors
Copy link
Contributor

@bors bors commented on 5b4d54e Apr 22, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FlaPer87/rust/special-unsafe = 5b4d54e merged ok, testing candidate = 7730310

@bors
Copy link
Contributor

@bors bors commented on 5b4d54e Apr 22, 2014

@bors
Copy link
Contributor

@bors bors commented on 5b4d54e Apr 22, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 7730310

Please sign in to comment.