lang: Handle allocation failures in compiler appends
38d73ae4bebe6cfa88f56fd5cb06746da45df47326c060df3671c5699deef7c9
The arena allocator callback now throws `AllocError` on exhaustion, and every slice append in the compiler and library handles that error. Most call sites treat exhaustion as fatal with `try!`. The assembler reports text and data overflow through its own error type. Assisted-by: Claude:opus-5.5
1 parent
6ee4af8c
compiler/radiance.rad
+11 -10
| 655 | 655 | } |
|
| 656 | 656 | set funcPath[desc.modPath.len - 1] = desc.fnName; |
|
| 657 | 657 | let funcArg = synthScopeAccess(arena, &funcPath[..desc.modPath.len]); |
|
| 658 | 658 | ||
| 659 | 659 | let a = alloc::arenaAllocator(&mut arena.arena); |
|
| 660 | - | let args = ast::nodeSlice(arena, 3) |
|
| 661 | - | .append(modArg, a) |
|
| 662 | - | .append(nameArg, a) |
|
| 663 | - | .append(funcArg, a); |
|
| 660 | + | let args = ast::nodeSlice(arena, 3); |
|
| 661 | + | try! args.append(modArg, a); |
|
| 662 | + | try! args.append(nameArg, a); |
|
| 663 | + | try! args.append(funcArg, a); |
|
| 664 | 664 | ||
| 665 | 665 | return ast::synthNode(arena, ast::NodeValue::Call(ast::Call { callee, args })); |
|
| 666 | 666 | } |
|
| 667 | 667 | ||
| 668 | 668 | /// Inject a test runner into the entry package's root module. |
| 720 | 720 | unsafe fn synthTestMainFn(arena: &mut ast::NodeArena, tests: &[TestDesc]) -> *ast::Node { |
|
| 721 | 721 | // Build array literal: `[testing::test(...), ...]`. |
|
| 722 | 722 | let a = alloc::arenaAllocator(&mut arena.arena); |
|
| 723 | 723 | let mut elements = ast::nodeSlice(arena, tests.len as u32); |
|
| 724 | 724 | for i in 0..tests.len { |
|
| 725 | - | elements.append(synthTestCall(arena, &tests[i]), a); |
|
| 725 | + | try! elements.append(synthTestCall(arena, &tests[i]), a); |
|
| 726 | 726 | } |
|
| 727 | 727 | let arrayLit = ast::synthNode(arena, ast::NodeValue::ArrayLit(elements)); |
|
| 728 | 728 | ||
| 729 | 729 | // Build: `&[...]`. |
|
| 730 | 730 | let testsRef = ast::synthNode(arena, ast::NodeValue::AddressOf(ast::AddressOf { |
|
| 731 | 731 | target: arrayLit, mutable: false, |
|
| 732 | 732 | })); |
|
| 733 | 733 | ||
| 734 | 734 | // Build: `testing::runAllTests(&[...])`. |
|
| 735 | 735 | let runFn = synthScopeAccess(arena, &["testing", "runAllTests"]); |
|
| 736 | - | let callArgs = ast::nodeSlice(arena, 1).append(testsRef, a); |
|
| 736 | + | let callArgs = try! ast::nodeSlice(arena, 1).append(testsRef, a); |
|
| 737 | 737 | let callExpr = ast::synthNode(arena, ast::NodeValue::Call(ast::Call { |
|
| 738 | 738 | callee: runFn, args: callArgs, |
|
| 739 | 739 | })); |
|
| 740 | 740 | ||
| 741 | 741 | // Build: `return testing::runAllTests(&[...]);` |
|
| 742 | 742 | let retStmt = ast::synthNode(arena, ast::NodeValue::Return { value: callExpr }); |
|
| 743 | - | let bodyStmts = ast::nodeSlice(arena, 1).append(retStmt, a); |
|
| 743 | + | let bodyStmts = try! ast::nodeSlice(arena, 1).append(retStmt, a); |
|
| 744 | 744 | let fnBody = ast::synthNode(arena, ast::NodeValue::Block(ast::Block { statements: bodyStmts, isUnsafe: false })); |
|
| 745 | 745 | ||
| 746 | 746 | // Build: `unsafe fn #testMain() -> i32` |
|
| 747 | 747 | let fnName = ast::synthNode(arena, ast::NodeValue::Ident(strings::intern(&mut STRING_POOL, "#testMain"))); |
|
| 748 | 748 | let returnType = ast::synthNode(arena, ast::NodeValue::TypeSig(ast::TypeSig::Integer { |
| 755 | 755 | }; |
|
| 756 | 756 | ||
| 757 | 757 | // Entry and function safety attributes. |
|
| 758 | 758 | let attrNode = ast::synthNode(arena, ast::NodeValue::Attribute(ast::Attribute::Default)); |
|
| 759 | 759 | let unsafeAttr = ast::synthNode(arena, ast::NodeValue::Attribute(ast::Attribute::Unsafe)); |
|
| 760 | - | let attrList = ast::nodeSlice(arena, 2).append(attrNode, a).append(unsafeAttr, a); |
|
| 760 | + | let attrList = try! ast::nodeSlice(arena, 2).append(attrNode, a); |
|
| 761 | + | try! attrList.append(unsafeAttr, a); |
|
| 761 | 762 | let fnAttrs = ast::Attributes { list: attrList }; |
|
| 762 | 763 | ||
| 763 | 764 | return ast::synthNode(arena, ast::NodeValue::FnDecl(ast::FnDecl { |
|
| 764 | 765 | name: fnName, sig: fnSig, body: fnBody, attrs: fnAttrs, |
|
| 765 | 766 | })); |
| 775 | 776 | panic "injectIntoBlock: expected Block node"; |
|
| 776 | 777 | }; |
|
| 777 | 778 | let allocator = alloc::arenaAllocator(&mut arena.arena); |
|
| 778 | 779 | let mut stmts = ast::nodeSlice(arena, block.statements.len + 1); |
|
| 779 | 780 | for stmt in block.statements { |
|
| 780 | - | stmts.append(stmt, allocator); |
|
| 781 | + | try! stmts.append(stmt, allocator); |
|
| 781 | 782 | } |
|
| 782 | - | stmts.append(decl, allocator); |
|
| 783 | + | try! stmts.append(decl, allocator); |
|
| 783 | 784 | return ast::allocNode(arena, blockNode.span, ast::NodeValue::Block( |
|
| 784 | 785 | ast::Block { statements: stmts, isUnsafe: block.isUnsafe } |
|
| 785 | 786 | )); |
|
| 786 | 787 | } |
|
| 787 | 788 |
lib/std/arch/rv64/asm/emit.rad
+9 -5
| 13 | 13 | let idx = a.symbols.len; |
|
| 14 | 14 | let offset: i32 = a.data.len as i32 |
|
| 15 | 15 | if a.section == super::Section::Data |
|
| 16 | 16 | else a.text.len as i32 * rv64::INSTR_SIZE; |
|
| 17 | 17 | ||
| 18 | - | a.symbols.append(super::Symbol { |
|
| 18 | + | try! a.symbols.append(super::Symbol { |
|
| 19 | 19 | name, |
|
| 20 | 20 | section: a.section, |
|
| 21 | 21 | offset, |
|
| 22 | 22 | isExported: dict::get(&a.exportMap, name) <> nil, |
|
| 23 | 23 | }, alloc::arenaAllocator(a.arena)); |
|
| 24 | 24 | dict::insert(&mut a.symbolMap, name, idx as i32); |
|
| 25 | 25 | } |
|
| 26 | 26 | ||
| 27 | 27 | /// Append one encoded instruction word to the text section. |
|
| 28 | 28 | export unsafe fn emitText(a: &mut super::Assembler, word: u32) throws (super::Error) { |
|
| 29 | - | a.text.append(word, alloc::arenaAllocator(a.arena)); |
|
| 29 | + | try a.text.append(word, alloc::arenaAllocator(a.arena)) catch { |
|
| 30 | + | throw super::Error::TextOverflow; |
|
| 31 | + | }; |
|
| 30 | 32 | } |
|
| 31 | 33 | ||
| 32 | 34 | /// Append `words` no-op instructions to the text section. |
|
| 33 | 35 | export unsafe fn emitTextPadding(a: &mut super::Assembler, words: u32) throws (super::Error) { |
|
| 34 | 36 | for _ in 0..words { |
| 36 | 38 | } |
|
| 37 | 39 | } |
|
| 38 | 40 | ||
| 39 | 41 | /// Append one byte to the data section. |
|
| 40 | 42 | export unsafe fn emitByte(a: &mut super::Assembler, byte: u8) throws (super::Error) { |
|
| 41 | - | a.data.append(byte, alloc::arenaAllocator(a.arena)); |
|
| 43 | + | try a.data.append(byte, alloc::arenaAllocator(a.arena)) catch { |
|
| 44 | + | throw super::Error::DataOverflow; |
|
| 45 | + | }; |
|
| 42 | 46 | } |
|
| 43 | 47 | ||
| 44 | 48 | /// Emit a little-endian integer with `bytes` bytes. |
|
| 45 | 49 | unsafe fn emitDataInt(a: &mut super::Assembler, bits: u64, bytes: u32) throws (super::Error) { |
|
| 46 | 50 | for i in 0..bytes { |
| 78 | 82 | } |
|
| 79 | 83 | } |
|
| 80 | 84 | ||
| 81 | 85 | /// Record a pending symbol fixup. |
|
| 82 | 86 | unsafe fn recordFixup(a: &mut super::Assembler, symbol: *[u8], info: super::FixupInfo) { |
|
| 83 | - | a.fixups.append(super::Fixup { symbol, info }, alloc::arenaAllocator(a.arena)); |
|
| 87 | + | try! a.fixups.append(super::Fixup { symbol, info }, alloc::arenaAllocator(a.arena)); |
|
| 84 | 88 | } |
|
| 85 | 89 | ||
| 86 | 90 | /// Record a text fixup that must be resolved after all program text is known. |
|
| 87 | 91 | unsafe fn recordExternalFixup(a: &mut super::Assembler, fixup: super::Fixup) { |
|
| 88 | - | a.externalFixups.append(fixup, alloc::arenaAllocator(a.arena)); |
|
| 92 | + | try! a.externalFixups.append(fixup, alloc::arenaAllocator(a.arena)); |
|
| 89 | 93 | } |
|
| 90 | 94 | ||
| 91 | 95 | /// Record a text-section symbol fixup and reserve its instruction words. |
|
| 92 | 96 | export unsafe fn recordTextFixup(a: &mut super::Assembler, symbol: *[u8], info: super::FixupInfo, words: u32) throws (super::Error) { |
|
| 93 | 97 | recordFixup(a, symbol, info); |
lib/std/arch/rv64/emit.rad
+7 -6
| 201 | 201 | let pendingBranchesBuf = pendingBranches as *mut [PendingBranch]; |
|
| 202 | 202 | let pendingCallsBuf = pendingCalls as *mut [PendingCall]; |
|
| 203 | 203 | let pendingJumpsBuf = pendingJumps as *mut [PendingJump]; |
|
| 204 | 204 | let pendingAddrLoadsBuf = pendingAddrLoads as *mut [PendingAddrLoad]; |
|
| 205 | 205 | let funcsBuf = funcs as *mut [types::FuncAddr]; |
|
| 206 | + | ||
| 206 | 207 | return Emitter { |
|
| 207 | 208 | allocator: alloc::arenaAllocator(arena), |
|
| 208 | 209 | code: code as *mut [u32], |
|
| 209 | 210 | codeLen: 0, |
|
| 210 | 211 | pendingBranches: &mut pendingBranchesBuf[..0], |
| 263 | 264 | recordFuncAt(e, name, codeLen); |
|
| 264 | 265 | } |
|
| 265 | 266 | ||
| 266 | 267 | /// Record a function's start position at `index` for printing. |
|
| 267 | 268 | export fn recordFuncAt(e: &mut Emitter, name: *[u8], index: u32) { |
|
| 268 | - | e.funcs.append(types::FuncAddr { name, index }, e.allocator); |
|
| 269 | + | try! e.funcs.append(types::FuncAddr { name, index }, e.allocator); |
|
| 269 | 270 | } |
|
| 270 | 271 | ||
| 271 | 272 | /// Record a local branch needing later patching. |
|
| 272 | 273 | /// Unconditional jumps use a single slot (J-type, +-1MB range). |
|
| 273 | 274 | /// Conditional branches use two slots (B-type has only +-4KB range, |
|
| 274 | 275 | /// so large functions may need the inverted-branch + JAL fallback). |
|
| 275 | 276 | export fn recordBranch(e: &mut Emitter, targetBlock: u32, kind: BranchKind) { |
|
| 276 | - | e.pendingBranches.append(PendingBranch { |
|
| 277 | + | try! e.pendingBranches.append(PendingBranch { |
|
| 277 | 278 | index: e.codeLen, |
|
| 278 | 279 | target: targetBlock, |
|
| 279 | 280 | kind: kind, |
|
| 280 | 281 | }, e.allocator); |
|
| 281 | 282 |
| 289 | 290 | ||
| 290 | 291 | /// Record a function call needing later patching. |
|
| 291 | 292 | /// Emits placeholder instructions that will be patched later. |
|
| 292 | 293 | /// Uses two slots to support long-distance calls. |
|
| 293 | 294 | export fn recordCall(e: &mut Emitter, target: *[u8]) { |
|
| 294 | - | e.pendingCalls.append(PendingCall { |
|
| 295 | + | try! e.pendingCalls.append(PendingCall { |
|
| 295 | 296 | index: e.codeLen, |
|
| 296 | 297 | target, |
|
| 297 | 298 | }, e.allocator); |
|
| 298 | 299 | ||
| 299 | 300 | emit(e, encode::nop()); // Placeholder for AUIPC. |
|
| 300 | 301 | emit(e, encode::nop()); // Placeholder for JALR. |
|
| 301 | 302 | } |
|
| 302 | 303 | ||
| 303 | 304 | /// Record a jump emitted by assembly that needs whole-program patching. |
|
| 304 | 305 | export fn recordJumpAt(e: &mut Emitter, target: *[u8], rd: gen::Reg, index: u32) { |
|
| 305 | - | e.pendingJumps.append(PendingJump { |
|
| 306 | + | try! e.pendingJumps.append(PendingJump { |
|
| 306 | 307 | index, |
|
| 307 | 308 | target, |
|
| 308 | 309 | rd, |
|
| 309 | 310 | }, e.allocator); |
|
| 310 | 311 | } |
| 320 | 321 | emit(e, encode::nop()); // Placeholder for ADDI. |
|
| 321 | 322 | } |
|
| 322 | 323 | ||
| 323 | 324 | /// Record a function address load already reserved by assembly. |
|
| 324 | 325 | export fn recordAddrLoadAt(e: &mut Emitter, target: *[u8], rd: gen::Reg, index: u32) { |
|
| 325 | - | e.pendingAddrLoads.append(PendingAddrLoad { |
|
| 326 | + | try! e.pendingAddrLoads.append(PendingAddrLoad { |
|
| 326 | 327 | index, |
|
| 327 | 328 | target, |
|
| 328 | 329 | rd: rd, |
|
| 329 | 330 | isData: false, |
|
| 330 | 331 | }, e.allocator); |
|
| 331 | 332 | } |
|
| 332 | 333 | ||
| 333 | 334 | /// Record a data address load needing later patching. |
|
| 334 | 335 | /// Uses an absolute 32-bit load sequence matching the current data memory map. |
|
| 335 | 336 | export fn recordDataAddrLoad(e: &mut Emitter, target: *[u8], rd: gen::Reg) { |
|
| 336 | - | e.pendingAddrLoads.append(PendingAddrLoad { |
|
| 337 | + | try! e.pendingAddrLoads.append(PendingAddrLoad { |
|
| 337 | 338 | index: e.codeLen, |
|
| 338 | 339 | target, |
|
| 339 | 340 | rd: rd, |
|
| 340 | 341 | isData: true, |
|
| 341 | 342 | }, e.allocator); |
lib/std/lang/alloc.rad
+5 -5
| 108 | 108 | /// |
|
| 109 | 109 | /// Bundles an allocation function with an opaque context pointer so that |
|
| 110 | 110 | /// any allocation strategy (arena, free-list, mmap, pool) can be used |
|
| 111 | 111 | /// through a uniform interface. The `func` field is called with the context |
|
| 112 | 112 | /// pointer, a byte size and an alignment, and must return a pointer to |
|
| 113 | - | /// the allocated memory or panic on failure. |
|
| 113 | + | /// the allocated memory or throw `AllocError` on failure. |
|
| 114 | 114 | export record Allocator: Copy { |
|
| 115 | 115 | /// Allocation function. Returns a pointer to `size` bytes |
|
| 116 | - | /// aligned to `alignment`, or panics on failure. |
|
| 117 | - | func: unsafe fn(*unsafe mut opaque, u32, u32) -> *mut opaque, |
|
| 116 | + | /// aligned to `alignment`, or throws `AllocError` on failure. |
|
| 117 | + | func: unsafe fn(*unsafe mut opaque, u32, u32) -> *mut opaque throws (AllocError), |
|
| 118 | 118 | /// Opaque context pointer passed to `func`. |
|
| 119 | 119 | ctx: *unsafe mut opaque, |
|
| 120 | 120 | } |
|
| 121 | 121 | ||
| 122 | 122 | /// Create an allocator whose arena must outlive all uses of the allocator. |
| 126 | 126 | ctx: (arena as *unsafe mut Arena) as *unsafe mut opaque, |
|
| 127 | 127 | }; |
|
| 128 | 128 | } |
|
| 129 | 129 | ||
| 130 | 130 | /// Arena allocation function conforming to the `Allocator` interface. |
|
| 131 | - | unsafe fn arenaAllocFn(ctx: *unsafe mut opaque, size: u32, alignment: u32) -> *mut opaque { |
|
| 131 | + | unsafe fn arenaAllocFn(ctx: *unsafe mut opaque, size: u32, alignment: u32) -> *mut opaque throws (AllocError) { |
|
| 132 | 132 | let arena = ctx as *unsafe mut Arena; |
|
| 133 | - | return try! alloc(arena, size, alignment); |
|
| 133 | + | return try alloc(arena, size, alignment); |
|
| 134 | 134 | } |
|
| 135 | 135 | ||
| 136 | 136 | /// Allocate raw storage that remains valid until the arena is reset. |
|
| 137 | 137 | export unsafe fn allocRaw(arena: &mut Arena, size: u32, alignment: u32) -> *unsafe mut opaque throws (AllocError) { |
|
| 138 | 138 | let owner = try alloc(arena, size, alignment); |
lib/std/lang/alloc/tests.rad
+21 -2
| 109 | 109 | static STORAGE: [u8; 256] = [0; 256]; |
|
| 110 | 110 | let mut arena = super::new(&mut STORAGE[..]); |
|
| 111 | 111 | let a = super::arenaAllocator(&mut arena); |
|
| 112 | 112 | ||
| 113 | 113 | // Allocate through the Allocator indirection. |
|
| 114 | - | let p1 = a.func(a.ctx, 16, 4); |
|
| 114 | + | let p1 = try! a.func(a.ctx, 16, 4); |
|
| 115 | 115 | try testing::expect(super::used(&arena) == 16); |
|
| 116 | 116 | ||
| 117 | - | let p2 = a.func(a.ctx, 8, 8); |
|
| 117 | + | let p2 = try! a.func(a.ctx, 8, 8); |
|
| 118 | 118 | try testing::expect(super::used(&arena) == 24); |
|
| 119 | 119 | ||
| 120 | 120 | // Verify the pointers are distinct. |
|
| 121 | 121 | try testing::expect(p1 as u64 <> p2 as u64); |
|
| 122 | 122 | } |
|
| 123 | + | ||
| 124 | + | /// Append reports arena exhaustion and preserves the slice and arena state. |
|
| 125 | + | @test unsafe fn testAllocatorAppendExhausted() throws (testing::TestError) { |
|
| 126 | + | static storage: [u8; 4] = [0; 4]; |
|
| 127 | + | let mut arena = super::new(&mut storage[..]); |
|
| 128 | + | let a = super::arenaAllocator(&mut arena); |
|
| 129 | + | let mut items: *mut [u32] = &mut []; |
|
| 130 | + | try! items.append(42, a); |
|
| 131 | + | let mut caught = false; |
|
| 132 | + | try items.append(99, a) catch error as super::AllocError { |
|
| 133 | + | try testing::expect(error == super::AllocError::OutOfMemory); |
|
| 134 | + | set caught = true; |
|
| 135 | + | }; |
|
| 136 | + | try testing::expect(caught); |
|
| 137 | + | try testing::expect(items.len == 1); |
|
| 138 | + | try testing::expect(items.cap == 1); |
|
| 139 | + | try testing::expect(items[0] == 42); |
|
| 140 | + | try testing::expect(arena.offset == 4); |
|
| 141 | + | } |
lib/std/lang/ast.rad
+1 -1
| 867 | 867 | let fnBody: *Node = synthNode(arena, NodeValue::Block(Block { statements: bodyStmts, isUnsafe: false })); |
|
| 868 | 868 | let fnDecl = synthNode(arena, NodeValue::FnDecl(FnDecl { |
|
| 869 | 869 | name: fnName, sig: fnSig, body: fnBody, attrs: nil, |
|
| 870 | 870 | })); |
|
| 871 | 871 | let mut rootStmts: *mut [*Node] = &mut []; |
|
| 872 | - | rootStmts.append(fnDecl, a); |
|
| 872 | + | try! rootStmts.append(fnDecl, a); |
|
| 873 | 873 | let modBody = synthNode(arena, NodeValue::Block(Block { statements: rootStmts, isUnsafe: false })); |
|
| 874 | 874 | ||
| 875 | 875 | return SynthFnMod { modBody, fnBody }; |
|
| 876 | 876 | } |
lib/std/lang/lower.rad
+19 -19
| 350 | 350 | } |
|
| 351 | 351 | } |
|
| 352 | 352 | let tag = self.errTagCounter; |
|
| 353 | 353 | ||
| 354 | 354 | set self.errTagCounter += 1; |
|
| 355 | - | self.errTags.append(ErrTagEntry { ty: errType, tag }, self.allocator); |
|
| 355 | + | try! self.errTags.append(ErrTagEntry { ty: errType, tag }, self.allocator); |
|
| 356 | 356 | ||
| 357 | 357 | return tag; |
|
| 358 | 358 | } |
|
| 359 | 359 | ||
| 360 | 360 | /// Emit one function to the active output. |
|
| 361 | 361 | unsafe fn emitFunction(self: &mut Lowerer, func: *unsafe il::Fn, role: FnRole) { |
|
| 362 | 362 | match self.output { |
|
| 363 | 363 | case FnOutput::Accumulate(accumulated) => { |
|
| 364 | 364 | let mut fns = accumulated; |
|
| 365 | - | fns.append(func, self.allocator); |
|
| 365 | + | try! fns.append(func, self.allocator); |
|
| 366 | 366 | set self.output = FnOutput::Accumulate(fns); |
|
| 367 | 367 | } |
|
| 368 | 368 | case FnOutput::Stream(sink) => { |
|
| 369 | 369 | sink.emitFn(sink.ctx, func, role); |
|
| 370 | 370 | } |
| 421 | 421 | return true; |
|
| 422 | 422 | } |
|
| 423 | 423 | ||
| 424 | 424 | /// Append a data value to the builder. |
|
| 425 | 425 | fn dataBuilderPush(b: &mut DataValueBuilder, value: il::DataValue) { |
|
| 426 | - | b.values.append(value, b.allocator); |
|
| 426 | + | try! b.values.append(value, b.allocator); |
|
| 427 | 427 | ||
| 428 | 428 | if value.count > 0 and not dataIsZero(value.item) { |
|
| 429 | 429 | set b.zeroInit = false; |
|
| 430 | 430 | } |
|
| 431 | 431 | } |
| 907 | 907 | ||
| 908 | 908 | /// Register a function symbol with its qualified name. |
|
| 909 | 909 | /// Called when lowering function declarations, so cross-package calls can find |
|
| 910 | 910 | /// the function by name. |
|
| 911 | 911 | fn registerFnSym(self: &mut Lowerer, sym: *unsafe resolver::Symbol, qualName: *[u8]) { |
|
| 912 | - | self.fnSyms.append(FnSymEntry { sym, qualName }, self.allocator); |
|
| 912 | + | try! self.fnSyms.append(FnSymEntry { sym, qualName }, self.allocator); |
|
| 913 | 913 | } |
|
| 914 | 914 | ||
| 915 | 915 | /// Look up a function's qualified name by its symbol. |
|
| 916 | 916 | /// Returns `nil` if the symbol wasn't registered (e.g. callee's module is not yet lowered). |
|
| 917 | 917 | // TODO: This is kind of dubious as an optimization, if it depends on the order |
| 1144 | 1144 | set values[i] = il::DataValue { |
|
| 1145 | 1145 | item: il::DataItem::Fn(methodNames[i]), |
|
| 1146 | 1146 | count: 1, |
|
| 1147 | 1147 | }; |
|
| 1148 | 1148 | } |
|
| 1149 | - | self.data.append(il::Data { |
|
| 1149 | + | try! self.data.append(il::Data { |
|
| 1150 | 1150 | name: vName, |
|
| 1151 | 1151 | size: traitInfo.methods.len as u32 * resolver::PTR_SIZE, |
|
| 1152 | 1152 | alignment: resolver::PTR_SIZE, |
|
| 1153 | 1153 | readOnly: true, |
|
| 1154 | 1154 | isZeroInit: false, |
| 1352 | 1352 | let qualName = qualifyName(self, nil, sym.name); |
|
| 1353 | 1353 | let mut b = dataBuilder(self.allocator); |
|
| 1354 | 1354 | try lowerConstDataInto(self, value, data.ty, layout.size, qualName, &mut b); |
|
| 1355 | 1355 | let result = dataBuilderFinish(b); |
|
| 1356 | 1356 | ||
| 1357 | - | self.data.append(il::Data { |
|
| 1357 | + | try! self.data.append(il::Data { |
|
| 1358 | 1358 | name: qualName, |
|
| 1359 | 1359 | size: layout.size, |
|
| 1360 | 1360 | alignment: layout.alignment, |
|
| 1361 | 1361 | readOnly, |
|
| 1362 | 1362 | isZeroInit: not readOnly and result.zeroInit, |
| 1763 | 1763 | values: *[il::DataValue], |
|
| 1764 | 1764 | dataPrefix: *[u8] |
|
| 1765 | 1765 | ) -> *[u8] throws (LowerError) { |
|
| 1766 | 1766 | let dataCount = self.data.len; |
|
| 1767 | 1767 | let name = try nextDeclDataName(self, dataPrefix, dataCount, "literal"); |
|
| 1768 | - | self.data.append(il::Data { |
|
| 1768 | + | try! self.data.append(il::Data { |
|
| 1769 | 1769 | name, |
|
| 1770 | 1770 | size, |
|
| 1771 | 1771 | alignment, |
|
| 1772 | 1772 | readOnly, |
|
| 1773 | 1773 | isZeroInit: not readOnly and dataValuesAreZeroInit(values), |
| 1895 | 1895 | } |
|
| 1896 | 1896 | if let name = found { |
|
| 1897 | 1897 | set dataName = name; |
|
| 1898 | 1898 | } else { |
|
| 1899 | 1899 | set dataName = try nextDataName(self); |
|
| 1900 | - | self.low.data.append(il::Data { |
|
| 1900 | + | try! self.low.data.append(il::Data { |
|
| 1901 | 1901 | name: dataName, |
|
| 1902 | 1902 | size, |
|
| 1903 | 1903 | alignment, |
|
| 1904 | 1904 | readOnly, |
|
| 1905 | 1905 | isZeroInit: not readOnly and result.zeroInit, |
| 2040 | 2040 | let vars = try! alloc::allocRawSlice(self.low.fnArena, @sizeOf(?il::Val), @alignOf(?il::Val), varCount) as *unsafe mut [?il::Val]; |
|
| 2041 | 2041 | ||
| 2042 | 2042 | for i in 0..varCount { |
|
| 2043 | 2043 | set vars[i] = nil; |
|
| 2044 | 2044 | } |
|
| 2045 | - | self.blockData.append(BlockData { |
|
| 2045 | + | try! self.blockData.append(BlockData { |
|
| 2046 | 2046 | label, |
|
| 2047 | 2047 | params: &mut [], |
|
| 2048 | 2048 | paramVars: &mut [], |
|
| 2049 | 2049 | instrs: &mut [], |
|
| 2050 | 2050 | locs: &mut [], |
| 2063 | 2063 | labelBase: *[u8], |
|
| 2064 | 2064 | param: il::Param |
|
| 2065 | 2065 | ) -> BlockId throws (LowerError) { |
|
| 2066 | 2066 | let block = try createBlock(self, labelBase); |
|
| 2067 | 2067 | let blk = getBlockMut(self, block); |
|
| 2068 | - | blk.params.append(param, self.allocator); |
|
| 2068 | + | try! blk.params.append(param, self.allocator); |
|
| 2069 | 2069 | ||
| 2070 | 2070 | return block; |
|
| 2071 | 2071 | } |
|
| 2072 | 2072 | ||
| 2073 | 2073 | /// Switch to building a different block. |
| 2137 | 2137 | else => {}, |
|
| 2138 | 2138 | } |
|
| 2139 | 2139 | } |
|
| 2140 | 2140 | // Record source location alongside instruction when enabled. |
|
| 2141 | 2141 | if self.low.options.debug { |
|
| 2142 | - | block.locs.append(self.srcLoc, self.allocator); |
|
| 2142 | + | try! block.locs.append(self.srcLoc, self.allocator); |
|
| 2143 | 2143 | } |
|
| 2144 | - | block.instrs.append(instr, self.allocator); |
|
| 2144 | + | try! block.instrs.append(instr, self.allocator); |
|
| 2145 | 2145 | } |
|
| 2146 | 2146 | ||
| 2147 | 2147 | /// Emit an unconditional jump to `target`. |
|
| 2148 | 2148 | unsafe fn emitJmp(self: &mut FnLowerer, target: BlockId) throws (LowerError) { |
|
| 2149 | 2149 | emit(self, il::Instr::Jmp { target: *target, args: &mut [] }); |
| 2615 | 2615 | for i in 0..preds.len { |
|
| 2616 | 2616 | if preds[i] == *pred { // Avoid duplicate predecessor entries. |
|
| 2617 | 2617 | return; |
|
| 2618 | 2618 | } |
|
| 2619 | 2619 | } |
|
| 2620 | - | preds.append(*pred, self.allocator); |
|
| 2620 | + | try! preds.append(*pred, self.allocator); |
|
| 2621 | 2621 | } |
|
| 2622 | 2622 | ||
| 2623 | 2623 | /// Finalize all blocks and return the block array. |
|
| 2624 | 2624 | unsafe fn finalizeBlocks(self: &mut FnLowerer) -> *unsafe [il::Block] throws (LowerError) { |
|
| 2625 | 2625 | let blockCount = self.blockData.len; |
| 2792 | 2792 | type: il::Type, |
|
| 2793 | 2793 | mutable: bool, |
|
| 2794 | 2794 | val: il::Val |
|
| 2795 | 2795 | ) -> Var { |
|
| 2796 | 2796 | let id = self.vars.len; |
|
| 2797 | - | self.vars.append(VarData { name, type, mutable, addressTaken: false }, self.allocator); |
|
| 2797 | + | try! self.vars.append(VarData { name, type, mutable, addressTaken: false }, self.allocator); |
|
| 2798 | 2798 | ||
| 2799 | 2799 | let v = Var(id); |
|
| 2800 | 2800 | if self.currentBlock <> nil { |
|
| 2801 | 2801 | defVar(self, v, val); |
|
| 2802 | 2802 | } |
| 2922 | 2922 | ||
| 2923 | 2923 | // Create block parameter and add it to the block. |
|
| 2924 | 2924 | let param = il::Param { value: reg, type }; |
|
| 2925 | 2925 | let blk = getBlockMut(self, block); |
|
| 2926 | 2926 | let paramIdx = blk.paramVars.len; |
|
| 2927 | - | blk.params.append(param, self.allocator); |
|
| 2928 | - | blk.paramVars.append(*v, self.allocator); // Associate variable with parameter. |
|
| 2927 | + | try! blk.params.append(param, self.allocator); |
|
| 2928 | + | try! blk.paramVars.append(*v, self.allocator); // Associate variable with parameter. |
|
| 2929 | 2929 | ||
| 2930 | 2930 | // Record that this variable's value in this block is now the parameter register. |
|
| 2931 | 2931 | // This must happen before the predecessor loop to handle self-referential loops. |
|
| 2932 | 2932 | set blk.vars[*v] = il::Val::Reg(reg); |
|
| 2933 | 2933 |
| 3131 | 3131 | } else { |
|
| 3132 | 3132 | set name = try paramName(&astParams[i].value); |
|
| 3133 | 3133 | } |
|
| 3134 | 3134 | let v = newVar(self, name, type, false, il::Val::Undef); |
|
| 3135 | 3135 | ||
| 3136 | - | self.params.append(FnParamBinding { var: v, reg }, self.allocator); |
|
| 3136 | + | try! self.params.append(FnParamBinding { var: v, reg }, self.allocator); |
|
| 3137 | 3137 | } |
|
| 3138 | 3138 | return params; |
|
| 3139 | 3139 | } |
|
| 3140 | 3140 | ||
| 3141 | 3141 | /// Resolve match subject. |
| 3592 | 3592 | set blocks[i] = try createBlock(self, "case"); |
|
| 3593 | 3593 | for pat in pats { |
|
| 3594 | 3594 | let cv = resolver::constValueEntry(self.low.resolver, pat) |
|
| 3595 | 3595 | else throw LowerError::MissingConst(pat); |
|
| 3596 | 3596 | ||
| 3597 | - | cases.append(il::SwitchCase { |
|
| 3597 | + | try! cases.append(il::SwitchCase { |
|
| 3598 | 3598 | value: constToScalar(cv), |
|
| 3599 | 3599 | target: *blocks[i], |
|
| 3600 | 3600 | args: &mut [] |
|
| 3601 | 3601 | }, self.allocator); |
|
| 3602 | 3602 | } |
| 6439 | 6439 | ||
| 6440 | 6440 | if let typeNode = clause.typeNode { |
|
| 6441 | 6441 | let errTy = try typeOf(self, typeNode); |
|
| 6442 | 6442 | set errTypes[i] = errTy; |
|
| 6443 | 6443 | ||
| 6444 | - | cases.append(il::SwitchCase { |
|
| 6444 | + | try! cases.append(il::SwitchCase { |
|
| 6445 | 6445 | value: getOrAssignErrorTag(self.low, errTy) as i64, |
|
| 6446 | 6446 | target: *blocks[i], |
|
| 6447 | 6447 | args: &mut [] |
|
| 6448 | 6448 | }, self.allocator); |
|
| 6449 | 6449 | } else { |
lib/std/lang/parser.rad
+22 -22
| 265 | 265 | return node(p, ast::NodeValue::ArrayRepeatLit( |
|
| 266 | 266 | ast::ArrayRepeatLit { item: firstExpr, count } |
|
| 267 | 267 | )); |
|
| 268 | 268 | } |
|
| 269 | 269 | // Regular array literal: `[a, b, ...]`. |
|
| 270 | - | let mut items = ast::nodeSlice(p.arena, 64).append(firstExpr, p.allocator); |
|
| 270 | + | let mut items = try! ast::nodeSlice(p.arena, 64).append(firstExpr, p.allocator); |
|
| 271 | 271 | ||
| 272 | 272 | while consume(p, scanner::TokenKind::Comma) and not check(p, scanner::TokenKind::RBracket) { |
|
| 273 | 273 | let elem = try parseNormalExpr(p); |
|
| 274 | - | items.append(elem, p.allocator); |
|
| 274 | + | try! items.append(elem, p.allocator); |
|
| 275 | 275 | } |
|
| 276 | 276 | try expect(p, scanner::TokenKind::RBracket, "expected `]` after array elements"); |
|
| 277 | 277 | ||
| 278 | 278 | return node(p, ast::NodeValue::ArrayLit(items)); |
|
| 279 | 279 | } |
| 704 | 704 | ||
| 705 | 705 | if kind == ast::Builtin::SliceOf { |
|
| 706 | 706 | // Parse comma-separated expressions until closing paren. |
|
| 707 | 707 | // Argument count validation is done in semantic analysis. |
|
| 708 | 708 | while not check(p, scanner::TokenKind::RParen) { |
|
| 709 | - | args.append(try parseExpr(p), p.allocator); |
|
| 709 | + | try! args.append(try parseExpr(p), p.allocator); |
|
| 710 | 710 | if not consume(p, scanner::TokenKind::Comma) { |
|
| 711 | 711 | break; |
|
| 712 | 712 | } |
|
| 713 | 713 | } |
|
| 714 | 714 | } else { |
|
| 715 | - | args.append(try parseType(p), p.allocator); |
|
| 715 | + | try! args.append(try parseType(p), p.allocator); |
|
| 716 | 716 | } |
|
| 717 | 717 | try expect(p, scanner::TokenKind::RParen, "expected `)` after builtin argument"); |
|
| 718 | 718 | ||
| 719 | 719 | return node(p, ast::NodeValue::BuiltinCall { kind, args }); |
|
| 720 | 720 | } |
| 787 | 787 | /// Parse leading attributes and declaration modifiers. |
|
| 788 | 788 | unsafe fn parseAttributes(p: &mut Parser) -> ?ast::Attributes { |
|
| 789 | 789 | let mut attrs = ast::nodeSlice(p.arena, 4); |
|
| 790 | 790 | ||
| 791 | 791 | if let attr = tryParseAnnotation(p) { |
|
| 792 | - | attrs.append(attr, p.allocator); |
|
| 792 | + | try! attrs.append(attr, p.allocator); |
|
| 793 | 793 | } |
|
| 794 | 794 | if consume(p, scanner::TokenKind::Export) { |
|
| 795 | - | attrs.append(nodeAttribute(p, ast::Attribute::Export), p.allocator); |
|
| 795 | + | try! attrs.append(nodeAttribute(p, ast::Attribute::Export), p.allocator); |
|
| 796 | 796 | } |
|
| 797 | 797 | if consume(p, scanner::TokenKind::Unsafe) { |
|
| 798 | - | attrs.append(nodeAttribute(p, ast::Attribute::Unsafe), p.allocator); |
|
| 798 | + | try! attrs.append(nodeAttribute(p, ast::Attribute::Unsafe), p.allocator); |
|
| 799 | 799 | } |
|
| 800 | 800 | if attrs.len > 0 { |
|
| 801 | 801 | return ast::Attributes { list: attrs }; |
|
| 802 | 802 | } |
|
| 803 | 803 | return nil; |
| 961 | 961 | throws (ParseError) |
|
| 962 | 962 | { |
|
| 963 | 963 | let mut statements = ast::nodeSlice(p.arena, capacity); |
|
| 964 | 964 | while not check(p, end) { |
|
| 965 | 965 | let stmt = try parseStmt(p); |
|
| 966 | - | statements.append(stmt, p.allocator); |
|
| 966 | + | try! statements.append(stmt, p.allocator); |
|
| 967 | 967 | ||
| 968 | 968 | if check(p, end) or check(p, scanner::TokenKind::Eof) { |
|
| 969 | 969 | break; |
|
| 970 | 970 | } |
|
| 971 | 971 | if not consume(p, scanner::TokenKind::Semicolon) { |
| 1001 | 1001 | return node(p, ast::NodeValue::Block(blk)); |
|
| 1002 | 1002 | } |
|
| 1003 | 1003 | ||
| 1004 | 1004 | /// Create a block containing a single statement node. |
|
| 1005 | 1005 | unsafe fn mkBlockWith(p: &mut Parser, node: *ast::Node) -> ast::Block { |
|
| 1006 | - | let stmts = ast::nodeSlice(p.arena, 1).append(node, p.allocator); |
|
| 1006 | + | let stmts = try! ast::nodeSlice(p.arena, 1).append(node, p.allocator); |
|
| 1007 | 1007 | return ast::Block { statements: stmts, isUnsafe: false }; |
|
| 1008 | 1008 | } |
|
| 1009 | 1009 | ||
| 1010 | 1010 | /// Parse the branch that follows `else` in let-else style constructs. |
|
| 1011 | 1011 | /// |
| 1369 | 1369 | } |
|
| 1370 | 1370 | let body = try parseBlock(p); |
|
| 1371 | 1371 | let clause = node(p, ast::NodeValue::CatchClause( |
|
| 1372 | 1372 | ast::CatchClause { binding, typeNode, body } |
|
| 1373 | 1373 | )); |
|
| 1374 | - | catches.append(clause, p.allocator); |
|
| 1374 | + | try! catches.append(clause, p.allocator); |
|
| 1375 | 1375 | } |
|
| 1376 | 1376 | return node(p, ast::NodeValue::Try( |
|
| 1377 | 1377 | ast::Try { expr, catches, shouldPanic, returnsOptional } |
|
| 1378 | 1378 | )); |
|
| 1379 | 1379 | } |
| 1456 | 1456 | let mut prongs = ast::nodeSlice(p.arena, 128); |
|
| 1457 | 1457 | while not check(p, scanner::TokenKind::RBrace) and |
|
| 1458 | 1458 | not check(p, scanner::TokenKind::Eof) // TODO: We shouldn't have to manually check for EOF. |
|
| 1459 | 1459 | { |
|
| 1460 | 1460 | let prongNode = try parseMatchProng(p); |
|
| 1461 | - | prongs.append(prongNode, p.allocator); |
|
| 1461 | + | try! prongs.append(prongNode, p.allocator); |
|
| 1462 | 1462 | consume(p, scanner::TokenKind::Comma); |
|
| 1463 | 1463 | } |
|
| 1464 | 1464 | try expect(p, scanner::TokenKind::RBrace, "expected `}` after match prongs"); |
|
| 1465 | 1465 | ||
| 1466 | 1466 | return node(p, ast::NodeValue::Match( |
| 1477 | 1477 | // Case prong: `case <pattern>, ... if <guard> => <body>`. |
|
| 1478 | 1478 | if consume(p, scanner::TokenKind::Case) { |
|
| 1479 | 1479 | let mut patterns = ast::nodeSlice(p.arena, 16); |
|
| 1480 | 1480 | loop { |
|
| 1481 | 1481 | let pattern = try parseMatchPattern(p); |
|
| 1482 | - | patterns.append(pattern, p.allocator); |
|
| 1482 | + | try! patterns.append(pattern, p.allocator); |
|
| 1483 | 1483 | ||
| 1484 | 1484 | if not consume(p, scanner::TokenKind::Comma) { |
|
| 1485 | 1485 | break; |
|
| 1486 | 1486 | } |
|
| 1487 | 1487 | // After a comma, check for tokens that start a new prong. |
| 1607 | 1607 | set recordField = ast::NodeValue::RecordField { |
|
| 1608 | 1608 | field: nil, type, value: nil, |
|
| 1609 | 1609 | }; |
|
| 1610 | 1610 | } |
|
| 1611 | 1611 | } |
|
| 1612 | - | fields.append(node(p, recordField), p.allocator); |
|
| 1612 | + | try! fields.append(node(p, recordField), p.allocator); |
|
| 1613 | 1613 | ||
| 1614 | 1614 | if not consume(p, scanner::TokenKind::Comma) { |
|
| 1615 | 1615 | break; |
|
| 1616 | 1616 | } |
|
| 1617 | 1617 | } |
| 1627 | 1627 | if not consume(p, scanner::TokenKind::Colon) { |
|
| 1628 | 1628 | return derives; |
|
| 1629 | 1629 | } |
|
| 1630 | 1630 | loop { |
|
| 1631 | 1631 | let t = try parseIdent(p, "expected trait name in derive list"); |
|
| 1632 | - | derives.append(t, p.allocator); |
|
| 1632 | + | try! derives.append(t, p.allocator); |
|
| 1633 | 1633 | ||
| 1634 | 1634 | if not consume(p, scanner::TokenKind::Plus) { |
|
| 1635 | 1635 | break; |
|
| 1636 | 1636 | } |
|
| 1637 | 1637 | } |
| 1672 | 1672 | if consume(p, scanner::TokenKind::DotDot) { |
|
| 1673 | 1673 | set ignoreRest = true; |
|
| 1674 | 1674 | break; |
|
| 1675 | 1675 | } |
|
| 1676 | 1676 | let field = try parseRecordLitField(p); |
|
| 1677 | - | fields.append(field, p.allocator); |
|
| 1677 | + | try! fields.append(field, p.allocator); |
|
| 1678 | 1678 | ||
| 1679 | 1679 | if not consume(p, scanner::TokenKind::Comma) { |
|
| 1680 | 1680 | break; |
|
| 1681 | 1681 | } |
|
| 1682 | 1682 | } |
| 1754 | 1754 | let variant = node(p, ast::NodeValue::UnionDeclVariant( |
|
| 1755 | 1755 | ast::UnionDeclVariant { |
|
| 1756 | 1756 | name: variantName, index: variants.len as u32, value: explicitValue, type: payloadType, |
|
| 1757 | 1757 | } |
|
| 1758 | 1758 | )); |
|
| 1759 | - | variants.append(variant, p.allocator); |
|
| 1759 | + | try! variants.append(variant, p.allocator); |
|
| 1760 | 1760 | ||
| 1761 | 1761 | if not consume(p, scanner::TokenKind::Comma) { |
|
| 1762 | 1762 | break; |
|
| 1763 | 1763 | } |
|
| 1764 | 1764 | } |
| 1828 | 1828 | try expect(p, scanner::TokenKind::LParen, "expected `(` after function name"); |
|
| 1829 | 1829 | let mut params = ast::nodeSlice(p.arena, 8); |
|
| 1830 | 1830 | ||
| 1831 | 1831 | while not check(p, scanner::TokenKind::RParen) { |
|
| 1832 | 1832 | let param = try parseFnParam(p); |
|
| 1833 | - | params.append(param, p.allocator); |
|
| 1833 | + | try! params.append(param, p.allocator); |
|
| 1834 | 1834 | ||
| 1835 | 1835 | if not consume(p, scanner::TokenKind::Comma) { |
|
| 1836 | 1836 | break; |
|
| 1837 | 1837 | } |
|
| 1838 | 1838 | } |
| 1875 | 1875 | // Keep existing attributes unchanged. |
|
| 1876 | 1876 | } else { |
|
| 1877 | 1877 | let mut list = ast::nodeSlice(p.arena, 4); |
|
| 1878 | 1878 | if let a = attrs { |
|
| 1879 | 1879 | for i in 0..a.list.len { |
|
| 1880 | - | list.append(a.list[i], p.allocator); |
|
| 1880 | + | try! list.append(a.list[i], p.allocator); |
|
| 1881 | 1881 | } |
|
| 1882 | 1882 | } |
|
| 1883 | 1883 | let attrNode = nodeAttribute(p, ast::Attribute::Extern); |
|
| 1884 | - | list.append(attrNode, p.allocator); |
|
| 1884 | + | try! list.append(attrNode, p.allocator); |
|
| 1885 | 1885 | set fnAttrs = ast::Attributes { list }; |
|
| 1886 | 1886 | } |
|
| 1887 | 1887 | } else { |
|
| 1888 | 1888 | set body = try parseBlock(p); |
|
| 1889 | 1889 | } |
| 2281 | 2281 | let mut methods = ast::nodeSlice(p.arena, ast::MAX_TRAIT_METHODS); |
|
| 2282 | 2282 | while not check(p, scanner::TokenKind::RBrace) and |
|
| 2283 | 2283 | not check(p, scanner::TokenKind::Eof) |
|
| 2284 | 2284 | { |
|
| 2285 | 2285 | let method = try parseTraitMethodSig(p); |
|
| 2286 | - | methods.append(method, p.allocator); |
|
| 2286 | + | try! methods.append(method, p.allocator); |
|
| 2287 | 2287 | } |
|
| 2288 | 2288 | try expect(p, scanner::TokenKind::RBrace, "expected `}` after trait methods"); |
|
| 2289 | 2289 | ||
| 2290 | 2290 | return node(p, ast::NodeValue::TraitDecl { name, supertraits, methods, attrs }); |
|
| 2291 | 2291 | } |
| 2331 | 2331 | { |
|
| 2332 | 2332 | let attrs = parseAttributes(p); |
|
| 2333 | 2333 | try expect(p, scanner::TokenKind::Fn, "expected `fn`"); |
|
| 2334 | 2334 | let method = try parseMethodDecl(p, attrs); |
|
| 2335 | 2335 | ||
| 2336 | - | methods.append(method, p.allocator); |
|
| 2336 | + | try! methods.append(method, p.allocator); |
|
| 2337 | 2337 | } |
|
| 2338 | 2338 | try expect(p, scanner::TokenKind::RBrace, "expected `}` after instance methods"); |
|
| 2339 | 2339 | ||
| 2340 | 2340 | return node(p, ast::NodeValue::InstanceDecl { traitName, targetType, methods }); |
|
| 2341 | 2341 | } |
| 2375 | 2375 | try expect(p, open, listExpectMessage(open)); |
|
| 2376 | 2376 | let mut items = ast::nodeSlice(p.arena, 8); |
|
| 2377 | 2377 | ||
| 2378 | 2378 | while not check(p, close) { |
|
| 2379 | 2379 | let item = try parseItem(p); |
|
| 2380 | - | items.append(item, p.allocator); |
|
| 2380 | + | try! items.append(item, p.allocator); |
|
| 2381 | 2381 | ||
| 2382 | 2382 | if not consume(p, scanner::TokenKind::Comma) { |
|
| 2383 | 2383 | break; |
|
| 2384 | 2384 | } |
|
| 2385 | 2385 | } |
lib/std/lang/resolver.rad
+18 -18
| 700 | 700 | /// Method index in the v-table. |
|
| 701 | 701 | methodIndex: u32, |
|
| 702 | 702 | }, |
|
| 703 | 703 | /// Standalone method call metadata. |
|
| 704 | 704 | MethodCall { method: *unsafe MethodEntry }, |
|
| 705 | - | /// Slice `.append(value, allocator)` method call. |
|
| 705 | + | /// Slice `.append(value, allocator)` with the allocator's thrown errors. |
|
| 706 | 706 | SliceAppend { elemType: *Type }, |
|
| 707 | 707 | /// Slice `.delete(index)` method call. |
|
| 708 | 708 | SliceDelete { elemType: *Type }, |
|
| 709 | 709 | } |
|
| 710 | 710 |
| 3479 | 3479 | for paramNode in decl.sig.params { |
|
| 3480 | 3480 | let paramTy = try infer(self, paramNode) catch e { |
|
| 3481 | 3481 | exitFn(self); |
|
| 3482 | 3482 | throw e; |
|
| 3483 | 3483 | }; |
|
| 3484 | - | paramTypes.append(allocType(self, paramTy), a); |
|
| 3484 | + | try! paramTypes.append(allocType(self, paramTy), a); |
|
| 3485 | 3485 | } |
|
| 3486 | 3486 | ||
| 3487 | 3487 | if decl.sig.throwList.len > MAX_FN_THROWS { |
|
| 3488 | 3488 | exitFn(self); |
|
| 3489 | 3489 | throw emitError(self, node, ErrorKind::FnThrowOverflow(CountMismatch { |
| 3494 | 3494 | for throwNode in decl.sig.throwList { |
|
| 3495 | 3495 | let throwTy = try infer(self, throwNode) catch e { |
|
| 3496 | 3496 | exitFn(self); |
|
| 3497 | 3497 | throw e; |
|
| 3498 | 3498 | }; |
|
| 3499 | - | throwList.append(allocType(self, throwTy), a); |
|
| 3499 | + | try! throwList.append(allocType(self, throwTy), a); |
|
| 3500 | 3500 | try ensureStorableType(self, throwNode, throwTy); |
|
| 3501 | 3501 | } |
|
| 3502 | 3502 | exitFn(self); |
|
| 3503 | 3503 | set fnType.paramTypes = ¶mTypes[..]; |
|
| 3504 | 3504 | set fnType.throwList = &throwList[..]; |
| 3680 | 3680 | ||
| 3681 | 3681 | // Compute field offset by aligning to field's alignment. |
|
| 3682 | 3682 | let fieldLayout = getTypeLayout(fieldType); |
|
| 3683 | 3683 | set currentOffset = mem::alignUp(currentOffset, fieldLayout.alignment); |
|
| 3684 | 3684 | ||
| 3685 | - | result.append(RecordField { name: fieldName, fieldType, offset: currentOffset as i32 }, a); |
|
| 3685 | + | try! result.append(RecordField { name: fieldName, fieldType, offset: currentOffset as i32 }, a); |
|
| 3686 | 3686 | ||
| 3687 | 3687 | // Advance offset past this field. |
|
| 3688 | 3688 | set currentOffset += fieldLayout.size; |
|
| 3689 | 3689 | ||
| 3690 | 3690 | // Track max alignment for record layout. |
| 3824 | 3824 | // Copy inherited methods into this trait's method table. |
|
| 3825 | 3825 | for inherited in superTrait.methods { |
|
| 3826 | 3826 | if let _ = findTraitMethod(traitType, inherited.name) { |
|
| 3827 | 3827 | throw emitError(self, superNode, ErrorKind::DuplicateBinding(inherited.name)); |
|
| 3828 | 3828 | } |
|
| 3829 | - | traitType.methods.append(TraitMethod { |
|
| 3829 | + | try! traitType.methods.append(TraitMethod { |
|
| 3830 | 3830 | name: inherited.name, |
|
| 3831 | 3831 | fnType: inherited.fnType, |
|
| 3832 | 3832 | mutable: inherited.mutable, |
|
| 3833 | 3833 | receiverClass: inherited.receiverClass, |
|
| 3834 | 3834 | index: traitType.methods.len as u32, |
|
| 3835 | 3835 | }, a); |
|
| 3836 | 3836 | } |
|
| 3837 | - | traitType.supertraits.append(superTrait, a); |
|
| 3837 | + | try! traitType.supertraits.append(superTrait, a); |
|
| 3838 | 3838 | } |
|
| 3839 | 3839 | ||
| 3840 | 3840 | if traitType.methods.len + methods.len > ast::MAX_TRAIT_METHODS { |
|
| 3841 | 3841 | throw emitError(self, node, ErrorKind::TraitMethodOverflow(CountMismatch { |
|
| 3842 | 3842 | expected: ast::MAX_TRAIT_METHODS, |
| 3883 | 3883 | actual: sig.params.len, |
|
| 3884 | 3884 | })); |
|
| 3885 | 3885 | } |
|
| 3886 | 3886 | for paramNode in sig.params { |
|
| 3887 | 3887 | let paramTy = try infer(self, paramNode); |
|
| 3888 | - | paramTypes.append(allocType(self, paramTy), a); |
|
| 3888 | + | try! paramTypes.append(allocType(self, paramTy), a); |
|
| 3889 | 3889 | } |
|
| 3890 | 3890 | if let ret = sig.returnType { |
|
| 3891 | 3891 | set retType = allocType(self, try infer(self, ret)); |
|
| 3892 | 3892 | } |
|
| 3893 | 3893 | // Resolve throws list. |
| 3897 | 3897 | actual: sig.throwList.len, |
|
| 3898 | 3898 | })); |
|
| 3899 | 3899 | } |
|
| 3900 | 3900 | for throwNode in sig.throwList { |
|
| 3901 | 3901 | let throwTy = try infer(self, throwNode); |
|
| 3902 | - | throwList.append(allocType(self, throwTy), a); |
|
| 3902 | + | try! throwList.append(allocType(self, throwTy), a); |
|
| 3903 | 3903 | } |
|
| 3904 | 3904 | let fnType = FnType { |
|
| 3905 | 3905 | paramTypes: ¶mTypes[..], |
|
| 3906 | 3906 | returnType: retType, |
|
| 3907 | 3907 | throwList: &throwList[..], |
|
| 3908 | 3908 | isUnsafe: ast::hasAttribute(attrMask, ast::Attribute::Unsafe), |
|
| 3909 | 3909 | }; |
|
| 3910 | - | traitType.methods.append(TraitMethod { |
|
| 3910 | + | try! traitType.methods.append(TraitMethod { |
|
| 3911 | 3911 | name: methodName, |
|
| 3912 | 3912 | fnType: allocFnType(self, fnType), |
|
| 3913 | 3913 | mutable, |
|
| 3914 | 3914 | receiverClass, |
|
| 3915 | 3915 | index: traitType.methods.len as u32, |
| 4090 | 4090 | ||
| 4091 | 4091 | // Build final function type: receiver plus trait's canonical types. |
|
| 4092 | 4092 | let a = alloc::arenaAllocator(&mut self.arena); |
|
| 4093 | 4093 | // TODO: Improve this pattern, maybe via something like `(&[]).append(..)`? |
|
| 4094 | 4094 | let mut paramTypes: *mut [*Type] = &mut []; |
|
| 4095 | - | paramTypes.append(allocType(self, receiverPtrType), a); |
|
| 4095 | + | try! paramTypes.append(allocType(self, receiverPtrType), a); |
|
| 4096 | 4096 | ||
| 4097 | 4097 | for ty in tm.fnType.paramTypes { |
|
| 4098 | - | paramTypes.append(ty, a); |
|
| 4098 | + | try! paramTypes.append(ty, a); |
|
| 4099 | 4099 | } |
|
| 4100 | 4100 | let fnType = FnType { |
|
| 4101 | 4101 | paramTypes: ¶mTypes[..], |
|
| 4102 | 4102 | returnType: tm.fnType.returnType, |
|
| 4103 | 4103 | throwList: tm.fnType.throwList, |
| 4241 | 4241 | let receiverPtrType = Type::Pointer { |
|
| 4242 | 4242 | class: receiverClass, |
|
| 4243 | 4243 | target: allocType(self, concreteType), |
|
| 4244 | 4244 | mutable: receiverMut, |
|
| 4245 | 4245 | }; |
|
| 4246 | - | paramTypes.append(allocType(self, receiverPtrType), a); |
|
| 4246 | + | try! paramTypes.append(allocType(self, receiverPtrType), a); |
|
| 4247 | 4247 | ||
| 4248 | 4248 | for paramNode in sig.params { |
|
| 4249 | 4249 | let case ast::NodeValue::FnParam(param) = paramNode.value |
|
| 4250 | 4250 | else throw emitError(self, paramNode, ErrorKind::ExpectedIdentifier); |
|
| 4251 | 4251 | let paramTy = try resolveValueType(self, param.type); |
|
| 4252 | - | paramTypes.append(allocType(self, paramTy), a); |
|
| 4252 | + | try! paramTypes.append(allocType(self, paramTy), a); |
|
| 4253 | 4253 | } |
|
| 4254 | 4254 | ||
| 4255 | 4255 | // Resolve return type. |
|
| 4256 | 4256 | let mut returnType = Type::Void; |
|
| 4257 | 4257 | if let retNode = sig.returnType { |
| 4260 | 4260 | ||
| 4261 | 4261 | // Resolve throw list. |
|
| 4262 | 4262 | let mut throwTypes: *mut [*Type] = &mut []; |
|
| 4263 | 4263 | for throwNode in sig.throwList { |
|
| 4264 | 4264 | let throwTy = try resolveValueType(self, throwNode); |
|
| 4265 | - | throwTypes.append(allocType(self, throwTy), a); |
|
| 4265 | + | try! throwTypes.append(allocType(self, throwTy), a); |
|
| 4266 | 4266 | } |
|
| 4267 | 4267 | ||
| 4268 | 4268 | let retTypePtr = allocType(self, returnType); |
|
| 4269 | 4269 | let throwList = &throwTypes[..]; |
|
| 4270 | 4270 |
| 4393 | 4393 | let tag = variantTag(variantDecl, &mut iota); |
|
| 4394 | 4394 | // Create a symbol for this variant. |
|
| 4395 | 4395 | let data = SymbolData::Variant { type: variantType, decl: node, ordinal: i, index: tag }; |
|
| 4396 | 4396 | let variantSym = allocSymbol(self, data, variantName, variantNode, 0); |
|
| 4397 | 4397 | ||
| 4398 | - | variants.append(UnionVariant { |
|
| 4398 | + | try! variants.append(UnionVariant { |
|
| 4399 | 4399 | name: variantName, |
|
| 4400 | 4400 | valueType: variantType, |
|
| 4401 | 4401 | symbol: variantSym, |
|
| 4402 | 4402 | }, a); |
|
| 4403 | 4403 | } |
| 7483 | 7483 | })); |
|
| 7484 | 7484 | } |
|
| 7485 | 7485 | ||
| 7486 | 7486 | for paramNode in t.params { |
|
| 7487 | 7487 | let paramTy = try resolveValueType(self, paramNode); |
|
| 7488 | - | paramTypes.append(allocType(self, paramTy), a); |
|
| 7488 | + | try! paramTypes.append(allocType(self, paramTy), a); |
|
| 7489 | 7489 | } |
|
| 7490 | 7490 | for tyNode in t.throwList { |
|
| 7491 | 7491 | let throwTy = try resolveValueType(self, tyNode); |
|
| 7492 | 7492 | try ensureStorableType(self, tyNode, throwTy); |
|
| 7493 | - | throwList.append(allocType(self, throwTy), a); |
|
| 7493 | + | try! throwList.append(allocType(self, throwTy), a); |
|
| 7494 | 7494 | } |
|
| 7495 | 7495 | let mut retType = allocType(self, Type::Void); |
|
| 7496 | 7496 | if let ret = t.returnType { |
|
| 7497 | 7497 | let resolvedRet = try resolveValueType(self, ret); |
|
| 7498 | 7498 | try ensureStorableType(self, ret, resolvedRet); |
| 7535 | 7535 | export unsafe fn resolveExpr( |
|
| 7536 | 7536 | self: &mut Resolver, expr: *ast::Node, arena: &mut ast::NodeArena |
|
| 7537 | 7537 | ) -> Diagnostics throws (ResolveError) { |
|
| 7538 | 7538 | let a = alloc::arenaAllocator(&mut arena.arena); |
|
| 7539 | 7539 | let exprStmt = ast::synthNode(arena, ast::NodeValue::ExprStmt(expr)); |
|
| 7540 | - | let bodyStmts = ast::nodeSlice(arena, 1).append(exprStmt, a); |
|
| 7540 | + | let bodyStmts = try! ast::nodeSlice(arena, 1).append(exprStmt, a); |
|
| 7541 | 7541 | let module = ast::synthFnModule(arena, ANALYZE_EXPR_FN_NAME, bodyStmts); |
|
| 7542 | 7542 | ||
| 7543 | 7543 | let case ast::NodeValue::Block(block) = module.modBody.value |
|
| 7544 | 7544 | else panic "resolveExpr: expected block for module body"; |
|
| 7545 | 7545 | enterScope(self, module.modBody); |
test/tests/slice.range.capacity.rad
+11 -5
| 1 | 1 | //! returns: 0 |
|
| 2 | 2 | //! Slice ranges retain the backing capacity after their start offset. |
|
| 3 | 3 | ||
| 4 | + | /// Allocation failures. |
|
| 5 | + | union AllocError: Copy { |
|
| 6 | + | /// The allocator cannot provide storage. |
|
| 7 | + | OutOfMemory, |
|
| 8 | + | } |
|
| 9 | + | ||
| 4 | 10 | /// Allocator interface used by append. |
|
| 5 | 11 | record Allocator: Copy { |
|
| 6 | 12 | /// Allocation callback. |
|
| 7 | - | func: unsafe fn(*unsafe mut opaque, u32, u32) -> *mut opaque, |
|
| 13 | + | func: unsafe fn(*unsafe mut opaque, u32, u32) -> *mut opaque throws (AllocError), |
|
| 8 | 14 | /// Callback context. |
|
| 9 | 15 | ctx: *unsafe mut opaque, |
|
| 10 | 16 | } |
|
| 11 | 17 | ||
| 12 | 18 | /// Reject allocation when append must use existing storage. |
|
| 13 | - | unsafe fn rejectAlloc(ctx: *unsafe mut opaque, size: u32, alignment: u32) -> *mut opaque { |
|
| 19 | + | unsafe fn rejectAlloc(ctx: *unsafe mut opaque, size: u32, alignment: u32) -> *mut opaque throws (AllocError) { |
|
| 14 | 20 | panic "rejectAlloc: unexpected allocation"; |
|
| 15 | 21 | } |
|
| 16 | 22 | ||
| 17 | 23 | /// Check dynamic range bounds and nested slice capacity. |
|
| 18 | 24 | fn checkRange(storage: *mut [i32], start: u32, end: u32) { |
| 45 | 51 | assert end.cap == 0; |
|
| 46 | 52 | checkRange((&mut storage[..2]) as *mut [i32], 1, 2); |
|
| 47 | 53 | checkRange((&mut storage[..]) as *mut [i32], 4, 4); |
|
| 48 | 54 | let mut items: *mut [i32] = &mut storage[1..1]; |
|
| 49 | 55 | let allocator = Allocator { func: rejectAlloc, ctx: &mut storage as *unsafe mut opaque }; |
|
| 50 | - | items.append(50, allocator); |
|
| 51 | - | items.append(60, allocator); |
|
| 52 | - | items.append(70, allocator); |
|
| 56 | + | try! items.append(50, allocator); |
|
| 57 | + | try! items.append(60, allocator); |
|
| 58 | + | try! items.append(70, allocator); |
|
| 53 | 59 | assert items.len == 3; |
|
| 54 | 60 | assert items.cap == 3; |
|
| 55 | 61 | assert items[0] == 50; |
|
| 56 | 62 | assert items[2] == 70; |
|
| 57 | 63 | return 0; |