| 1 |
// { dg-additional-options "-frust-compile-until=compilation -frust-borrowcheck -fdiagnostics-show-caret -fdiagnostics-show-line-numbers" } |
| 2 |
// { dg-enable-nn-line-numbers "" } |
| 3 |
|
| 4 |
#[lang = "sized"] |
| 5 |
pub trait Sized {} |
| 6 |
|
| 7 |
struct Reference<'a> { |
| 8 |
value: &'a i32, |
| 9 |
} |
| 10 |
|
| 11 |
impl<'a> Reference<'a> { |
| 12 |
fn new<'a>(value: &'a i32) -> Reference<'a> { |
| 13 |
Reference { value: value } |
| 14 |
} |
| 15 |
} |
| 16 |
|
| 17 |
struct ReferenceMut<'a> { |
| 18 |
value: &'a mut i32, |
| 19 |
} |
| 20 |
|
| 21 |
impl<'a> ReferenceMut<'a> { |
| 22 |
fn new<'a>(value: &'a mut i32) -> ReferenceMut<'a> { |
| 23 |
ReferenceMut { value: value } |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
fn immutable_borrow_while_immutable_borrowed_struct() { |
| 28 |
let x = 0; |
| 29 |
let y = Reference::new(&x); |
| 30 |
let z = &x; |
| 31 |
let w = y; |
| 32 |
} |
| 33 |
|
| 34 |
fn immutable_borrow_while_mutable_borrowed_struct() { |
| 35 |
let mut x = 0; |
| 36 |
let y = ReferenceMut::new(&mut x); |
| 37 |
let z = &x; //~ ERROR |
| 38 |
// { dg-error "use of borrowed value" "" { target *-*-* } .-1 } |
| 39 |
let w = y; |
| 40 |
/* |
| 41 |
{ dg-begin-multiline-output "" } |
| 42 |
NN | let y = ReferenceMut::new(&mut x); |
| 43 |
| ~ |
| 44 |
| | |
| 45 |
| borrow occurs here |
| 46 |
NN | let z = &x; //~ ERROR |
| 47 |
| ^ |
| 48 |
| | |
| 49 |
| borrowed value used here |
| 50 |
{ dg-end-multiline-output "" } |
| 51 |
*/ |
| 52 |
} |
| 53 |
|
| 54 |
fn mutable_borrow_while_immutable_borrowed_struct() { |
| 55 |
let x = 0; |
| 56 |
let y = Reference::new(&x); |
| 57 |
let z = &mut x; //~ ERROR |
| 58 |
// { dg-error "use of borrowed value" "" { target *-*-* } .-1 } |
| 59 |
let w = y; |
| 60 |
/* |
| 61 |
{ dg-begin-multiline-output "" } |
| 62 |
NN | let y = Reference::new(&x); |
| 63 |
| ~ |
| 64 |
| | |
| 65 |
| borrow occurs here |
| 66 |
NN | let z = &mut x; //~ ERROR |
| 67 |
| ^ |
| 68 |
| | |
| 69 |
| borrowed value used here |
| 70 |
{ dg-end-multiline-output "" } |
| 71 |
*/ |
| 72 |
} |
| 73 |
|
| 74 |
fn mutable_borrow_while_mutable_borrowed_struct() { |
| 75 |
let mut x = 0; |
| 76 |
let y = ReferenceMut::new(&mut x); |
| 77 |
let z = &mut x; //~ ERROR |
| 78 |
// { dg-error "use of borrowed value" "" { target *-*-* } .-1 } |
| 79 |
let w = y; |
| 80 |
/* |
| 81 |
{ dg-begin-multiline-output "" } |
| 82 |
NN | let y = ReferenceMut::new(&mut x); |
| 83 |
| ~ |
| 84 |
| | |
| 85 |
| borrow occurs here |
| 86 |
NN | let z = &mut x; //~ ERROR |
| 87 |
| ^ |
| 88 |
| | |
| 89 |
| borrowed value used here |
| 90 |
{ dg-end-multiline-output "" } |
| 91 |
*/ |
| 92 |
} |
| 93 |
|
| 94 |
fn immutable_reborrow_while_immutable_borrowed_struct() { |
| 95 |
let x = 0; |
| 96 |
let y = Reference::new(&x); |
| 97 |
let z = &*y.value; |
| 98 |
} |
| 99 |
|
| 100 |
fn immutable_reborrow_while_mutable_borrowed_struct() { |
| 101 |
let mut x = 0; |
| 102 |
let y = Reference::new(&mut x); |
| 103 |
let z = &*y.value; |
| 104 |
} |
| 105 |
|
| 106 |
fn mutable_reborrow_while_immutable_borrowed_struct() { |
| 107 |
// { dg-error "Cannot reborrow immutable borrow as mutable" "" { target *-*-* } .-1 } |
| 108 |
/* |
| 109 |
{ dg-begin-multiline-output "" } |
| 110 |
NN | fn mutable_reborrow_while_immutable_borrowed_struct() { |
| 111 |
| ^~ |
| 112 |
{ dg-end-multiline-output "" } |
| 113 |
*/ |
| 114 |
let x = 0; |
| 115 |
let y = Reference::new(&x); |
| 116 |
let z = &mut *y.value; //~ ERROR |
| 117 |
} |
| 118 |
|
| 119 |
fn read_while_mutable_borrowed_struct() { |
| 120 |
let mut x = 0; |
| 121 |
let y = ReferenceMut::new(&mut x); |
| 122 |
let z = x; //~ ERROR |
| 123 |
// { dg-error "use of borrowed value" "" { target *-*-* } .-1 } |
| 124 |
let w = y; |
| 125 |
/* |
| 126 |
{ dg-begin-multiline-output "" } |
| 127 |
NN | let y = ReferenceMut::new(&mut x); |
| 128 |
| ~ |
| 129 |
| | |
| 130 |
| borrow occurs here |
| 131 |
NN | let z = x; //~ ERROR |
| 132 |
| ^ |
| 133 |
| | |
| 134 |
| borrowed value used here |
| 135 |
{ dg-end-multiline-output "" } |
| 136 |
*/ |
| 137 |
} |
| 138 |
|
| 139 |
fn write_while_borrowed_struct() { |
| 140 |
let mut x = 0; |
| 141 |
let y = Reference::new(&x); |
| 142 |
x = 1; //~ ERROR |
| 143 |
// { dg-error "use of borrowed value" "" { target *-*-* } .-1 } |
| 144 |
let z = y; |
| 145 |
/* |
| 146 |
{ dg-begin-multiline-output "" } |
| 147 |
NN | let y = Reference::new(&x); |
| 148 |
| ~ |
| 149 |
| | |
| 150 |
| borrow occurs here |
| 151 |
NN | x = 1; //~ ERROR |
| 152 |
| ^ |
| 153 |
| | |
| 154 |
| borrowed value used here |
| 155 |
{ dg-end-multiline-output "" } |
| 156 |
*/ |
| 157 |
} |
| 158 |
|
| 159 |
fn write_while_immutable_borrowed_struct() { |
| 160 |
let x = 0; |
| 161 |
let y = Reference::new(&x); |
| 162 |
x = 1; //~ ERROR |
| 163 |
// { dg-error "use of borrowed value" "" { target *-*-* } .-1 } |
| 164 |
let z = y; |
| 165 |
/* |
| 166 |
{ dg-begin-multiline-output "" } |
| 167 |
NN | let y = Reference::new(&x); |
| 168 |
| ~ |
| 169 |
| | |
| 170 |
| borrow occurs here |
| 171 |
NN | x = 1; //~ ERROR |
| 172 |
| ^ |
| 173 |
| | |
| 174 |
| borrowed value used here |
| 175 |
{ dg-end-multiline-output "" } |
| 176 |
*/ |
| 177 |
} |