test: Add rejection fixtures
5094b8e699b74b54ff50cf03135c45ffe2c87bdc4259068cb2573240076f24a7
The integration driver has no check for programs the compiler must reject. Safety rules such as static mutation, reference escapes and linear discards have no end-to-end coverage. Add 27 rejection fixtures and check them in the `bin` command. Each line `//! rejects: LINE:COL: MESSAGE` must match an error of the fixture exactly as the compiler prints it, without the path and `error:`, and the compiler must report no other errors. A line listed twice expects the error twice. The fixtures are compiled with the standard library and run in the same batch as the other fixture checks. The verifier check skips them, since they produce no IL. Exact positions and messages pin each error to the construct under test, which a count of matching messages does not. Assisted-by: Claude:opus-5.5
1 parent
826d60ec
test/integration/fixtures.rad
+120 -7
| 1 | 1 | //! Compiler test fixtures in `test/tests`: compiling one to an image or to |
|
| 2 | - | //! binary IL, checking the IL snapshots of all of them, and running the ones |
|
| 3 | - | //! that have an expected exit status. |
|
| 2 | + | //! binary IL, checking the IL snapshots of all of them, running the ones |
|
| 3 | + | //! that have an expected exit status, and checking the diagnostics of the |
|
| 4 | + | //! ones the compiler must reject. |
|
| 4 | 5 | ||
| 5 | 6 | use std::fmt; |
|
| 6 | 7 | use std::fs; |
|
| 7 | 8 | use std::io; |
|
| 8 | 9 | use std::mem; |
| 18 | 19 | use std::process::Command; |
|
| 19 | 20 | ||
| 20 | 21 | /// IL snapshot checker that runs each fixture with a `.ril` file. |
|
| 21 | 22 | constant RUNNER: *[u8] = "test/runner.rv64"; |
|
| 22 | 23 | ||
| 24 | + | /// Marker of a rejection fixture. Each line `//! rejects: LINE:COL: MESSAGE` |
|
| 25 | + | /// expects one error at that position of the fixture with that message. |
|
| 26 | + | export constant REJECTS: *[u8] = "//! rejects:"; |
|
| 27 | + | ||
| 28 | + | /// Part of a compiler output line that marks an error diagnostic. |
|
| 29 | + | constant ERROR: *[u8] = ": error: "; |
|
| 30 | + | ||
| 23 | 31 | /// What the checks of one fixture need and where their results are. |
|
| 24 | 32 | record Case: Copy { |
|
| 25 | 33 | /// Fixture source path. |
|
| 26 | 34 | source: *[u8], |
|
| 35 | + | /// Fixture source text. |
|
| 36 | + | content: *[u8], |
|
| 27 | 37 | /// Path of the fixture executable. |
|
| 28 | 38 | image: *[u8], |
|
| 29 | 39 | /// Index of the IL check in the batch, if the fixture has a snapshot. |
|
| 30 | 40 | snapshot: ?u32, |
|
| 31 | 41 | /// Output of the IL check. |
| 34 | 44 | returns: ?*[u8], |
|
| 35 | 45 | /// Index of the execution in the batch, if the executable exists. |
|
| 36 | 46 | execution: ?u32, |
|
| 37 | 47 | /// Expected stored size of the read-write data as written, if any. |
|
| 38 | 48 | rwData: ?*[u8], |
|
| 49 | + | /// Whether the fixture must be rejected, so that its compilation runs in |
|
| 50 | + | /// the batch. |
|
| 51 | + | rejected: bool, |
|
| 52 | + | /// Compiler output of the rejected compilation. |
|
| 53 | + | diagnostics: *[u8], |
|
| 39 | 54 | } |
|
| 40 | 55 | ||
| 41 | 56 | /// Collect the compiler arguments that compile a fixture to the executable |
|
| 42 | 57 | /// `output`, when it ends in `.rv64`, or otherwise to binary IL in the |
|
| 43 | 58 | /// directory `output`, which must then hold `test.ril`. Returns the file the |
| 47 | 62 | { |
|
| 48 | 63 | let content = try h.load(source); |
|
| 49 | 64 | let base = harness::stripExt(source); |
|
| 50 | 65 | let isImage = mem::endsWith(output, ".rv64"); |
|
| 51 | 66 | let produced = output if isImage else h.join(&[output, "/test.ril"]); |
|
| 52 | - | let usesStd = harness::marker(content, "//! uses-std") <> nil; |
|
| 67 | + | // Rejection fixtures resolve against the complete standard library. |
|
| 68 | + | let usesStd = harness::marker(content, "//! uses-std") <> nil |
|
| 69 | + | or harness::marker(content, REJECTS) <> nil; |
|
| 53 | 70 | if isImage { |
|
| 54 | 71 | h.add(&["-o", output]); |
|
| 55 | 72 | } else { |
|
| 56 | 73 | try harness::makeDirs(output); |
|
| 57 | 74 | h.add(&["-ril", output]); |
| 147 | 164 | } |
|
| 148 | 165 | let got = h.num(layout.rwData.size); |
|
| 149 | 166 | verdict(h, c.source, what, h.join(&["expected: ", expected, ", got: ", got])); |
|
| 150 | 167 | } |
|
| 151 | 168 | ||
| 169 | + | /// Count the errors in the compiler `output`. With an `expected` line, count |
|
| 170 | + | /// only the error lines equal to it. |
|
| 171 | + | fn countErrors(output: *[u8], expected: ?*[u8]) -> u32 { |
|
| 172 | + | let mut count: u32 = 0; |
|
| 173 | + | let mut lines = mem::split(output, '\n'); |
|
| 174 | + | while let line = lines.next() { |
|
| 175 | + | if mem::find(line, ERROR) == nil { |
|
| 176 | + | continue; |
|
| 177 | + | } |
|
| 178 | + | if let text = expected { |
|
| 179 | + | if not mem::eq(line, text) { |
|
| 180 | + | continue; |
|
| 181 | + | } |
|
| 182 | + | } |
|
| 183 | + | set count += 1; |
|
| 184 | + | } |
|
| 185 | + | return count; |
|
| 186 | + | } |
|
| 187 | + | ||
| 188 | + | /// Count the lines of `content` equal to `line`. |
|
| 189 | + | fn countLines(content: *[u8], line: *[u8]) -> u32 { |
|
| 190 | + | let mut count: u32 = 0; |
|
| 191 | + | let mut lines = mem::split(content, '\n'); |
|
| 192 | + | while let other = lines.next() { |
|
| 193 | + | if mem::eq(other, line) { |
|
| 194 | + | set count += 1; |
|
| 195 | + | } |
|
| 196 | + | } |
|
| 197 | + | return count; |
|
| 198 | + | } |
|
| 199 | + | ||
| 200 | + | /// Print the error lines of the compiler `output` to standard error. |
|
| 201 | + | fn printErrors(output: *[u8]) { |
|
| 202 | + | let mut lines = mem::split(output, '\n'); |
|
| 203 | + | while let line = lines.next() { |
|
| 204 | + | if mem::find(line, ERROR) <> nil { |
|
| 205 | + | io::printError(" "); |
|
| 206 | + | io::printError(line); |
|
| 207 | + | io::printError("\n"); |
|
| 208 | + | } |
|
| 209 | + | } |
|
| 210 | + | } |
|
| 211 | + | ||
| 212 | + | /// Check the rejected compilation of a fixture against its `//! rejects:` |
|
| 213 | + | /// lines. Each line `//! rejects: LINE:COL: MESSAGE` must match one error of |
|
| 214 | + | /// the fixture as the compiler prints it, without the path and `error:`, and |
|
| 215 | + | /// the compiler must report no other errors. |
|
| 216 | + | fn checkRejects(h: &mut Harness, c: Case) { |
|
| 217 | + | let what = " rejects"; |
|
| 218 | + | let output = h.text(c.diagnostics); |
|
| 219 | + | let mut reason: ?*[u8] = nil; |
|
| 220 | + | let mut want: u32 = 0; |
|
| 221 | + | let mut lines = mem::split(c.content, '\n'); |
|
| 222 | + | while let line = lines.next() { |
|
| 223 | + | let rest = mem::stripPrefix(REJECTS, line) else continue; |
|
| 224 | + | let mut start: u32 = 0; |
|
| 225 | + | while start < rest.len and rest[start] == ' ' { |
|
| 226 | + | set start += 1; |
|
| 227 | + | } |
|
| 228 | + | let spec = &rest[start ..]; |
|
| 229 | + | let gap = mem::find(spec, ": ") else { |
|
| 230 | + | return verdict(h, c.source, what, h.join(&["invalid expectation: ", spec])); |
|
| 231 | + | }; |
|
| 232 | + | // A line listed twice expects the error twice, so with the total |
|
| 233 | + | // below the errors are exactly those listed. |
|
| 234 | + | let expected = h.join(&[c.source, ":", &spec[.. gap], ERROR, &spec[gap + 2 ..]]); |
|
| 235 | + | let times = countLines(c.content, line); |
|
| 236 | + | let got = countErrors(output, expected); |
|
| 237 | + | if reason == nil and got <> times { |
|
| 238 | + | set reason = h.join(&[ |
|
| 239 | + | "expected ", h.num(times), " of error ", spec, ", got ", h.num(got) |
|
| 240 | + | ]); |
|
| 241 | + | } |
|
| 242 | + | set want += 1; |
|
| 243 | + | } |
|
| 244 | + | let total = countErrors(output, nil); |
|
| 245 | + | if reason == nil and total <> want { |
|
| 246 | + | set reason = h.join(&["expected ", h.num(want), " errors, got ", h.num(total)]); |
|
| 247 | + | } |
|
| 248 | + | if reason <> nil { |
|
| 249 | + | printErrors(output); |
|
| 250 | + | } |
|
| 251 | + | verdict(h, c.source, what, reason); |
|
| 252 | + | } |
|
| 253 | + | ||
| 152 | 254 | /// Run the checks of the fixtures: the IL snapshot of each fixture that has |
|
| 153 | - | /// one, the exit status of each that declares one, and the stored size of its |
|
| 154 | - | /// read-write data when declared. All children run in one batch; results are |
|
| 155 | - | /// printed in the order of the fixtures. With no `tests`, all fixtures in |
|
| 156 | - | /// `test/tests` are checked. |
|
| 255 | + | /// one, the exit status of each that declares one, the stored size of its |
|
| 256 | + | /// read-write data when declared, and the diagnostics of each that must be |
|
| 257 | + | /// rejected. All children run in one batch; results are printed in the order |
|
| 258 | + | /// of the fixtures. With no `tests`, all fixtures in `test/tests` are checked. |
|
| 157 | 259 | export fn bin(h: &mut Harness, tests: *[*[u8]]) throws (sys::Error) { |
|
| 158 | 260 | if not try harness::exists(RUNNER) { |
|
| 159 | 261 | io::printError(h.join(&["error: runner binary not found: ", RUNNER, "\n"])); |
|
| 160 | 262 | io::printError("hint: run 'make test' first\n"); |
|
| 161 | 263 | set h.failed += 1; |
| 181 | 283 | verdict(h, source, "", "source not readable"); |
|
| 182 | 284 | continue; |
|
| 183 | 285 | }; |
|
| 184 | 286 | let mut c = Case { |
|
| 185 | 287 | source, |
|
| 288 | + | content, |
|
| 186 | 289 | image, |
|
| 187 | 290 | snapshot: nil, |
|
| 188 | 291 | report: h.path(&[h.num(cases.len), ".log"]), |
|
| 189 | 292 | returns: harness::marker(content, "//! returns:"), |
|
| 190 | 293 | execution: nil, |
|
| 191 | 294 | rwData: harness::marker(content, "//! rw-data-size:"), |
|
| 295 | + | rejected: harness::marker(content, REJECTS) <> nil, |
|
| 296 | + | diagnostics: h.path(&[h.num(cases.len), ".rejects.log"]), |
|
| 192 | 297 | }; |
|
| 298 | + | if c.rejected { |
|
| 299 | + | try prepare(h, source, h.path(&[h.num(cases.len), ".rv64"])); |
|
| 300 | + | let compile = h.cc(h.take(), Output::File(c.diagnostics)); |
|
| 301 | + | try! batch.append(compile, &mut h.arena); |
|
| 302 | + | } |
|
| 193 | 303 | if try harness::exists(h.join(&[harness::stripExt(source), ".ril"])) { |
|
| 194 | 304 | let target = Target::Hosted(process::Memory { |
|
| 195 | 305 | memory: 0, data: 0, stack: 1024 |
|
| 196 | 306 | }); |
|
| 197 | 307 | set c.snapshot = batch.len; |
| 235 | 345 | if c.returns == nil or c.execution <> nil { |
|
| 236 | 346 | if let expected = c.rwData { |
|
| 237 | 347 | checkRwData(h, c, expected); |
|
| 238 | 348 | } |
|
| 239 | 349 | } |
|
| 350 | + | if c.rejected { |
|
| 351 | + | checkRejects(h, c); |
|
| 352 | + | } |
|
| 240 | 353 | } |
|
| 241 | 354 | io::print("\ntest result: "); |
|
| 242 | 355 | io::print("ok. " if h.failed == 0 else "FAILED. "); |
|
| 243 | 356 | let passed = h.passed; |
|
| 244 | 357 | let failed = h.failed; |
test/integration/verify.rad
+7 -2
| 47 | 47 | return h.pass(); |
|
| 48 | 48 | } |
|
| 49 | 49 | return h.fail("verify", what); |
|
| 50 | 50 | } |
|
| 51 | 51 | ||
| 52 | - | /// Compile every fixture that has no native assembly to binary IL, verify the |
|
| 53 | - | /// packages with `checker`, and compare the verdicts with the expected ones. |
|
| 52 | + | /// Compile every fixture that has no native assembly and that the compiler |
|
| 53 | + | /// accepts to binary IL, verify the packages with `checker`, and compare the |
|
| 54 | + | /// verdicts with the expected ones. |
|
| 54 | 55 | export fn check(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) { |
|
| 55 | 56 | try h.begin("verify"); |
|
| 56 | 57 | let mut checker: *[u8] = "test/verify/check.rv64"; |
|
| 57 | 58 | if args.len == 1 { |
|
| 58 | 59 | set checker = args[0]; |
| 65 | 66 | if try harness::exists(h.join(&[base, ".ras"])) |
|
| 66 | 67 | or try harness::exists(h.join(&[base, ".start.ras"])) |
|
| 67 | 68 | { |
|
| 68 | 69 | continue; |
|
| 69 | 70 | } |
|
| 71 | + | // Rejection fixtures produce no IL. |
|
| 72 | + | if harness::marker(try h.load(source), fixtures::REJECTS) <> nil { |
|
| 73 | + | continue; |
|
| 74 | + | } |
|
| 70 | 75 | let name = &base["test/tests/".len ..]; |
|
| 71 | 76 | try fixtures::prepare(h, source, h.path(&[name])); |
|
| 72 | 77 | try! batch.append(h.cc(h.take(), h.out(&[name, ".log"])), &mut h.arena); |
|
| 73 | 78 | try! names.append(name, &mut h.arena); |
|
| 74 | 79 | } |
test/tests/address.literal.escape.reject.rad
added
+37 -0
| 1 | + | //! rejects: 16:22: type mismatch: expected *mut [i32], got &mut [i32] |
|
| 2 | + | //! rejects: 21:28: type mismatch: expected *record { a: i32, b: i32 }, got &record { a: i32, b: i32 } |
|
| 3 | + | //! rejects: 26:21: type mismatch: expected *union { One: <record>, Nothing: void }, got &union { One: <record>, Nothing: void } |
|
| 4 | + | //! rejects: 31:32: type mismatch: expected *mut record { a: i32, b: i32 }, got &mut record { a: i32, b: i32 } |
|
| 5 | + | //! rejects: 36:25: type mismatch: expected *union { One: <record>, Nothing: void }, got &mut union { One: <record>, Nothing: void } |
|
| 6 | + | //! The address of a literal that is not shared constant data cannot become an |
|
| 7 | + | //! owned pointer: a temporary lives only until the end of its use, and a |
|
| 8 | + | //! mutable one is fresh storage that only a reference can address. |
|
| 9 | + | ||
| 10 | + | record R: Copy { a: i32, b: i32 } |
|
| 11 | + | ||
| 12 | + | union V: Copy { One(i32), Nothing } |
|
| 13 | + | ||
| 14 | + | /// Mutable array literal as an owned pointer. |
|
| 15 | + | fn mutableArray() -> *mut [i32] { |
|
| 16 | + | return &mut [1, 2]; |
|
| 17 | + | } |
|
| 18 | + | ||
| 19 | + | /// Record built from a runtime value as an owned pointer. |
|
| 20 | + | fn runtimeRecord(x: i32) -> *R { |
|
| 21 | + | return &R { a: x, b: 1 }; |
|
| 22 | + | } |
|
| 23 | + | ||
| 24 | + | /// Union variant built from a runtime value as an owned pointer. |
|
| 25 | + | fn runtimeVariant(x: i32) -> *V { |
|
| 26 | + | return &V::One(x); |
|
| 27 | + | } |
|
| 28 | + | ||
| 29 | + | /// Mutable record as an owned mutable pointer. |
|
| 30 | + | fn mutableRecord() -> *mut R { |
|
| 31 | + | return &mut R { a: 1, b: 2 }; |
|
| 32 | + | } |
|
| 33 | + | ||
| 34 | + | /// Mutable variant as an owned pointer. |
|
| 35 | + | fn mutableVariant() -> *V { |
|
| 36 | + | return &mut V::One(1); |
|
| 37 | + | } |
test/tests/address.literal.local.mutable.reject.rad
added
+9 -0
| 1 | + | //! rejects: 8:33: reference local requires an immutable binding to existing storage |
|
| 2 | + | //! A mutable reference to a temporary cannot be kept in a local either. |
|
| 3 | + | ||
| 4 | + | record R: Copy { a: i32, b: i32 } |
|
| 5 | + | ||
| 6 | + | /// Mutable reference to a temporary kept in a local. |
|
| 7 | + | fn keptMutable() { |
|
| 8 | + | let r = &mut R { a: 1, b: 2 }; |
|
| 9 | + | } |
test/tests/address.literal.local.reject.rad
added
+10 -0
| 1 | + | //! rejects: 9:29: reference local requires an immutable binding to existing storage |
|
| 2 | + | //! A reference to a temporary cannot be kept in a local, because the temporary |
|
| 3 | + | //! ends with the expression that made it. |
|
| 4 | + | ||
| 5 | + | record R: Copy { a: i32, b: i32 } |
|
| 6 | + | ||
| 7 | + | /// Reference to a temporary kept in a local. |
|
| 8 | + | fn keptRecord(x: i32) { |
|
| 9 | + | let r = &R { a: x, b: 1 }; |
|
| 10 | + | } |
test/tests/address.value.escape.reject.rad
added
+96 -0
| 1 | + | //! rejects: 35:18: type mismatch: expected *i32, got &i32 |
|
| 2 | + | //! rejects: 40:18: type mismatch: expected *record { a: i32, b: i32 }, got &record { a: i32, b: i32 } |
|
| 3 | + | //! rejects: 45:19: type mismatch: expected *i32, got &i32 |
|
| 4 | + | //! rejects: 50:22: type mismatch: expected *i32, got &i32 |
|
| 5 | + | //! rejects: 55:22: type mismatch: expected *u64, got &u64 |
|
| 6 | + | //! rejects: 60:20: type mismatch: expected *bool, got &bool |
|
| 7 | + | //! rejects: 65:27: type mismatch: expected *i32, got &i32 |
|
| 8 | + | //! rejects: 70:20: type mismatch: expected *i32, got &i32 |
|
| 9 | + | //! rejects: 75:22: type mismatch: expected *u32, got &u32 |
|
| 10 | + | //! rejects: 80:22: type mismatch: expected *mut i32, got &mut i32 |
|
| 11 | + | //! rejects: 85:23: type mismatch: expected *mut i32, got &mut i32 |
|
| 12 | + | //! rejects: 90:13: type mismatch: expected *i32, got &<int> |
|
| 13 | + | //! rejects: 95:15: type mismatch: expected *u32, got &u32 |
|
| 14 | + | //! The address of a value that is not a place is a reference to a temporary, |
|
| 15 | + | //! so it cannot become an owned pointer that outlives the expression. |
|
| 16 | + | ||
| 17 | + | record R: Copy { a: i32, b: i32 } |
|
| 18 | + | ||
| 19 | + | record One: Copy { n: u32 } |
|
| 20 | + | ||
| 21 | + | fn five() -> i32 { |
|
| 22 | + | return 5; |
|
| 23 | + | } |
|
| 24 | + | ||
| 25 | + | fn pair() -> R { |
|
| 26 | + | return R { a: 3, b: 4 }; |
|
| 27 | + | } |
|
| 28 | + | ||
| 29 | + | fn single() -> One { |
|
| 30 | + | return One { n: 7 }; |
|
| 31 | + | } |
|
| 32 | + | ||
| 33 | + | /// Call result as an owned pointer. |
|
| 34 | + | fn callResult() -> *i32 { |
|
| 35 | + | return &five(); |
|
| 36 | + | } |
|
| 37 | + | ||
| 38 | + | /// Aggregate call result as an owned pointer. |
|
| 39 | + | fn aggregateResult() -> *R { |
|
| 40 | + | return &pair(); |
|
| 41 | + | } |
|
| 42 | + | ||
| 43 | + | /// Arithmetic result as an owned pointer. |
|
| 44 | + | fn arithmetic(x: i32) -> *i32 { |
|
| 45 | + | return &(x + 1); |
|
| 46 | + | } |
|
| 47 | + | ||
| 48 | + | /// Constant cast result as an owned pointer. |
|
| 49 | + | fn constantCast() -> *i32 { |
|
| 50 | + | return &(5 as i32); |
|
| 51 | + | } |
|
| 52 | + | ||
| 53 | + | /// Runtime cast result as an owned pointer. |
|
| 54 | + | fn runtimeCast(x: i32) -> *u64 { |
|
| 55 | + | return &(x as u64); |
|
| 56 | + | } |
|
| 57 | + | ||
| 58 | + | /// Comparison result as an owned pointer. |
|
| 59 | + | fn comparison(x: i32) -> *bool { |
|
| 60 | + | return &(x == 1); |
|
| 61 | + | } |
|
| 62 | + | ||
| 63 | + | /// Conditional result as an owned pointer. |
|
| 64 | + | fn conditional(x: i32, c: bool) -> *i32 { |
|
| 65 | + | return &(x if c else 0); |
|
| 66 | + | } |
|
| 67 | + | ||
| 68 | + | /// Field of a call result as an owned pointer. |
|
| 69 | + | fn callField() -> *i32 { |
|
| 70 | + | return &pair().a; |
|
| 71 | + | } |
|
| 72 | + | ||
| 73 | + | /// Field of a scalar record call result as an owned pointer. |
|
| 74 | + | fn scalarField() -> *u32 { |
|
| 75 | + | return &single().n; |
|
| 76 | + | } |
|
| 77 | + | ||
| 78 | + | /// Mutable address of a call result as an owned pointer. |
|
| 79 | + | fn mutableCall() -> *mut i32 { |
|
| 80 | + | return &mut five(); |
|
| 81 | + | } |
|
| 82 | + | ||
| 83 | + | /// Mutable address of an arithmetic result as an owned pointer. |
|
| 84 | + | fn mutableArithmetic(x: i32) -> *mut i32 { |
|
| 85 | + | return &mut (x + 1); |
|
| 86 | + | } |
|
| 87 | + | ||
| 88 | + | /// Number literal as an owned pointer. |
|
| 89 | + | fn number() -> *i32 { |
|
| 90 | + | return &5; |
|
| 91 | + | } |
|
| 92 | + | ||
| 93 | + | /// Constant array length as an owned pointer. |
|
| 94 | + | fn arrayLength(a: [i32; 3]) -> *u32 { |
|
| 95 | + | return &a.len; |
|
| 96 | + | } |
test/tests/address.value.linear.field.reject.rad
added
+15 -0
| 1 | + | //! rejects: 14:29: linear value cannot be discarded |
|
| 2 | + | //! The address of a field of a linear call result discards the rest of the |
|
| 3 | + | //! linear value. |
|
| 4 | + | ||
| 5 | + | record M: Once { n: u32 } |
|
| 6 | + | ||
| 7 | + | fn makeRecord() -> M { |
|
| 8 | + | return M { n: 1 }; |
|
| 9 | + | } |
|
| 10 | + | ||
| 11 | + | fn peekField(n: &u32) {} |
|
| 12 | + | ||
| 13 | + | fn field() { |
|
| 14 | + | peekField(&makeRecord().n); |
|
| 15 | + | } |
test/tests/address.value.linear.mutable.reject.rad
added
+14 -0
| 1 | + | //! rejects: 13:21: linear value cannot be discarded |
|
| 2 | + | //! A mutable address of a linear call result is rejected as well. |
|
| 3 | + | ||
| 4 | + | union C: Once { Value(u32) } |
|
| 5 | + | ||
| 6 | + | fn make() -> C { |
|
| 7 | + | return C::Value(1); |
|
| 8 | + | } |
|
| 9 | + | ||
| 10 | + | fn touch(c: &mut C) {} |
|
| 11 | + | ||
| 12 | + | fn exclusive() { |
|
| 13 | + | touch(&mut make()); |
|
| 14 | + | } |
test/tests/address.value.linear.reject.rad
added
+15 -0
| 1 | + | //! rejects: 14:16: linear value cannot be discarded |
|
| 2 | + | //! Taking the address of a linear call result borrows a temporary that nothing |
|
| 3 | + | //! consumes, so the value is discarded. |
|
| 4 | + | ||
| 5 | + | union C: Once { Value(u32) } |
|
| 6 | + | ||
| 7 | + | fn make() -> C { |
|
| 8 | + | return C::Value(1); |
|
| 9 | + | } |
|
| 10 | + | ||
| 11 | + | fn peek(c: &C) {} |
|
| 12 | + | ||
| 13 | + | fn shared() { |
|
| 14 | + | peek(&make()); |
|
| 15 | + | } |
test/tests/address.value.local.mutable.reject.rad
added
+14 -0
| 1 | + | //! rejects: 13:25: reference local requires an immutable binding to existing storage |
|
| 2 | + | //! A mutable reference to a field of a temporary cannot be kept in a local |
|
| 3 | + | //! either. |
|
| 4 | + | ||
| 5 | + | record R: Copy { a: i32, b: i32 } |
|
| 6 | + | ||
| 7 | + | fn pair() -> R { |
|
| 8 | + | return R { a: 3, b: 4 }; |
|
| 9 | + | } |
|
| 10 | + | ||
| 11 | + | /// Mutable reference to a field of a call result kept in a local. |
|
| 12 | + | fn keepField() { |
|
| 13 | + | let r = &mut pair().a; |
|
| 14 | + | } |
test/tests/address.value.local.reject.rad
added
+12 -0
| 1 | + | //! rejects: 11:19: reference local requires an immutable binding to existing storage |
|
| 2 | + | //! A reference to a temporary cannot be kept in a local, because the temporary |
|
| 3 | + | //! ends with the expression that made it. |
|
| 4 | + | ||
| 5 | + | fn five() -> i32 { |
|
| 6 | + | return 5; |
|
| 7 | + | } |
|
| 8 | + | ||
| 9 | + | /// Reference to a call result kept in a local. |
|
| 10 | + | fn keepCall() { |
|
| 11 | + | let r = &five(); |
|
| 12 | + | } |
test/tests/alloc.arena.stale.reject.rad
added
+23 -0
| 1 | + | //! rejects: 22:18: conflicting borrow of 'arena' |
|
| 2 | + | //! A borrowed allocator cannot outlive a move of its arena. The arena moves |
|
| 3 | + | //! into `moved` while `stale` still borrows it, so the old copy and the moved |
|
| 4 | + | //! arena cannot hand out the same bytes. |
|
| 5 | + | ||
| 6 | + | use std::lang::alloc; |
|
| 7 | + | ||
| 8 | + | /// Grow a slice through `stale`, after the arena moved to a new stack slot. |
|
| 9 | + | fn moved(arena: alloc::Arena, stale: &mut opaque alloc::Allocator) -> i32 { |
|
| 10 | + | let mut arena = arena; |
|
| 11 | + | let mut s: *mut [u32] = &mut []; |
|
| 12 | + | try! s.reserve(16, stale); |
|
| 13 | + | for i in 0..16 { |
|
| 14 | + | try! s.append(77, stale); |
|
| 15 | + | } |
|
| 16 | + | return s.len as i32; |
|
| 17 | + | } |
|
| 18 | + | ||
| 19 | + | @default fn main(buf: *mut [u8; 4096]) -> i32 { |
|
| 20 | + | let mut arena = alloc::new(&mut buf[..]); |
|
| 21 | + | let stale: &mut opaque alloc::Allocator = &mut arena; |
|
| 22 | + | return moved(arena, stale); |
|
| 23 | + | } |
test/tests/alloc.nodearena.stale.reject.rad
added
+21 -0
| 1 | + | //! rejects: 12:17: conflicting borrow of 'na' |
|
| 2 | + | //! A node arena borrowed as an allocator cannot be moved while the borrow is |
|
| 3 | + | //! live, so no allocator outlives the arena record it came from. |
|
| 4 | + | ||
| 5 | + | use std::lang::alloc; |
|
| 6 | + | use std::lang::ast; |
|
| 7 | + | ||
| 8 | + | /// Grow a slice through `stale` after the node arena moved to a new stack slot. |
|
| 9 | + | fn moved(na: ast::NodeArena) -> u32 { |
|
| 10 | + | let mut na = na; |
|
| 11 | + | let stale: &mut opaque alloc::Allocator = &mut na; |
|
| 12 | + | let other = na; |
|
| 13 | + | let mut s: *mut [u32] = &mut []; |
|
| 14 | + | try! s.append(1, stale); |
|
| 15 | + | return s.len; |
|
| 16 | + | } |
|
| 17 | + | ||
| 18 | + | @default fn main(buf: *mut [u8; 4096]) -> i32 { |
|
| 19 | + | let na = ast::nodeArena(&mut buf[..]); |
|
| 20 | + | return moved(na) as i32; |
|
| 21 | + | } |
test/tests/borrow.index.overlap.reject.rad
added
+24 -0
| 1 | + | //! rejects: 19:49: conflicting borrow of 'owner' |
|
| 2 | + | //! Two mutable borrows through indices of the same owned slice field can |
|
| 3 | + | //! reach the same element, even though other fields of the record are |
|
| 4 | + | //! disjoint from it. |
|
| 5 | + | ||
| 6 | + | /// A record with two slice fields. |
|
| 7 | + | record Owner { |
|
| 8 | + | /// First slice. |
|
| 9 | + | first: *mut [u32], |
|
| 10 | + | /// Second slice. |
|
| 11 | + | second: *mut [u32], |
|
| 12 | + | } |
|
| 13 | + | ||
| 14 | + | /// Take two mutable references. |
|
| 15 | + | fn take(a: &mut u32, b: &mut u32) {} |
|
| 16 | + | ||
| 17 | + | /// Borrow `first[0]` and `first[1]` together. |
|
| 18 | + | fn overlap(owner: &mut Owner) { |
|
| 19 | + | take(&mut owner.first[0], &mut owner.first[1]); |
|
| 20 | + | } |
|
| 21 | + | ||
| 22 | + | @default fn main() -> i32 { |
|
| 23 | + | return 0; |
|
| 24 | + | } |
test/tests/constant.mutable.pointer.reject.rad
added
+14 -0
| 1 | + | //! rejects: 6:22: a constant cannot hold a mutable pointer |
|
| 2 | + | //! A constant cannot hold an owned mutable slice, because each use of the |
|
| 3 | + | //! constant would be a new owned handle to the same data. |
|
| 4 | + | ||
| 5 | + | /// Constant that would give out owned handles to one array. |
|
| 6 | + | constant C: *mut [u32] = &mut [1, 2]; |
|
| 7 | + | ||
| 8 | + | @default fn main() -> i32 { |
|
| 9 | + | let a = C; |
|
| 10 | + | let b = C; |
|
| 11 | + | set a[0] = 9; |
|
| 12 | + | set b[0] = 42; |
|
| 13 | + | return a[0] as i32; |
|
| 14 | + | } |
test/tests/entry.storage.reject.rad
added
+163 -0
| 1 | + | //! rejects: 31:49: parameter of the default function after the environment must have type `*mut T` |
|
| 2 | + | //! rejects: 36:53: parameter of the default function after the environment must have type `*mut T` |
|
| 3 | + | //! rejects: 41:46: parameter of the default function after the environment must have type `*mut T` |
|
| 4 | + | //! rejects: 46:44: parameter of the default function after the environment must have type `*mut T` |
|
| 5 | + | //! rejects: 51:30: parameter of the default function after the environment must have type `*mut T` |
|
| 6 | + | //! rejects: 56:39: parameter of the default function after the environment must have type `*mut T` |
|
| 7 | + | //! rejects: 61:42: parameter of the default function after the environment must have type `*mut T` |
|
| 8 | + | //! rejects: 66:53: parameter of the default function after the environment must have type `*mut T` |
|
| 9 | + | //! rejects: 71:49: parameter of the default function after the environment must have type `*mut T` |
|
| 10 | + | //! rejects: 76:37: type of a default function storage parameter must be valid when zero-filled |
|
| 11 | + | //! rejects: 81:46: type of a default function storage parameter must be valid when zero-filled |
|
| 12 | + | //! rejects: 86:34: type of a default function storage parameter must be valid when zero-filled |
|
| 13 | + | //! rejects: 97:39: type of a default function storage parameter must be valid when zero-filled |
|
| 14 | + | //! rejects: 110:28: type of a default function storage parameter must be valid when zero-filled |
|
| 15 | + | //! rejects: 123:28: type of a default function storage parameter must be valid when zero-filled |
|
| 16 | + | //! rejects: 136:40: type of a default function storage parameter must be valid when zero-filled |
|
| 17 | + | //! rejects: 147:45: type of a default function storage parameter must be valid when zero-filled |
|
| 18 | + | //! rejects: 158:29: type of a default function storage parameter must be valid when zero-filled |
|
| 19 | + | //! Parameters of the default function after the environment must be owned |
|
| 20 | + | //! mutable pointers to types that are valid when zero-filled. |
|
| 21 | + | ||
| 22 | + | use std::sys; |
|
| 23 | + | ||
| 24 | + | /// Record that is valid when zero-filled. |
|
| 25 | + | record Good { |
|
| 26 | + | /// Integer field. |
|
| 27 | + | n: u32, |
|
| 28 | + | } |
|
| 29 | + | ||
| 30 | + | /// Immutable pointer to an array. |
|
| 31 | + | @default fn immutable(env: *sys::Env, a: *[u8; 8]) -> i32 { |
|
| 32 | + | return 0; |
|
| 33 | + | } |
|
| 34 | + | ||
| 35 | + | /// Reference to an array. |
|
| 36 | + | @default fn reference(env: *sys::Env, a: &mut [u8; 8]) -> i32 { |
|
| 37 | + | return 0; |
|
| 38 | + | } |
|
| 39 | + | ||
| 40 | + | /// Slice of unknown size. |
|
| 41 | + | @default fn slice(env: *sys::Env, a: *mut [u8]) -> i32 { |
|
| 42 | + | return 0; |
|
| 43 | + | } |
|
| 44 | + | ||
| 45 | + | /// Unsafe pointer to an array. |
|
| 46 | + | @default fn unsafePtr(a: *unsafe mut [u8; 8]) -> i32 { |
|
| 47 | + | return 0; |
|
| 48 | + | } |
|
| 49 | + | ||
| 50 | + | /// Array by value. |
|
| 51 | + | @default fn byValue(a: [u8; 8]) -> i32 { |
|
| 52 | + | return 0; |
|
| 53 | + | } |
|
| 54 | + | ||
| 55 | + | /// Ordinary extra parameter after the environment. |
|
| 56 | + | @default fn scalar(env: *sys::Env, n: u32) -> i32 { |
|
| 57 | + | return 0; |
|
| 58 | + | } |
|
| 59 | + | ||
| 60 | + | /// Extra parameter after a valid storage parameter. |
|
| 61 | + | @default fn trailing(a: *mut [u8; 8], n: u32) -> i32 { |
|
| 62 | + | return 0; |
|
| 63 | + | } |
|
| 64 | + | ||
| 65 | + | /// Reference to a record. |
|
| 66 | + | @default fn recordReference(env: *sys::Env, a: &mut Good) -> i32 { |
|
| 67 | + | return 0; |
|
| 68 | + | } |
|
| 69 | + | ||
| 70 | + | /// Immutable pointer to a record. |
|
| 71 | + | @default fn recordImmutable(env: *sys::Env, a: *Good) -> i32 { |
|
| 72 | + | return 0; |
|
| 73 | + | } |
|
| 74 | + | ||
| 75 | + | /// Pointer elements are not valid when zero-filled. |
|
| 76 | + | @default fn pointers(a: *mut [*u8; 8]) -> i32 { |
|
| 77 | + | return 0; |
|
| 78 | + | } |
|
| 79 | + | ||
| 80 | + | /// A pointer is not valid when zero-filled. |
|
| 81 | + | @default fn pointer(env: *sys::Env, a: *mut *u8) -> i32 { |
|
| 82 | + | return 0; |
|
| 83 | + | } |
|
| 84 | + | ||
| 85 | + | /// Function pointers are not valid when zero-filled. |
|
| 86 | + | @default fn functions(a: *mut fn()) -> i32 { |
|
| 87 | + | return 0; |
|
| 88 | + | } |
|
| 89 | + | ||
| 90 | + | /// A record with a pointer field is not valid when zero-filled. |
|
| 91 | + | record Linked { |
|
| 92 | + | /// Pointer field. |
|
| 93 | + | next: *Linked, |
|
| 94 | + | } |
|
| 95 | + | ||
| 96 | + | /// Record elements are valid only when all their fields are. |
|
| 97 | + | @default fn records(a: *mut [Linked; 8]) -> i32 { |
|
| 98 | + | return 0; |
|
| 99 | + | } |
|
| 100 | + | ||
| 101 | + | /// Record that holds a nested record with a pointer field. |
|
| 102 | + | record Nested { |
|
| 103 | + | /// Integer field. |
|
| 104 | + | n: u32, |
|
| 105 | + | /// Nested record. |
|
| 106 | + | inner: Linked, |
|
| 107 | + | } |
|
| 108 | + | ||
| 109 | + | /// A nested record counts. |
|
| 110 | + | @default fn nested(a: *mut Nested) -> i32 { |
|
| 111 | + | return 0; |
|
| 112 | + | } |
|
| 113 | + | ||
| 114 | + | /// Union whose zero tag has a pointer payload. |
|
| 115 | + | union Pointed { |
|
| 116 | + | /// Variant with tag 0. |
|
| 117 | + | First(*u8), |
|
| 118 | + | /// Variant without payload. |
|
| 119 | + | Second, |
|
| 120 | + | } |
|
| 121 | + | ||
| 122 | + | /// A union is valid when zero-filled only if its tag 0 variant is. |
|
| 123 | + | @default fn unions(a: *mut Pointed) -> i32 { |
|
| 124 | + | return 0; |
|
| 125 | + | } |
|
| 126 | + | ||
| 127 | + | /// Union without a variant with tag 0. |
|
| 128 | + | union Late { |
|
| 129 | + | /// Explicit tag 1. |
|
| 130 | + | First = 1, |
|
| 131 | + | /// Explicit tag 2. |
|
| 132 | + | Second = 2, |
|
| 133 | + | } |
|
| 134 | + | ||
| 135 | + | /// A union without tag 0 has no zero value. |
|
| 136 | + | @default fn lateUnions(a: *mut [Late; 2]) -> i32 { |
|
| 137 | + | return 0; |
|
| 138 | + | } |
|
| 139 | + | ||
| 140 | + | /// Record with a field of a union without tag 0. |
|
| 141 | + | record Holder { |
|
| 142 | + | /// Union field. |
|
| 143 | + | late: Late, |
|
| 144 | + | } |
|
| 145 | + | ||
| 146 | + | /// A record with such a field has no zero value. |
|
| 147 | + | @default fn holders(env: *sys::Env, a: *mut Holder) -> i32 { |
|
| 148 | + | return 0; |
|
| 149 | + | } |
|
| 150 | + | ||
| 151 | + | /// Record that must be consumed once. |
|
| 152 | + | record Ticket: Once { |
|
| 153 | + | /// Integer field. |
|
| 154 | + | n: u32, |
|
| 155 | + | } |
|
| 156 | + | ||
| 157 | + | /// Linear types are not valid when zero-filled. |
|
| 158 | + | @default fn tickets(a: *mut Ticket) -> i32 { |
|
| 159 | + | return 0; |
|
| 160 | + | } |
|
| 161 | + | ||
| 162 | + | /// Storage-like parameters of an ordinary function are ordinary parameters. |
|
| 163 | + | fn ordinary(a: *[u8; 8], b: &mut [bool; 2], c: *mut [u8], d: *mut *u8) {} |
test/tests/infer.empty.array.reject.rad
added
+7 -0
| 1 | + | //! rejects: 5:19: cannot infer type from context; add an explicit type annotation |
|
| 2 | + | ||
| 3 | + | /// An empty array has no element type context. |
|
| 4 | + | @default fn main() -> u64 { |
|
| 5 | + | let values = []; |
|
| 6 | + | return 0; |
|
| 7 | + | } |
test/tests/infer.nested.integer.reject.rad
added
+12 -0
| 1 | + | //! rejects: 9:18: type mismatch: expected *[u8], got *[<int>]; add an explicit integer or element type |
|
| 2 | + | ||
| 3 | + | /// Accept bytes with a concrete element type. |
|
| 4 | + | fn useBytes(bytes: *[u8]) {} |
|
| 5 | + | ||
| 6 | + | /// An integer array without element type context needs an annotation. |
|
| 7 | + | @default fn main() -> u64 { |
|
| 8 | + | for bytes in &[&[255], &[0, 3]] { |
|
| 9 | + | useBytes(bytes); |
|
| 10 | + | } |
|
| 11 | + | return 0; |
|
| 12 | + | } |
test/tests/match.catchall.reject.rad
added
+28 -0
| 1 | + | //! rejects: 10:26: match catch-all prong must be last |
|
| 2 | + | //! rejects: 18:26: match catch-all prong must be last |
|
| 3 | + | //! rejects: 26:26: match catch-all prong must be last |
|
| 4 | + | //! Prongs after an unguarded catch-all can never run. |
|
| 5 | + | ||
| 6 | + | /// `else` followed by a case. |
|
| 7 | + | fn afterElse(x: u32) -> u32 { |
|
| 8 | + | match x { |
|
| 9 | + | else => return 1, |
|
| 10 | + | case 2 => return 2, |
|
| 11 | + | } |
|
| 12 | + | } |
|
| 13 | + | ||
| 14 | + | /// A wildcard case followed by a case. |
|
| 15 | + | fn afterWildcard(x: u32) -> u32 { |
|
| 16 | + | match x { |
|
| 17 | + | case _ => return 1, |
|
| 18 | + | case 2 => return 2, |
|
| 19 | + | } |
|
| 20 | + | } |
|
| 21 | + | ||
| 22 | + | /// A binding followed by a case. |
|
| 23 | + | fn afterBinding(x: u32) -> u32 { |
|
| 24 | + | match x { |
|
| 25 | + | value => return value, |
|
| 26 | + | case 2 => return 2, |
|
| 27 | + | } |
|
| 28 | + | } |
test/tests/match.variant.fields.reject.rad
added
+12 -0
| 1 | + | //! rejects: 11:5: union match non-exhaustive: missing case for variant 'Pair' |
|
| 2 | + | //! A literal in a later field of a variant refines the pattern, so the variant |
|
| 3 | + | //! is not covered and the match needs an `else`. |
|
| 4 | + | ||
| 5 | + | union V: Copy { Pair(u32, u32), Nothing } |
|
| 6 | + | ||
| 7 | + | fn f(v: V) -> u32 { |
|
| 8 | + | match v { |
|
| 9 | + | case V::Pair(_, 7) => return 1, |
|
| 10 | + | case V::Nothing => return 2, |
|
| 11 | + | } |
|
| 12 | + | } |
test/tests/parser.keyword.reject.rad
added
+7 -0
| 1 | + | //! rejects: 5:9: reserved keyword cannot be used as an identifier, got `record` |
|
| 2 | + | ||
| 3 | + | /// A parser failure is a supported rejection fixture. |
|
| 4 | + | @default fn main() -> u64 { |
|
| 5 | + | let record: u32 = 1; |
|
| 6 | + | return 0; |
|
| 7 | + | } |
test/tests/placeholder.value.reject.rad
added
+20 -0
| 1 | + | //! rejects: 8:18: placeholder cannot be used as a value |
|
| 2 | + | //! rejects: 14:12: placeholder cannot be used as a value |
|
| 3 | + | //! rejects: 19:21: placeholder cannot be used as a value |
|
| 4 | + | //! A placeholder is a pattern, so it cannot supply a value. |
|
| 5 | + | ||
| 6 | + | /// Placeholder as a binding initializer. |
|
| 7 | + | fn initializer() -> u32 { |
|
| 8 | + | let x: u32 = _; |
|
| 9 | + | return x; |
|
| 10 | + | } |
|
| 11 | + | ||
| 12 | + | /// Placeholder as a return value. |
|
| 13 | + | fn returned() -> u32 { |
|
| 14 | + | return _; |
|
| 15 | + | } |
|
| 16 | + | ||
| 17 | + | /// Placeholder as a call argument. |
|
| 18 | + | fn argument(value: u32) -> u32 { |
|
| 19 | + | return argument(_); |
|
| 20 | + | } |
test/tests/scope.local.overflow.reject.rad
added
+40 -0
| 1 | + | //! rejects: 38:17: too many symbols in one scope |
|
| 2 | + | //! A local scope holds a fixed number of symbols. The 33rd local overflows it |
|
| 3 | + | //! and the error names the scope limit. |
|
| 4 | + | ||
| 5 | + | @default fn main() -> i32 { |
|
| 6 | + | let a: u8 = 0; |
|
| 7 | + | let b: u8 = 0; |
|
| 8 | + | let c: u8 = 0; |
|
| 9 | + | let d: u8 = 0; |
|
| 10 | + | let e: u8 = 0; |
|
| 11 | + | let f: u8 = 0; |
|
| 12 | + | let g: u8 = 0; |
|
| 13 | + | let h: u8 = 0; |
|
| 14 | + | let i: u8 = 0; |
|
| 15 | + | let j: u8 = 0; |
|
| 16 | + | let k: u8 = 0; |
|
| 17 | + | let l: u8 = 0; |
|
| 18 | + | let m: u8 = 0; |
|
| 19 | + | let n: u8 = 0; |
|
| 20 | + | let o: u8 = 0; |
|
| 21 | + | let p: u8 = 0; |
|
| 22 | + | let q: u8 = 0; |
|
| 23 | + | let r: u8 = 0; |
|
| 24 | + | let s: u8 = 0; |
|
| 25 | + | let t: u8 = 0; |
|
| 26 | + | let u: u8 = 0; |
|
| 27 | + | let v: u8 = 0; |
|
| 28 | + | let w: u8 = 0; |
|
| 29 | + | let x: u8 = 0; |
|
| 30 | + | let y: u8 = 0; |
|
| 31 | + | let z: u8 = 0; |
|
| 32 | + | let A: u8 = 0; |
|
| 33 | + | let B: u8 = 0; |
|
| 34 | + | let C: u8 = 0; |
|
| 35 | + | let D: u8 = 0; |
|
| 36 | + | let E: u8 = 0; |
|
| 37 | + | let F: u8 = 0; |
|
| 38 | + | let G: u8 = 0; |
|
| 39 | + | return 0; |
|
| 40 | + | } |
test/tests/static.move.reject.rad
added
+25 -0
| 1 | + | //! rejects: 19:13: cannot move a value out of a static outside unsafe code |
|
| 2 | + | //! Safe code cannot move a value out of a static, because the static would |
|
| 3 | + | //! still hold it and the value would have two owners. |
|
| 4 | + | ||
| 5 | + | /// Record that moves when it is used by value. |
|
| 6 | + | record Cell { |
|
| 7 | + | /// Counter. |
|
| 8 | + | count: u32, |
|
| 9 | + | } |
|
| 10 | + | ||
| 11 | + | /// Static record. |
|
| 12 | + | static CELL: Cell = Cell { count: 0 }; |
|
| 13 | + | ||
| 14 | + | /// Helper that takes ownership of a cell. |
|
| 15 | + | fn consume(cell: Cell) {} |
|
| 16 | + | ||
| 17 | + | /// Move the cell out twice. |
|
| 18 | + | fn moveTwice() { |
|
| 19 | + | consume(CELL); |
|
| 20 | + | consume(CELL); |
|
| 21 | + | } |
|
| 22 | + | ||
| 23 | + | @default fn main() -> i32 { |
|
| 24 | + | return 0; |
|
| 25 | + | } |
test/tests/static.mutation.reject.rad
added
+149 -0
| 1 | + | //! rejects: 66:9: cannot mutate a static outside unsafe code |
|
| 2 | + | //! rejects: 71:9: cannot mutate a static outside unsafe code |
|
| 3 | + | //! rejects: 76:9: cannot mutate a static outside unsafe code |
|
| 4 | + | //! rejects: 81:9: cannot mutate a static outside unsafe code |
|
| 5 | + | //! rejects: 86:9: cannot mutate a static outside unsafe code |
|
| 6 | + | //! rejects: 91:9: cannot mutate a static outside unsafe code |
|
| 7 | + | //! rejects: 96:28: cannot mutate a static outside unsafe code |
|
| 8 | + | //! rejects: 101:18: cannot mutate a static outside unsafe code |
|
| 9 | + | //! rejects: 106:16: cannot mutate a static outside unsafe code |
|
| 10 | + | //! rejects: 111:17: cannot mutate a static outside unsafe code |
|
| 11 | + | //! rejects: 116:17: cannot mutate a static outside unsafe code |
|
| 12 | + | //! rejects: 121:18: cannot mutate a static outside unsafe code |
|
| 13 | + | //! rejects: 126:17: cannot mutate a static outside unsafe code |
|
| 14 | + | //! rejects: 131:5: cannot mutate a static outside unsafe code |
|
| 15 | + | //! rejects: 136:5: cannot mutate a static outside unsafe code |
|
| 16 | + | //! rejects: 141:16: cannot mutate a static outside unsafe code |
|
| 17 | + | //! Safe code has no way to write a static: not by assignment, by a mutable |
|
| 18 | + | //! address of any class, by a method or a helper that takes the place mutably, |
|
| 19 | + | //! or by a pattern that binds into it. |
|
| 20 | + | ||
| 21 | + | /// Union with a payload in each variant. |
|
| 22 | + | union Slot { |
|
| 23 | + | /// Small variant. |
|
| 24 | + | Small(u32), |
|
| 25 | + | /// Number variant. |
|
| 26 | + | Num(u64), |
|
| 27 | + | } |
|
| 28 | + | ||
| 29 | + | /// Record with an array and a counter. |
|
| 30 | + | record Cell { |
|
| 31 | + | /// Elements. |
|
| 32 | + | items: [u32; 4], |
|
| 33 | + | /// Counter. |
|
| 34 | + | count: u32, |
|
| 35 | + | } |
|
| 36 | + | ||
| 37 | + | /// Static scalar. |
|
| 38 | + | static NUMBER: u32 = 0; |
|
| 39 | + | ||
| 40 | + | /// Static array. |
|
| 41 | + | static ARRAY: [u32; 4] = [0; 4]; |
|
| 42 | + | ||
| 43 | + | /// Static record. |
|
| 44 | + | static CELL: Cell = Cell { items: [0; 4], count: 0 }; |
|
| 45 | + | ||
| 46 | + | /// Static union. |
|
| 47 | + | static SLOT: Slot = Slot::Num(0); |
|
| 48 | + | ||
| 49 | + | /// Change a cell. |
|
| 50 | + | fn (cell: *mut Cell) bump() { |
|
| 51 | + | set cell.count += 1; |
|
| 52 | + | } |
|
| 53 | + | ||
| 54 | + | /// Change a cell through a reference. |
|
| 55 | + | fn (cell: &mut Cell) bumpRef() { |
|
| 56 | + | set cell.count += 1; |
|
| 57 | + | } |
|
| 58 | + | ||
| 59 | + | /// Helper that writes through a mutable reference. |
|
| 60 | + | fn store(target: &mut u32) { |
|
| 61 | + | set *target = 1; |
|
| 62 | + | } |
|
| 63 | + | ||
| 64 | + | /// Assign the whole static. |
|
| 65 | + | fn assignWhole() { |
|
| 66 | + | set NUMBER = 1; |
|
| 67 | + | } |
|
| 68 | + | ||
| 69 | + | /// Assign a field. |
|
| 70 | + | fn assignField() { |
|
| 71 | + | set CELL.count = 1; |
|
| 72 | + | } |
|
| 73 | + | ||
| 74 | + | /// Assign an element. |
|
| 75 | + | fn assignElement() { |
|
| 76 | + | set ARRAY[1] = 1; |
|
| 77 | + | } |
|
| 78 | + | ||
| 79 | + | /// Assign an element of an array in a record. |
|
| 80 | + | fn assignNested() { |
|
| 81 | + | set CELL.items[1] = 1; |
|
| 82 | + | } |
|
| 83 | + | ||
| 84 | + | /// Update an element in place. |
|
| 85 | + | fn assignCompound() { |
|
| 86 | + | set ARRAY[1] += 1; |
|
| 87 | + | } |
|
| 88 | + | ||
| 89 | + | /// Assign a range. |
|
| 90 | + | fn assignRange() { |
|
| 91 | + | set ARRAY[0..2] = 5; |
|
| 92 | + | } |
|
| 93 | + | ||
| 94 | + | /// Reference to a scalar. |
|
| 95 | + | fn referenceScalar() { |
|
| 96 | + | let r: &mut u32 = &mut NUMBER; |
|
| 97 | + | } |
|
| 98 | + | ||
| 99 | + | /// Reference with an inferred class. |
|
| 100 | + | fn referenceInferred() { |
|
| 101 | + | let r = &mut NUMBER; |
|
| 102 | + | } |
|
| 103 | + | ||
| 104 | + | /// Reference to an element passed to a helper. |
|
| 105 | + | fn referenceThroughHelper() { |
|
| 106 | + | store(&mut ARRAY[0]); |
|
| 107 | + | } |
|
| 108 | + | ||
| 109 | + | /// Owned element pointer. |
|
| 110 | + | fn ownedElement() -> *mut u32 { |
|
| 111 | + | return &mut ARRAY[0]; |
|
| 112 | + | } |
|
| 113 | + | ||
| 114 | + | /// Owned slice. |
|
| 115 | + | fn ownedSlice() -> *mut [u32] { |
|
| 116 | + | return &mut ARRAY[..]; |
|
| 117 | + | } |
|
| 118 | + | ||
| 119 | + | /// Mutable range. |
|
| 120 | + | fn ownedRange() { |
|
| 121 | + | let s = &mut ARRAY[1..3]; |
|
| 122 | + | } |
|
| 123 | + | ||
| 124 | + | /// Owned pointer from a field of a record. |
|
| 125 | + | fn ownedField() -> *mut u32 { |
|
| 126 | + | return &mut CELL.count; |
|
| 127 | + | } |
|
| 128 | + | ||
| 129 | + | /// Receiver of a method that takes an owned mutable pointer. |
|
| 130 | + | fn methodOwned() { |
|
| 131 | + | CELL.bump(); |
|
| 132 | + | } |
|
| 133 | + | ||
| 134 | + | /// Receiver of a method that takes a mutable reference. |
|
| 135 | + | fn methodReference() { |
|
| 136 | + | CELL.bumpRef(); |
|
| 137 | + | } |
|
| 138 | + | ||
| 139 | + | /// Mutable pattern binding through an address. |
|
| 140 | + | fn patternBinding() { |
|
| 141 | + | match &mut SLOT { |
|
| 142 | + | case Slot::Small(p) => {} |
|
| 143 | + | else => {} |
|
| 144 | + | } |
|
| 145 | + | } |
|
| 146 | + | ||
| 147 | + | @default fn main() -> i32 { |
|
| 148 | + | return 0; |
|
| 149 | + | } |
test/tests/static.owned.alias.reject.rad
added
+19 -0
| 1 | + | //! rejects: 10:17: cannot mutate a static outside unsafe code |
|
| 2 | + | //! Safe code cannot derive an owned mutable pointer from a static, so it |
|
| 3 | + | //! cannot hold two owned handles to one storage. |
|
| 4 | + | ||
| 5 | + | /// Storage that two handles would alias. |
|
| 6 | + | static BUF: [u32; 4] = undefined; |
|
| 7 | + | ||
| 8 | + | /// Derive an owned mutable slice of the static. |
|
| 9 | + | fn grab() -> *mut [u32] { |
|
| 10 | + | return &mut BUF[..]; |
|
| 11 | + | } |
|
| 12 | + | ||
| 13 | + | @default fn main() -> i32 { |
|
| 14 | + | let a = grab(); |
|
| 15 | + | let b = grab(); |
|
| 16 | + | set a[0] = 1; |
|
| 17 | + | set b[0] = 42; |
|
| 18 | + | return a[0] as i32; |
|
| 19 | + | } |
test/tests/static.reference.call.reject.rad
added
+37 -0
| 1 | + | //! rejects: 23:9: cannot mutate a static outside unsafe code |
|
| 2 | + | //! rejects: 27:9: cannot mutate a static outside unsafe code |
|
| 3 | + | //! A static may hold a mutable pointer, but safe code cannot replace it while |
|
| 4 | + | //! a reference to it is live, since safe code cannot write a static. So a |
|
| 5 | + | //! reference into a static stays valid across a call. |
|
| 6 | + | ||
| 7 | + | /// Union whose payload is a pointer in one variant and a number in the other. |
|
| 8 | + | union Slot { |
|
| 9 | + | /// Pointer variant. |
|
| 10 | + | Ptr(*mut u32), |
|
| 11 | + | /// Number variant. |
|
| 12 | + | Num(u64), |
|
| 13 | + | } |
|
| 14 | + | ||
| 15 | + | /// Target of the pointer. |
|
| 16 | + | static TARGET: [u32; 1] = [7]; |
|
| 17 | + | ||
| 18 | + | /// Slot that holds either variant. |
|
| 19 | + | static SLOT: Slot = Slot::Num(0); |
|
| 20 | + | ||
| 21 | + | /// Change the variant of the slot. |
|
| 22 | + | fn clobber() { |
|
| 23 | + | set SLOT = Slot::Num(0xdead0000); |
|
| 24 | + | } |
|
| 25 | + | ||
| 26 | + | @default fn main() -> i32 { |
|
| 27 | + | set SLOT = Slot::Ptr(&mut TARGET[0]); |
|
| 28 | + | let s: &mut Slot = &mut SLOT; |
|
| 29 | + | match s { |
|
| 30 | + | case Slot::Ptr(p) => { |
|
| 31 | + | clobber(); |
|
| 32 | + | set **p = 99; |
|
| 33 | + | return TARGET[0] as i32; |
|
| 34 | + | } |
|
| 35 | + | else => return 1, |
|
| 36 | + | } |
|
| 37 | + | } |
test/tests/trait.type.local.reject.rad
added
+15 -0
| 1 | + | //! rejects: 9:9: a trait is not a type; use a trait object such as `&mut opaque T` |
|
| 2 | + | //! A local trait used as a bare parameter type is rejected with a diagnostic |
|
| 3 | + | //! that names the problem. The trait object type is `&mut opaque T`. |
|
| 4 | + | ||
| 5 | + | trait T { |
|
| 6 | + | fn (&mut T) get() -> u32; |
|
| 7 | + | } |
|
| 8 | + | ||
| 9 | + | fn f(a: T) -> i32 { |
|
| 10 | + | return 0; |
|
| 11 | + | } |
|
| 12 | + | ||
| 13 | + | @default fn main() -> i32 { |
|
| 14 | + | return 0; |
|
| 15 | + | } |
test/tests/trait.type.scoped.reject.rad
added
+14 -0
| 1 | + | //! rejects: 8:38: a trait is not a type; use a trait object such as `&mut opaque T` |
|
| 2 | + | //! A trait named through a module path where a type is expected is a |
|
| 3 | + | //! diagnostic, not an internal error. The trait object type is |
|
| 4 | + | //! `&mut opaque alloc::Allocator`. |
|
| 5 | + | ||
| 6 | + | use std::lang::alloc; |
|
| 7 | + | ||
| 8 | + | fn f(a: &mut alloc::Arena) -> alloc::Allocator { |
|
| 9 | + | return a; |
|
| 10 | + | } |
|
| 11 | + | ||
| 12 | + | @default fn main() -> i32 { |
|
| 13 | + | return 0; |
|
| 14 | + | } |