Skip to content

Commit 4a8d712

Browse files
committed
Use type-safe wrapper for TypeFlags
1 parent de06faf commit 4a8d712

File tree

2 files changed

+59
-46
lines changed

2 files changed

+59
-46
lines changed

src/librustc/middle/traits/doc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ want to be able to cache results even when all the types in the trait
287287
reference are not fully known. In that case, it may happen that the
288288
trait selection process is also influencing type variables, so we have
289289
to be able to not only cache the *result* of the selection process,
290-
but *reply* its effects on the type variables.
290+
but *replay* its effects on the type variables.
291291
292292
## An example
293293

src/librustc/middle/ty.rs

Lines changed: 58 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -589,22 +589,33 @@ pub struct ctxt<'tcx> {
589589
// through the type during type construction, so that we can quickly
590590
// check whether the type has various kinds of types in it without
591591
// recursing over the type itself.
592-
const HAS_PARAMS: uint = 1;
593-
const HAS_SELF: uint = 2;
594-
const HAS_TY_INFER: uint = 4;
595-
const HAS_RE_INFER: uint = 8;
596-
const HAS_REGIONS: uint = 16;
597-
const HAS_TY_ERR: uint = 32;
598-
const HAS_TY_BOT: uint = 64;
599-
const NEEDS_SUBST: uint = HAS_PARAMS | HAS_SELF | HAS_REGIONS;
592+
bitflags! {
593+
flags TypeFlags: u32 {
594+
const NO_TYPE_FLAGS = 0b0,
595+
const HAS_PARAMS = 0b1,
596+
const HAS_SELF = 0b10,
597+
const HAS_TY_INFER = 0b100,
598+
const HAS_RE_INFER = 0b1000,
599+
const HAS_REGIONS = 0b10000,
600+
const HAS_TY_ERR = 0b100000,
601+
const HAS_TY_BOT = 0b1000000,
602+
const NEEDS_SUBST = HAS_PARAMS.bits | HAS_SELF.bits | HAS_REGIONS.bits,
603+
}
604+
}
600605

601606
pub type t_box = &'static t_box_;
602607

603608
#[deriving(Show)]
604609
pub struct t_box_ {
605610
pub sty: sty,
606611
pub id: uint,
607-
pub flags: uint,
612+
pub flags: TypeFlags,
613+
}
614+
615+
impl fmt::Show for TypeFlags {
616+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
617+
write!(f, "{}", self.bits)
618+
}
608619
}
609620

610621
// To reduce refcounting cost, we're representing types as unsafe pointers
@@ -631,8 +642,8 @@ pub fn get(t: t) -> t_box {
631642
}
632643
}
633644

634-
fn tbox_has_flag(tb: t_box, flag: uint) -> bool {
635-
(tb.flags & flag) != 0u
645+
fn tbox_has_flag(tb: t_box, flag: TypeFlags) -> bool {
646+
tb.flags.intersects(flag)
636647
}
637648
pub fn type_has_params(t: t) -> bool {
638649
tbox_has_flag(get(t), HAS_PARAMS)
@@ -887,7 +898,7 @@ mod primitives {
887898
pub static $name: t_box_ = t_box_ {
888899
sty: $sty,
889900
id: $id,
890-
flags: 0,
901+
flags: super::NO_TYPE_FLAGS,
891902
};
892903
)
893904
)
@@ -1578,32 +1589,32 @@ pub fn mk_t(cx: &ctxt, st: sty) -> t {
15781589
_ => ()
15791590
}
15801591

1581-
let mut flags = 0u;
1582-
fn rflags(r: Region) -> uint {
1592+
let mut flags = NO_TYPE_FLAGS;
1593+
fn rflags(r: Region) -> TypeFlags {
15831594
HAS_REGIONS | {
15841595
match r {
15851596
ty::ReInfer(_) => HAS_RE_INFER,
1586-
_ => 0u
1597+
_ => NO_TYPE_FLAGS,
15871598
}
15881599
}
15891600
}
1590-
fn sflags(substs: &Substs) -> uint {
1591-
let mut f = 0u;
1601+
fn sflags(substs: &Substs) -> TypeFlags {
1602+
let mut f = NO_TYPE_FLAGS;
15921603
let mut i = substs.types.iter();
15931604
for tt in i {
1594-
f |= get(*tt).flags;
1605+
f = f | get(*tt).flags;
15951606
}
15961607
match substs.regions {
15971608
subst::ErasedRegions => {}
15981609
subst::NonerasedRegions(ref regions) => {
15991610
for r in regions.iter() {
1600-
f |= rflags(*r)
1611+
f = f | rflags(*r)
16011612
}
16021613
}
16031614
}
16041615
return f;
16051616
}
1606-
fn flags_for_bounds(bounds: &ExistentialBounds) -> uint {
1617+
fn flags_for_bounds(bounds: &ExistentialBounds) -> TypeFlags {
16071618
rflags(bounds.region_bound)
16081619
}
16091620
match &st {
@@ -1616,53 +1627,53 @@ pub fn mk_t(cx: &ctxt, st: sty) -> t {
16161627
// But doing so caused sporadic memory corruption, and
16171628
// neither I (tjc) nor nmatsakis could figure out why,
16181629
// so we're doing it this way.
1619-
&ty_bot => flags |= HAS_TY_BOT,
1620-
&ty_err => flags |= HAS_TY_ERR,
1630+
&ty_bot => flags = flags | HAS_TY_BOT,
1631+
&ty_err => flags = flags | HAS_TY_ERR,
16211632
&ty_param(ref p) => {
16221633
if p.space == subst::SelfSpace {
1623-
flags |= HAS_SELF;
1634+
flags = flags | HAS_SELF;
16241635
} else {
1625-
flags |= HAS_PARAMS;
1636+
flags = flags | HAS_PARAMS;
16261637
}
16271638
}
1628-
&ty_unboxed_closure(_, ref region) => flags |= rflags(*region),
1629-
&ty_infer(_) => flags |= HAS_TY_INFER,
1639+
&ty_unboxed_closure(_, ref region) => flags = flags | rflags(*region),
1640+
&ty_infer(_) => flags = flags | HAS_TY_INFER,
16301641
&ty_enum(_, ref substs) | &ty_struct(_, ref substs) => {
1631-
flags |= sflags(substs);
1642+
flags = flags | sflags(substs);
16321643
}
16331644
&ty_trait(box TyTrait { ref substs, ref bounds, .. }) => {
1634-
flags |= sflags(substs);
1635-
flags |= flags_for_bounds(bounds);
1645+
flags = flags | sflags(substs);
1646+
flags = flags | flags_for_bounds(bounds);
16361647
}
16371648
&ty_uniq(tt) | &ty_vec(tt, _) | &ty_open(tt) => {
1638-
flags |= get(tt).flags
1649+
flags = flags | get(tt).flags
16391650
}
16401651
&ty_ptr(ref m) => {
1641-
flags |= get(m.ty).flags;
1652+
flags = flags | get(m.ty).flags;
16421653
}
16431654
&ty_rptr(r, ref m) => {
1644-
flags |= rflags(r);
1645-
flags |= get(m.ty).flags;
1655+
flags = flags | rflags(r);
1656+
flags = flags | get(m.ty).flags;
16461657
}
1647-
&ty_tup(ref ts) => for tt in ts.iter() { flags |= get(*tt).flags; },
1658+
&ty_tup(ref ts) => for tt in ts.iter() { flags = flags | get(*tt).flags; },
16481659
&ty_bare_fn(ref f) => {
1649-
for a in f.sig.inputs.iter() { flags |= get(*a).flags; }
1650-
flags |= get(f.sig.output).flags;
1660+
for a in f.sig.inputs.iter() { flags = flags | get(*a).flags; }
1661+
flags = flags | get(f.sig.output).flags;
16511662
// T -> _|_ is *not* _|_ !
1652-
flags &= !HAS_TY_BOT;
1663+
flags = flags - HAS_TY_BOT;
16531664
}
16541665
&ty_closure(ref f) => {
16551666
match f.store {
16561667
RegionTraitStore(r, _) => {
1657-
flags |= rflags(r);
1668+
flags = flags | rflags(r);
16581669
}
16591670
_ => {}
16601671
}
1661-
for a in f.sig.inputs.iter() { flags |= get(*a).flags; }
1662-
flags |= get(f.sig.output).flags;
1672+
for a in f.sig.inputs.iter() { flags = flags | get(*a).flags; }
1673+
flags = flags | get(f.sig.output).flags;
16631674
// T -> _|_ is *not* _|_ !
1664-
flags &= !HAS_TY_BOT;
1665-
flags |= flags_for_bounds(&f.bounds);
1675+
flags = flags - HAS_TY_BOT;
1676+
flags = flags | flags_for_bounds(&f.bounds);
16661677
}
16671678
}
16681679

@@ -1977,14 +1988,16 @@ impl ItemSubsts {
19771988

19781989
// Type utilities
19791990

1980-
pub fn type_is_nil(ty: t) -> bool { get(ty).sty == ty_nil }
1991+
pub fn type_is_nil(ty: t) -> bool {
1992+
get(ty).sty == ty_nil
1993+
}
19811994

19821995
pub fn type_is_bot(ty: t) -> bool {
1983-
(get(ty).flags & HAS_TY_BOT) != 0
1996+
get(ty).flags.intersects(HAS_TY_BOT)
19841997
}
19851998

19861999
pub fn type_is_error(ty: t) -> bool {
1987-
(get(ty).flags & HAS_TY_ERR) != 0
2000+
get(ty).flags.intersects(HAS_TY_ERR)
19882001
}
19892002

19902003
pub fn type_needs_subst(ty: t) -> bool {

0 commit comments

Comments
 (0)