Skip to content

Commit 1617ec4

Browse files
committed
Clean up TypeFlags
* Reorder flags to group similar ones together * Make some flags more granular * Compute `HAS_FREE_LOCAL_NAMES` from the other flags * Remove `HAS_TY_CLOSURE` * Add some more doc comments
1 parent ebc86b4 commit 1617ec4

File tree

4 files changed

+99
-87
lines changed

4 files changed

+99
-87
lines changed

src/librustc/ty/flags.rs

+7-16
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,10 @@ impl FlagComputation {
8080
&ty::Error => self.add_flags(TypeFlags::HAS_TY_ERR),
8181

8282
&ty::Param(_) => {
83-
self.add_flags(TypeFlags::HAS_FREE_LOCAL_NAMES);
84-
self.add_flags(TypeFlags::HAS_PARAMS);
83+
self.add_flags(TypeFlags::HAS_TY_PARAM);
8584
}
8685

8786
&ty::Generator(_, ref substs, _) => {
88-
self.add_flags(TypeFlags::HAS_TY_CLOSURE);
89-
self.add_flags(TypeFlags::HAS_FREE_LOCAL_NAMES);
9087
self.add_substs(substs);
9188
}
9289

@@ -97,8 +94,6 @@ impl FlagComputation {
9794
}
9895

9996
&ty::Closure(_, ref substs) => {
100-
self.add_flags(TypeFlags::HAS_TY_CLOSURE);
101-
self.add_flags(TypeFlags::HAS_FREE_LOCAL_NAMES);
10297
self.add_substs(substs);
10398
}
10499

@@ -107,12 +102,10 @@ impl FlagComputation {
107102
}
108103

109104
&ty::Placeholder(..) => {
110-
self.add_flags(TypeFlags::HAS_FREE_LOCAL_NAMES);
111105
self.add_flags(TypeFlags::HAS_TY_PLACEHOLDER);
112106
}
113107

114108
&ty::Infer(infer) => {
115-
self.add_flags(TypeFlags::HAS_FREE_LOCAL_NAMES); // it might, right?
116109
self.add_flags(TypeFlags::HAS_TY_INFER);
117110
match infer {
118111
ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => {}
@@ -128,17 +121,17 @@ impl FlagComputation {
128121
}
129122

130123
&ty::Projection(ref data) => {
131-
self.add_flags(TypeFlags::HAS_PROJECTION);
124+
self.add_flags(TypeFlags::HAS_TY_PROJECTION);
132125
self.add_projection_ty(data);
133126
}
134127

135128
&ty::UnnormalizedProjection(ref data) => {
136-
self.add_flags(TypeFlags::HAS_PROJECTION);
129+
self.add_flags(TypeFlags::HAS_TY_PROJECTION);
137130
self.add_projection_ty(data);
138131
}
139132

140133
&ty::Opaque(_, substs) => {
141-
self.add_flags(TypeFlags::HAS_PROJECTION | TypeFlags::HAS_TY_OPAQUE);
134+
self.add_flags(TypeFlags::HAS_TY_OPAQUE);
142135
self.add_substs(substs);
143136
}
144137

@@ -221,22 +214,20 @@ impl FlagComputation {
221214
match c.val {
222215
ty::ConstKind::Unevaluated(_, substs, _) => {
223216
self.add_substs(substs);
224-
self.add_flags(TypeFlags::HAS_PROJECTION);
217+
self.add_flags(TypeFlags::HAS_CT_PROJECTION);
225218
}
226219
ty::ConstKind::Infer(infer) => {
227-
self.add_flags(TypeFlags::HAS_FREE_LOCAL_NAMES | TypeFlags::HAS_CT_INFER);
220+
self.add_flags(TypeFlags::HAS_CT_INFER);
228221
match infer {
229222
InferConst::Fresh(_) => {}
230223
InferConst::Var(_) => self.add_flags(TypeFlags::KEEP_IN_LOCAL_TCX),
231224
}
232225
}
233226
ty::ConstKind::Bound(debruijn, _) => self.add_binder(debruijn),
234227
ty::ConstKind::Param(_) => {
235-
self.add_flags(TypeFlags::HAS_FREE_LOCAL_NAMES);
236-
self.add_flags(TypeFlags::HAS_PARAMS);
228+
self.add_flags(TypeFlags::HAS_CT_PARAM);
237229
}
238230
ty::ConstKind::Placeholder(_) => {
239-
self.add_flags(TypeFlags::HAS_FREE_LOCAL_NAMES);
240231
self.add_flags(TypeFlags::HAS_CT_PLACEHOLDER);
241232
}
242233
ty::ConstKind::Value(_) => {}

src/librustc/ty/fold.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub trait TypeFoldable<'tcx>: fmt::Debug + Clone {
8585
self.has_type_flags(TypeFlags::HAS_TY_ERR)
8686
}
8787
fn has_param_types(&self) -> bool {
88-
self.has_type_flags(TypeFlags::HAS_PARAMS)
88+
self.has_type_flags(TypeFlags::HAS_TY_PARAM | TypeFlags::HAS_CT_PARAM)
8989
}
9090
fn has_infer_types(&self) -> bool {
9191
self.has_type_flags(TypeFlags::HAS_TY_INFER)
@@ -100,9 +100,7 @@ pub trait TypeFoldable<'tcx>: fmt::Debug + Clone {
100100
self.has_type_flags(TypeFlags::KEEP_IN_LOCAL_TCX)
101101
}
102102
fn needs_infer(&self) -> bool {
103-
self.has_type_flags(
104-
TypeFlags::HAS_TY_INFER | TypeFlags::HAS_RE_INFER | TypeFlags::HAS_CT_INFER,
105-
)
103+
self.has_type_flags(TypeFlags::NEEDS_INFER)
106104
}
107105
fn has_placeholders(&self) -> bool {
108106
self.has_type_flags(

src/librustc/ty/mod.rs

+89-61
Original file line numberDiff line numberDiff line change
@@ -515,79 +515,107 @@ pub struct CReaderCacheKey {
515515
pub pos: usize,
516516
}
517517

518-
// Flags that we track on types. These flags are propagated upwards
519-
// through the type during type construction, so that we can quickly
520-
// check whether the type has various kinds of types in it without
521-
// recursing over the type itself.
522518
bitflags! {
519+
/// Flags that we track on types. These flags are propagated upwards
520+
/// through the type during type construction, so that we can quickly check
521+
/// whether the type has various kinds of types in it without recursing
522+
/// over the type itself.
523523
pub struct TypeFlags: u32 {
524-
const HAS_PARAMS = 1 << 0;
525-
const HAS_TY_INFER = 1 << 1;
526-
const HAS_RE_INFER = 1 << 2;
527-
const HAS_RE_PLACEHOLDER = 1 << 3;
528-
529-
/// Does this have any `ReEarlyBound` regions? Used to
530-
/// determine whether substitition is required, since those
531-
/// represent regions that are bound in a `ty::Generics` and
532-
/// hence may be substituted.
533-
const HAS_RE_EARLY_BOUND = 1 << 4;
534-
535-
/// Does this have any region that "appears free" in the type?
536-
/// Basically anything but `ReLateBound` and `ReErased`.
537-
const HAS_FREE_REGIONS = 1 << 5;
538-
539-
/// Is an error type reachable?
540-
const HAS_TY_ERR = 1 << 6;
541-
const HAS_PROJECTION = 1 << 7;
542-
543-
// FIXME: Rename this to the actual property since it's used for generators too
544-
const HAS_TY_CLOSURE = 1 << 8;
524+
// Does this have parameters? Used to determine whether substitution is
525+
// required.
526+
/// Does this have [Param]?
527+
const HAS_TY_PARAM = 1 << 0;
528+
/// Does this have [ReEarlyBound]?
529+
const HAS_RE_PARAM = 1 << 1;
530+
/// Does this have [ConstKind::Param]?
531+
const HAS_CT_PARAM = 1 << 2;
532+
533+
const NEEDS_SUBST = TypeFlags::HAS_TY_PARAM.bits
534+
| TypeFlags::HAS_RE_PARAM.bits
535+
| TypeFlags::HAS_CT_PARAM.bits;
536+
537+
/// Does this have [Infer]?
538+
const HAS_TY_INFER = 1 << 3;
539+
/// Does this have [ReVar]?
540+
const HAS_RE_INFER = 1 << 4;
541+
/// Does this have [ConstKind::Infer]?
542+
const HAS_CT_INFER = 1 << 5;
543+
544+
/// Does this have inference variables? Used to determine whether
545+
/// inference is required.
546+
const NEEDS_INFER = TypeFlags::HAS_TY_INFER.bits
547+
| TypeFlags::HAS_RE_INFER.bits
548+
| TypeFlags::HAS_CT_INFER.bits;
549+
550+
/// Does this have [Placeholder]?
551+
const HAS_TY_PLACEHOLDER = 1 << 6;
552+
/// Does this have [RePlaceholder]?
553+
const HAS_RE_PLACEHOLDER = 1 << 7;
554+
/// Does this have [ConstKind::Placeholder]?
555+
const HAS_CT_PLACEHOLDER = 1 << 8;
545556

546557
/// `true` if there are "names" of types and regions and so forth
547558
/// that are local to a particular fn
548-
const HAS_FREE_LOCAL_NAMES = 1 << 9;
559+
const HAS_FREE_LOCAL_NAMES = TypeFlags::HAS_TY_PARAM.bits
560+
| TypeFlags::HAS_RE_PARAM.bits
561+
| TypeFlags::HAS_CT_PARAM.bits
562+
| TypeFlags::HAS_TY_INFER.bits
563+
| TypeFlags::HAS_RE_INFER.bits
564+
| TypeFlags::HAS_CT_INFER.bits
565+
| TypeFlags::HAS_TY_PLACEHOLDER.bits
566+
| TypeFlags::HAS_RE_PLACEHOLDER.bits
567+
| TypeFlags::HAS_CT_PLACEHOLDER.bits;
568+
569+
/// Does this have [Projection] or [UnnormalizedProjection]?
570+
const HAS_TY_PROJECTION = 1 << 9;
571+
/// Does this have [Opaque]?
572+
const HAS_TY_OPAQUE = 1 << 10;
573+
/// Does this have [ConstKind::Unevaluated]?
574+
const HAS_CT_PROJECTION = 1 << 11;
575+
576+
/// Could this type be normalized further?
577+
const HAS_PROJECTION = TypeFlags::HAS_TY_PROJECTION.bits
578+
| TypeFlags::HAS_TY_OPAQUE.bits
579+
| TypeFlags::HAS_CT_PROJECTION.bits;
549580

550581
/// Present if the type belongs in a local type context.
551-
/// Only set for Infer other than Fresh.
552-
const KEEP_IN_LOCAL_TCX = 1 << 10;
553-
554-
/// Does this have any `ReLateBound` regions? Used to check
555-
/// if a global bound is safe to evaluate.
556-
const HAS_RE_LATE_BOUND = 1 << 11;
582+
/// Set for placeholders and inference variables that are not "Fresh".
583+
const KEEP_IN_LOCAL_TCX = 1 << 12;
557584

558-
/// Does this have any `ReErased` regions?
559-
const HAS_RE_ERASED = 1 << 12;
585+
/// Is an error type reachable?
586+
const HAS_TY_ERR = 1 << 13;
560587

561-
const HAS_TY_PLACEHOLDER = 1 << 13;
588+
/// Does this have any region that "appears free" in the type?
589+
/// Basically anything but [ReLateBound] and [ReErased].
590+
const HAS_FREE_REGIONS = 1 << 14;
562591

563-
const HAS_CT_INFER = 1 << 14;
564-
const HAS_CT_PLACEHOLDER = 1 << 15;
565-
/// Does this have any [Opaque] types.
566-
const HAS_TY_OPAQUE = 1 << 16;
592+
/// Does this have any [ReLateBound] regions? Used to check
593+
/// if a global bound is safe to evaluate.
594+
const HAS_RE_LATE_BOUND = 1 << 15;
567595

568-
const NEEDS_SUBST = TypeFlags::HAS_PARAMS.bits |
569-
TypeFlags::HAS_RE_EARLY_BOUND.bits;
596+
/// Does this have any [ReErased] regions?
597+
const HAS_RE_ERASED = 1 << 16;
570598

571599
/// Flags representing the nominal content of a type,
572600
/// computed by FlagsComputation. If you add a new nominal
573601
/// flag, it should be added here too.
574-
const NOMINAL_FLAGS = TypeFlags::HAS_PARAMS.bits |
575-
TypeFlags::HAS_TY_INFER.bits |
576-
TypeFlags::HAS_RE_INFER.bits |
577-
TypeFlags::HAS_RE_PLACEHOLDER.bits |
578-
TypeFlags::HAS_RE_EARLY_BOUND.bits |
579-
TypeFlags::HAS_FREE_REGIONS.bits |
580-
TypeFlags::HAS_TY_ERR.bits |
581-
TypeFlags::HAS_PROJECTION.bits |
582-
TypeFlags::HAS_TY_CLOSURE.bits |
583-
TypeFlags::HAS_FREE_LOCAL_NAMES.bits |
584-
TypeFlags::KEEP_IN_LOCAL_TCX.bits |
585-
TypeFlags::HAS_RE_LATE_BOUND.bits |
586-
TypeFlags::HAS_RE_ERASED.bits |
587-
TypeFlags::HAS_TY_PLACEHOLDER.bits |
588-
TypeFlags::HAS_CT_INFER.bits |
589-
TypeFlags::HAS_CT_PLACEHOLDER.bits |
590-
TypeFlags::HAS_TY_OPAQUE.bits;
602+
const NOMINAL_FLAGS = TypeFlags::HAS_TY_PARAM.bits
603+
| TypeFlags::HAS_RE_PARAM.bits
604+
| TypeFlags::HAS_CT_PARAM.bits
605+
| TypeFlags::HAS_TY_INFER.bits
606+
| TypeFlags::HAS_RE_INFER.bits
607+
| TypeFlags::HAS_CT_INFER.bits
608+
| TypeFlags::HAS_TY_PLACEHOLDER.bits
609+
| TypeFlags::HAS_RE_PLACEHOLDER.bits
610+
| TypeFlags::HAS_CT_PLACEHOLDER.bits
611+
| TypeFlags::HAS_TY_PROJECTION.bits
612+
| TypeFlags::HAS_TY_OPAQUE.bits
613+
| TypeFlags::HAS_CT_PROJECTION.bits
614+
| TypeFlags::KEEP_IN_LOCAL_TCX.bits
615+
| TypeFlags::HAS_TY_ERR.bits
616+
| TypeFlags::HAS_FREE_REGIONS.bits
617+
| TypeFlags::HAS_RE_LATE_BOUND.bits
618+
| TypeFlags::HAS_RE_ERASED.bits;
591619
}
592620
}
593621

@@ -1816,10 +1844,10 @@ impl<'tcx> ParamEnv<'tcx> {
18161844
Reveal::UserFacing => ParamEnvAnd { param_env: self, value },
18171845

18181846
Reveal::All => {
1819-
if value.has_placeholders() || value.needs_infer() || value.has_param_types() {
1820-
ParamEnvAnd { param_env: self, value }
1821-
} else {
1847+
if value.is_global() {
18221848
ParamEnvAnd { param_env: self.without_caller_bounds(), value }
1849+
} else {
1850+
ParamEnvAnd { param_env: self, value }
18231851
}
18241852
}
18251853
}

src/librustc/ty/sty.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1768,7 +1768,7 @@ impl RegionKind {
17681768
}
17691769
ty::ReEarlyBound(..) => {
17701770
flags = flags | TypeFlags::HAS_FREE_REGIONS;
1771-
flags = flags | TypeFlags::HAS_RE_EARLY_BOUND;
1771+
flags = flags | TypeFlags::HAS_RE_PARAM;
17721772
}
17731773
ty::ReEmpty(_) | ty::ReStatic | ty::ReFree { .. } | ty::ReScope { .. } => {
17741774
flags = flags | TypeFlags::HAS_FREE_REGIONS;
@@ -1781,11 +1781,6 @@ impl RegionKind {
17811781
}
17821782
}
17831783

1784-
match *self {
1785-
ty::ReStatic | ty::ReEmpty(_) | ty::ReErased | ty::ReLateBound(..) => (),
1786-
_ => flags = flags | TypeFlags::HAS_FREE_LOCAL_NAMES,
1787-
}
1788-
17891784
debug!("type_flags({:?}) = {:?}", self, flags);
17901785

17911786
flags

0 commit comments

Comments
 (0)