Skip to content

Commit f4f4a35

Browse files
committed
Add new tests showing multiple lifetime parameters in use
1 parent f36a891 commit f4f4a35

3 files changed

+165
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
// Test that a type which is covariant with respect to its region
12+
// parameter yields an error when used in a contravariant way.
13+
//
14+
// Note: see variance-regions-*.rs for the tests that check that the
15+
// variance inference works in the first place.
16+
17+
// `S` is contravariant with respect to both parameters.
18+
struct S<'a, 'b> {
19+
f: &'a int,
20+
g: &'b int,
21+
}
22+
23+
fn use_<'short,'long>(c: S<'long, 'short>,
24+
s: &'short int,
25+
l: &'long int,
26+
_where:Option<&'short &'long ()>) {
27+
28+
let _: S<'long, 'short> = c; // OK
29+
let _: S<'short, 'short> = c; // OK
30+
31+
// Test whether S<_,'short> <: S<_,'long>. Since
32+
// 'short <= 'long, this would be true if the Contravariant type were
33+
// covariant with respect to its parameter 'a.
34+
35+
let _: S<'long, 'long> = c; //~ ERROR mismatched types
36+
//~^ ERROR cannot infer an appropriate lifetime
37+
}
38+
39+
fn main() {}

src/test/compile-fail/regions-variance-contravariant-use-covariant.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
// Note: see variance-regions-*.rs for the tests that check that the
1515
// variance inference works in the first place.
1616

17-
// This is covariant with respect to 'a, meaning that
18-
// Covariant<'foo> <: Covariant<'static> because
17+
// This is contravariant with respect to 'a, meaning that
18+
// Contravariant<'foo> <: Contravariant<'static> because
1919
// 'foo <= 'static
2020
struct Contravariant<'a> {
2121
f: &'a int

src/test/run-pass/regions-mock-tcx.rs

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
// Test a sample usage pattern for regions. Makes use of the
12+
// following features:
13+
//
14+
// - Multiple lifetime parameters
15+
// - Arenas
16+
17+
extern mod extra;
18+
19+
use extra::arena;
20+
use extra::arena::Arena;
21+
use std::hashmap::HashMap;
22+
use std::cast;
23+
use std::libc;
24+
use std::mem;
25+
26+
type Type<'tcx> = &'tcx TypeStructure<'tcx>;
27+
28+
#[deriving(Eq)]
29+
enum TypeStructure<'tcx> {
30+
TypeInt,
31+
TypeFunction(Type<'tcx>, Type<'tcx>),
32+
}
33+
34+
struct TypeContext<'tcx, 'ast> {
35+
ty_arena: &'tcx Arena,
36+
types: ~[Type<'tcx>],
37+
type_table: HashMap<NodeId, Type<'tcx>>,
38+
39+
ast_arena: &'ast Arena,
40+
ast_counter: uint,
41+
}
42+
43+
impl<'tcx,'ast> TypeContext<'tcx, 'ast> {
44+
fn new(ty_arena: &'tcx Arena, ast_arena: &'ast Arena)
45+
-> TypeContext<'tcx, 'ast> {
46+
TypeContext { ty_arena: ty_arena,
47+
types: ~[],
48+
type_table: HashMap::new(),
49+
50+
ast_arena: ast_arena,
51+
ast_counter: 0 }
52+
}
53+
54+
fn add_type(&mut self, s: TypeStructure<'tcx>) -> Type<'tcx> {
55+
for &ty in self.types.iter() {
56+
if *ty == s {
57+
return ty;
58+
}
59+
}
60+
61+
let ty = self.ty_arena.alloc(|| s);
62+
self.types.push(ty);
63+
ty
64+
}
65+
66+
fn set_type(&mut self, id: NodeId, ty: Type<'tcx>) -> Type<'tcx> {
67+
self.type_table.insert(id, ty);
68+
ty
69+
}
70+
71+
fn ast(&mut self, a: AstKind<'ast>) -> Ast<'ast> {
72+
let id = self.ast_counter;
73+
self.ast_counter += 1;
74+
self.ast_arena.alloc(|| AstStructure { id: NodeId {id:id}, kind: a })
75+
}
76+
}
77+
78+
#[deriving(Eq, IterBytes)]
79+
struct NodeId {
80+
id: uint
81+
}
82+
83+
type Ast<'ast> = &'ast AstStructure<'ast>;
84+
85+
struct AstStructure<'ast> {
86+
id: NodeId,
87+
kind: AstKind<'ast>
88+
}
89+
90+
enum AstKind<'ast> {
91+
ExprInt,
92+
ExprVar(uint),
93+
ExprLambda(Ast<'ast>),
94+
// ...
95+
}
96+
97+
fn compute_types<'tcx,'ast>(tcx: &mut TypeContext<'tcx,'ast>,
98+
ast: Ast<'ast>) -> Type<'tcx>
99+
{
100+
match ast.kind {
101+
ExprInt | ExprVar(_) => {
102+
let ty = tcx.add_type(TypeInt);
103+
tcx.set_type(ast.id, ty)
104+
}
105+
106+
ExprLambda(ast) => {
107+
let arg_ty = tcx.add_type(TypeInt);
108+
let body_ty = compute_types(tcx, ast);
109+
let lambda_ty = tcx.add_type(TypeFunction(arg_ty, body_ty));
110+
tcx.set_type(ast.id, lambda_ty)
111+
}
112+
113+
// ...
114+
}
115+
}
116+
117+
pub fn main() {
118+
let ty_arena = arena::Arena::new();
119+
let ast_arena = arena::Arena::new();
120+
let mut tcx = TypeContext::new(&ty_arena, &ast_arena);
121+
let ast = tcx.ast(ExprInt);
122+
let ty = compute_types(&mut tcx, ast);
123+
assert_eq!(*ty, TypeInt);
124+
}

0 commit comments

Comments
 (0)