1 // Copyright 2012-2014 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 // ignore-fast `use` standards don't resolve
13 // Test a sample usage pattern for regions. Makes use of the
14 // following features:
16 // - Multiple lifetime parameters
20 extern crate collections;
23 use collections::HashMap;
28 type Type<'tcx> = &'tcx TypeStructure<'tcx>;
31 enum TypeStructure<'tcx> {
33 TypeFunction(Type<'tcx>, Type<'tcx>),
35 impl<'tcx> Eq for TypeStructure<'tcx> {
36 fn eq(&self, other: &TypeStructure<'tcx>) -> bool {
37 match (*self, *other) {
38 (TypeInt, TypeInt) => true,
39 (TypeFunction(s_a, s_b), TypeFunction(o_a, o_b)) => *s_a == *o_a && *s_b == *o_b,
45 struct TypeContext<'tcx, 'ast> {
46 ty_arena: &'tcx Arena,
47 types: Vec<Type<'tcx>> ,
48 type_table: HashMap<NodeId, Type<'tcx>>,
50 ast_arena: &'ast Arena,
54 impl<'tcx,'ast> TypeContext<'tcx, 'ast> {
55 fn new(ty_arena: &'tcx Arena, ast_arena: &'ast Arena)
56 -> TypeContext<'tcx, 'ast> {
57 TypeContext { ty_arena: ty_arena,
59 type_table: HashMap::new(),
65 fn add_type(&mut self, s: TypeStructure<'tcx>) -> Type<'tcx> {
66 for &ty in self.types.iter() {
72 let ty = self.ty_arena.alloc(|| s);
77 fn set_type(&mut self, id: NodeId, ty: Type<'tcx>) -> Type<'tcx> {
78 self.type_table.insert(id, ty);
82 fn ast(&mut self, a: AstKind<'ast>) -> Ast<'ast> {
83 let id = self.ast_counter;
84 self.ast_counter += 1;
85 self.ast_arena.alloc(|| AstStructure { id: NodeId {id:id}, kind: a })
94 type Ast<'ast> = &'ast AstStructure<'ast>;
96 struct AstStructure<'ast> {
104 ExprLambda(Ast<'ast>),
107 fn compute_types<'tcx,'ast>(tcx: &mut TypeContext<'tcx,'ast>,
108 ast: Ast<'ast>) -> Type<'tcx>
111 ExprInt | ExprVar(_) => {
112 let ty = tcx.add_type(TypeInt);
113 tcx.set_type(ast.id, ty)
116 let arg_ty = tcx.add_type(TypeInt);
117 let body_ty = compute_types(tcx, ast);
118 let lambda_ty = tcx.add_type(TypeFunction(arg_ty, body_ty));
119 tcx.set_type(ast.id, lambda_ty)
125 let ty_arena = arena::Arena::new();
126 let ast_arena = arena::Arena::new();
127 let mut tcx = TypeContext::new(&ty_arena, &ast_arena);
128 let ast = tcx.ast(ExprInt);
129 let ty = compute_types(&mut tcx, ast);
130 assert_eq!(*ty, TypeInt);