parent
3abe3671bd
commit
29177864c3
9
src/test/compile-fail/ret-by-reference-from-temporary.rs
Normal file
9
src/test/compile-fail/ret-by-reference-from-temporary.rs
Normal file
@ -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});
|
||||
}
|
8
src/test/compile-fail/ret-by-reference-local-value.rs
Normal file
8
src/test/compile-fail/ret-by-reference-local-value.rs
Normal file
@ -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() {}
|
7
src/test/compile-fail/ret-by-reference-mutable-field.rs
Normal file
7
src/test/compile-fail/ret-by-reference-mutable-field.rs
Normal file
@ -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() {}
|
12
src/test/compile-fail/ret-by-reference-safety-1.rs
Normal file
12
src/test/compile-fail/ret-by-reference-safety-1.rs
Normal file
@ -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;
|
||||
}
|
12
src/test/compile-fail/ret-by-reference-safety-2.rs
Normal file
12
src/test/compile-fail/ret-by-reference-safety-2.rs
Normal file
@ -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;
|
||||
}
|
7
src/test/compile-fail/ret-by-reference-specify-param.rs
Normal file
7
src/test/compile-fail/ret-by-reference-specify-param.rs
Normal file
@ -0,0 +1,7 @@
|
||||
// error-pattern:must specify referenced parameter
|
||||
|
||||
fn f(a: int, b: int) -> &int {
|
||||
ret a;
|
||||
}
|
||||
|
||||
fn main() {}
|
7
src/test/compile-fail/ret-by-reference-temporary.rs
Normal file
7
src/test/compile-fail/ret-by-reference-temporary.rs
Normal file
@ -0,0 +1,7 @@
|
||||
// error-pattern:can not return a reference to a temporary
|
||||
|
||||
fn f(a: int) -> &int {
|
||||
ret 10;
|
||||
}
|
||||
|
||||
fn main() {}
|
7
src/test/compile-fail/ret-by-reference-wrong-param.rs
Normal file
7
src/test/compile-fail/ret-by-reference-wrong-param.rs
Normal file
@ -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() {}
|
23
src/test/run-pass/ret-by-reference.rs
Normal file
23
src/test/run-pass/ret-by-reference.rs
Normal file
@ -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;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user