]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/flags.rs
Rollup merge of #35613 - matthew-piziak:array-docs-trait-justification, r=steveklabnik
[rust.git] / src / librustc / ty / flags.rs
1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use ty::subst::Substs;
12 use ty::{self, Ty, TypeFlags, TypeFoldable};
13
14 pub struct FlagComputation {
15     pub flags: TypeFlags,
16
17     // maximum depth of any bound region that we have seen thus far
18     pub depth: u32,
19 }
20
21 impl FlagComputation {
22     fn new() -> FlagComputation {
23         FlagComputation { flags: TypeFlags::empty(), depth: 0 }
24     }
25
26     pub fn for_sty(st: &ty::TypeVariants) -> FlagComputation {
27         let mut result = FlagComputation::new();
28         result.add_sty(st);
29         result
30     }
31
32     fn add_flags(&mut self, flags: TypeFlags) {
33         self.flags = self.flags | (flags & TypeFlags::NOMINAL_FLAGS);
34     }
35
36     fn add_depth(&mut self, depth: u32) {
37         if depth > self.depth {
38             self.depth = depth;
39         }
40     }
41
42     /// Adds the flags/depth from a set of types that appear within the current type, but within a
43     /// region binder.
44     fn add_bound_computation(&mut self, computation: &FlagComputation) {
45         self.add_flags(computation.flags);
46
47         // The types that contributed to `computation` occurred within
48         // a region binder, so subtract one from the region depth
49         // within when adding the depth to `self`.
50         let depth = computation.depth;
51         if depth > 0 {
52             self.add_depth(depth - 1);
53         }
54     }
55
56     fn add_sty(&mut self, st: &ty::TypeVariants) {
57         match st {
58             &ty::TyBool |
59             &ty::TyChar |
60             &ty::TyInt(_) |
61             &ty::TyFloat(_) |
62             &ty::TyUint(_) |
63             &ty::TyNever |
64             &ty::TyStr => {
65             }
66
67             // You might think that we could just return TyError for
68             // any type containing TyError as a component, and get
69             // rid of the TypeFlags::HAS_TY_ERR flag -- likewise for ty_bot (with
70             // the exception of function types that return bot).
71             // But doing so caused sporadic memory corruption, and
72             // neither I (tjc) nor nmatsakis could figure out why,
73             // so we're doing it this way.
74             &ty::TyError => {
75                 self.add_flags(TypeFlags::HAS_TY_ERR)
76             }
77
78             &ty::TyParam(ref p) => {
79                 self.add_flags(TypeFlags::HAS_LOCAL_NAMES);
80                 if p.is_self() {
81                     self.add_flags(TypeFlags::HAS_SELF);
82                 } else {
83                     self.add_flags(TypeFlags::HAS_PARAMS);
84                 }
85             }
86
87             &ty::TyClosure(_, ref substs) => {
88                 self.add_flags(TypeFlags::HAS_TY_CLOSURE);
89                 self.add_flags(TypeFlags::HAS_LOCAL_NAMES);
90                 self.add_substs(&substs.func_substs);
91                 self.add_tys(&substs.upvar_tys);
92             }
93
94             &ty::TyInfer(infer) => {
95                 self.add_flags(TypeFlags::HAS_LOCAL_NAMES); // it might, right?
96                 self.add_flags(TypeFlags::HAS_TY_INFER);
97                 match infer {
98                     ty::FreshTy(_) |
99                     ty::FreshIntTy(_) |
100                     ty::FreshFloatTy(_) => {}
101                     _ => self.add_flags(TypeFlags::KEEP_IN_LOCAL_TCX)
102                 }
103             }
104
105             &ty::TyEnum(_, substs) | &ty::TyStruct(_, substs) => {
106                 self.add_substs(substs);
107             }
108
109             &ty::TyProjection(ref data) => {
110                 self.add_flags(TypeFlags::HAS_PROJECTION);
111                 self.add_projection_ty(data);
112             }
113
114             &ty::TyAnon(_, substs) => {
115                 self.add_flags(TypeFlags::HAS_PROJECTION);
116                 self.add_substs(substs);
117             }
118
119             &ty::TyTrait(ref obj) => {
120                 let mut computation = FlagComputation::new();
121                 computation.add_substs(obj.principal.skip_binder().substs);
122                 for projection_bound in &obj.projection_bounds {
123                     let mut proj_computation = FlagComputation::new();
124                     proj_computation.add_existential_projection(&projection_bound.0);
125                     self.add_bound_computation(&proj_computation);
126                 }
127                 self.add_bound_computation(&computation);
128                 self.add_region(obj.region_bound);
129             }
130
131             &ty::TyBox(tt) | &ty::TyArray(tt, _) | &ty::TySlice(tt) => {
132                 self.add_ty(tt)
133             }
134
135             &ty::TyRawPtr(ref m) => {
136                 self.add_ty(m.ty);
137             }
138
139             &ty::TyRef(r, ref m) => {
140                 self.add_region(*r);
141                 self.add_ty(m.ty);
142             }
143
144             &ty::TyTuple(ref ts) => {
145                 self.add_tys(&ts[..]);
146             }
147
148             &ty::TyFnDef(_, substs, ref f) => {
149                 self.add_substs(substs);
150                 self.add_fn_sig(&f.sig);
151             }
152
153             &ty::TyFnPtr(ref f) => {
154                 self.add_fn_sig(&f.sig);
155             }
156         }
157     }
158
159     fn add_ty(&mut self, ty: Ty) {
160         self.add_flags(ty.flags.get());
161         self.add_depth(ty.region_depth);
162     }
163
164     fn add_tys(&mut self, tys: &[Ty]) {
165         for &ty in tys {
166             self.add_ty(ty);
167         }
168     }
169
170     fn add_fn_sig(&mut self, fn_sig: &ty::PolyFnSig) {
171         let mut computation = FlagComputation::new();
172
173         computation.add_tys(&fn_sig.0.inputs);
174         computation.add_ty(fn_sig.0.output);
175
176         self.add_bound_computation(&computation);
177     }
178
179     fn add_region(&mut self, r: ty::Region) {
180         match r {
181             ty::ReVar(..) => {
182                 self.add_flags(TypeFlags::HAS_RE_INFER);
183                 self.add_flags(TypeFlags::KEEP_IN_LOCAL_TCX);
184             }
185             ty::ReSkolemized(..) => {
186                 self.add_flags(TypeFlags::HAS_RE_INFER);
187                 self.add_flags(TypeFlags::HAS_RE_SKOL);
188                 self.add_flags(TypeFlags::KEEP_IN_LOCAL_TCX);
189             }
190             ty::ReLateBound(debruijn, _) => { self.add_depth(debruijn.depth); }
191             ty::ReEarlyBound(..) => { self.add_flags(TypeFlags::HAS_RE_EARLY_BOUND); }
192             ty::ReStatic | ty::ReErased => {}
193             _ => { self.add_flags(TypeFlags::HAS_FREE_REGIONS); }
194         }
195
196         if !r.is_global() {
197             self.add_flags(TypeFlags::HAS_LOCAL_NAMES);
198         }
199     }
200
201     fn add_existential_projection(&mut self, projection: &ty::ExistentialProjection) {
202         self.add_substs(projection.trait_ref.substs);
203         self.add_ty(projection.ty);
204     }
205
206     fn add_projection_ty(&mut self, projection_ty: &ty::ProjectionTy) {
207         self.add_substs(projection_ty.trait_ref.substs);
208     }
209
210     fn add_substs(&mut self, substs: &Substs) {
211         self.add_tys(&substs.types);
212         for &r in &substs.regions {
213             self.add_region(r);
214         }
215     }
216 }