Skip to content

Commit 85aaf39

Browse files
committed
Reimplement type_needs_drop on top of TypeContents
1 parent 6439e28 commit 85aaf39

File tree

1 file changed

+17
-81
lines changed

1 file changed

+17
-81
lines changed

src/librustc/middle/ty.rs

+17-81
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,6 @@ struct ctxt_ {
259259
rcache: creader_cache,
260260
ccache: constness_cache,
261261
short_names_cache: HashMap<t, @~str>,
262-
needs_drop_cache: HashMap<t, bool>,
263262
needs_unwind_cleanup_cache: HashMap<t, bool>,
264263
tc_cache: @mut LinearMap<uint, TypeContents>,
265264
ast_ty_to_ty_cache: HashMap<node_id, ast_ty_to_ty_cache_entry>,
@@ -822,7 +821,6 @@ pub fn mk_ctxt(s: session::Session,
822821
rcache: mk_rcache(),
823822
ccache: HashMap(),
824823
short_names_cache: new_ty_hash(),
825-
needs_drop_cache: new_ty_hash(),
826824
needs_unwind_cleanup_cache: new_ty_hash(),
827825
tc_cache: @mut LinearMap::new(),
828826
ast_ty_to_ty_cache: HashMap(),
@@ -1600,79 +1598,7 @@ pub fn type_is_immediate(ty: t) -> bool {
16001598
}
16011599
16021600
pub fn type_needs_drop(cx: ctxt, ty: t) -> bool {
1603-
match cx.needs_drop_cache.find(&ty) {
1604-
Some(result) => return result,
1605-
None => {/* fall through */ }
1606-
}
1607-
1608-
let mut accum = false;
1609-
let result = match /*bad*/copy get(ty).sty {
1610-
// scalar types
1611-
ty_nil | ty_bot | ty_bool | ty_int(_) | ty_float(_) | ty_uint(_) |
1612-
ty_type | ty_ptr(_) | ty_rptr(_, _) |
1613-
ty_estr(vstore_fixed(_)) |
1614-
ty_estr(vstore_slice(_)) |
1615-
ty_evec(_, vstore_slice(_)) |
1616-
ty_self => false,
1617-
1618-
ty_box(_) | ty_uniq(_) |
1619-
ty_opaque_box | ty_opaque_closure_ptr(*) |
1620-
ty_estr(vstore_uniq) |
1621-
ty_estr(vstore_box) |
1622-
ty_evec(_, vstore_uniq) |
1623-
ty_evec(_, vstore_box) => true,
1624-
1625-
ty_trait(_, _, vstore_box) |
1626-
ty_trait(_, _, vstore_uniq) => true,
1627-
ty_trait(_, _, vstore_fixed(_)) |
1628-
ty_trait(_, _, vstore_slice(_)) => false,
1629-
1630-
ty_param(*) | ty_infer(*) | ty_err => true,
1631-
1632-
ty_evec(mt, vstore_fixed(_)) => type_needs_drop(cx, mt.ty),
1633-
ty_unboxed_vec(mt) => type_needs_drop(cx, mt.ty),
1634-
ty_rec(flds) => {
1635-
for flds.each |f| {
1636-
if type_needs_drop(cx, f.mt.ty) { accum = true; }
1637-
}
1638-
accum
1639-
}
1640-
ty_struct(did, ref substs) => {
1641-
// Any struct with a dtor needs a drop
1642-
ty_dtor(cx, did).is_present() || {
1643-
for vec::each(ty::struct_fields(cx, did, substs)) |f| {
1644-
if type_needs_drop(cx, f.mt.ty) { accum = true; }
1645-
}
1646-
accum
1647-
}
1648-
}
1649-
ty_tup(elts) => {
1650-
for elts.each |m| { if type_needs_drop(cx, *m) { accum = true; } }
1651-
accum
1652-
}
1653-
ty_enum(did, ref substs) => {
1654-
let variants = enum_variants(cx, did);
1655-
for vec::each(*variants) |variant| {
1656-
for variant.args.each |aty| {
1657-
// Perform any type parameter substitutions.
1658-
let arg_ty = subst(cx, substs, *aty);
1659-
if type_needs_drop(cx, arg_ty) { accum = true; }
1660-
}
1661-
if accum { break; }
1662-
}
1663-
accum
1664-
}
1665-
ty_bare_fn(*) => false,
1666-
ty_closure(ref fty) => {
1667-
match fty.sigil {
1668-
ast::BorrowedSigil => false,
1669-
ast::ManagedSigil | ast::OwnedSigil => true,
1670-
}
1671-
}
1672-
};
1673-
1674-
cx.needs_drop_cache.insert(ty, result);
1675-
return result;
1601+
type_contents(cx, ty).needs_drop(cx)
16761602
}
16771603
16781604
// Some things don't need cleanups during unwinding because the
@@ -1819,7 +1745,7 @@ pub impl TypeContents {
18191745
18201746
static fn nonimplicitly_copyable(cx: ctxt) -> TypeContents {
18211747
let base = TypeContents::noncopyable(cx) + TC_OWNED_POINTER;
1822-
if cx.vecs_implicitly_copyable {base} else {base + TC_OWNED_SLICE}
1748+
if cx.vecs_implicitly_copyable {base} else {base + TC_OWNED_VEC}
18231749
}
18241750
18251751
fn is_safe_for_default_mode(&self, cx: ctxt) -> bool {
@@ -1828,7 +1754,17 @@ pub impl TypeContents {
18281754
18291755
static fn nondefault_mode(cx: ctxt) -> TypeContents {
18301756
let tc = TypeContents::nonimplicitly_copyable(cx);
1831-
tc + TC_BIG + TC_OWNED_SLICE // disregard cx.vecs_implicitly_copyable
1757+
tc + TC_BIG + TC_OWNED_VEC // disregard cx.vecs_implicitly_copyable
1758+
}
1759+
1760+
fn needs_drop(&self, cx: ctxt) -> bool {
1761+
let tc = TC_MANAGED + TC_DTOR + TypeContents::owned(cx);
1762+
self.intersects(tc)
1763+
}
1764+
1765+
static fn owned(&self, _cx: ctxt) -> TypeContents {
1766+
//! Any kind of owned contents.
1767+
TC_OWNED_CLOSURE + TC_OWNED_POINTER + TC_OWNED_VEC
18321768
}
18331769
}
18341770
@@ -1859,8 +1795,8 @@ const TC_BORROWED_POINTER: TypeContents = TypeContents{bits:0b0000_00000001};
18591795
/// Contains an owned pointer (~T) but not slice of some kind
18601796
const TC_OWNED_POINTER: TypeContents = TypeContents{bits:0b000000000010};
18611797
1862-
/// Contains an owned slice
1863-
const TC_OWNED_SLICE: TypeContents = TypeContents{bits:0b000000000100};
1798+
/// Contains an owned vector ~[] or owned string ~str
1799+
const TC_OWNED_VEC: TypeContents = TypeContents{bits:0b000000000100};
18641800
18651801
/// Contains a ~fn() or a ~Trait, which is non-copyable.
18661802
const TC_OWNED_CLOSURE: TypeContents = TypeContents{bits:0b000000001000};
@@ -1963,7 +1899,7 @@ pub fn type_contents(cx: ctxt, ty: t) -> TypeContents {
19631899
}
19641900
19651901
ty_estr(vstore_uniq) => {
1966-
TC_OWNED_SLICE
1902+
TC_OWNED_VEC
19671903
}
19681904
19691905
ty_closure(ref c) => {
@@ -1996,7 +1932,7 @@ pub fn type_contents(cx: ctxt, ty: t) -> TypeContents {
19961932
}
19971933
19981934
ty_evec(mt, vstore_uniq) => {
1999-
TC_OWNED_SLICE + tc_mt(cx, mt, cache)
1935+
TC_OWNED_VEC + tc_mt(cx, mt, cache)
20001936
}
20011937
20021938
ty_evec(mt, vstore_box) => {

0 commit comments

Comments
 (0)