Skip to content

Commit 58c0df2

Browse files
committed
add #[non_owned] and #[mutable] attributes
1 parent 063851f commit 58c0df2

File tree

1 file changed

+44
-25
lines changed

1 file changed

+44
-25
lines changed

src/librustc/middle/ty.rs

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,7 +1784,7 @@ pub impl TypeContents {
17841784
}
17851785
17861786
fn nonowned(_cx: ctxt) -> TypeContents {
1787-
TC_MANAGED + TC_BORROWED_POINTER
1787+
TC_MANAGED + TC_BORROWED_POINTER + TC_NON_OWNED
17881788
}
17891789
17901790
fn contains_managed(&self) -> bool {
@@ -1838,40 +1838,43 @@ impl ToStr for TypeContents {
18381838
}
18391839
18401840
/// Constant for a type containing nothing of interest.
1841-
static TC_NONE: TypeContents = TypeContents{bits:0b0000_00000000};
1841+
static TC_NONE: TypeContents = TypeContents{bits: 0b0000_0000_0000};
18421842
18431843
/// Contains a borrowed value with a lifetime other than static
1844-
static TC_BORROWED_POINTER: TypeContents = TypeContents{bits:0b0000_00000001};
1844+
static TC_BORROWED_POINTER: TypeContents = TypeContents{bits: 0b0000_0000_0001};
18451845
18461846
/// Contains an owned pointer (~T) but not slice of some kind
1847-
static TC_OWNED_POINTER: TypeContents = TypeContents{bits:0b000000000010};
1847+
static TC_OWNED_POINTER: TypeContents = TypeContents{bits: 0b0000_0000_0010};
18481848
18491849
/// Contains an owned vector ~[] or owned string ~str
1850-
static TC_OWNED_VEC: TypeContents = TypeContents{bits:0b000000000100};
1850+
static TC_OWNED_VEC: TypeContents = TypeContents{bits: 0b0000_0000_0100};
18511851
18521852
/// Contains a ~fn() or a ~Trait, which is non-copyable.
1853-
static TC_OWNED_CLOSURE: TypeContents = TypeContents{bits:0b000000001000};
1853+
static TC_OWNED_CLOSURE: TypeContents = TypeContents{bits: 0b0000_0000_1000};
18541854
18551855
/// Type with a destructor
1856-
static TC_DTOR: TypeContents = TypeContents{bits:0b000000010000};
1856+
static TC_DTOR: TypeContents = TypeContents{bits: 0b0000_0001_0000};
18571857
18581858
/// Contains a managed value
1859-
static TC_MANAGED: TypeContents = TypeContents{bits:0b000000100000};
1859+
static TC_MANAGED: TypeContents = TypeContents{bits: 0b0000_0010_0000};
18601860
18611861
/// &mut with any region
1862-
static TC_BORROWED_MUT: TypeContents = TypeContents{bits:0b000001000000};
1862+
static TC_BORROWED_MUT: TypeContents = TypeContents{bits: 0b0000_0100_0000};
18631863
18641864
/// Mutable content, whether owned or by ref
1865-
static TC_MUTABLE: TypeContents = TypeContents{bits:0b000010000000};
1865+
static TC_MUTABLE: TypeContents = TypeContents{bits: 0b0000_1000_0000};
18661866
1867-
/// Mutable content, whether owned or by ref
1868-
static TC_ONCE_CLOSURE: TypeContents = TypeContents{bits:0b000100000000};
1867+
/// One-shot closure
1868+
static TC_ONCE_CLOSURE: TypeContents = TypeContents{bits: 0b0001_0000_0000};
18691869
18701870
/// An enum with no variants.
1871-
static TC_EMPTY_ENUM: TypeContents = TypeContents{bits:0b010000000000};
1871+
static TC_EMPTY_ENUM: TypeContents = TypeContents{bits: 0b0010_0000_0000};
1872+
1873+
/// Contains a type marked with `#[non_owned]`
1874+
static TC_NON_OWNED: TypeContents = TypeContents{bits: 0b0100_0000_0000};
18721875
18731876
/// All possible contents.
1874-
static TC_ALL: TypeContents = TypeContents{bits:0b011111111111};
1877+
static TC_ALL: TypeContents = TypeContents{bits: 0b0111_1111_1111};
18751878
18761879
pub fn type_is_copyable(cx: ctxt, t: ty::t) -> bool {
18771880
type_contents(cx, t).is_copy(cx)
@@ -1939,7 +1942,7 @@ pub fn type_contents(cx: ctxt, ty: t) -> TypeContents {
19391942
19401943
let _i = indenter();
19411944
1942-
let mut result = match get(ty).sty {
1945+
let result = match get(ty).sty {
19431946
// Scalar and unique types are sendable, constant, and owned
19441947
ty_nil | ty_bot | ty_bool | ty_int(_) | ty_uint(_) | ty_float(_) |
19451948
ty_bare_fn(_) | ty_ptr(_) => {
@@ -2013,14 +2016,19 @@ pub fn type_contents(cx: ctxt, ty: t) -> TypeContents {
20132016
20142017
ty_struct(did, ref substs) => {
20152018
let flds = struct_fields(cx, did, substs);
2016-
let flds_tc = flds.foldl(
2019+
let mut res = flds.foldl(
20172020
TC_NONE,
20182021
|tc, f| tc + tc_mt(cx, f.mt, cache));
20192022
if ty::has_dtor(cx, did) {
2020-
flds_tc + TC_DTOR
2021-
} else {
2022-
flds_tc
2023+
res += TC_DTOR;
2024+
}
2025+
if has_attr(cx, did, "mutable") {
2026+
res += TC_MUTABLE;
20232027
}
2028+
if has_attr(cx, did, "non_owned") {
2029+
res += TC_NON_OWNED;
2030+
}
2031+
res
20242032
}
20252033
20262034
ty_tup(ref tys) => {
@@ -2029,7 +2037,7 @@ pub fn type_contents(cx: ctxt, ty: t) -> TypeContents {
20292037
20302038
ty_enum(did, ref substs) => {
20312039
let variants = substd_enum_variants(cx, did, substs);
2032-
if variants.is_empty() {
2040+
let mut res = if variants.is_empty() {
20332041
// we somewhat arbitrary declare that empty enums
20342042
// are non-copyable
20352043
TC_EMPTY_ENUM
@@ -2039,7 +2047,14 @@ pub fn type_contents(cx: ctxt, ty: t) -> TypeContents {
20392047
*tc,
20402048
|tc, arg_ty| *tc + tc_ty(cx, *arg_ty, cache))
20412049
})
2050+
};
2051+
if has_attr(cx, did, "mutable") {
2052+
res += TC_MUTABLE;
2053+
}
2054+
if has_attr(cx, did, "non_owned") {
2055+
res += TC_NON_OWNED;
20422056
}
2057+
res
20432058
}
20442059
20452060
ty_param(p) => {
@@ -3841,28 +3856,32 @@ pub fn lookup_trait_def(cx: ctxt, did: ast::def_id) -> @ty::TraitDef {
38413856
}
38423857
}
38433858
3844-
// Determine whether an item is annotated with #[packed] or not
3845-
pub fn lookup_packed(tcx: ctxt,
3846-
did: def_id) -> bool {
3859+
/// Determine whether an item is annotated with an attribute
3860+
pub fn has_attr(tcx: ctxt, did: def_id, attr: &str) -> bool {
38473861
if is_local(did) {
38483862
match tcx.items.find(&did.node) {
38493863
Some(
38503864
&ast_map::node_item(@ast::item {
38513865
attrs: ref attrs,
38523866
_
3853-
}, _)) => attr::attrs_contains_name(*attrs, "packed"),
3867+
}, _)) => attr::attrs_contains_name(*attrs, attr),
38543868
_ => tcx.sess.bug(fmt!("lookup_packed: %? is not an item",
38553869
did))
38563870
}
38573871
} else {
38583872
let mut ret = false;
38593873
do csearch::get_item_attrs(tcx.cstore, did) |meta_items| {
3860-
ret = attr::contains_name(meta_items, "packed");
3874+
ret = attr::contains_name(meta_items, attr);
38613875
}
38623876
ret
38633877
}
38643878
}
38653879
3880+
/// Determine whether an item is annotated with `#[packed]` or not
3881+
pub fn lookup_packed(tcx: ctxt, did: def_id) -> bool {
3882+
has_attr(tcx, did, "packed")
3883+
}
3884+
38663885
// Look up a field ID, whether or not it's local
38673886
// Takes a list of type substs in case the struct is generic
38683887
pub fn lookup_field_type(tcx: ctxt,

0 commit comments

Comments
 (0)