]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/rscope.rs
Changed issue number to 36105
[rust.git] / src / librustc_typeck / rscope.rs
1 // Copyright 2012 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 rustc::hir::def_id::DefId;
12 use rustc::ty;
13 use rustc::ty::subst::Substs;
14
15 use astconv::AstConv;
16
17 use std::cell::Cell;
18 use syntax_pos::Span;
19
20 #[derive(Clone)]
21 pub struct ElisionFailureInfo {
22     pub name: String,
23     pub lifetime_count: usize,
24     pub have_bound_regions: bool
25 }
26
27 pub type ElidedLifetime = Result<ty::Region, Option<Vec<ElisionFailureInfo>>>;
28
29 /// Defines strategies for handling regions that are omitted.  For
30 /// example, if one writes the type `&Foo`, then the lifetime of
31 /// this reference has been omitted. When converting this
32 /// type, the generic functions in astconv will invoke `anon_regions`
33 /// on the provided region-scope to decide how to translate this
34 /// omitted region.
35 ///
36 /// It is not always legal to omit regions, therefore `anon_regions`
37 /// can return `Err(())` to indicate that this is not a scope in which
38 /// regions can legally be omitted.
39 pub trait RegionScope {
40     fn anon_regions(&self,
41                     span: Span,
42                     count: usize)
43                     -> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>>;
44
45     /// If an object omits any explicit lifetime bound, and none can
46     /// be derived from the object traits, what should we use? If
47     /// `None` is returned, an explicit annotation is required.
48     fn object_lifetime_default(&self, span: Span) -> Option<ty::Region>;
49
50     /// The "base" default is the initial default for a scope. This is
51     /// 'static except for in fn bodies, where it is a fresh inference
52     /// variable. You shouldn't call this except for as part of
53     /// computing `object_lifetime_default` (in particular, in legacy
54     /// modes, it may not be relevant).
55     fn base_object_lifetime_default(&self, span: Span) -> ty::Region;
56
57     /// If this scope allows anonymized types, return the generics in
58     /// scope, that anonymized types will close over. For example,
59     /// if you have a function like:
60     ///
61     ///     fn foo<'a, T>() -> impl Trait { ... }
62     ///
63     /// then, for the rscope that is used when handling the return type,
64     /// `anon_type_scope()` would return a `Some(AnonTypeScope {...})`,
65     /// on which `.fresh_substs(...)` can be used to obtain identity
66     /// Substs for `'a` and `T`, to track them in `TyAnon`. This property
67     /// is controlled by the region scope because it's fine-grained enough
68     /// to allow restriction of anonymized types to the syntactical extent
69     /// of a function's return type.
70     fn anon_type_scope(&self) -> Option<AnonTypeScope> {
71         None
72     }
73 }
74
75 #[derive(Copy, Clone)]
76 pub struct AnonTypeScope {
77     enclosing_item: DefId
78 }
79
80 impl<'gcx: 'tcx, 'tcx> AnonTypeScope {
81     pub fn new(enclosing_item: DefId) -> AnonTypeScope {
82         AnonTypeScope {
83             enclosing_item: enclosing_item
84         }
85     }
86
87     pub fn fresh_substs(&self, astconv: &AstConv<'gcx, 'tcx>, span: Span)
88                         -> &'tcx Substs<'tcx> {
89         use collect::mk_item_substs;
90
91         mk_item_substs(astconv, span, self.enclosing_item)
92     }
93 }
94
95 /// A scope wrapper which optionally allows anonymized types.
96 #[derive(Copy, Clone)]
97 pub struct MaybeWithAnonTypes<R> {
98     base_scope: R,
99     anon_scope: Option<AnonTypeScope>
100 }
101
102 impl<R: RegionScope> MaybeWithAnonTypes<R>  {
103     pub fn new(base_scope: R, anon_scope: Option<AnonTypeScope>) -> Self {
104         MaybeWithAnonTypes {
105             base_scope: base_scope,
106             anon_scope: anon_scope
107         }
108     }
109 }
110
111 impl<R: RegionScope> RegionScope for MaybeWithAnonTypes<R> {
112     fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
113         self.base_scope.object_lifetime_default(span)
114     }
115
116     fn anon_regions(&self,
117                     span: Span,
118                     count: usize)
119                     -> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>> {
120         self.base_scope.anon_regions(span, count)
121     }
122
123     fn base_object_lifetime_default(&self, span: Span) -> ty::Region {
124         self.base_scope.base_object_lifetime_default(span)
125     }
126
127     fn anon_type_scope(&self) -> Option<AnonTypeScope> {
128         self.anon_scope
129     }
130 }
131
132 // A scope in which all regions must be explicitly named. This is used
133 // for types that appear in structs and so on.
134 #[derive(Copy, Clone)]
135 pub struct ExplicitRscope;
136
137 impl RegionScope for ExplicitRscope {
138     fn anon_regions(&self,
139                     _span: Span,
140                     _count: usize)
141                     -> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>> {
142         Err(None)
143     }
144
145     fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
146         Some(self.base_object_lifetime_default(span))
147     }
148
149     fn base_object_lifetime_default(&self, _span: Span) -> ty::Region {
150         ty::ReStatic
151     }
152 }
153
154 // Same as `ExplicitRscope`, but provides some extra information for diagnostics
155 pub struct UnelidableRscope(Option<Vec<ElisionFailureInfo>>);
156
157 impl UnelidableRscope {
158     pub fn new(v: Option<Vec<ElisionFailureInfo>>) -> UnelidableRscope {
159         UnelidableRscope(v)
160     }
161 }
162
163 impl RegionScope for UnelidableRscope {
164     fn anon_regions(&self,
165                     _span: Span,
166                     _count: usize)
167                     -> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>> {
168         let UnelidableRscope(ref v) = *self;
169         Err(v.clone())
170     }
171
172     fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
173         Some(self.base_object_lifetime_default(span))
174     }
175
176     fn base_object_lifetime_default(&self, _span: Span) -> ty::Region {
177         ty::ReStatic
178     }
179 }
180
181 // A scope in which omitted anonymous region defaults to
182 // `default`. This is used after the `->` in function signatures. The
183 // latter use may go away. Note that object-lifetime defaults work a
184 // bit differently, as specified in RFC #599.
185 pub struct ElidableRscope {
186     default: ty::Region,
187 }
188
189 impl ElidableRscope {
190     pub fn new(r: ty::Region) -> ElidableRscope {
191         ElidableRscope { default: r }
192     }
193 }
194
195 impl RegionScope for ElidableRscope {
196     fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
197         // Per RFC #599, object-lifetimes default to 'static unless
198         // overridden by context, and this takes precedence over
199         // lifetime elision.
200         Some(self.base_object_lifetime_default(span))
201     }
202
203     fn base_object_lifetime_default(&self, _span: Span) -> ty::Region {
204         ty::ReStatic
205     }
206
207     fn anon_regions(&self,
208                     _span: Span,
209                     count: usize)
210                     -> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>>
211     {
212         Ok(vec![self.default; count])
213     }
214 }
215
216 /// A scope in which we generate anonymous, late-bound regions for
217 /// omitted regions. This occurs in function signatures.
218 pub struct BindingRscope {
219     anon_bindings: Cell<u32>,
220 }
221
222 impl BindingRscope {
223     pub fn new() -> BindingRscope {
224         BindingRscope {
225             anon_bindings: Cell::new(0),
226         }
227     }
228
229     fn next_region(&self) -> ty::Region {
230         let idx = self.anon_bindings.get();
231         self.anon_bindings.set(idx + 1);
232         ty::ReLateBound(ty::DebruijnIndex::new(1), ty::BrAnon(idx))
233     }
234 }
235
236 impl RegionScope for BindingRscope {
237     fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
238         // Per RFC #599, object-lifetimes default to 'static unless
239         // overridden by context, and this takes precedence over the
240         // binding defaults in a fn signature.
241         Some(self.base_object_lifetime_default(span))
242     }
243
244     fn base_object_lifetime_default(&self, _span: Span) -> ty::Region {
245         ty::ReStatic
246     }
247
248     fn anon_regions(&self,
249                     _: Span,
250                     count: usize)
251                     -> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>>
252     {
253         Ok((0..count).map(|_| self.next_region()).collect())
254     }
255 }
256
257 /// A scope which overrides the default object lifetime but has no other effect.
258 pub struct ObjectLifetimeDefaultRscope<'r> {
259     base_scope: &'r (RegionScope+'r),
260     default: ty::ObjectLifetimeDefault,
261 }
262
263 impl<'r> ObjectLifetimeDefaultRscope<'r> {
264     pub fn new(base_scope: &'r (RegionScope+'r),
265                default: ty::ObjectLifetimeDefault)
266                -> ObjectLifetimeDefaultRscope<'r>
267     {
268         ObjectLifetimeDefaultRscope {
269             base_scope: base_scope,
270             default: default,
271         }
272     }
273 }
274
275 impl<'r> RegionScope for ObjectLifetimeDefaultRscope<'r> {
276     fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
277         match self.default {
278             ty::ObjectLifetimeDefault::Ambiguous =>
279                 None,
280
281             ty::ObjectLifetimeDefault::BaseDefault =>
282                 // NB: This behavior changed in Rust 1.3.
283                 Some(self.base_object_lifetime_default(span)),
284
285             ty::ObjectLifetimeDefault::Specific(r) =>
286                 Some(r),
287         }
288     }
289
290     fn base_object_lifetime_default(&self, span: Span) -> ty::Region {
291         self.base_scope.base_object_lifetime_default(span)
292     }
293
294     fn anon_regions(&self,
295                     span: Span,
296                     count: usize)
297                     -> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>>
298     {
299         self.base_scope.anon_regions(span, count)
300     }
301
302     fn anon_type_scope(&self) -> Option<AnonTypeScope> {
303         self.base_scope.anon_type_scope()
304     }
305 }
306
307 /// A scope which simply shifts the Debruijn index of other scopes
308 /// to account for binding levels.
309 pub struct ShiftedRscope<'r> {
310     base_scope: &'r (RegionScope+'r)
311 }
312
313 impl<'r> ShiftedRscope<'r> {
314     pub fn new(base_scope: &'r (RegionScope+'r)) -> ShiftedRscope<'r> {
315         ShiftedRscope { base_scope: base_scope }
316     }
317 }
318
319 impl<'r> RegionScope for ShiftedRscope<'r> {
320     fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
321         self.base_scope.object_lifetime_default(span)
322             .map(|r| ty::fold::shift_region(r, 1))
323     }
324
325     fn base_object_lifetime_default(&self, span: Span) -> ty::Region {
326         ty::fold::shift_region(self.base_scope.base_object_lifetime_default(span), 1)
327     }
328
329     fn anon_regions(&self,
330                     span: Span,
331                     count: usize)
332                     -> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>>
333     {
334         match self.base_scope.anon_regions(span, count) {
335             Ok(mut v) => {
336                 for r in &mut v {
337                     *r = ty::fold::shift_region(*r, 1);
338                 }
339                 Ok(v)
340             }
341             Err(errs) => {
342                 Err(errs)
343             }
344         }
345     }
346
347     fn anon_type_scope(&self) -> Option<AnonTypeScope> {
348         self.base_scope.anon_type_scope()
349     }
350 }