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.
12 use rustc::hir::def_id::DefId;
14 use rustc::ty::subst::Substs;
22 pub struct ElisionFailureInfo {
23 /// Where we can find the argument pattern.
24 pub parent: Option<hir::BodyId>,
25 /// The index of the argument in the original definition.
27 pub lifetime_count: usize,
28 pub have_bound_regions: bool
31 pub type ElidedLifetime = Result<ty::Region, Option<Vec<ElisionFailureInfo>>>;
33 /// Defines strategies for handling regions that are omitted. For
34 /// example, if one writes the type `&Foo`, then the lifetime of
35 /// this reference has been omitted. When converting this
36 /// type, the generic functions in astconv will invoke `anon_region`
37 /// on the provided region-scope to decide how to translate this
40 /// It is not always legal to omit regions, therefore `anon_region`
41 /// can return `Err(())` to indicate that this is not a scope in which
42 /// regions can legally be omitted.
43 pub trait RegionScope {
44 fn anon_region(&self, span: Span)
45 -> Result<ty::Region, Option<Vec<ElisionFailureInfo>>>;
47 /// If an object omits any explicit lifetime bound, and none can
48 /// be derived from the object traits, what should we use? If
49 /// `None` is returned, an explicit annotation is required.
50 fn object_lifetime_default(&self, span: Span) -> Option<ty::Region>;
52 /// The "base" default is the initial default for a scope. This is
53 /// 'static except for in fn bodies, where it is a fresh inference
54 /// variable. You shouldn't call this except for as part of
55 /// computing `object_lifetime_default` (in particular, in legacy
56 /// modes, it may not be relevant).
57 fn base_object_lifetime_default(&self, span: Span) -> ty::Region;
59 /// If this scope allows anonymized types, return the generics in
60 /// scope, that anonymized types will close over. For example,
61 /// if you have a function like:
63 /// fn foo<'a, T>() -> impl Trait { ... }
65 /// then, for the rscope that is used when handling the return type,
66 /// `anon_type_scope()` would return a `Some(AnonTypeScope {...})`,
67 /// on which `.fresh_substs(...)` can be used to obtain identity
68 /// Substs for `'a` and `T`, to track them in `TyAnon`. This property
69 /// is controlled by the region scope because it's fine-grained enough
70 /// to allow restriction of anonymized types to the syntactical extent
71 /// of a function's return type.
72 fn anon_type_scope(&self) -> Option<AnonTypeScope> {
77 #[derive(Copy, Clone)]
78 pub struct AnonTypeScope {
82 impl<'gcx: 'tcx, 'tcx> AnonTypeScope {
83 pub fn new(enclosing_item: DefId) -> AnonTypeScope {
85 enclosing_item: enclosing_item
89 pub fn fresh_substs(&self, astconv: &AstConv<'gcx, 'tcx>, span: Span)
90 -> &'tcx Substs<'tcx> {
91 use collect::mk_item_substs;
93 mk_item_substs(astconv, span, self.enclosing_item)
97 /// A scope wrapper which optionally allows anonymized types.
98 #[derive(Copy, Clone)]
99 pub struct MaybeWithAnonTypes<R> {
101 anon_scope: Option<AnonTypeScope>
104 impl<R: RegionScope> MaybeWithAnonTypes<R> {
105 pub fn new(base_scope: R, anon_scope: Option<AnonTypeScope>) -> Self {
107 base_scope: base_scope,
108 anon_scope: anon_scope
113 impl<R: RegionScope> RegionScope for MaybeWithAnonTypes<R> {
114 fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
115 self.base_scope.object_lifetime_default(span)
118 fn anon_region(&self, span: Span)
119 -> Result<ty::Region, Option<Vec<ElisionFailureInfo>>> {
120 self.base_scope.anon_region(span)
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_region(&self, _span: Span)
139 -> Result<ty::Region, Option<Vec<ElisionFailureInfo>>> {
143 fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
144 Some(self.base_object_lifetime_default(span))
147 fn base_object_lifetime_default(&self, _span: Span) -> ty::Region {
152 // Same as `ExplicitRscope`, but provides some extra information for diagnostics
153 pub struct UnelidableRscope(Option<Vec<ElisionFailureInfo>>);
155 impl UnelidableRscope {
156 pub fn new(v: Option<Vec<ElisionFailureInfo>>) -> UnelidableRscope {
161 impl RegionScope for UnelidableRscope {
162 fn anon_region(&self, _span: Span)
163 -> Result<ty::Region, Option<Vec<ElisionFailureInfo>>> {
167 fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
168 Some(self.base_object_lifetime_default(span))
171 fn base_object_lifetime_default(&self, _span: Span) -> ty::Region {
176 // A scope in which omitted anonymous region defaults to
177 // `default`. This is used after the `->` in function signatures. The
178 // latter use may go away. Note that object-lifetime defaults work a
179 // bit differently, as specified in RFC #599.
180 pub struct ElidableRscope {
184 impl ElidableRscope {
185 pub fn new(r: ty::Region) -> ElidableRscope {
186 ElidableRscope { default: r }
190 impl RegionScope for ElidableRscope {
191 fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
192 // Per RFC #599, object-lifetimes default to 'static unless
193 // overridden by context, and this takes precedence over
195 Some(self.base_object_lifetime_default(span))
198 fn base_object_lifetime_default(&self, _span: Span) -> ty::Region {
202 fn anon_region(&self, _span: Span)
203 -> Result<ty::Region, Option<Vec<ElisionFailureInfo>>>
209 /// A scope that behaves as an ElidabeRscope with a `'static` default region
210 /// that should also warn if the `static_in_const` feature is unset.
211 #[derive(Copy, Clone)]
212 pub struct StaticRscope<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
213 tcx: &'a ty::TyCtxt<'a, 'gcx, 'tcx>,
216 impl<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> StaticRscope<'a, 'gcx, 'tcx> {
217 /// create a new StaticRscope from a reference to the `TyCtxt`
218 pub fn new(tcx: &'a ty::TyCtxt<'a, 'gcx, 'tcx>) -> Self {
219 StaticRscope { tcx: tcx }
223 impl<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> RegionScope for StaticRscope<'a, 'gcx, 'tcx> {
224 fn anon_region(&self, span: Span)
225 -> Result<ty::Region, Option<Vec<ElisionFailureInfo>>> {
226 if !self.tcx.sess.features.borrow().static_in_const {
229 .struct_span_err(span,
230 "this needs a `'static` lifetime or the \
231 `static_in_const` feature, see #35897")
237 fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
238 Some(self.base_object_lifetime_default(span))
241 fn base_object_lifetime_default(&self, _span: Span) -> ty::Region {
246 /// A scope in which we generate anonymous, late-bound regions for
247 /// omitted regions. This occurs in function signatures.
248 pub struct BindingRscope {
249 anon_bindings: Cell<u32>,
253 pub fn new() -> BindingRscope {
255 anon_bindings: Cell::new(0),
260 impl RegionScope for BindingRscope {
261 fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
262 // Per RFC #599, object-lifetimes default to 'static unless
263 // overridden by context, and this takes precedence over the
264 // binding defaults in a fn signature.
265 Some(self.base_object_lifetime_default(span))
268 fn base_object_lifetime_default(&self, _span: Span) -> ty::Region {
272 fn anon_region(&self, _: Span)
273 -> Result<ty::Region, Option<Vec<ElisionFailureInfo>>>
275 let idx = self.anon_bindings.get();
276 self.anon_bindings.set(idx + 1);
277 Ok(ty::ReLateBound(ty::DebruijnIndex::new(1), ty::BrAnon(idx)))
281 /// A scope which overrides the default object lifetime but has no other effect.
282 pub struct ObjectLifetimeDefaultRscope<'r> {
283 base_scope: &'r (RegionScope+'r),
284 default: ty::ObjectLifetimeDefault<'r>,
287 impl<'r> ObjectLifetimeDefaultRscope<'r> {
288 pub fn new(base_scope: &'r (RegionScope+'r),
289 default: ty::ObjectLifetimeDefault<'r>)
290 -> ObjectLifetimeDefaultRscope<'r>
292 ObjectLifetimeDefaultRscope {
293 base_scope: base_scope,
299 impl<'r> RegionScope for ObjectLifetimeDefaultRscope<'r> {
300 fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
302 ty::ObjectLifetimeDefault::Ambiguous =>
305 ty::ObjectLifetimeDefault::BaseDefault =>
306 // NB: This behavior changed in Rust 1.3.
307 Some(self.base_object_lifetime_default(span)),
309 ty::ObjectLifetimeDefault::Specific(r) =>
314 fn base_object_lifetime_default(&self, span: Span) -> ty::Region {
315 self.base_scope.base_object_lifetime_default(span)
318 fn anon_region(&self, span: Span)
319 -> Result<ty::Region, Option<Vec<ElisionFailureInfo>>>
321 self.base_scope.anon_region(span)
324 fn anon_type_scope(&self) -> Option<AnonTypeScope> {
325 self.base_scope.anon_type_scope()
329 /// A scope which simply shifts the Debruijn index of other scopes
330 /// to account for binding levels.
331 pub struct ShiftedRscope<'r> {
332 base_scope: &'r (RegionScope+'r)
335 impl<'r> ShiftedRscope<'r> {
336 pub fn new(base_scope: &'r (RegionScope+'r)) -> ShiftedRscope<'r> {
337 ShiftedRscope { base_scope: base_scope }
341 impl<'r> RegionScope for ShiftedRscope<'r> {
342 fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
343 self.base_scope.object_lifetime_default(span)
344 .map(|r| ty::fold::shift_region(r, 1))
347 fn base_object_lifetime_default(&self, span: Span) -> ty::Region {
348 ty::fold::shift_region(self.base_scope.base_object_lifetime_default(span), 1)
351 fn anon_region(&self, span: Span)
352 -> Result<ty::Region, Option<Vec<ElisionFailureInfo>>>
354 self.base_scope.anon_region(span).map(|r| ty::fold::shift_region(r, 1))
357 fn anon_type_scope(&self) -> Option<AnonTypeScope> {
358 self.base_scope.anon_type_scope()