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.
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.
11 use rustc::hir::def_id::DefId;
13 use rustc::ty::subst::Substs;
21 pub struct ElisionFailureInfo {
23 pub lifetime_count: usize,
24 pub have_bound_regions: bool
27 pub type ElidedLifetime = Result<ty::Region, Option<Vec<ElisionFailureInfo>>>;
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
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,
43 -> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>>;
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>;
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;
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:
61 /// fn foo<'a, T>() -> impl Trait { ... }
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> {
75 #[derive(Copy, Clone)]
76 pub struct AnonTypeScope {
80 impl<'gcx: 'tcx, 'tcx> AnonTypeScope {
81 pub fn new(enclosing_item: DefId) -> AnonTypeScope {
83 enclosing_item: enclosing_item
87 pub fn fresh_substs(&self, astconv: &AstConv<'gcx, 'tcx>, span: Span)
88 -> &'tcx Substs<'tcx> {
89 use collect::mk_item_substs;
91 mk_item_substs(astconv, span, self.enclosing_item)
95 /// A scope wrapper which optionally allows anonymized types.
96 #[derive(Copy, Clone)]
97 pub struct MaybeWithAnonTypes<R> {
99 anon_scope: Option<AnonTypeScope>
102 impl<R: RegionScope> MaybeWithAnonTypes<R> {
103 pub fn new(base_scope: R, anon_scope: Option<AnonTypeScope>) -> Self {
105 base_scope: base_scope,
106 anon_scope: anon_scope
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)
116 fn anon_regions(&self,
119 -> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>> {
120 self.base_scope.anon_regions(span, count)
123 fn base_object_lifetime_default(&self, span: Span) -> ty::Region {
124 self.base_scope.base_object_lifetime_default(span)
127 fn anon_type_scope(&self) -> Option<AnonTypeScope> {
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;
137 impl RegionScope for ExplicitRscope {
138 fn anon_regions(&self,
141 -> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>> {
145 fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
146 Some(self.base_object_lifetime_default(span))
149 fn base_object_lifetime_default(&self, _span: Span) -> ty::Region {
154 // Same as `ExplicitRscope`, but provides some extra information for diagnostics
155 pub struct UnelidableRscope(Option<Vec<ElisionFailureInfo>>);
157 impl UnelidableRscope {
158 pub fn new(v: Option<Vec<ElisionFailureInfo>>) -> UnelidableRscope {
163 impl RegionScope for UnelidableRscope {
164 fn anon_regions(&self,
167 -> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>> {
168 let UnelidableRscope(ref v) = *self;
172 fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
173 Some(self.base_object_lifetime_default(span))
176 fn base_object_lifetime_default(&self, _span: Span) -> ty::Region {
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 {
189 impl ElidableRscope {
190 pub fn new(r: ty::Region) -> ElidableRscope {
191 ElidableRscope { default: r }
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
200 Some(self.base_object_lifetime_default(span))
203 fn base_object_lifetime_default(&self, _span: Span) -> ty::Region {
207 fn anon_regions(&self,
210 -> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>>
212 Ok(vec![self.default; count])
216 /// A scope that behaves as an ElidabeRscope with a `'static` default region
217 /// that should also warn if the `static_in_const` feature is unset.
218 #[derive(Copy, Clone)]
219 pub struct StaticRscope<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
220 tcx: &'a ty::TyCtxt<'a, 'gcx, 'tcx>,
223 impl<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> StaticRscope<'a, 'gcx, 'tcx> {
224 /// create a new StaticRscope from a reference to the `TyCtxt`
225 pub fn new(tcx: &'a ty::TyCtxt<'a, 'gcx, 'tcx>) -> Self {
226 StaticRscope { tcx: tcx }
230 impl<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> RegionScope for StaticRscope<'a, 'gcx, 'tcx> {
231 fn anon_regions(&self,
234 -> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>> {
235 if !self.tcx.sess.features.borrow().static_in_const {
238 .struct_span_err(span,
239 "this needs a `'static` lifetime or the \
240 `static_in_const` feature, see #35897")
243 Ok(vec![ty::ReStatic; count])
246 fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
247 Some(self.base_object_lifetime_default(span))
250 fn base_object_lifetime_default(&self, _span: Span) -> ty::Region {
255 /// A scope in which we generate anonymous, late-bound regions for
256 /// omitted regions. This occurs in function signatures.
257 pub struct BindingRscope {
258 anon_bindings: Cell<u32>,
262 pub fn new() -> BindingRscope {
264 anon_bindings: Cell::new(0),
268 fn next_region(&self) -> ty::Region {
269 let idx = self.anon_bindings.get();
270 self.anon_bindings.set(idx + 1);
271 ty::ReLateBound(ty::DebruijnIndex::new(1), ty::BrAnon(idx))
275 impl RegionScope for BindingRscope {
276 fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
277 // Per RFC #599, object-lifetimes default to 'static unless
278 // overridden by context, and this takes precedence over the
279 // binding defaults in a fn signature.
280 Some(self.base_object_lifetime_default(span))
283 fn base_object_lifetime_default(&self, _span: Span) -> ty::Region {
287 fn anon_regions(&self,
290 -> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>>
292 Ok((0..count).map(|_| self.next_region()).collect())
296 /// A scope which overrides the default object lifetime but has no other effect.
297 pub struct ObjectLifetimeDefaultRscope<'r> {
298 base_scope: &'r (RegionScope+'r),
299 default: ty::ObjectLifetimeDefault<'r>,
302 impl<'r> ObjectLifetimeDefaultRscope<'r> {
303 pub fn new(base_scope: &'r (RegionScope+'r),
304 default: ty::ObjectLifetimeDefault<'r>)
305 -> ObjectLifetimeDefaultRscope<'r>
307 ObjectLifetimeDefaultRscope {
308 base_scope: base_scope,
314 impl<'r> RegionScope for ObjectLifetimeDefaultRscope<'r> {
315 fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
317 ty::ObjectLifetimeDefault::Ambiguous =>
320 ty::ObjectLifetimeDefault::BaseDefault =>
321 // NB: This behavior changed in Rust 1.3.
322 Some(self.base_object_lifetime_default(span)),
324 ty::ObjectLifetimeDefault::Specific(r) =>
329 fn base_object_lifetime_default(&self, span: Span) -> ty::Region {
330 self.base_scope.base_object_lifetime_default(span)
333 fn anon_regions(&self,
336 -> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>>
338 self.base_scope.anon_regions(span, count)
341 fn anon_type_scope(&self) -> Option<AnonTypeScope> {
342 self.base_scope.anon_type_scope()
346 /// A scope which simply shifts the Debruijn index of other scopes
347 /// to account for binding levels.
348 pub struct ShiftedRscope<'r> {
349 base_scope: &'r (RegionScope+'r)
352 impl<'r> ShiftedRscope<'r> {
353 pub fn new(base_scope: &'r (RegionScope+'r)) -> ShiftedRscope<'r> {
354 ShiftedRscope { base_scope: base_scope }
358 impl<'r> RegionScope for ShiftedRscope<'r> {
359 fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
360 self.base_scope.object_lifetime_default(span)
361 .map(|r| ty::fold::shift_region(r, 1))
364 fn base_object_lifetime_default(&self, span: Span) -> ty::Region {
365 ty::fold::shift_region(self.base_scope.base_object_lifetime_default(span), 1)
368 fn anon_regions(&self,
371 -> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>>
373 match self.base_scope.anon_regions(span, count) {
376 *r = ty::fold::shift_region(*r, 1);
386 fn anon_type_scope(&self) -> Option<AnonTypeScope> {
387 self.base_scope.anon_type_scope()