rustc: add rooting, write-guards to slices etc
This commit is contained in:
parent
f3a6ea2643
commit
be08c3e514
12 changed files with 194 additions and 32 deletions
|
|
@ -1,5 +1,8 @@
|
|||
// error-pattern:borrowed
|
||||
|
||||
// Test that write guards trigger when there is a write to a field
|
||||
// of a frozen structure.
|
||||
|
||||
struct S {
|
||||
x: int
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
// error-pattern:borrowed
|
||||
|
||||
// Test that write guards trigger when there is a write to a directly
|
||||
// frozen @mut box.
|
||||
|
||||
fn main() {
|
||||
let x = @mut 3;
|
||||
let y: &mut int = x;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
// error-pattern:borrowed
|
||||
|
||||
// Test that write guards trigger when mut box is frozen
|
||||
// as part of argument coercion.
|
||||
|
||||
fn f(_x: &int, y: @mut int) {
|
||||
*y = 2;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,37 @@
|
|||
// error-pattern:borrowed
|
||||
|
||||
// Test that write guards trigger when there is a coercion to
|
||||
// a slice on the receiver of a method.
|
||||
|
||||
trait MyMutSlice {
|
||||
fn my_mut_slice(self) -> Self;
|
||||
}
|
||||
|
||||
impl<'self, T> MyMutSlice for &'self mut [T] {
|
||||
fn my_mut_slice(self) -> &'self mut [T] {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
trait MySlice {
|
||||
fn my_slice(self) -> Self;
|
||||
}
|
||||
|
||||
impl<'self, T> MySlice for &'self [T] {
|
||||
fn my_slice(self) -> &'self [T] {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
fn add(x:&mut [int], y:&[int])
|
||||
{
|
||||
x[0] = x[0] + y[0];
|
||||
}
|
||||
|
||||
pub fn main()
|
||||
{
|
||||
let z = @mut [1,2,3];
|
||||
let z2 = z;
|
||||
add(z.my_mut_slice(), z2.my_slice());
|
||||
print(fmt!("%d\n", z[0]));
|
||||
}
|
||||
16
src/test/run-fail/borrowck-wg-one-mut-one-imm-slices.rs
Normal file
16
src/test/run-fail/borrowck-wg-one-mut-one-imm-slices.rs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
// error-pattern:borrowed
|
||||
|
||||
// Test that write guards trigger when arguments are coerced to slices.
|
||||
|
||||
fn add(x:&mut [int], y:&[int])
|
||||
{
|
||||
x[0] = x[0] + y[0];
|
||||
}
|
||||
|
||||
pub fn main()
|
||||
{
|
||||
let z = @mut [1,2,3];
|
||||
let z2 = z;
|
||||
add(z, z2);
|
||||
print(fmt!("%d\n", z[0]));
|
||||
}
|
||||
17
src/test/run-fail/borrowck-wg-one-mut-one-imm.rs
Normal file
17
src/test/run-fail/borrowck-wg-one-mut-one-imm.rs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
// error-pattern:borrowed
|
||||
|
||||
// Test that write guards trigger when we are indexing into
|
||||
// an @mut vector.
|
||||
|
||||
fn add(x:&mut int, y:&int)
|
||||
{
|
||||
*x = *x + *y;
|
||||
}
|
||||
|
||||
pub fn main()
|
||||
{
|
||||
let z = @mut [1,2,3];
|
||||
let z2 = z;
|
||||
add(&mut z[0], &z2[0]);
|
||||
print(fmt!("%d\n", z[0]));
|
||||
}
|
||||
17
src/test/run-fail/borrowck-wg-two-array-indices.rs
Normal file
17
src/test/run-fail/borrowck-wg-two-array-indices.rs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
// error-pattern:borrowed
|
||||
|
||||
// Test that arguments trigger when there are *two mutable* borrows
|
||||
// of indices.
|
||||
|
||||
fn add(x:&mut int, y:&mut int)
|
||||
{
|
||||
*x = *x + *y;
|
||||
}
|
||||
|
||||
pub fn main()
|
||||
{
|
||||
let z = @mut [1,2,3];
|
||||
let z2 = z;
|
||||
add(&mut z[0], &mut z2[0]);
|
||||
print(fmt!("%d\n", z[0]));
|
||||
}
|
||||
14
src/test/run-pass/borrowck-wg-two-imm-borrows.rs
Normal file
14
src/test/run-pass/borrowck-wg-two-imm-borrows.rs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
// Test that we can borrow the same @mut box twice, so long as both are imm.
|
||||
|
||||
fn add(x:&int, y:&int)
|
||||
{
|
||||
*x + *y;
|
||||
}
|
||||
|
||||
pub fn main()
|
||||
{
|
||||
let z = @mut [1,2,3];
|
||||
let z2 = z;
|
||||
add(&z[0], &z2[0]);
|
||||
print(fmt!("%d\n", z[0]));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue