.githooks/
compiler/
kernel/
lib/
examples/
std/
arch/
bits/
char/
collections/
fs/
hash/
lang/
alloc/
ast/
gen/
il/
layout/
lower/
regions.rad
4.6 KiB
tests.rad
12.8 KiB
manifest/
module/
parser/
resolver/
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/lower/tests.rad
raw
| 1 | //! Tests for the lowering error messages and for lowering with an exhausted arena. |
| 2 | |
| 3 | use std::lang::alloc; |
| 4 | use std::lang::ast; |
| 5 | use std::lang::il; |
| 6 | use std::lang::module; |
| 7 | use std::lang::resolver; |
| 8 | use std::lang::resolver::tests; |
| 9 | use std::mem; |
| 10 | use std::mem::heap; |
| 11 | use std::testing; |
| 12 | |
| 13 | /// Backing storage for the node of the error payloads. |
| 14 | static NODE_STORAGE: [u8; 1024] = [0; 1024]; |
| 15 | |
| 16 | /// Test that each lowering error has a message and that no two messages are equal. |
| 17 | @test unsafe fn testErrorMessagesAreSpecific() throws (testing::TestError) { |
| 18 | let mut arena = ast::nodeArena(&mut NODE_STORAGE[..]); |
| 19 | let errors: [super::LowerError; 28] = [ |
| 20 | super::LowerError::OutputFailed, |
| 21 | super::LowerError::MissingSymbol(try! ast::synthNode(&mut arena, ast::NodeValue::Nil)), |
| 22 | super::LowerError::MissingType(try! ast::synthNode(&mut arena, ast::NodeValue::Nil)), |
| 23 | super::LowerError::MissingConst(try! ast::synthNode(&mut arena, ast::NodeValue::Nil)), |
| 24 | super::LowerError::ExpectedOptional, |
| 25 | super::LowerError::ExpectedRecord, |
| 26 | super::LowerError::ExpectedArray, |
| 27 | super::LowerError::ExpectedBlock(try! ast::synthNode(&mut arena, ast::NodeValue::Nil)), |
| 28 | super::LowerError::ExpectedIdentifier, |
| 29 | super::LowerError::ExpectedFunctionParam, |
| 30 | super::LowerError::ExpectedFunction, |
| 31 | super::LowerError::OutsideOfLoop, |
| 32 | super::LowerError::InvalidUse, |
| 33 | super::LowerError::UnexpectedNodeValue(try! ast::synthNode( |
| 34 | &mut arena, ast::NodeValue::Nil |
| 35 | )), |
| 36 | super::LowerError::UnexpectedType(resolver::Type::Void), |
| 37 | super::LowerError::MissingTarget, |
| 38 | super::LowerError::MissingMetadata, |
| 39 | super::LowerError::ExpectedSliceOrArray, |
| 40 | super::LowerError::ExpectedVariant, |
| 41 | super::LowerError::ExpectedCall, |
| 42 | super::LowerError::FieldNotFound, |
| 43 | super::LowerError::ImmutableAssignment, |
| 44 | super::LowerError::NilInNonOptional, |
| 45 | super::LowerError::InvalidArgCount, |
| 46 | super::LowerError::Unsupported, |
| 47 | super::LowerError::UnknownIntrinsic, |
| 48 | super::LowerError::AllocationFailed, |
| 49 | super::LowerError::ErrorTagCollision, |
| 50 | ]; |
| 51 | for i in 0 .. errors.len { |
| 52 | let message = super::errorMessage(errors[i]); |
| 53 | try testing::expect(message.len > 0); |
| 54 | for j in 0 .. i { |
| 55 | try testing::expect(not mem::eq(message, super::errorMessage(errors[j]))); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /// Backing storage for the function arena of the allocation failure test. |
| 61 | static FN_STORAGE: [u8; 65536] = [0; 65536]; |
| 62 | |
| 63 | /// Step between the function arena sizes that the allocation failure test tries. |
| 64 | constant FN_ARENA_STEP: u32 = 512; |
| 65 | |
| 66 | /// Program with several functions, so that exhaustion can happen while a |
| 67 | /// function is lowered, when it is stored, and when it is collected. |
| 68 | constant ALLOC_PROGRAM: *[u8] = |
| 69 | "fn a(x: i32) -> i32 { return x + 1; } fn b(x: i32) -> i32 { return a(x) * 2; }"; |
| 70 | |
| 71 | /// Test that lowering with an exhausted function arena throws `AllocationFailed` |
| 72 | /// instead of panicking, and that a large enough arena succeeds. |
| 73 | @test unsafe fn testLowerArenaExhaustion() throws (testing::TestError) { |
| 74 | let mut failures: u32 = 0; |
| 75 | // The smallest heap still holds its header. |
| 76 | let mut size: u32 = 64; |
| 77 | while size <= FN_STORAGE.len { |
| 78 | let mut res = tests::testResolver(); |
| 79 | let result = try tests::resolveProgramStr(&mut res, ALLOC_PROGRAM); |
| 80 | try tests::expectNoErrors(&result); |
| 81 | |
| 82 | let arena = heap::over(&mut FN_STORAGE[.. size]); |
| 83 | try super::lower(res, result.root, "test", arena) catch err { |
| 84 | try testing::expect(err == super::LowerError::AllocationFailed); |
| 85 | set failures += 1; |
| 86 | }; |
| 87 | set size += FN_ARENA_STEP; |
| 88 | } |
| 89 | try testing::expect(failures > 0); |
| 90 | try testing::expect(failures < FN_STORAGE.len / FN_ARENA_STEP); |
| 91 | } |
| 92 | |
| 93 | /// Offset basis of the reference 64-bit FNV-1a hash. |
| 94 | constant REF_FNV_BASIS: u64 = 0xcbf29ce484222325; |
| 95 | /// Prime of the reference 64-bit FNV-1a hash. |
| 96 | constant REF_FNV_PRIME: u64 = 0x100000001b3; |
| 97 | |
| 98 | /// Reference 64-bit FNV-1a hash of `name`. |
| 99 | fn referenceHash(name: *[u8]) -> u64 { |
| 100 | let mut h = REF_FNV_BASIS; |
| 101 | for byte in name { |
| 102 | set h = (h ^ byte as u64) * REF_FNV_PRIME; |
| 103 | } |
| 104 | return h; |
| 105 | } |
| 106 | |
| 107 | /// Test that the reference hash matches published 64-bit FNV-1a vectors. |
| 108 | @test fn testReferenceHash() throws (testing::TestError) { |
| 109 | try testing::expect(referenceHash("") == 0xcbf29ce484222325); |
| 110 | try testing::expect(referenceHash("a") == 0xaf63dc4c8601ec8c); |
| 111 | try testing::expect(referenceHash("foobar") == 0x85944171f73967e8); |
| 112 | } |
| 113 | |
| 114 | /// Test that the tag of an error type is the FNV-1a hash of its name, and that |
| 115 | /// different error types have different tags. |
| 116 | @test unsafe fn testErrorTagIsNameHash() throws (testing::TestError) { |
| 117 | let mut res = tests::testResolver(); |
| 118 | let result = try tests::resolveProgramStr( |
| 119 | &mut res, "union First { Failure } union Second { Failure }" |
| 120 | ); |
| 121 | try tests::expectNoErrors(&result); |
| 122 | let case ast::NodeValue::Module(block) = result.root.value |
| 123 | else throw testing::TestError::Failed; |
| 124 | let firstSym = resolver::symbolFor(&res, block.statements[0]) |
| 125 | else throw testing::TestError::Failed; |
| 126 | let secondSym = resolver::symbolFor(&res, block.statements[1]) |
| 127 | else throw testing::TestError::Failed; |
| 128 | let case resolver::SymbolData::Type(firstId) = res.symbols[*firstSym].data |
| 129 | else throw testing::TestError::Failed; |
| 130 | let case resolver::SymbolData::Type(secondId) = res.symbols[*secondSym].data |
| 131 | else throw testing::TestError::Failed; |
| 132 | |
| 133 | let mut low = super::lowerer( |
| 134 | res, |
| 135 | module::ModuleTable { entries: &[] }, |
| 136 | "test", |
| 137 | heap::over(&mut FN_STORAGE[..]), |
| 138 | super::LowerOptions { |
| 139 | debug: false, buildTest: false, tableScope: super::TableScope::Program |
| 140 | } |
| 141 | ); |
| 142 | let first = try super::getOrAssignErrorTag(&mut low, resolver::Type::Nominal(firstId)) catch { |
| 143 | throw testing::TestError::Failed; |
| 144 | }; |
| 145 | let second = try super::getOrAssignErrorTag(&mut low, resolver::Type::Nominal(secondId)) catch { |
| 146 | throw testing::TestError::Failed; |
| 147 | }; |
| 148 | let word = try super::getOrAssignErrorTag(&mut low, resolver::Type::U32) catch { |
| 149 | throw testing::TestError::Failed; |
| 150 | }; |
| 151 | let again = try super::getOrAssignErrorTag(&mut low, resolver::Type::Nominal(firstId)) catch { |
| 152 | throw testing::TestError::Failed; |
| 153 | }; |
| 154 | try testing::expect(first == referenceHash("First")); |
| 155 | try testing::expect(second == referenceHash("Second")); |
| 156 | try testing::expect(word == referenceHash("u32")); |
| 157 | try testing::expect(first <> second); |
| 158 | try testing::expect(again == first); |
| 159 | } |
| 160 | |
| 161 | /// Array type hashes include the decimal length in the canonical spelling. |
| 162 | @test unsafe fn testArrayTypeNameHash() throws (testing::TestError) { |
| 163 | let res = tests::testResolver(); |
| 164 | let mut low = super::lowerer( |
| 165 | res, |
| 166 | module::ModuleTable { entries: &[] }, |
| 167 | "test", |
| 168 | heap::over(&mut FN_STORAGE[..]), |
| 169 | super::LowerOptions { |
| 170 | debug: false, buildTest: false, tableScope: super::TableScope::Program |
| 171 | } |
| 172 | ); |
| 173 | let byte = resolver::allocType(&mut low.resolver, resolver::Type::U8); |
| 174 | let lengths: [u32; 4] = [0, 1, 10, 4294967295]; |
| 175 | let names: [*[u8]; 4] = ["[u8; 0]", "[u8; 1]", "[u8; 10]", "[u8; 4294967295]"]; |
| 176 | for length, i in lengths { |
| 177 | let ty = resolver::Type::Array(resolver::ArrayType { item: byte, length }); |
| 178 | let tag = try super::getOrAssignErrorTag(&mut low, ty) catch { |
| 179 | throw testing::TestError::Failed; |
| 180 | }; |
| 181 | assert tag == referenceHash(names[i]); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /// A mixed union case copies the pointer field without a second copy of the union. |
| 186 | @test unsafe fn testMixedUnionFieldSnapshot() throws (testing::TestError) { |
| 187 | let mut res = tests::testResolver(); |
| 188 | let result = try tests::resolveProgramStr( |
| 189 | &mut res, |
| 190 | "union U: Copy { A(*u32), B(u32) } fn f(xs: &[U]) -> u32 { match xs[0] { case U::A(p) => return *p, case U::B(n) => return n, } }" |
| 191 | ); |
| 192 | try tests::expectNoErrors(&result); |
| 193 | let program = try super::lower(res, result.root, "test", heap::over( |
| 194 | &mut FN_STORAGE[..] |
| 195 | )) catch { |
| 196 | throw testing::TestError::Failed; |
| 197 | }; |
| 198 | let mut reads: u32 = 0; |
| 199 | let mut copies: u32 = 0; |
| 200 | for func in program.fns { |
| 201 | for block in func.blocks { |
| 202 | for instr in block.instrs { |
| 203 | if let case il::Instr::Blit { layout, .. } = instr { |
| 204 | for slot in program.layouts[layout].slots { |
| 205 | if let case il::SlotKind::Union { .. } = slot.kind { |
| 206 | set copies += 1; |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | if let case il::Instr::UnionGet { .. } = instr { |
| 211 | set reads += 1; |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | assert reads == 1; |
| 217 | assert copies == 0; |
| 218 | } |
| 219 | |
| 220 | /// A page heap output must point to a heap with typed or plain rights. |
| 221 | @test unsafe fn testPageHeapOutputType() throws (testing::TestError) { |
| 222 | for out in ["u32", "&mut u32", "&mut ?*Scratch"] { |
| 223 | let mut res = tests::testResolver(); |
| 224 | let source = try! alloc::concatBytes( |
| 225 | res.arena, |
| 226 | &[ |
| 227 | "@language record Scratch {} @intrinsic fn pageHeap(h: u64, out: ", |
| 228 | out, ") -> i64; fn f(out: ", out, ") -> i64 { return pageHeap(0, out); }", |
| 229 | ] |
| 230 | ); |
| 231 | let result = try tests::resolveProgramStr(&mut res, source); |
| 232 | try tests::expectNoErrors(&result); |
| 233 | let arena = heap::over(&mut FN_STORAGE[..]); |
| 234 | let mut failed = false; |
| 235 | try super::lower(res, result.root, "test", arena) catch err { |
| 236 | let case super::LowerError::UnexpectedType(_) = err else { |
| 237 | throw testing::TestError::Failed; |
| 238 | }; |
| 239 | set failed = true; |
| 240 | }; |
| 241 | assert failed; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /// A single borrowed field selects its declared content region. |
| 246 | @test unsafe fn testSingleFieldRegionMap() throws (testing::TestError) { |
| 247 | let mut res = tests::testResolver(); |
| 248 | let result = try tests::resolveProgramStr( |
| 249 | &mut res, |
| 250 | "record R['a, 'b]: Copy { p: &'b u32 } fn f(p: &'b u32, spare: &'a u32) -> R['a, 'b] { return R { p }; } fn g(input: R['a, 'b], a: &'a u32) -> &'a u32 { return a; }" |
| 251 | ); |
| 252 | try tests::expectNoErrors(&result); |
| 253 | let program = try super::lower(res, result.root, "test", heap::over( |
| 254 | &mut FN_STORAGE[..] |
| 255 | )) catch { |
| 256 | throw testing::TestError::Failed; |
| 257 | }; |
| 258 | assert program.fns[0].borrows.len == 1; |
| 259 | let borrow = program.fns[0].borrows[0]; |
| 260 | assert borrow.result == 0; |
| 261 | assert borrow.param == 0; |
| 262 | assert borrow.source == nil; |
| 263 | assert program.fns[1].borrows.len == 1; |
| 264 | assert program.fns[1].borrows[0].param == 1; |
| 265 | } |
| 266 | |
| 267 | /// Integer primitives require their exact source signatures. |
| 268 | @test unsafe fn testIntegerIntrinsicSignatures() throws (testing::TestError) { |
| 269 | for source in &[ |
| 270 | "@intrinsic fn rotateLeft32(x: u64, n: u32) -> u64; fn f() -> u64 { return rotateLeft32(0, 0); }", |
| 271 | "@intrinsic fn leadingZeros64(x: u64) -> u64; fn f() -> u64 { return leadingZeros64(0); }", |
| 272 | "@intrinsic fn load64Le(b: &[u8], i: u32) -> u32; fn f(b: &[u8]) -> u32 { return load64Le(b, 0); }", |
| 273 | "@intrinsic fn store32Le(b: &[u8], i: u32, x: u32); fn f(b: &[u8]) { store32Le(b, 0, 0); }", |
| 274 | ] { |
| 275 | let mut res = tests::testResolver(); |
| 276 | let result = try tests::resolveProgramStr(&mut res, source); |
| 277 | try tests::expectNoErrors(&result); |
| 278 | let arena = heap::over(&mut FN_STORAGE[..]); |
| 279 | let mut failed = false; |
| 280 | try super::lower(res, result.root, "test", arena) catch err { |
| 281 | assert err == super::LowerError::Unsupported; |
| 282 | set failed = true; |
| 283 | }; |
| 284 | assert failed; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | /// Payload matches dispatch on tags and bind fields in the selected block. |
| 289 | @test unsafe fn testUnionMatchSwitch() throws (testing::TestError) { |
| 290 | let mut res = tests::testResolver(); |
| 291 | let result = try tests::resolveProgramStr( |
| 292 | &mut res, |
| 293 | "union U: Copy { A(u32), B(u32), C, D, E, F, G, H, I } fn f(x: U) -> u32 { match x { case U::A(a) => return a, case U::B(b) => return b, case U::C, U::D => return 3, case U::E => return 4, case U::F => return 5, case U::G => return 6, case U::H => return 7, case U::I => return 8, } }" |
| 294 | ); |
| 295 | try tests::expectNoErrors(&result); |
| 296 | let program = try super::lower(res, result.root, "test", heap::over( |
| 297 | &mut FN_STORAGE[..] |
| 298 | )) catch { |
| 299 | throw testing::TestError::Failed; |
| 300 | }; |
| 301 | let mut switches: u32 = 0; |
| 302 | for func in program.fns { |
| 303 | for block in func.blocks { |
| 304 | for instr in block.instrs { |
| 305 | if let case il::Instr::Switch { cases, .. } = instr { |
| 306 | assert cases.len == 8; |
| 307 | set switches += 1; |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | assert switches == 1; |
| 313 | } |