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
12 use middle::ty;
13
14 use std::cell::Cell;
15 use syntax::ast;
16 use syntax::codemap::Span;
17
18 /// Defines strategies for handling regions that are omitted. For
19 /// example, if one writes the type `&Foo`, then the lifetime of
20 /// this reference has been omitted. When converting this
21 /// type, the generic functions in astconv will invoke `anon_regions`
22 /// on the provided region-scope to decide how to translate this
23 /// omitted region.
24 ///
25 /// It is not always legal to omit regions, therefore `anon_regions`
26 /// can return `Err(())` to indicate that this is not a scope in which
27 /// regions can legally be omitted.
28 pub trait RegionScope {
29 fn anon_regions(&self,
30 span: Span,
31 count: uint)
32 -> Result<Vec<ty::Region> , ()>;
33 }
34
35 // A scope in which all regions must be explicitly named
36 pub struct ExplicitRscope;
37
38 impl RegionScope for ExplicitRscope {
39 fn anon_regions(&self,
40 _span: Span,
41 _count: uint)
42 -> Result<Vec<ty::Region> , ()> {
43 Err(())
44 }
45 }
46
47 /// A scope in which we generate anonymous, late-bound regions for
48 /// omitted regions. This occurs in function signatures.
49 pub struct BindingRscope {
50 binder_id: ast::NodeId,
51 anon_bindings: Cell<uint>,
52 }
53
54 impl BindingRscope {
55 pub fn new(binder_id: ast::NodeId) -> BindingRscope {
56 BindingRscope {
57 binder_id: binder_id,
58 anon_bindings: Cell::new(0),
59 }
60 }
61 }
62
63 impl RegionScope for BindingRscope {
64 fn anon_regions(&self,
65 _: Span,
66 count: uint)
67 -> Result<Vec<ty::Region> , ()> {
68 let idx = self.anon_bindings.get();
69 self.anon_bindings.set(idx + count);
70 Ok(Vec::from_fn(count, |i| ty::ReLateBound(self.binder_id,
71 ty::BrAnon(idx + i))))
72 }
73 }
librustc/middle/typeck/rscope.rs:27:36-27:36 -trait- definition:
/// regions can legally be omitted.
pub trait RegionScope {
fn anon_regions(&self,
references:- 1563: impl RegionScope for BindingRscope {
64: fn anon_regions(&self,
librustc/middle/typeck/astconv.rs:
246: pub fn ast_path_to_substs_and_ty<AC:AstConv,
247: RS:RegionScope>(
248: this: &AC,
--
532: // internal notion of a type.
533: pub fn ast_ty_to_ty<AC:AstConv, RS:RegionScope>(
534: this: &AC, rscope: &RS, ast_ty: &ast::Ty) -> ty::t {
--
728: pub fn ty_of_arg<AC: AstConv, RS: RegionScope>(this: &AC, rscope: &RS, a: &ast::Arg,
729: expected_ty: Option<ty::t>) -> ty::t {
librustc/middle/typeck/collect.rs:
100: pub trait ToTy {
101: fn to_ty<RS:RegionScope>(&self, rs: &RS, ast_ty: &ast::Ty) -> ty::t;
102: }
--
104: impl<'a> ToTy for CrateCtxt<'a> {
105: fn to_ty<RS:RegionScope>(&self, rs: &RS, ast_ty: &ast::Ty) -> ty::t {
106: ast_ty_to_ty(self, rs, ast_ty)
librustc/middle/typeck/check/mod.rs:
1082: impl<'a> RegionScope for infer::InferCtxt<'a> {
1083: fn anon_regions(&self, span: Span, count: uint)
librustc/middle/typeck/astconv.rs:
462: fn mk_pointer<AC:AstConv,
463: RS:RegionScope>(
464: this: &AC,
librustc/middle/typeck/rscope.rs:48:57-48:57 -struct- definition:
/// omitted regions. This occurs in function signatures.
pub struct BindingRscope {
binder_id: ast::NodeId,
references:- 463: impl RegionScope for BindingRscope {
64: fn anon_regions(&self,