Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce a magical KindMimic type #10144

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/librustc/middle/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,18 @@ pub enum LangItem {
TyDescStructLangItem, // 36
TyVisitorTraitLangItem, // 37
OpaqueStructLangItem, // 38

KindMimicLangItem, // 39
}

pub struct LanguageItems {
items: [Option<DefId>, ..39]
items: [Option<DefId>, ..40]
}

impl LanguageItems {
pub fn new() -> LanguageItems {
LanguageItems {
items: [ None, ..39 ]
items: [ None, ..40 ]
}
}

Expand Down Expand Up @@ -145,6 +147,8 @@ impl LanguageItems {
37 => "ty_visitor",
38 => "opaque",

39 => "kind_mimic",

_ => "???"
}
}
Expand Down Expand Up @@ -292,6 +296,9 @@ impl LanguageItems {
pub fn opaque(&self) -> Option<DefId> {
self.items[OpaqueStructLangItem as uint]
}
pub fn kind_mimic(&self) -> Option<DefId> {
self.items[KindMimicLangItem as uint]
}
}

struct LanguageItemCollector<'self> {
Expand Down Expand Up @@ -374,6 +381,7 @@ impl<'self> LanguageItemCollector<'self> {
item_refs.insert("ty_desc", TyDescStructLangItem as uint);
item_refs.insert("ty_visitor", TyVisitorTraitLangItem as uint);
item_refs.insert("opaque", OpaqueStructLangItem as uint);
item_refs.insert("kind_mimic", KindMimicLangItem as uint);

LanguageItemCollector {
crate: crate,
Expand Down
17 changes: 11 additions & 6 deletions src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2129,13 +2129,18 @@ pub fn type_contents(cx: ctxt, ty: t) -> TypeContents {

ty_struct(did, ref substs) => {
let flds = struct_fields(cx, did, substs);
let mut res = flds.iter().fold(
TC_NONE,
|tc, f| tc + tc_mt(cx, f.mt, cache));
if ty::has_dtor(cx, did) {
res = res + TC_DTOR;
let mimic_item = cx.lang_items.kind_mimic();
if mimic_item.map_default(false, |x| did == x) {
tc_ty(cx, substs.tps[0], cache)
} else {
let mut res = flds.iter().fold(
TC_NONE,
|tc, f| tc + tc_mt(cx, f.mt, cache));
if ty::has_dtor(cx, did) {
res = res + TC_DTOR;
}
apply_tc_attr(cx, did, res)
}
apply_tc_attr(cx, did, res)
}

ty_tup(ref tys) => {
Expand Down
3 changes: 3 additions & 0 deletions src/librustc/middle/typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ pub fn collect_item_types(ccx: @mut CrateCtxt, crate: &ast::Crate) {
match ccx.tcx.lang_items.opaque() {
Some(id) => { collect_intrinsic_type(ccx, id); } None => {}
}
match ccx.tcx.lang_items.kind_mimic() {
Some(id) => { collect_intrinsic_type(ccx, id); } None => {}
}

let mut visitor = CollectItemTypesVisitor{ ccx: ccx };
visit::walk_crate(&mut visitor, crate, ());
Expand Down
9 changes: 9 additions & 0 deletions src/libstd/kinds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,12 @@ pub trait Freeze {
pub trait Sized {
// Empty.
}

/// A special zero-size type with the kinds of the type parameter.
///
/// It can be used to define new low-level types like smart pointers or dynamic
/// arrays with the correct kinds, since raw pointers are *always* `Send` and
/// `Freeze`.
#[lang="kind_mimic"]
#[unstable]
pub struct KindMimic<T>;
19 changes: 15 additions & 4 deletions src/libstd/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ reference is a unique handle and the type is marked as non-`Freeze`.
use ptr::RawPtr;
use unstable::intrinsics::transmute;
use ops::Drop;
use kinds::{Freeze, Send};
use kinds::{Freeze, Send, KindMimic};
use clone::{Clone, DeepClone};

struct RcBox<T> {
Expand All @@ -35,7 +35,8 @@ struct RcBox<T> {
#[unsafe_no_drop_flag]
#[no_send]
pub struct Rc<T> {
priv ptr: *mut RcBox<T>
priv ptr: *mut RcBox<T>,
priv mimic: KindMimic<T> // bubble up lack of a `Freeze` kind
}

impl<T: Freeze> Rc<T> {
Expand All @@ -48,6 +49,16 @@ impl<T: Freeze> Rc<T> {
}
}

impl<T: Send> Rc<T> {
/// Construct a new reference-counted box from a `Send` value
#[inline]
pub fn from_send(value: T) -> Rc<T> {
unsafe {
Rc::new_unchecked(value)
}
}
}

impl<T> Rc<T> {
/// Unsafety construct a new reference-counted box from any value.
///
Expand All @@ -56,7 +67,7 @@ impl<T> Rc<T> {
/// managed pointers.
#[inline]
pub unsafe fn new_unchecked(value: T) -> Rc<T> {
Rc{ptr: transmute(~RcBox{value: value, count: 1})}
Rc{ptr: transmute(~RcBox{value: value, count: 1}), mimic: KindMimic}
}

/// Borrow the value contained in the reference-counted box
Expand All @@ -71,7 +82,7 @@ impl<T> Clone for Rc<T> {
fn clone(&self) -> Rc<T> {
unsafe {
(*self.ptr).count += 1;
Rc{ptr: self.ptr}
Rc{ptr: self.ptr, mimic: KindMimic}
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions src/test/compile-fail/kind_mimic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2013 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.

use std::kinds::KindMimic;

#[no_send]
struct Bar;

#[no_freeze]
struct Baz;

fn bar<T: Send>(_: T) {}
fn baz<T: Freeze>(_: T) {}

fn main() {
bar(KindMimic::<Bar>); //~ ERROR instantiating a type parameter with an incompatible type `std::kinds::KindMimic<Bar>`, which does not fulfill `Send`
baz(KindMimic::<Baz>); //~ ERROR instantiating a type parameter with an incompatible type `std::kinds::KindMimic<Baz>`, which does not fulfill `Freeze`
}