.githooks/
compiler/
kernel/
lib/
examples/
std/
arch/
bits/
char/
collections/
fs/
hash/
lang/
alloc/
ast/
gen/
il/
layout/
lower/
manifest/
module/
parser/
resolver/
tests/
fixtures/
core.rad
161.9 KiB
opacity.rad
14.5 KiB
regions.rad
41.7 KiB
safety.rad
191.8 KiB
structure.rad
188.8 KiB
metadata.rad
2.6 KiB
printer.rad
33.0 KiB
refs.rad
66.5 KiB
tests.rad
23.6 KiB
scanner/
sexpr/
alloc.rad
1.2 KiB
ast.rad
36.0 KiB
gen.rad
513 B
il.rad
48.5 KiB
layout.rad
6.9 KiB
lower.rad
487.6 KiB
manifest.rad
22.5 KiB
module.rad
17.8 KiB
package.rad
1.5 KiB
parser.rad
114.1 KiB
pretty.rad
103.6 KiB
resolver.rad
508.1 KiB
scanner.rad
19.3 KiB
sexpr.rad
7.1 KiB
strings.rad
3.3 KiB
types.rad
620 B
mem/
sys/
arch.rad
68 B
bits.rad
1.6 KiB
char.rad
1.5 KiB
collections.rad
39 B
fmt.rad
11.6 KiB
fs.rad
11.0 KiB
hash.rad
41 B
intrinsics.rad
467 B
io.rad
1.7 KiB
lang.rad
335 B
mem.rad
12.0 KiB
mem.ras
919 B
process.rad
6.2 KiB
std.manifest
4.9 KiB
std.rad
349 B
sys.rad
1.0 KiB
testing.rad
2.4 KiB
tests.rad
26.6 KiB
vec.rad
3.2 KiB
scripts/
seed/
sublime/
test/
vim/
.gitignore
333 B
.gitsigners
224 B
AGENTS.md
8.3 KiB
CONTRIBUTING
2.1 KiB
LICENSE
1.1 KiB
Makefile
2.5 KiB
README
3.4 KiB
STYLE
3.2 KiB
std.lib
2.1 KiB
std.lib.test
1.1 KiB
lib/std/lang/resolver/tests/regions.rad
raw
| 1 | //! Resolver tests for region names, returned references, and borrow blocks. |
| 2 | |
| 3 | use std::testing; |
| 4 | use std::lang::resolver; |
| 5 | use std::lang::resolver::tests::*; |
| 6 | |
| 7 | /// Borrowed declarations shared by aggregate region checks. |
| 8 | constant BORROWED_TYPES: *[u8] = |
| 9 | "record S['r]: Copy { a: &'r [u8], n: u32 } union V['r]: Copy { None, Bytes(&'r [u8]), Inner(S['r]) } record M['r]: Copy { items: [?V['r]; 4], len: u32 } fn f(x: &'r [u8]) -> S['r] { return S { a: &x[1..], n: 0 }; }"; |
| 10 | |
| 11 | /// Implicit local regions reject overlapping writers and references that escape. |
| 12 | @test unsafe fn testImplicitWriterRegions() throws (testing::TestError) { |
| 13 | let writer: *[u8] = |
| 14 | "record W['r] { buf: &'r mut [u8] } fn (w: &'v mut W['r]) split() -> W['v] { return W { buf: &mut w.buf[..1] }; }"; |
| 15 | try expectProgramError( |
| 16 | program( |
| 17 | &[ |
| 18 | writer, |
| 19 | "fn f(buf: &'r mut [u8]) { let mut w = W { buf }; let first = w.split(); let second = w.split(); }", |
| 20 | ] |
| 21 | ), |
| 22 | resolver::ErrorKind::BorrowConflict("w") |
| 23 | ); |
| 24 | try expectProgramError( |
| 25 | program( |
| 26 | &[ |
| 27 | writer, |
| 28 | "fn f(buf: &'r mut [u8]) { let mut w = W { buf }; let pair = [w.split(), w.split()]; }", |
| 29 | ] |
| 30 | ), |
| 31 | resolver::ErrorKind::BorrowConflict("w") |
| 32 | ); |
| 33 | try expectProgramError( |
| 34 | program( |
| 35 | &[ |
| 36 | writer, |
| 37 | "fn f(buf: &'r mut [u8]) { let mut keep: ?W['r] = nil; { let mut w = W { buf }; set keep = w.split(); } }", |
| 38 | ] |
| 39 | ), |
| 40 | resolver::ErrorKind::Region(resolver::RegionError::Mismatch) |
| 41 | ); |
| 42 | try expectAnalyzeOk( |
| 43 | program( |
| 44 | &[ |
| 45 | writer, |
| 46 | "fn f(buf: &'r mut [u8]) { let mut w = W { buf }; { let first = w.split(); } let second = w.split(); }", |
| 47 | ] |
| 48 | ) |
| 49 | ); |
| 50 | try expectProgramError( |
| 51 | program( |
| 52 | &[ |
| 53 | writer, |
| 54 | "fn f(buf: &'r mut [u8]) -> W['r] { let mut w = W { buf }; return w.split(); }", |
| 55 | ] |
| 56 | ), |
| 57 | resolver::ErrorKind::Region(resolver::RegionError::Escape) |
| 58 | ); |
| 59 | let optionalWriter = program( |
| 60 | &[ |
| 61 | writer, |
| 62 | "fn (w: &'v mut W['r]) next() -> ?W['v] { return w.split(); }", |
| 63 | ] |
| 64 | ); |
| 65 | try expectAnalyzeOk( |
| 66 | program( |
| 67 | &[ |
| 68 | optionalWriter, |
| 69 | "fn f(buf: &'r mut [u8]) { let mut w = W { buf }; if let child = w.next(); child.buf.len > 0 {} let last = w.split(); }", |
| 70 | ] |
| 71 | ) |
| 72 | ); |
| 73 | try expectAnalyzeOk( |
| 74 | program( |
| 75 | &[ |
| 76 | optionalWriter, |
| 77 | "fn f(buf: &'r mut [u8]) { let mut w = W { buf }; while let child = w.next(); child.buf.len > 0 { break; } let last = w.split(); }", |
| 78 | ] |
| 79 | ) |
| 80 | ); |
| 81 | try expectProgramError( |
| 82 | program( |
| 83 | &[ |
| 84 | optionalWriter, |
| 85 | "fn f(buf: &'r mut [u8]) { let mut w = W { buf }; let child = w.next() else return; let last = w.split(); }", |
| 86 | ] |
| 87 | ), |
| 88 | resolver::ErrorKind::BorrowConflict("w") |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | /// Implicit content regions use the shortest enclosing source scope. |
| 93 | @test unsafe fn testImplicitRegionBounds() throws (testing::TestError) { |
| 94 | try expectAnalyzeOk( |
| 95 | "record R['r]: Copy { a: &'r [u8], b: &'r [u8] } fn f(input: &[u8]) { let local: [u8; 1] = [0]; let r = R { a: input, b: &local[..] }; assert r.a.len == input.len; }" |
| 96 | ); |
| 97 | try expectProgramError( |
| 98 | "record R['r]: Copy { a: &'r [u8] } fn f() { let outer: [u8; 1] = [0]; let mut keep = R { a: &outer[..] }; { let inner: [u8; 1] = [0]; set keep = R { a: &inner[..] }; } }", |
| 99 | resolver::ErrorKind::Region(resolver::RegionError::Mismatch) |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | /// Mutable aggregate contents retain their source loan until the value ends. |
| 104 | @test unsafe fn testMutableAggregateLoans() throws (testing::TestError) { |
| 105 | try expectProgramError( |
| 106 | "record W['r] { buf: &'r mut [u8], pos: u32 } fn (w: &'v mut W['r]) split() -> W['v] { return W { buf: &mut w.buf[..1], pos: 0 }; } fn f(buf: &'r mut [u8]) { let mut w = W { buf, pos: 0 }; let v: &'v mut W['r] = &mut w { let mut section = v.split(); set v.pos = 1; set section.buf[0] = 9; } }", |
| 107 | resolver::ErrorKind::BorrowConflict("w") |
| 108 | ); |
| 109 | try expectProgramError( |
| 110 | "record W['r] { buf: &'r mut [u8], pos: u32 } fn (w: &'v mut W['r]) split() -> W['v] { return W { buf: &mut w.buf[..1], pos: 0 }; } fn f(buf: &'r mut [u8]) { let mut w = W { buf: &mut buf[..], pos: 0 }; set buf[0] = 1; set w.buf[0] = 2; }", |
| 111 | resolver::ErrorKind::BorrowConflict("buf") |
| 112 | ); |
| 113 | try expectProgramError( |
| 114 | "record W['r]: Copy { buf: &'r mut [u8] }", |
| 115 | resolver::ErrorKind::CopyContainsNonCopy |
| 116 | ); |
| 117 | try expectProgramError( |
| 118 | "record W['r] { buf: &'r mut [u8] } fn f(buf: &'r mut [u8]) { let mut keep: ?W['r] = nil; { let w = W { buf: &mut buf[..] }; set keep = w; } set buf[0] = 1; }", |
| 119 | resolver::ErrorKind::BorrowConflict("buf") |
| 120 | ); |
| 121 | try expectProgramError( |
| 122 | "record W['r] { buf: &'r mut [u8] } fn (w: &'v mut W['r]) split() -> W['v] { return W { buf: &mut w.buf[..1] }; } fn both(a: W['a], b: W['b]) {} fn f(w: &'v mut W['r]) { both(w.split(), w.split()); }", |
| 123 | resolver::ErrorKind::BorrowConflict("w") |
| 124 | ); |
| 125 | try expectProgramError( |
| 126 | "record W['r] { buf: &'r mut [u8] } fn (w: &'v mut W['r]) next() -> ?W['v] { return W { buf: &mut w.buf[..1] }; } fn f(w: &'v mut W['r]) { let section = w.next() else return; set w.buf[0] = 1; }", |
| 127 | resolver::ErrorKind::BorrowConflict("w") |
| 128 | ); |
| 129 | try expectProgramError( |
| 130 | "record W['r] { buf: &'r mut [u8], pos: u32 } fn (w: &'v mut W['r]) split() -> W['v] { return W { buf: &mut w.buf[..1], pos: 0 }; } fn f(w: W['r]) { let a = w; let b = w; }", |
| 131 | resolver::ErrorKind::AffineUseAfterMove("w") |
| 132 | ); |
| 133 | } |
| 134 | |
| 135 | /// Buffered views retain their exclusive parent loan until the view ends. |
| 136 | @test unsafe fn testBufferedViewLoans() throws (testing::TestError) { |
| 137 | try expectProgramError( |
| 138 | "record R['b] { buf: &'b mut [u8] } fn (r: &'v mut R['b]) view() -> &'v [u8] { return &r.buf[..]; } fn f(r: &'v mut R['b]) { let first = r.view(); let second = r.view(); assert first.len == second.len; }", |
| 139 | resolver::ErrorKind::BorrowConflict("r") |
| 140 | ); |
| 141 | try expectProgramError( |
| 142 | "record P['a, 'b]: Copy { a: &'a [u8], b: &'b [u8] } fn f(pairs: &[P['a, 'b]]) -> &'a [u8] { return pairs[0].b; }", |
| 143 | resolver::ErrorKind::Region(resolver::RegionError::Escape) |
| 144 | ); |
| 145 | try expectProgramError( |
| 146 | "record P['a, 'b]: Copy { a: &'a [u8], b: &'b [u8], n: u32 } fn f(pairs: &'v [P['a, 'b]]) -> &'a u32 { return &pairs[0].n; }", |
| 147 | resolver::ErrorKind::Region(resolver::RegionError::Escape) |
| 148 | ); |
| 149 | } |
| 150 | |
| 151 | /// Owned mutable fields do not make a borrowed shared source exclusive. |
| 152 | @test unsafe fn testSharedMoveOnlyContents() throws (testing::TestError) { |
| 153 | try expectAnalyzeOk( |
| 154 | "record R['r] { bytes: &'r [u8], pool: *mut [u32] } fn make(bytes: &'r [u8]) -> R['r] { return R { bytes, pool: &mut [] }; } fn f(bytes: &'r [u8]) { let mut r = make(bytes); assert bytes.len == r.bytes.len; }" |
| 155 | ); |
| 156 | try expectProgramError( |
| 157 | "record R['a, 'b] { input: &'a [u8], output: &'b mut [u8] } fn make(input: &'a [u8], output: &'b mut [u8]) -> R['a, 'b] { return R { input, output }; } fn f(input: &'a [u8], output: &'b mut [u8]) { let mut r = make(input, output); assert input.len == r.input.len; set output[0] = 1; set r.output[0] = 2; }", |
| 158 | resolver::ErrorKind::BorrowConflict("output") |
| 159 | ); |
| 160 | } |
| 161 | |
| 162 | /// Functions return references reached through their region parameter. |
| 163 | @test unsafe fn testRegionReturns() throws (testing::TestError) { |
| 164 | try expectAnalyzeOk( |
| 165 | "record Pair: Copy { a: u32, b: u32 } fn a(p: &'r Pair) -> &'r u32 { return &p.a; }" |
| 166 | ); |
| 167 | try expectAnalyzeOk( |
| 168 | "record Pair: Copy { a: u32, b: u32 } fn a(p: &'r Pair, c: bool) -> ?&'r u32 { if c { return nil; } return &p.a if c else &p.b; }" |
| 169 | ); |
| 170 | try expectAnalyzeOk( |
| 171 | "record Pair: Copy { a: u32, b: u32 } fn a(p: &'r mut Pair) -> &'r mut u32 { return &mut p.b; } fn b(p: &'r mut Pair) -> &'r mut u32 { return a(p); }" |
| 172 | ); |
| 173 | try expectAnalyzeOk( |
| 174 | "record Pair: Copy { a: u32, b: u32 } fn a(p: &'r Pair) -> ?&'r u32 { return &p.a; } fn b(p: &'r Pair) -> &'r u32 { if let v = a(p) { return v; } let w = a(p) else { return &p.b; }; return w; }" |
| 175 | ); |
| 176 | try expectAnalyzeOk( |
| 177 | "record Pair: Copy { a: u32, b: u32 } fn (p: &'r Pair) a(c: bool) -> &'r u32 { return &p.a; }" |
| 178 | ); |
| 179 | try expectAnalyzeOk( |
| 180 | "fn a(s: &'r [u32], n: u32) -> &'r [u32] { if n > s.len { return s; } return &s[n..]; }" |
| 181 | ); |
| 182 | try expectAnalyzeOk( |
| 183 | "record Buf { items: [u32; 4], list: *[u32] } fn a(b: &'r mut Buf) -> &'r mut [u32] { return &mut b.items[1..]; } fn c(b: &'r Buf, i: u32) -> ?&'r [u32] { if i == 0 { return nil; } return &b.list[..i]; }" |
| 184 | ); |
| 185 | } |
| 186 | |
| 187 | /// Throwing functions return references, and `try` continues the borrow of |
| 188 | /// the call. |
| 189 | @test unsafe fn testRegionThrows() throws (testing::TestError) { |
| 190 | try expectAnalyzeOk( |
| 191 | "record Pair: Copy { a: u32, b: u32 } fn f(p: &'r Pair, c: bool) -> &'r u32 throws (u32) { if c { throw 1; } return &p.a; } fn g(p: &'r Pair) -> &'r u32 throws (u32) { return try f(p, true); } fn h(p: &'r Pair) -> ?&'r u32 { return try? f(p, true); } fn k(p: &'r Pair) -> &'r u32 { return try f(p, false) catch { return &p.b; }; } fn m(p: &'r Pair) -> &'r u32 { return try! f(p, false); }" |
| 192 | ); |
| 193 | try expectAnalyzeOk( |
| 194 | "fn s(x: &'r [u32]) -> &'r [u32] throws (u32) { if x.len == 0 { throw 0; } return &x[1..]; } fn t(x: &'r [u32]) -> ?&'r [u32] { let y = try s(x) catch { return nil; }; return y; }" |
| 195 | ); |
| 196 | try expectProgramError( |
| 197 | "record Pair: Copy { a: u32, b: u32 } fn f(p: &'r Pair) -> &'r u32 throws (u32) { return &p.a; } fn g(p: &'r Pair, q: &Pair) -> &'r u32 throws (u32) { return try f(q); }", |
| 198 | resolver::ErrorKind::Region(resolver::RegionError::Escape) |
| 199 | ); |
| 200 | try expectProgramError( |
| 201 | "fn s(x: &'r [u32]) -> &'r [u32] throws (u32) { return x; } fn t(x: &'r [u32], y: &[u32]) -> &'r [u32] { if let z = try? s(y) { return z; } return x; }", |
| 202 | resolver::ErrorKind::Region(resolver::RegionError::Escape) |
| 203 | ); |
| 204 | try expectProgramError( |
| 205 | "record Pair: Copy { a: u32, b: u32 } fn f(p: &'r mut Pair) -> &'r mut u32 throws (u32) { return &mut p.a; } fn g(w: &mut Pair) throws (u32) { let s = try f(w); set w.b = 1; set *s = 2; }", |
| 206 | resolver::ErrorKind::BorrowConflict("w") |
| 207 | ); |
| 208 | } |
| 209 | |
| 210 | /// Region names are declared once, by a function or a borrow block. |
| 211 | @test unsafe fn testRegionNames() throws (testing::TestError) { |
| 212 | try expectProgramError( |
| 213 | "record Pair: Copy { a: u32, b: u32 } fn a(p: &'r Pair) { let q: &'r Pair = p { } }", |
| 214 | resolver::ErrorKind::Region(resolver::RegionError::Duplicate("'r")) |
| 215 | ); |
| 216 | try expectProgramError( |
| 217 | "record Pair: Copy { a: u32, b: u32 } fn a(p: &Pair) -> u32 { let q: &'s Pair = p { } let v: &'s u32 = &p.a; return *v; }", |
| 218 | resolver::ErrorKind::Region(resolver::RegionError::Unknown("'s")) |
| 219 | ); |
| 220 | } |
| 221 | |
| 222 | /// A signature declares each region that its receiver or parameters name. |
| 223 | /// A region named only by the result or the body is not declared. |
| 224 | @test unsafe fn testImplicitRegionNames() throws (testing::TestError) { |
| 225 | try expectAnalyzeOk( |
| 226 | "record Pair: Copy { a: u32, b: u32 } fn a(p: &'r Pair) -> &'r u32 { return &p.a; }" |
| 227 | ); |
| 228 | try expectAnalyzeOk( |
| 229 | "fn pick(a: &'r u32, b: &'r u32, c: bool) -> &'r u32 { return a if c else b; }" |
| 230 | ); |
| 231 | try expectAnalyzeOk( |
| 232 | "record Pair: Copy { a: u32, b: u32 } fn (p: &Pair) pick(x: &'a u32) -> &'a u32 { return x; }" |
| 233 | ); |
| 234 | try expectProgramError( |
| 235 | "record Pair: Copy { a: u32, b: u32 } fn a(p: &Pair) -> &'r u32 { return &p.a; }", |
| 236 | resolver::ErrorKind::Region(resolver::RegionError::Unknown("'r")) |
| 237 | ); |
| 238 | try expectProgramError( |
| 239 | "record Pair: Copy { a: u32, b: u32 } fn (p: &Pair) a() -> &'r u32 { return &p.a; }", |
| 240 | resolver::ErrorKind::Region(resolver::RegionError::Unknown("'r")) |
| 241 | ); |
| 242 | try expectProgramError( |
| 243 | "record Pair: Copy { a: u32, b: u32 } fn a(p: &Pair) -> u32 { let v: &'s u32 = &p.a; return *v; }", |
| 244 | resolver::ErrorKind::Region(resolver::RegionError::Unknown("'s")) |
| 245 | ); |
| 246 | } |
| 247 | |
| 248 | /// A returned reference names a region supplied by an input. |
| 249 | @test unsafe fn testRegionReturnShape() throws (testing::TestError) { |
| 250 | let kind = resolver::ErrorKind::Region(resolver::RegionError::Return); |
| 251 | try expectProgramError( |
| 252 | "record Pair: Copy { a: u32, b: u32 } fn a(p: &'r [&u32]) -> &'r [&u32] { return p; }", |
| 253 | kind |
| 254 | ); |
| 255 | try expectProgramError( |
| 256 | "record Pair: Copy { a: u32, b: u32 } fn a(p: &Pair) -> &u32 { return &p.a; }", |
| 257 | resolver::ErrorKind::InvalidRefPosition |
| 258 | ); |
| 259 | } |
| 260 | |
| 261 | /// A returned reference cannot reach storage other than its region parameter's. |
| 262 | @test unsafe fn testRegionEscape() throws (testing::TestError) { |
| 263 | let kind = resolver::ErrorKind::Region(resolver::RegionError::Escape); |
| 264 | try expectProgramError( |
| 265 | "record Pair: Copy { a: u32, b: u32 } fn a(p: &'r Pair, q: &Pair) -> &'r u32 { return &q.a; }", |
| 266 | kind |
| 267 | ); |
| 268 | try expectProgramError( |
| 269 | "record Pair: Copy { a: u32, b: u32 } fn a(p: &'r Pair) -> &'r u32 { let x: u32 = p.a; return &x; }", |
| 270 | kind |
| 271 | ); |
| 272 | try expectProgramError( |
| 273 | "record Pair: Copy { a: u32, b: u32 } fn a(p: &'r Pair) -> ?&'r u32 { return &(p.a + 1); }", |
| 274 | kind |
| 275 | ); |
| 276 | try expectProgramError( |
| 277 | "record Pair: Copy { a: u32, b: u32 } fn a(p: &'r Pair, q: &Pair, c: bool) -> &'r u32 { return &p.a if c else &q.b; }", |
| 278 | kind |
| 279 | ); |
| 280 | try expectProgramError( |
| 281 | "record Pair: Copy { a: u32, b: u32 } fn f(p: &'r Pair) -> &'r u32 { return &p.a; } fn a(p: &'r Pair, q: &Pair) -> &'r u32 { return f(q); }", |
| 282 | kind |
| 283 | ); |
| 284 | try expectProgramError( |
| 285 | "fn a(s: &'r [u32], t: &[u32]) -> &'r [u32] { return &t[1..]; }", |
| 286 | kind |
| 287 | ); |
| 288 | try expectProgramError( |
| 289 | "fn a(s: &'r [u32]) -> &'r [u32] { let x: [u32; 2] = [s[0], s[1]]; return &x[..]; }", |
| 290 | kind |
| 291 | ); |
| 292 | } |
| 293 | |
| 294 | /// The borrowed storage is reachable only through the reference of a borrow |
| 295 | /// block, and returned references keep their source borrowed. |
| 296 | @test unsafe fn testBorrowBlocks() throws (testing::TestError) { |
| 297 | try expectProgramError( |
| 298 | "record Pair: Copy { a: u32, b: u32 } fn a() -> u32 { let x = Pair { a: 1, b: 2 }; let p: &'s Pair = &x { return x.a + p.b; } }", |
| 299 | resolver::ErrorKind::BorrowConflict("x") |
| 300 | ); |
| 301 | try expectProgramError( |
| 302 | "record Pair: Copy { a: u32, b: u32 } fn f(p: &'r mut Pair) -> &'r mut u32 { return &mut p.a; } fn a() -> u32 { let mut x = Pair { a: 1, b: 2 }; let w: &'w mut Pair = &mut x { let s: &mut u32 = f(w); let t: &mut u32 = f(w); set *s = 3; return *t; } }", |
| 303 | resolver::ErrorKind::BorrowConflict("x") |
| 304 | ); |
| 305 | try expectProgramError( |
| 306 | "record Pair: Copy { a: u32, b: u32 } fn f(p: &'r mut Pair) -> ?&'r mut u32 { return &mut p.a; } fn a(w: &mut Pair) { if let s = f(w) { set w.b = 1; set *s = 2; } }", |
| 307 | resolver::ErrorKind::BorrowConflict("w") |
| 308 | ); |
| 309 | try expectAnalyzeOk( |
| 310 | "record Pair: Copy { a: u32, b: u32 } fn a() -> u32 { let mut x = Pair { a: 1, b: 2 }; let p: &'s Pair = &x { assert p.a == 1; } set x.a = 3; return x.a; }" |
| 311 | ); |
| 312 | } |
| 313 | |
| 314 | /// Records and unions with a region hold references of that region, and |
| 315 | /// functions return them from the parameter that names it. |
| 316 | @test unsafe fn testBorrowedTypes() throws (testing::TestError) { |
| 317 | try expectAnalyzeOk( |
| 318 | "record A['r]: Copy { b: B['r] } record B['r]: Copy { x: &'r [u8] } fn f(x: &'r [u8]) -> A['r] { return A { b: B { x } }; }" |
| 319 | ); |
| 320 | try expectAnalyzeOk( |
| 321 | BORROWED_TYPES |
| 322 | ); |
| 323 | try expectAnalyzeOk( |
| 324 | program( |
| 325 | &[ |
| 326 | BORROWED_TYPES, |
| 327 | "fn g(x: &'r [u8]) -> M['r] { let mut m: M['r] = M { items: [nil; 4], len: 0 }; set m.items[0] = V::Bytes(x); set m.items[1] = V::Inner(f(x)); set m.len = 2; return m; }", |
| 328 | ] |
| 329 | ) |
| 330 | ); |
| 331 | try expectAnalyzeOk( |
| 332 | program( |
| 333 | &[ |
| 334 | BORROWED_TYPES, |
| 335 | "fn g(x: &'r [u8]) -> ?S['r] throws (u32) { if x.len == 0 { throw 1; } return S { a: x, n: 1 }; } fn h(x: &'r [u8]) -> S['r] throws (u32) { if let s = try g(x) { return s; } throw 2; }", |
| 336 | ] |
| 337 | ) |
| 338 | ); |
| 339 | try expectAnalyzeOk( |
| 340 | program( |
| 341 | &[ |
| 342 | BORROWED_TYPES, |
| 343 | "fn g() -> u32 { let buf: [u8; 4] = [1, 2, 3, 4]; let mut n: u32 = 0; let b: &'b [u8] = &buf[..] { let s = f(b); let a = s.a; set n = a.len + s.n; } return n; }", |
| 344 | ] |
| 345 | ) |
| 346 | ); |
| 347 | try expectAnalyzeOk( |
| 348 | program( |
| 349 | &[ |
| 350 | BORROWED_TYPES, |
| 351 | "fn (s: &S['r]) size() -> u32 { return s.a.len; } fn k(v: V['r]) -> u32 { match v { case V::Bytes(b) => return b.len, else => return 0 } }", |
| 352 | ] |
| 353 | ) |
| 354 | ); |
| 355 | try expectAnalyzeOk( |
| 356 | program( |
| 357 | &[ |
| 358 | BORROWED_TYPES, |
| 359 | "fn g(x: &'r [u8], c: bool) -> V['r] { let v = V::Bytes(x); if c { return V::None; } return v; }", |
| 360 | ] |
| 361 | ) |
| 362 | ); |
| 363 | } |
| 364 | |
| 365 | /// Accessors retain the region of contents passed by value or through a view. |
| 366 | @test unsafe fn testBorrowedContents() throws (testing::TestError) { |
| 367 | try expectAnalyzeOk( |
| 368 | "record R['r]: Copy { bytes: &'r [u8] } fn both(bytes: &'r [u8]) -> [R['r]; 2] { return [R { bytes }; 2]; }" |
| 369 | ); |
| 370 | try expectAnalyzeOk( |
| 371 | "record R['r]: Copy { bytes: &'r [u8] } fn body(r: R['r]) -> &'r [u8] { return r.bytes; }" |
| 372 | ); |
| 373 | try expectAnalyzeOk( |
| 374 | "record R['r]: Copy { bytes: &'r [u8] } fn (r: &R['r]) body() -> &'r [u8] { return r.bytes; }" |
| 375 | ); |
| 376 | try expectAnalyzeOk( |
| 377 | "record R['r] { bytes: &'r [u8], pos: u32 } fn (r: &mut R['r]) next() -> ?&'r [u8] { if r.pos == r.bytes.len { return nil; } let at = r.pos; set r.pos += 1; return &r.bytes[at..at + 1]; }" |
| 378 | ); |
| 379 | } |
| 380 | |
| 381 | /// A type with a region holds only shared references that name its region, |
| 382 | /// and only types with a region hold it. |
| 383 | @test unsafe fn testBorrowedDeclarations() throws (testing::TestError) { |
| 384 | let field = resolver::ErrorKind::Region(resolver::RegionError::Field); |
| 385 | let indirect = resolver::ErrorKind::Region(resolver::RegionError::Indirect); |
| 386 | let argument = resolver::ErrorKind::Region(resolver::RegionError::Argument); |
| 387 | try expectProgramError("record S['r] { a: &[u8] }", field); |
| 388 | try expectProgramError("union U['r] { A(&[u8]) }", field); |
| 389 | try expectAnalyzeOk(program(&[BORROWED_TYPES, "record T['r] { p: *S['r] }"])); |
| 390 | try expectAnalyzeOk(program(&[BORROWED_TYPES, "record T['r] { p: &'r *S['r] }"])); |
| 391 | try expectProgramError( |
| 392 | program(&[BORROWED_TYPES, "record T['r] { p: *unsafe S['r] }"]), indirect |
| 393 | ); |
| 394 | try expectProgramError("trait Tr { fn (&Tr) f(); } record T['r] { o: &opaque Tr }", field); |
| 395 | try expectProgramError(program(&[BORROWED_TYPES, "record T['r] { p: &S['r] }"]), field); |
| 396 | try expectProgramError("record S { a: &[u8] }", resolver::ErrorKind::InvalidRefPosition); |
| 397 | try expectProgramError( |
| 398 | program(&[BORROWED_TYPES, "record T { s: S }"]), |
| 399 | argument |
| 400 | ); |
| 401 | try expectProgramError( |
| 402 | program(&[BORROWED_TYPES, "record T { s: S['r] }"]), |
| 403 | resolver::ErrorKind::Region(resolver::RegionError::Unknown( |
| 404 | "'r" |
| 405 | )) |
| 406 | ); |
| 407 | try expectProgramError("record P { x: u32 } fn f(p: P['r]) {}", argument); |
| 408 | try expectAnalyzeOk(program(&[BORROWED_TYPES, "fn g(a: *[S['r]]) {}"])); |
| 409 | try expectProgramError( |
| 410 | program(&[BORROWED_TYPES, "fn g(a: *unsafe [S['r]]) {}"]), |
| 411 | resolver::ErrorKind::InvalidRefPosition |
| 412 | ); |
| 413 | } |
| 414 | |
| 415 | /// References to values of types with a region nest at any depth: in fields, |
| 416 | /// results and recursive types, each position names its own region. |
| 417 | @test unsafe fn testNestedBorrowedValues() throws (testing::TestError) { |
| 418 | try expectAnalyzeOk( |
| 419 | program( |
| 420 | &[ |
| 421 | BORROWED_TYPES, |
| 422 | "record T['r] { p: &'r S['r] } union U['r] { A(?&'r [S['r]]) } ", |
| 423 | "fn get(t: T['r]) -> &'r [u8] { return t.p.a; } ", |
| 424 | "fn inner(t: T['r]) -> &'r S['r] { return t.p; } ", |
| 425 | "fn first(items: &'r [S['r]]) -> ?&'r S['r] { if items.len == 0 { return nil; } return &items[0]; } ", |
| 426 | "fn all(items: &'r [S['r]], any: bool) -> &'r [S['r]] { if any { return items; } return &[]; }", |
| 427 | ] |
| 428 | ) |
| 429 | ); |
| 430 | try expectAnalyzeOk( |
| 431 | "record Node['r] { value: u32, next: ?&'r Node['r] } fn next(n: &'r Node['r]) -> ?&'r Node['r] { return n.next; }" |
| 432 | ); |
| 433 | try expectAnalyzeOk( |
| 434 | "record Pair['a, 'b] { view: &'a [u8] } fn outer(p: &'b Pair['a, 'b]) -> &'a [u8] { return p.view; }" |
| 435 | ); |
| 436 | let escape = resolver::ErrorKind::Region(resolver::RegionError::Escape); |
| 437 | try expectProgramError( |
| 438 | program( |
| 439 | &[ |
| 440 | BORROWED_TYPES, |
| 441 | "fn local(s: S['r]) -> &'r S['r] { let copy = s; return © }", |
| 442 | ] |
| 443 | ), |
| 444 | escape |
| 445 | ); |
| 446 | try expectProgramError( |
| 447 | "record Node['r] { value: u32, next: ?&'r Node['r] } fn skip(n: &'s Node['r]) -> &'r Node['r] { return n; }", |
| 448 | escape |
| 449 | ); |
| 450 | try expectProgramError( |
| 451 | program( |
| 452 | &[ |
| 453 | BORROWED_TYPES, |
| 454 | "fn other(items: &'r [S['s]]) -> ?&'r S['r] { return &items[0]; }", |
| 455 | ] |
| 456 | ), |
| 457 | escape |
| 458 | ); |
| 459 | } |
| 460 | |
| 461 | /// A reference local can be reassigned. Its new value must fit its region, and |
| 462 | /// it retains the storage of each value it holds. |
| 463 | @test unsafe fn testMutableReferenceLocals() throws (testing::TestError) { |
| 464 | try expectAnalyzeOk( |
| 465 | "fn f(x: &'r u32, y: &'r u32, c: bool) -> &'r u32 { let mut p = x; if c { set p = y; } return p; }" |
| 466 | ); |
| 467 | try expectAnalyzeOk( |
| 468 | "record Node['r] { value: u32, next: ?&'r Node['r] } fn last(head: &'r Node['r]) -> &'r Node['r] { let mut node = head; while let next = node.next { set node = next; } return node; }" |
| 469 | ); |
| 470 | try expectProgramError( |
| 471 | "fn f(x: &'r u32) -> &'r u32 { let y: u32 = 1; let mut p = x; set p = &y; return p; }", |
| 472 | resolver::ErrorKind::Region(resolver::RegionError::Mismatch) |
| 473 | ); |
| 474 | try expectProgramError( |
| 475 | "fn f() -> u32 { let mut a: u32 = 1; let mut b: u32 = 2; let mut p = &a; set p = &b; set b = 3; return *p; }", |
| 476 | resolver::ErrorKind::BorrowConflict("b") |
| 477 | ); |
| 478 | try expectProgramError( |
| 479 | "fn f() -> u32 { let mut a: u32 = 1; let b: u32 = 2; let mut p = &a; set a = 3; set p = &b; return *p; }", |
| 480 | resolver::ErrorKind::BorrowConflict("a") |
| 481 | ); |
| 482 | } |
| 483 | |
| 484 | /// A function stores references of a region into an object that its caller |
| 485 | /// passes, through a mutable reference. A call passes only sources that fit the |
| 486 | /// region of the object that receives them. |
| 487 | @test unsafe fn testStoresThroughReferences() throws (testing::TestError) { |
| 488 | let mismatch = resolver::ErrorKind::Region(resolver::RegionError::Mismatch); |
| 489 | try expectAnalyzeOk(program(&[ |
| 490 | BORROWED_TYPES, "fn g(p: &mut M['a], x: &'a [u8]) { set p.items[0] = V::Bytes(x); }" |
| 491 | ])); |
| 492 | try expectAnalyzeOk( |
| 493 | "record Node['r] { value: u32, next: ?&'r Node['r] } fn link(node: &'r mut Node['r], next: &'r Node['r]) { set node.next = next; }" |
| 494 | ); |
| 495 | try expectAnalyzeOk( |
| 496 | program( |
| 497 | &[ |
| 498 | BORROWED_TYPES, |
| 499 | "fn fill(out: &mut [?S['r]], s: S['r]) { set out[0] = s; } ", |
| 500 | "fn h(input: &'r [u8]) -> u32 { let mut items: [?S['r]; 2] = [nil; 2]; fill(&mut items[..], f(input)); return items.len; }", |
| 501 | ] |
| 502 | ) |
| 503 | ); |
| 504 | try expectProgramError( |
| 505 | program( |
| 506 | &[ |
| 507 | BORROWED_TYPES, |
| 508 | "fn g(p: &mut M['a], x: &'b [u8]) { set p.items[0] = V::Bytes(x); }", |
| 509 | ] |
| 510 | ), |
| 511 | mismatch |
| 512 | ); |
| 513 | try expectProgramError( |
| 514 | program( |
| 515 | &[ |
| 516 | BORROWED_TYPES, |
| 517 | "fn g(p: &mut M['a]) { let b: [u8; 1] = [0]; set p.items[0] = V::Bytes(&b[..]); }", |
| 518 | ] |
| 519 | ), |
| 520 | mismatch |
| 521 | ); |
| 522 | try expectProgramError( |
| 523 | program( |
| 524 | &[ |
| 525 | BORROWED_TYPES, |
| 526 | "fn put(p: &mut M['a], x: &'a [u8]) { set p.items[0] = V::Bytes(x); } ", |
| 527 | "fn g(p: &mut M['a]) { let b: [u8; 1] = [0]; put(p, &b[..]); }", |
| 528 | ] |
| 529 | ), |
| 530 | mismatch |
| 531 | ); |
| 532 | } |
| 533 | |
| 534 | /// A pattern error in a recovered block leaves the enclosing subject intact, so |
| 535 | /// later prongs bind the regions of the outer subject. |
| 536 | @test unsafe fn testRecoveredSubjectRegions() throws (testing::TestError) { |
| 537 | let mut a = testResolver(); |
| 538 | let result = try resolveProgramStr( |
| 539 | &mut a, |
| 540 | program( |
| 541 | &[ |
| 542 | "record R['r]: Copy { bytes: &'r [u8] } union U['r]: Copy { A(R['r]), B(u32) } ", |
| 543 | "fn f(x: U['a], y: U['b]) -> ?&'a [u8] { match x { ", |
| 544 | "case U::B(_) => { match y { case U::C(_) => {} else => {} } } ", |
| 545 | "case U::A(r) => { return r.bytes; } } return nil; }", |
| 546 | ] |
| 547 | ) |
| 548 | ); |
| 549 | try expectErrorKind(&result, resolver::ErrorKind::UnresolvedSymbol("C")); |
| 550 | try testing::expect(result.diagnostics.errors.len == 1); |
| 551 | } |
| 552 | |
| 553 | /// A value of a type with a region holds references of one named region, |
| 554 | /// which is borrowed shared, and keeps that region where it is stored. |
| 555 | @test unsafe fn testBorrowedRegions() throws (testing::TestError) { |
| 556 | let unnamed = resolver::ErrorKind::Region(resolver::RegionError::Unnamed); |
| 557 | let mismatch = resolver::ErrorKind::Region(resolver::RegionError::Mismatch); |
| 558 | try expectProgramError( |
| 559 | "record R['r]: Copy { bytes: &'r [u8] } fn f(x: ?R['a], y: R['b]) -> R['a] { let r = x else y; return r; }", |
| 560 | mismatch |
| 561 | ); |
| 562 | try expectProgramError( |
| 563 | program( |
| 564 | &[ |
| 565 | BORROWED_TYPES, |
| 566 | "fn g(x: &'r [u8]) -> S['r] { let buf: [u8; 2] = [0; 2]; return S { a: &buf[..], n: 0 }; }", |
| 567 | ] |
| 568 | ), |
| 569 | resolver::ErrorKind::Region(resolver::RegionError::Escape) |
| 570 | ); |
| 571 | try expectProgramError( |
| 572 | program( |
| 573 | &[ |
| 574 | BORROWED_TYPES, |
| 575 | "fn g(x: &'a [u8], y: &'b [u8]) -> S['a] { return S { a: y, n: 0 }; }", |
| 576 | ] |
| 577 | ), |
| 578 | resolver::ErrorKind::Region(resolver::RegionError::Escape) |
| 579 | ); |
| 580 | try expectProgramError( |
| 581 | "record P['r] { a: &'r [u8], b: &'r [u8] } fn g(x: &'a [u8], y: &'b [u8]) { let p = P { a: x, b: y }; }", |
| 582 | unnamed |
| 583 | ); |
| 584 | try expectProgramError( |
| 585 | program( |
| 586 | &[ |
| 587 | BORROWED_TYPES, |
| 588 | "fn g(x: &'a [u8], y: &'b [u8]) { let mut m: M['a] = M { items: [nil; 4], len: 0 }; set m.items[0] = V::Bytes(y); }", |
| 589 | ] |
| 590 | ), |
| 591 | mismatch |
| 592 | ); |
| 593 | |
| 594 | try expectAnalyzeOk(program( |
| 595 | &[BORROWED_TYPES, "fn g() { let buf: [u8; 2] = [0; 2]; let s = f(&buf[..]); }"] |
| 596 | )); |
| 597 | // A mutable local holds references that outlive it where its value has none. |
| 598 | try expectAnalyzeOk( |
| 599 | program( |
| 600 | &[ |
| 601 | BORROWED_TYPES, |
| 602 | "fn g(x: &[u8]) { let mut m = M { items: [nil; 4], len: 0 }; set m.items[0] = V::Bytes(x); }", |
| 603 | ] |
| 604 | ) |
| 605 | ); |
| 606 | try expectProgramError( |
| 607 | program( |
| 608 | &[ |
| 609 | BORROWED_TYPES, |
| 610 | "fn g() { let mut m = M { items: [nil; 4], len: 0 }; { let b: [u8; 1] = [0]; set m.items[0] = V::Bytes(&b[..]); } }", |
| 611 | ] |
| 612 | ), |
| 613 | mismatch |
| 614 | ); |
| 615 | try expectProgramError( |
| 616 | program( |
| 617 | &[ |
| 618 | BORROWED_TYPES, |
| 619 | "fn g(o: &'o [u8]) { let mut keep: S['o] = S { a: o, n: 0 }; let buf: [u8; 2] = [0; 2]; let b: &'b [u8] = &buf[..] { set keep = S { a: b, n: 0 }; } }", |
| 620 | ] |
| 621 | ), |
| 622 | mismatch |
| 623 | ); |
| 624 | try expectProgramError( |
| 625 | program(&[ |
| 626 | BORROWED_TYPES, "fn g(x: &'a [u8], y: &'b [u8]) { let s: S['a] = f(y); }" |
| 627 | ]), |
| 628 | mismatch |
| 629 | ); |
| 630 | } |
| 631 | |
| 632 | /// Destructuring preserves each field's independent content region. |
| 633 | @test unsafe fn testIndependentFieldRegions() throws (testing::TestError) { |
| 634 | try expectAnalyzeOk( |
| 635 | "record P['a, 'b]: Copy { x: &'b [u8], y: &'a [u8] } fn f(p: P['a, 'b]) -> &'b [u8] { let case P { x, y: _ } = p else panic; return x; }" |
| 636 | ); |
| 637 | try expectAnalyzeOk( |
| 638 | "union U['a, 'b]: Copy { A(&'a [u8]), B(&'b [u8]) } fn f(p: U['a, 'b], backup: &'b [u8]) -> &'b [u8] { if let case U::B(x) = p { return x; } return backup; }" |
| 639 | ); |
| 640 | try expectProgramError( |
| 641 | "record P['a, 'b]: Copy { x: &'b [u8], y: &'a [u8] } fn f(p: P['a, 'b]) -> &'a [u8] { return p.x; }", |
| 642 | resolver::ErrorKind::Region(resolver::RegionError::Escape) |
| 643 | ); |
| 644 | try expectProgramError( |
| 645 | "record P['a, 'b]: Copy { x: &'b [u8], y: &'a [u8] } fn f(p: P['a, 'b]) -> &'a [u8] { let case P { x, y: _ } = p else panic; return x; }", |
| 646 | resolver::ErrorKind::Region(resolver::RegionError::Escape) |
| 647 | ); |
| 648 | } |
| 649 | |
| 650 | /// Aggregate evaluation keeps mutable references live between its elements. |
| 651 | @test unsafe fn testAggregatePendingLoans() throws (testing::TestError) { |
| 652 | try expectProgramError( |
| 653 | "record W['r] { buf: &'r mut [u8] } record Pair['r] { a: W['r], b: W['r] } fn (w: &'v mut W['r]) split() -> W['v] { return W { buf: &mut w.buf[..1] }; } fn f(w: &'v mut W['r]) { let p = Pair { a: w.split(), b: w.split() }; }", |
| 654 | resolver::ErrorKind::BorrowConflict("w") |
| 655 | ); |
| 656 | try expectProgramError( |
| 657 | "record W['r] { buf: &'r mut [u8] } fn (w: &'v mut W['r]) split() -> W['v] { return W { buf: &mut w.buf[..1] }; } fn f(w: &'v mut W['r]) { let p = [w.split(), w.split()]; }", |
| 658 | resolver::ErrorKind::BorrowConflict("w") |
| 659 | ); |
| 660 | } |
| 661 | |
| 662 | /// Every input of a mutable result remains borrowed. |
| 663 | @test unsafe fn testMultipleResultLoans() throws (testing::TestError) { |
| 664 | try expectProgramError( |
| 665 | "record M['a, 'b] { x: &'a mut [u8], y: &'b mut [u8] } fn make(x: &'a mut [u8], y: &'b mut [u8]) -> M['a, 'b] { return M { x: &mut x[..], y: &mut y[..] }; } fn f(a: &'a mut [u8], b: &'b mut [u8]) { let m = make(a, b); set b[0] = 1; }", |
| 666 | resolver::ErrorKind::BorrowConflict("b") |
| 667 | ); |
| 668 | try expectProgramError( |
| 669 | "fn pick(a: &'r mut [u8], b: &'r mut [u8], c: bool) -> &'r mut [u8] { return a if c else b; } fn f(a: &'r mut [u8], b: &'r mut [u8]) { let s = pick(a, b, true); set b[0] = 1; set s[0] = 2; }", |
| 670 | resolver::ErrorKind::BorrowConflict("b") |
| 671 | ); |
| 672 | } |
| 673 | |
| 674 | /// A returned mutable view assigned to an outer value keeps its parent loan. |
| 675 | @test unsafe fn testAssignedViewLoan() throws (testing::TestError) { |
| 676 | try expectProgramError( |
| 677 | "record W['r] { buf: &'r mut [u8] } fn (w: &'v mut W['r]) split() -> W['v] { return W { buf: &mut w.buf[..1] }; } fn f(w: &'v mut W['r]) { let mut keep: ?W['v] = nil; { set keep = w.split(); } set w.buf[0] = 1; }", |
| 678 | resolver::ErrorKind::BorrowConflict("w") |
| 679 | ); |
| 680 | } |
| 681 | |
| 682 | /// A loop cannot retain a parent reborrow across its back edge. |
| 683 | @test unsafe fn testLoopViewLoan() throws (testing::TestError) { |
| 684 | try expectProgramError( |
| 685 | "record W['r] { buf: &'r mut [u8] } fn (w: &'v mut W['r]) split() -> W['v] { return W { buf: &mut w.buf[..1] }; } fn f(w: &'v mut W['r]) { let mut keep: ?W['v] = nil; for i in 0 .. 2 { set keep = w.split(); } }", |
| 686 | resolver::ErrorKind::BorrowConflict("w") |
| 687 | ); |
| 688 | } |
| 689 | |
| 690 | /// Mutable contents are protected even when the enclosing type names the region. |
| 691 | @test unsafe fn testMutableContentCallLoan() throws (testing::TestError) { |
| 692 | try expectProgramError( |
| 693 | "record W['r] { buf: &'r mut [u8] } fn make(buf: &'r mut [u8]) -> W['r] { return W { buf: &mut buf[..] }; } fn f(input: W['r]) { let mut w = input; let child = make(&mut w.buf[..]); set w.buf[0] = 1; }", |
| 694 | resolver::ErrorKind::BorrowConflict("w") |
| 695 | ); |
| 696 | } |
| 697 | |
| 698 | /// Optional range fills retain the address region of their fill value. |
| 699 | @test unsafe fn testOptionalReferenceFillRegion() throws (testing::TestError) { |
| 700 | try expectProgramError( |
| 701 | "fn f() { let outer: u32 = 1; let r: &'r u32 = &outer { let mut values: [?&'r u32; 1] = [r]; { let inner: u32 = 2; set values[..] = &inner; } } }", |
| 702 | resolver::ErrorKind::Region(resolver::RegionError::Mismatch) |
| 703 | ); |
| 704 | try expectAnalyzeOk( |
| 705 | "fn f() { let outer: u32 = 1; let r: &'r u32 = &outer { let mut values: [?&'r u32; 1] = [nil]; set values[..] = r; } }" |
| 706 | ); |
| 707 | } |
| 708 | |
| 709 | /// Function values compare the sources of their store contracts. |
| 710 | @test unsafe fn testFunctionStoreContractEquality() throws (testing::TestError) { |
| 711 | try expectAnalyzeOk( |
| 712 | "record R['r] { value: &'r u32 } fn a(out: &mut R['r], value: &'r u32) { set out.value = value; } fn b(out: &mut R['r], value: &'r u32) { set out.value = value; } fn f() { let mut op = a; set op = b; }" |
| 713 | ); |
| 714 | try expectAnalyzeOk( |
| 715 | "record R['r]: Copy { value: &'r u32 } fn a(out: &R['r], value: &'r u32) {} fn f() { let op: fn(&R['r], &'r u32) = a; }" |
| 716 | ); |
| 717 | let mut a = testResolver(); |
| 718 | let result = try resolveProgramStr( |
| 719 | &mut a, |
| 720 | "record R['r] { value: &'r u32 } fn a(out: &mut R['a], x: &'a u32, y: &'b u32) { set out.value = x; } fn b(out: &mut R['a], x: &'b u32, y: &'a u32) { set out.value = y; } fn f() { let mut op = a; set op = b; }" |
| 721 | ); |
| 722 | let error = try expectError(&result); |
| 723 | let case resolver::ErrorKind::TypeMismatch(_) = error.kind else { |
| 724 | throw testing::TestError::Failed; |
| 725 | }; |
| 726 | } |
| 727 | |
| 728 | /// A function type declares new regions for the names of its parameters, and |
| 729 | /// those names give it the store contract of a function with the same |
| 730 | /// signature. |
| 731 | @test unsafe fn testFunctionTypeRegions() throws (testing::TestError) { |
| 732 | let store: *[u8] = |
| 733 | "record R['r] { value: ?&'r u32 } fn a(out: &mut R['r], v: &'r u32) { set out.value = v; }"; |
| 734 | try expectAnalyzeOk(program( |
| 735 | &[store, "fn f() { let op: fn(&mut R['s], &'s u32) = a; }"] |
| 736 | )); |
| 737 | try expectAnalyzeOk( |
| 738 | program( |
| 739 | &[ |
| 740 | store, |
| 741 | "fn g(out: &mut R['r], x: &'r u32) { let op: fn(&mut R['s], &'s u32) = a; op(out, x); }", |
| 742 | ] |
| 743 | ) |
| 744 | ); |
| 745 | try expectAnalyzeOk( |
| 746 | program( |
| 747 | &[ |
| 748 | store, |
| 749 | "record S { f: fn(&mut R['s], &'s u32) } fn g(out: &mut R['r], x: &'r u32) { let s = S { f: a }; let op = s.f; op(out, x); }", |
| 750 | ] |
| 751 | ) |
| 752 | ); |
| 753 | try expectAnalyzeOk( |
| 754 | program( |
| 755 | &[ |
| 756 | store, |
| 757 | "fn call(op: fn(&mut R['s], &'s u32), out: &mut R['r], x: &'r u32) { op(out, x); } fn g(out: &mut R['r], x: &'r u32) { call(a, out, x); }", |
| 758 | ] |
| 759 | ) |
| 760 | ); |
| 761 | try expectProgramError( |
| 762 | program( |
| 763 | &[ |
| 764 | store, |
| 765 | "fn g(out: &mut R['r], x: &'r u32) { let op: fn(&mut R['s], &'s u32) = a; let local: u32 = 5; op(out, &local); }", |
| 766 | ] |
| 767 | ), |
| 768 | resolver::ErrorKind::Region(resolver::RegionError::Mismatch) |
| 769 | ); |
| 770 | let mut mismatch = testResolver(); |
| 771 | let result = try resolveProgramStr( |
| 772 | &mut mismatch, |
| 773 | program(&[store, "fn f() { let op: fn(&mut R['s], &'t u32) = a; }"]) |
| 774 | ); |
| 775 | let error = try expectError(&result); |
| 776 | let case resolver::ErrorKind::TypeMismatch(_) = error.kind else { |
| 777 | throw testing::TestError::Failed; |
| 778 | }; |
| 779 | try expectProgramError( |
| 780 | "fn h(x: &u32) {} fn g(x: &'r u32) { let op: fn(&'r u32) = h; }", |
| 781 | resolver::ErrorKind::Region(resolver::RegionError::Duplicate("'r")) |
| 782 | ); |
| 783 | try expectProgramError( |
| 784 | "fn call(op: fn(&'s u32), x: &'s u32) { op(x); }", |
| 785 | resolver::ErrorKind::Region(resolver::RegionError::Duplicate("'s")) |
| 786 | ); |
| 787 | } |
| 788 | |
| 789 | /// Assigned reference values retain each source through later bindings. |
| 790 | @test unsafe fn testAssignedReferenceDependencies() throws (testing::TestError) { |
| 791 | try expectProgramError( |
| 792 | "fn f() { let x: u32 = 1; let mut y: u32 = 2; let mut q = &x; { let mut p = &x; set p = &y; set q = p; } set y = 3; assert *q == 3; }", |
| 793 | resolver::ErrorKind::BorrowConflict("y") |
| 794 | ); |
| 795 | try expectProgramError( |
| 796 | "fn f(c: bool) { let x: u32 = 1; let mut y: u32 = 2; let mut q = &x; { let mut p = &x; if c { set p = &y; } set q = p; } set y = 3; assert *q == 3; }", |
| 797 | resolver::ErrorKind::BorrowConflict("y") |
| 798 | ); |
| 799 | try expectProgramError( |
| 800 | "fn f() { let x: u32 = 1; let mut y: u32 = 2; let mut q = &x; { let mut p = &x; set p = &y; let optional: ?&u32 = p; if let value = optional; *value > 0 { set q = value; } } set y = 3; }", |
| 801 | resolver::ErrorKind::BorrowConflict("y") |
| 802 | ); |
| 803 | try expectProgramError( |
| 804 | "fn f() { let x: u32 = 1; let mut y: u32 = 2; let mut q = &x; { let mut p = &x; set p = &y; let optional: ?&u32 = p; let value = optional else return; set q = value; } set y = 3; }", |
| 805 | resolver::ErrorKind::BorrowConflict("y") |
| 806 | ); |
| 807 | try expectProgramError( |
| 808 | "fn f() { let x: [u32; 1] = [1]; let mut y: [u32; 1] = [2]; let mut q = &x[..]; { let mut p = &x[..]; set p = &y[..]; match p { value => { set q = value; } } } set y[0] = 3; }", |
| 809 | resolver::ErrorKind::BorrowConflict("y") |
| 810 | ); |
| 811 | try expectAnalyzeOk( |
| 812 | "record Pair['a, 'b]: Copy { first: &'a u32, second: &'b u32 } fn f() { let x: u32 = 1; let mut y: u32 = 2; let mut q = &x; { let mut pair = Pair { first: &x, second: &x }; set pair.second = &y; set q = pair.first; } set y = 3; assert *q == 1; }" |
| 813 | ); |
| 814 | try expectAnalyzeOk( |
| 815 | "fn f() { let x: u32 = 1; let mut y: u32 = 2; { let mut q = &x; { let mut p = &x; set p = &y; set q = p; } assert *q == 2; } set y = 3; }" |
| 816 | ); |
| 817 | } |
| 818 | |
| 819 | /// A source stored by a call stays borrowed for the target binding's scope. |
| 820 | @test unsafe fn testCallStoredDependencies() throws (testing::TestError) { |
| 821 | let fill: *[u8] = |
| 822 | "record R['r]: Copy { value: ?&'r u32 } fn fill(out: &mut R['r], value: &'r u32) { set out.value = value; }"; |
| 823 | try expectProgramError( |
| 824 | program( |
| 825 | &[ |
| 826 | fill, |
| 827 | "fn f() { let mut x: u32 = 1; let mut r = R { value: nil }; fill(&mut r, &x); set x = 2; if let v = r.value { assert *v == 2; } }", |
| 828 | ] |
| 829 | ), |
| 830 | resolver::ErrorKind::BorrowConflict("x") |
| 831 | ); |
| 832 | try expectProgramError( |
| 833 | program( |
| 834 | &[ |
| 835 | fill, |
| 836 | "fn f() { let x: u32 = 1; let mut y: u32 = 2; let mut r = R { value: nil }; { let mut p = &x; set p = &y; fill(&mut r, p); } set y = 3; }", |
| 837 | ] |
| 838 | ), |
| 839 | resolver::ErrorKind::BorrowConflict("y") |
| 840 | ); |
| 841 | try expectProgramError( |
| 842 | program( |
| 843 | &[ |
| 844 | fill, |
| 845 | "fn f() { let mut x: u32 = 1; let mut r = R { value: nil }; { let out = &mut r; fill(out, &x); } set x = 2; }", |
| 846 | ] |
| 847 | ), |
| 848 | resolver::ErrorKind::BorrowConflict("x") |
| 849 | ); |
| 850 | let wrapper: *[u8] = |
| 851 | "record R['r]: Copy { value: ?&'r u32 } record Wrapper['r] { out: &'r mut R['r] } fn fill(wrapper: Wrapper['r], value: &'r u32) { set wrapper.out.value = value; }"; |
| 852 | try expectProgramError( |
| 853 | program( |
| 854 | &[ |
| 855 | wrapper, |
| 856 | "fn f() { let mut x: u32 = 1; let mut r = R { value: nil }; fill(Wrapper { out: &mut r }, &x); set x = 2; }", |
| 857 | ] |
| 858 | ), |
| 859 | resolver::ErrorKind::BorrowConflict("x") |
| 860 | ); |
| 861 | try expectProgramError( |
| 862 | program( |
| 863 | &[ |
| 864 | wrapper, |
| 865 | "fn f() { let mut x: u32 = 1; let mut r = R { value: nil }; { let wrapper = Wrapper { out: &mut r }; fill(wrapper, &x); } set x = 2; }", |
| 866 | ] |
| 867 | ), |
| 868 | resolver::ErrorKind::BorrowConflict("x") |
| 869 | ); |
| 870 | try expectAnalyzeOk( |
| 871 | program( |
| 872 | &[ |
| 873 | wrapper, |
| 874 | "fn f() { let mut first = R { value: nil }; let mut second = R { value: nil }; { let mut wrapper = Wrapper { out: &mut first }; set wrapper = Wrapper { out: &mut second }; } set second.value = nil; }", |
| 875 | ] |
| 876 | ) |
| 877 | ); |
| 878 | try expectAnalyzeOk( |
| 879 | program( |
| 880 | &[ |
| 881 | fill, |
| 882 | "fn f() { let mut x: u32 = 1; { let mut r = R { value: nil }; fill(&mut r, &x); if let v = r.value { assert *v == 1; } } set x = 2; }", |
| 883 | ] |
| 884 | ) |
| 885 | ); |
| 886 | } |
| 887 | |
| 888 | /// Unknown joins cannot prove that references fit a writable parameter position. |
| 889 | @test unsafe fn testUnknownRegionStores() throws (testing::TestError) { |
| 890 | let select: *[u8] = |
| 891 | "fn select(x: &'r u32, y: &'r u32, c: bool) -> &'r u32 { return x if c else y; }"; |
| 892 | try expectProgramError( |
| 893 | program( |
| 894 | &[ |
| 895 | select, |
| 896 | "fn put(a: &mut ?&u32, b: &mut ?&u32, v: &u32, w: &u32, c: bool) { set *(a if c else b) = select(v, w, c); }", |
| 897 | ] |
| 898 | ), |
| 899 | resolver::ErrorKind::Region(resolver::RegionError::Mismatch) |
| 900 | ); |
| 901 | try expectProgramError( |
| 902 | program( |
| 903 | &[ |
| 904 | select, |
| 905 | "fn put(a: &mut [?&u32], b: &mut [?&u32], v: &u32, w: &u32, c: bool) { set (a if c else b)[0] = select(v, w, c); }", |
| 906 | ] |
| 907 | ), |
| 908 | resolver::ErrorKind::Region(resolver::RegionError::Mismatch) |
| 909 | ); |
| 910 | try expectAnalyzeOk( |
| 911 | program( |
| 912 | &[ |
| 913 | select, |
| 914 | "fn read(v: &u32, w: &u32, c: bool) -> u32 { return *select(v, w, c); }", |
| 915 | ] |
| 916 | ) |
| 917 | ); |
| 918 | } |
| 919 | |
| 920 | /// Shared cursor contents remain live across independent cursor advances. |
| 921 | @test unsafe fn testImplicitCursorContentSource() throws (testing::TestError) { |
| 922 | try expectAnalyzeOk( |
| 923 | "record Reader['src] { bytes: &'src [u8], pos: u32 } union Token['src]: Copy { Text(&'src [u8]), Number(u8) } fn (r: &mut Reader['src]) next() -> ?Token['src] { if r.pos == r.bytes.len { return nil; } let at = r.pos; set r.pos += 1; return Token::Text(&r.bytes[at .. at + 1]); } fn demo() { let input: [u8; 2] = ['a', 'b']; let mut reader = Reader { bytes: &input[..], pos: 0 }; let first = reader.next() else return; let second = reader.next() else return; }" |
| 924 | ); |
| 925 | } |
| 926 | |
| 927 | /// Shared results from mutable contents keep conflicting writes excluded. |
| 928 | @test unsafe fn testImplicitMutableCursorContentLoan() throws (testing::TestError) { |
| 929 | try expectProgramError( |
| 930 | "record Reader['src] { bytes: &'src mut [u8] } fn (r: &'v mut Reader['src]) input() -> &'v [u8] { return &r.bytes[..]; } fn demo() { let mut input: [u8; 2] = [1, 2]; let mut reader = Reader { bytes: &mut input[..] }; let first = reader.input(); set reader.bytes[0] = 3; assert first[0] == 1; }", |
| 931 | resolver::ErrorKind::BorrowConflict("reader") |
| 932 | ); |
| 933 | } |