4 #![allow(unused_imports)]
6 // Test a sample usage pattern for regions. Makes use of the
9 // - Multiple lifetime parameters
12 #![feature(rustc_private, libc)]
14 extern crate rustc_arena;
17 use TypeStructure::{TypeInt, TypeFunction};
18 use AstKind::{ExprInt, ExprVar, ExprLambda};
19 use rustc_arena::TypedArena;
20 use std::collections::HashMap;
23 type Type<'tcx> = &'tcx TypeStructure<'tcx>;
25 #[derive(Copy, Clone, Debug)]
26 enum TypeStructure<'tcx> {
28 TypeFunction(Type<'tcx>, Type<'tcx>),
31 impl<'tcx> PartialEq for TypeStructure<'tcx> {
32 fn eq(&self, other: &TypeStructure<'tcx>) -> bool {
33 match (*self, *other) {
34 (TypeInt, TypeInt) => true,
35 (TypeFunction(s_a, s_b), TypeFunction(o_a, o_b)) => *s_a == *o_a && *s_b == *o_b,
41 impl<'tcx> Eq for TypeStructure<'tcx> {}
43 type TyArena<'tcx> = TypedArena<TypeStructure<'tcx>>;
44 type AstArena<'ast> = TypedArena<AstStructure<'ast>>;
46 struct TypeContext<'tcx, 'ast> {
47 ty_arena: &'tcx TyArena<'tcx>,
48 types: Vec<Type<'tcx>> ,
49 type_table: HashMap<NodeId, Type<'tcx>>,
51 ast_arena: &'ast AstArena<'ast>,
55 impl<'tcx,'ast> TypeContext<'tcx, 'ast> {
56 fn new(ty_arena: &'tcx TyArena<'tcx>, ast_arena: &'ast AstArena<'ast>)
57 -> TypeContext<'tcx, 'ast> {
58 TypeContext { ty_arena: ty_arena,
60 type_table: HashMap::new(),
66 fn add_type(&mut self, s: TypeStructure<'tcx>) -> Type<'tcx> {
67 for &ty in &self.types {
73 let ty = self.ty_arena.alloc(s);
78 fn set_type(&mut self, id: NodeId, ty: Type<'tcx>) -> Type<'tcx> {
79 self.type_table.insert(id, ty);
83 fn ast(&mut self, a: AstKind<'ast>) -> Ast<'ast> {
84 let id = self.ast_counter;
85 self.ast_counter += 1;
86 self.ast_arena.alloc(AstStructure { id: NodeId {id:id}, kind: a })
90 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
95 type Ast<'ast> = &'ast AstStructure<'ast>;
97 #[derive(Copy, Clone)]
98 struct AstStructure<'ast> {
103 #[derive(Copy, Clone)]
107 ExprLambda(Ast<'ast>),
110 fn compute_types<'tcx,'ast>(tcx: &mut TypeContext<'tcx,'ast>,
111 ast: Ast<'ast>) -> Type<'tcx>
114 ExprInt | ExprVar(_) => {
115 let ty = tcx.add_type(TypeInt);
116 tcx.set_type(ast.id, ty)
119 let arg_ty = tcx.add_type(TypeInt);
120 let body_ty = compute_types(tcx, ast);
121 let lambda_ty = tcx.add_type(TypeFunction(arg_ty, body_ty));
122 tcx.set_type(ast.id, lambda_ty)
128 let ty_arena = TypedArena::default();
129 let ast_arena = TypedArena::default();
130 let mut tcx = TypeContext::new(&ty_arena, &ast_arena);
131 let ast = tcx.ast(ExprInt);
132 let ty = compute_types(&mut tcx, ast);
133 assert_eq!(*ty, TypeInt);