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 impl<'tcx> TotalEq for TypeStructure<'tcx> {}
47 struct TypeContext<'tcx, 'ast> {
48 ty_arena: &'tcx Arena,
49 types: Vec<Type<'tcx>> ,
50 type_table: HashMap<NodeId, Type<'tcx>>,
52 ast_arena: &'ast Arena,
56 impl<'tcx,'ast> TypeContext<'tcx, 'ast> {
57 fn new(ty_arena: &'tcx Arena, ast_arena: &'ast Arena)
58 -> TypeContext<'tcx, 'ast> {
59 TypeContext { ty_arena: ty_arena,
61 type_table: HashMap::new(),
67 fn add_type(&mut self, s: TypeStructure<'tcx>) -> Type<'tcx> {
68 for &ty in self.types.iter() {
74 let ty = self.ty_arena.alloc(|| s);
79 fn set_type(&mut self, id: NodeId, ty: Type<'tcx>) -> Type<'tcx> {
80 self.type_table.insert(id, ty);
84 fn ast(&mut self, a: AstKind<'ast>) -> Ast<'ast> {
85 let id = self.ast_counter;
86 self.ast_counter += 1;
87 self.ast_arena.alloc(|| AstStructure { id: NodeId {id:id}, kind: a })
91 #[deriving(Eq, TotalEq, Hash)]
96 type Ast<'ast> = &'ast AstStructure<'ast>;
98 struct AstStructure<'ast> {
106 ExprLambda(Ast<'ast>),
109 fn compute_types<'tcx,'ast>(tcx: &mut TypeContext<'tcx,'ast>,
110 ast: Ast<'ast>) -> Type<'tcx>
113 ExprInt | ExprVar(_) => {
114 let ty = tcx.add_type(TypeInt);
115 tcx.set_type(ast.id, ty)
118 let arg_ty = tcx.add_type(TypeInt);
119 let body_ty = compute_types(tcx, ast);
120 let lambda_ty = tcx.add_type(TypeFunction(arg_ty, body_ty));
121 tcx.set_type(ast.id, lambda_ty)
127 let ty_arena = arena::Arena::new();
128 let ast_arena = arena::Arena::new();
129 let mut tcx = TypeContext::new(&ty_arena, &ast_arena);
130 let ast = tcx.ast(ExprInt);
131 let ty = compute_types(&mut tcx, ast);
132 assert_eq!(*ty, TypeInt);