Add tests for returning by reference

Issue 
This commit is contained in:
Marijn Haverbeke 2011-09-15 16:37:37 +02:00
parent 3abe3671bd
commit 29177864c3
9 changed files with 92 additions and 0 deletions

@ -0,0 +1,9 @@
// error-pattern:a reference binding can't be rooted in a temporary
fn f(a: {x: int}) -> &int {
ret a.x;
}
fn main() {
let &_a = f({x: 4});
}

@ -0,0 +1,8 @@
// error-pattern:can not return a reference to a function-local value
fn f(a: {mutable x: int}) -> &int {
let x = {y: 4};
ret x.y;
}
fn main() {}

@ -0,0 +1,7 @@
// error-pattern:can not return a reference to a mutable field
fn f(a: {mutable x: int}) -> &int {
ret a.x;
}
fn main() {}

@ -0,0 +1,12 @@
// error-pattern:taking the value of x will invalidate reference a
fn f(a: {mutable x: int}) -> &!int {
ret a.x;
}
fn main() {
let x = {mutable x: 4};
let &a = f(x);
x;
a;
}

@ -0,0 +1,12 @@
// error-pattern:overwriting x will invalidate reference a
fn f(a: {x: {mutable x: int}}) -> &{mutable x: int} {
ret a.x;
}
fn main() {
let x = {x: {mutable x: 4}};
let &a = f(x);
x = {x: {mutable x: 5}};
a;
}

@ -0,0 +1,7 @@
// error-pattern:must specify referenced parameter
fn f(a: int, b: int) -> &int {
ret a;
}
fn main() {}

@ -0,0 +1,7 @@
// error-pattern:can not return a reference to a temporary
fn f(a: int) -> &int {
ret 10;
}
fn main() {}

@ -0,0 +1,7 @@
// error-pattern:can not return a reference to the wrong parameter
fn f(a: int, b: int) -> &1 int {
ret a;
}
fn main() {}

@ -0,0 +1,23 @@
tag option<T> { some(T); none; }
fn get<@T>(opt: option<T>) -> &T {
alt opt {
some(x) { ret x; }
}
}
fn get_mut(a: {mutable x: @int}, _b: int) -> &!0 @int {
ret a.x;
}
fn main() {
let x = some(@50);
let &y = get(x);
assert *y == 50;
assert get(some(10)) == 10;
let y = {mutable x: @50};
let &box = get_mut(y, 4);
assert *box == 50;
assert *get_mut({mutable x: @70}, 5) == 70;
}