From ceb0a632cfd6a4eada6bd27bf6a3754e95dcac86 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 27 Nov 2022 01:07:35 -0700 Subject: std.mem.Allocator: allow shrink to fail closes #13535 --- src/Sema.zig | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'src/Sema.zig') diff --git a/src/Sema.zig b/src/Sema.zig index eb465d25a3..ae64b4d981 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -2244,7 +2244,7 @@ fn failWithOwnedErrorMsg(sema: *Sema, err_msg: *Module.ErrorMsg) CompileError { .hidden = cur_reference_trace - max_references, }); } - err_msg.reference_trace = reference_stack.toOwnedSlice(); + err_msg.reference_trace = try reference_stack.toOwnedSlice(); } if (sema.owner_func) |func| { func.state = .sema_failure; @@ -5500,20 +5500,18 @@ pub fn analyzeExport( // Add to export_owners table. const eo_gop = mod.export_owners.getOrPutAssumeCapacity(sema.owner_decl_index); if (!eo_gop.found_existing) { - eo_gop.value_ptr.* = &[0]*Export{}; + eo_gop.value_ptr.* = .{}; } - eo_gop.value_ptr.* = try gpa.realloc(eo_gop.value_ptr.*, eo_gop.value_ptr.len + 1); - eo_gop.value_ptr.*[eo_gop.value_ptr.len - 1] = new_export; - errdefer eo_gop.value_ptr.* = gpa.shrink(eo_gop.value_ptr.*, eo_gop.value_ptr.len - 1); + try eo_gop.value_ptr.append(gpa, new_export); + errdefer _ = eo_gop.value_ptr.pop(); // Add to exported_decl table. const de_gop = mod.decl_exports.getOrPutAssumeCapacity(exported_decl_index); if (!de_gop.found_existing) { - de_gop.value_ptr.* = &[0]*Export{}; + de_gop.value_ptr.* = .{}; } - de_gop.value_ptr.* = try gpa.realloc(de_gop.value_ptr.*, de_gop.value_ptr.len + 1); - de_gop.value_ptr.*[de_gop.value_ptr.len - 1] = new_export; - errdefer de_gop.value_ptr.* = gpa.shrink(de_gop.value_ptr.*, de_gop.value_ptr.len - 1); + try de_gop.value_ptr.append(gpa, new_export); + errdefer _ = de_gop.value_ptr.pop(); } fn zirSetAlignStack(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!void { @@ -10762,7 +10760,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError .payload = undefined, }, } }); - var cond_body = case_block.instructions.toOwnedSlice(gpa); + var cond_body = try case_block.instructions.toOwnedSlice(gpa); defer gpa.free(cond_body); var wip_captures = try WipCaptureScope.init(gpa, sema.perm_arena, child_block.wip_capture_scope); @@ -10800,7 +10798,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError sema.air_extra.appendSliceAssumeCapacity(cond_body); } gpa.free(prev_then_body); - prev_then_body = case_block.instructions.toOwnedSlice(gpa); + prev_then_body = try case_block.instructions.toOwnedSlice(gpa); prev_cond_br = new_cond_br; } } @@ -16318,7 +16316,7 @@ fn zirCondbr( defer sub_block.instructions.deinit(gpa); try sema.analyzeBodyRuntimeBreak(&sub_block, then_body); - const true_instructions = sub_block.instructions.toOwnedSlice(gpa); + const true_instructions = try sub_block.instructions.toOwnedSlice(gpa); defer gpa.free(true_instructions); const err_cond = blk: { -- cgit v1.2.3 From f4666678886c2a7a993ad30b63de4ff25594085a Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 29 Nov 2022 13:40:57 -0700 Subject: stage2: fix crash on comptime lazy `@ctz` and `@clz` --- lib/std/math/big/int.zig | 28 ++++++++++++++++++++++++++++ src/Sema.zig | 1 + src/value.zig | 38 ++++++++++++++------------------------ 3 files changed, 43 insertions(+), 24 deletions(-) (limited to 'src/Sema.zig') diff --git a/lib/std/math/big/int.zig b/lib/std/math/big/int.zig index 9d0228db4b..534e8a570d 100644 --- a/lib/std/math/big/int.zig +++ b/lib/std/math/big/int.zig @@ -2376,6 +2376,34 @@ pub const Const = struct { pub fn eq(a: Const, b: Const) bool { return order(a, b) == .eq; } + + pub fn clz(a: Const, bits: Limb) Limb { + // Limbs are stored in little-endian order but we need + // to iterate big-endian. + var total_limb_lz: Limb = 0; + var i: usize = a.limbs.len; + const bits_per_limb = @sizeOf(Limb) * 8; + while (i != 0) { + i -= 1; + const limb = a.limbs[i]; + const this_limb_lz = @clz(limb); + total_limb_lz += this_limb_lz; + if (this_limb_lz != bits_per_limb) break; + } + const total_limb_bits = a.limbs.len * bits_per_limb; + return total_limb_lz + bits - total_limb_bits; + } + + pub fn ctz(a: Const) Limb { + // Limbs are stored in little-endian order. + var result: Limb = 0; + for (a.limbs) |limb| { + const limb_tz = @ctz(limb); + result += limb_tz; + if (limb_tz != @sizeOf(Limb) * 8) break; + } + return result; + } }; /// An arbitrary-precision big integer along with an allocator which manages the memory. diff --git a/src/Sema.zig b/src/Sema.zig index ae64b4d981..be44ca6a5f 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -19299,6 +19299,7 @@ fn zirBitCount( .Int => { if (try sema.resolveMaybeUndefVal(operand)) |val| { if (val.isUndef()) return sema.addConstUndef(result_scalar_ty); + try sema.resolveLazyValue(val); return sema.addIntUnsigned(result_scalar_ty, comptimeOp(val, operand_ty, target)); } else { try sema.requireRuntimeBlock(block, src, operand_src); diff --git a/src/value.zig b/src/value.zig index 94769b4da7..a029be6c7b 100644 --- a/src/value.zig +++ b/src/value.zig @@ -1677,22 +1677,8 @@ pub const Value = extern union { @panic("TODO implement i64 Value clz"); }, .int_big_positive => { - // TODO: move this code into std lib big ints const bigint = val.castTag(.int_big_positive).?.asBigInt(); - // Limbs are stored in little-endian order but we need - // to iterate big-endian. - var total_limb_lz: u64 = 0; - var i: usize = bigint.limbs.len; - const bits_per_limb = @sizeOf(std.math.big.Limb) * 8; - while (i != 0) { - i -= 1; - const limb = bigint.limbs[i]; - const this_limb_lz = @clz(limb); - total_limb_lz += this_limb_lz; - if (this_limb_lz != bits_per_limb) break; - } - const total_limb_bits = bigint.limbs.len * bits_per_limb; - return total_limb_lz + ty_bits - total_limb_bits; + return bigint.clz(ty_bits); }, .int_big_negative => { @panic("TODO implement int_big_negative Value clz"); @@ -1703,6 +1689,12 @@ pub const Value = extern union { return ty_bits; }, + .lazy_align, .lazy_size => { + var bigint_buf: BigIntSpace = undefined; + const bigint = val.toBigIntAdvanced(&bigint_buf, target, null) catch unreachable; + return bigint.clz(ty_bits); + }, + else => unreachable, } } @@ -1721,16 +1713,8 @@ pub const Value = extern union { @panic("TODO implement i64 Value ctz"); }, .int_big_positive => { - // TODO: move this code into std lib big ints const bigint = val.castTag(.int_big_positive).?.asBigInt(); - // Limbs are stored in little-endian order. - var result: u64 = 0; - for (bigint.limbs) |limb| { - const limb_tz = @ctz(limb); - result += limb_tz; - if (limb_tz != @sizeOf(std.math.big.Limb) * 8) break; - } - return result; + return bigint.ctz(); }, .int_big_negative => { @panic("TODO implement int_big_negative Value ctz"); @@ -1741,6 +1725,12 @@ pub const Value = extern union { return ty_bits; }, + .lazy_align, .lazy_size => { + var bigint_buf: BigIntSpace = undefined; + const bigint = val.toBigIntAdvanced(&bigint_buf, target, null) catch unreachable; + return bigint.ctz(); + }, + else => unreachable, } } -- cgit v1.2.3