authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-05-07 22:12:04+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-10 20:42:29-07:00
log2ffef605c75b62ba49e21bfb3256537a4a2c0a5e
treeba18403418bcd0c1e6f6f52f9effa51dfd91c878
parent4c3c605e5f53c91430efac821ce1b863cbb5bf06

Replace uses of Value.zero, Value.one, Value.negative_one

This is a bit nasty, mainly because Type.onePossibleValue is now errorable, which is a quite viral change.

16 files changed, 286 insertions(+), 223 deletions(-)

src/Air.zig+1-1
......@@ -1485,7 +1485,7 @@ pub fn refToIndexAllowNone(inst: Inst.Ref) ?Inst.Index {
14851485}
14861486
14871487/// Returns `null` if runtime-known.
1488pub fn value(air: Air, inst: Inst.Ref, mod: *const Module) ?Value {
1488pub fn value(air: Air, inst: Inst.Ref, mod: *Module) !?Value {
14891489 const ref_int = @enumToInt(inst);
14901490 if (ref_int < ref_start_index) {
14911491 const ip_index = @intToEnum(InternPool.Index, ref_int);
src/Module.zig+11-1
......@@ -5750,7 +5750,7 @@ pub fn analyzeFnBody(mod: *Module, func: *Fn, arena: Allocator) SemaError!Air {
57505750
57515751 const arg_val = if (!arg_tv.val.isGenericPoison())
57525752 arg_tv.val
5753 else if (arg_tv.ty.onePossibleValue(mod)) |opv|
5753 else if (try arg_tv.ty.onePossibleValue(mod)) |opv|
57545754 opv
57555755 else
57565756 break :t arg_tv.ty;
......@@ -6887,6 +6887,16 @@ pub fn singleConstPtrType(mod: *Module, child_type: Type) Allocator.Error!Type {
68876887}
68886888
68896889pub fn intValue(mod: *Module, ty: Type, x: anytype) Allocator.Error!Value {
6890 if (std.debug.runtime_safety) {
6891 // TODO: decide if this also works for ABI int types like enums
6892 const tag = ty.zigTypeTag(mod);
6893 assert(tag == .Int or tag == .ComptimeInt);
6894 }
6895 if (@TypeOf(x) == comptime_int) {
6896 if (comptime std.math.cast(u64, x)) |casted| return intValue_u64(mod, ty, casted);
6897 if (comptime std.math.cast(i64, x)) |casted| return intValue_i64(mod, ty, casted);
6898 @compileError("Out-of-range comptime_int passed to Module.intValue");
6899 }
68906900 if (std.math.cast(u64, x)) |casted| return intValue_u64(mod, ty, casted);
68916901 if (std.math.cast(i64, x)) |casted| return intValue_i64(mod, ty, casted);
68926902 var limbs_buffer: [4]usize = undefined;
src/Sema.zig+153-104
......@@ -3062,9 +3062,9 @@ fn zirEnumDecl(
30623062 }
30633063 } else if (any_values) {
30643064 const tag_val = if (last_tag_val) |val|
3065 try sema.intAdd(val, Value.one, enum_obj.tag_ty)
3065 try sema.intAdd(val, try mod.intValue(enum_obj.tag_ty, 1), enum_obj.tag_ty)
30663066 else
3067 Value.zero;
3067 try mod.intValue(enum_obj.tag_ty, 0);
30683068 last_tag_val = tag_val;
30693069 const copied_tag_val = try tag_val.copy(decl_arena_allocator);
30703070 const gop_val = enum_obj.values.getOrPutAssumeCapacityContext(copied_tag_val, .{
......@@ -4709,7 +4709,7 @@ fn zirValidateArrayInit(
47094709 // Determine whether the value stored to this pointer is comptime-known.
47104710
47114711 if (array_ty.isTuple()) {
4712 if (array_ty.structFieldValueComptime(mod, i)) |opv| {
4712 if (try array_ty.structFieldValueComptime(mod, i)) |opv| {
47134713 element_vals[i] = opv;
47144714 continue;
47154715 }
......@@ -8132,7 +8132,7 @@ fn zirErrorToInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
81328132 if (!op_ty.isAnyError()) {
81338133 const names = op_ty.errorSetNames();
81348134 switch (names.len) {
8135 0 => return sema.addConstant(Type.err_int, Value.zero),
8135 0 => return sema.addConstant(Type.err_int, try mod.intValue(Type.err_int, 0)),
81368136 1 => return sema.addIntUnsigned(Type.err_int, sema.mod.global_error_set.get(names[0]).?),
81378137 else => {},
81388138 }
......@@ -8167,7 +8167,7 @@ fn zirIntToError(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
81678167 try sema.requireRuntimeBlock(block, src, operand_src);
81688168 if (block.wantSafety()) {
81698169 const is_lt_len = try block.addUnOp(.cmp_lt_errors_len, operand);
8170 const zero_val = try sema.addConstant(Type.err_int, Value.zero);
8170 const zero_val = try sema.addConstant(Type.err_int, try mod.intValue(Type.err_int, 0));
81718171 const is_non_zero = try block.addBinOp(.cmp_neq, operand, zero_val);
81728172 const ok = try block.addBinOp(.bit_and, is_lt_len, is_non_zero);
81738173 try sema.addSafetyCheck(block, ok, .invalid_error_code);
......@@ -9656,7 +9656,7 @@ fn intCast(
96569656
96579657 if (wanted_bits == 0) {
96589658 const ok = if (is_vector) ok: {
9659 const zeros = try Value.Tag.repeated.create(sema.arena, Value.zero);
9659 const zeros = try Value.Tag.repeated.create(sema.arena, try mod.intValue(operand_scalar_ty, 0));
96609660 const zero_inst = try sema.addConstant(sema.typeOf(operand), zeros);
96619661 const is_in_range = try block.addCmpVector(operand, zero_inst, .eq);
96629662 const all_in_range = try block.addInst(.{
......@@ -9665,7 +9665,7 @@ fn intCast(
96659665 });
96669666 break :ok all_in_range;
96679667 } else ok: {
9668 const zero_inst = try sema.addConstant(sema.typeOf(operand), Value.zero);
9668 const zero_inst = try sema.addConstant(sema.typeOf(operand), try mod.intValue(operand_ty, 0));
96699669 const is_in_range = try block.addBinOp(.cmp_lte, operand, zero_inst);
96709670 break :ok is_in_range;
96719671 };
......@@ -9705,8 +9705,9 @@ fn intCast(
97059705 // If the destination type is signed, then we need to double its
97069706 // range to account for negative values.
97079707 const dest_range_val = if (wanted_info.signedness == .signed) range_val: {
9708 const range_minus_one = try dest_max_val.shl(Value.one, unsigned_operand_ty, sema.arena, sema.mod);
9709 break :range_val try sema.intAdd(range_minus_one, Value.one, unsigned_operand_ty);
9708 const one = try mod.intValue(unsigned_operand_ty, 1);
9709 const range_minus_one = try dest_max_val.shl(one, unsigned_operand_ty, sema.arena, sema.mod);
9710 break :range_val try sema.intAdd(range_minus_one, one, unsigned_operand_ty);
97109711 } else dest_max_val;
97119712 const dest_range = try sema.addConstant(unsigned_operand_ty, dest_range_val);
97129713
......@@ -9747,7 +9748,7 @@ fn intCast(
97479748 // no shrinkage, yes sign loss
97489749 // requirement: signed to unsigned >= 0
97499750 const ok = if (is_vector) ok: {
9750 const zero_val = try Value.Tag.repeated.create(sema.arena, Value.zero);
9751 const zero_val = try Value.Tag.repeated.create(sema.arena, try mod.intValue(operand_scalar_ty, 0));
97519752 const zero_inst = try sema.addConstant(operand_ty, zero_val);
97529753 const is_in_range = try block.addCmpVector(operand, zero_inst, .gte);
97539754 const all_in_range = try block.addInst(.{
......@@ -9759,7 +9760,7 @@ fn intCast(
97599760 });
97609761 break :ok all_in_range;
97619762 } else ok: {
9762 const zero_inst = try sema.addConstant(operand_ty, Value.zero);
9763 const zero_inst = try sema.addConstant(operand_ty, try mod.intValue(operand_ty, 0));
97639764 const is_in_range = try block.addBinOp(.cmp_gte, operand, zero_inst);
97649765 break :ok is_in_range;
97659766 };
......@@ -11250,7 +11251,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1125011251
1125111252 while (item.compareScalar(.lte, item_last, operand_ty, mod)) : ({
1125211253 // Previous validation has resolved any possible lazy values.
11253 item = try sema.intAddScalar(item, Value.one, operand_ty);
11254 item = try sema.intAddScalar(item, try mod.intValue(operand_ty, 1), operand_ty);
1125411255 }) {
1125511256 cases_len += 1;
1125611257
......@@ -11696,7 +11697,7 @@ const RangeSetUnhandledIterator = struct {
1169611697 fn next(it: *RangeSetUnhandledIterator) !?Value {
1169711698 while (it.range_i < it.ranges.len) : (it.range_i += 1) {
1169811699 if (!it.first) {
11699 it.cur = try it.sema.intAddScalar(it.cur, Value.one, it.ty);
11700 it.cur = try it.sema.intAddScalar(it.cur, try it.sema.mod.intValue(it.ty, 1), it.ty);
1170011701 }
1170111702 it.first = false;
1170211703 if (it.cur.compareScalar(.lt, it.ranges[it.range_i].first, it.ty, it.sema.mod)) {
......@@ -11705,7 +11706,7 @@ const RangeSetUnhandledIterator = struct {
1170511706 it.cur = it.ranges[it.range_i].last;
1170611707 }
1170711708 if (!it.first) {
11708 it.cur = try it.sema.intAddScalar(it.cur, Value.one, it.ty);
11709 it.cur = try it.sema.intAddScalar(it.cur, try it.sema.mod.intValue(it.ty, 1), it.ty);
1170911710 }
1171011711 it.first = false;
1171111712 if (it.cur.compareScalar(.lte, it.max, it.ty, it.sema.mod)) {
......@@ -12150,7 +12151,7 @@ fn zirEmbedFile(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
1215012151 // into the final binary, and never loads the data into memory.
1215112152 // - When a Decl is destroyed, it can free the `*Module.EmbedFile`.
1215212153 embed_file.owner_decl = try anon_decl.finish(
12153 try Type.array(anon_decl.arena(), embed_file.bytes.len, Value.zero, Type.u8, mod),
12154 try Type.array(anon_decl.arena(), embed_file.bytes.len, try mod.intValue(Type.u8, 0), Type.u8, mod),
1215412155 try Value.Tag.bytes.create(anon_decl.arena(), bytes_including_null),
1215512156 0, // default alignment
1215612157 );
......@@ -12235,14 +12236,14 @@ fn zirShl(
1223512236 var i: usize = 0;
1223612237 while (i < rhs_ty.vectorLen(mod)) : (i += 1) {
1223712238 const rhs_elem = try rhs_val.elemValue(sema.mod, i);
12238 if (rhs_elem.compareHetero(.lt, Value.zero, mod)) {
12239 if (rhs_elem.compareHetero(.lt, try mod.intValue(scalar_rhs_ty, 0), mod)) {
1223912240 return sema.fail(block, rhs_src, "shift by negative amount '{}' at index '{d}'", .{
1224012241 rhs_elem.fmtValue(scalar_ty, sema.mod),
1224112242 i,
1224212243 });
1224312244 }
1224412245 }
12245 } else if (rhs_val.compareHetero(.lt, Value.zero, mod)) {
12246 } else if (rhs_val.compareHetero(.lt, try mod.intValue(rhs_ty, 0), mod)) {
1224612247 return sema.fail(block, rhs_src, "shift by negative amount '{}'", .{
1224712248 rhs_val.fmtValue(scalar_ty, sema.mod),
1224812249 });
......@@ -12348,7 +12349,7 @@ fn zirShl(
1234812349 })
1234912350 else
1235012351 ov_bit;
12351 const zero_ov = try sema.addConstant(Type.u1, Value.zero);
12352 const zero_ov = try sema.addConstant(Type.u1, try mod.intValue(Type.u1, 0));
1235212353 const no_ov = try block.addBinOp(.cmp_eq, any_ov_bit, zero_ov);
1235312354
1235412355 try sema.addSafetyCheck(block, no_ov, .shl_overflow);
......@@ -12417,14 +12418,14 @@ fn zirShr(
1241712418 var i: usize = 0;
1241812419 while (i < rhs_ty.vectorLen(mod)) : (i += 1) {
1241912420 const rhs_elem = try rhs_val.elemValue(sema.mod, i);
12420 if (rhs_elem.compareHetero(.lt, Value.zero, mod)) {
12421 if (rhs_elem.compareHetero(.lt, try mod.intValue(rhs_ty.childType(mod), 0), mod)) {
1242112422 return sema.fail(block, rhs_src, "shift by negative amount '{}' at index '{d}'", .{
1242212423 rhs_elem.fmtValue(scalar_ty, sema.mod),
1242312424 i,
1242412425 });
1242512426 }
1242612427 }
12427 } else if (rhs_val.compareHetero(.lt, Value.zero, mod)) {
12428 } else if (rhs_val.compareHetero(.lt, try mod.intValue(rhs_ty, 0), mod)) {
1242812429 return sema.fail(block, rhs_src, "shift by negative amount '{}'", .{
1242912430 rhs_val.fmtValue(scalar_ty, sema.mod),
1243012431 });
......@@ -13156,9 +13157,9 @@ fn zirNegate(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
1315613157 }
1315713158
1315813159 const lhs = if (rhs_ty.zigTypeTag(mod) == .Vector)
13159 try sema.addConstant(rhs_ty, try Value.Tag.repeated.create(sema.arena, Value.zero))
13160 try sema.addConstant(rhs_ty, try Value.Tag.repeated.create(sema.arena, try mod.intValue(rhs_scalar_ty, 0)))
1316013161 else
13161 try sema.resolveInst(.zero);
13162 try sema.addConstant(rhs_ty, try mod.intValue(rhs_ty, 0));
1316213163
1316313164 return sema.analyzeArithmetic(block, .sub, lhs, rhs, src, lhs_src, rhs_src, true);
1316413165}
......@@ -13180,9 +13181,9 @@ fn zirNegateWrap(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
1318013181 }
1318113182
1318213183 const lhs = if (rhs_ty.zigTypeTag(mod) == .Vector)
13183 try sema.addConstant(rhs_ty, try Value.Tag.repeated.create(sema.arena, Value.zero))
13184 try sema.addConstant(rhs_ty, try Value.Tag.repeated.create(sema.arena, try mod.intValue(rhs_scalar_ty, 0)))
1318413185 else
13185 try sema.resolveInst(.zero);
13186 try sema.addConstant(rhs_ty, try mod.intValue(rhs_ty, 0));
1318613187
1318713188 return sema.analyzeArithmetic(block, .subwrap, lhs, rhs, src, lhs_src, rhs_src, true);
1318813189}
......@@ -13293,9 +13294,14 @@ fn zirDiv(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins
1329313294 if (maybe_lhs_val) |lhs_val| {
1329413295 if (!lhs_val.isUndef()) {
1329513296 if (try lhs_val.compareAllWithZeroAdvanced(.eq, sema)) {
13297 const scalar_zero = switch (scalar_tag) {
13298 .ComptimeFloat, .Float => Value.float_zero, // TODO migrate to internpool
13299 .ComptimeInt, .Int => try mod.intValue(resolved_type.scalarType(mod), 0),
13300 else => unreachable,
13301 };
1329613302 const zero_val = if (is_vector) b: {
13297 break :b try Value.Tag.repeated.create(sema.arena, Value.zero);
13298 } else Value.zero;
13303 break :b try Value.Tag.repeated.create(sema.arena, scalar_zero);
13304 } else scalar_zero;
1329913305 return sema.addConstant(resolved_type, zero_val);
1330013306 }
1330113307 }
......@@ -13318,7 +13324,7 @@ fn zirDiv(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins
1331813324 if (lhs_val.isUndef()) {
1331913325 if (lhs_scalar_ty.isSignedInt(mod) and rhs_scalar_ty.isSignedInt(mod)) {
1332013326 if (maybe_rhs_val) |rhs_val| {
13321 if (try sema.compareAll(rhs_val, .neq, Value.negative_one, resolved_type)) {
13327 if (try sema.compareAll(rhs_val, .neq, try mod.intValue(resolved_type, -1), resolved_type)) {
1332213328 return sema.addConstUndef(resolved_type);
1332313329 }
1332413330 }
......@@ -13427,9 +13433,14 @@ fn zirDivExact(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1342713433 return sema.failWithUseOfUndef(block, rhs_src);
1342813434 } else {
1342913435 if (try lhs_val.compareAllWithZeroAdvanced(.eq, sema)) {
13436 const scalar_zero = switch (scalar_tag) {
13437 .ComptimeFloat, .Float => Value.float_zero, // TODO migrate to internpool
13438 .ComptimeInt, .Int => try mod.intValue(resolved_type.scalarType(mod), 0),
13439 else => unreachable,
13440 };
1343013441 const zero_val = if (is_vector) b: {
13431 break :b try Value.Tag.repeated.create(sema.arena, Value.zero);
13432 } else Value.zero;
13442 break :b try Value.Tag.repeated.create(sema.arena, scalar_zero);
13443 } else scalar_zero;
1343313444 return sema.addConstant(resolved_type, zero_val);
1343413445 }
1343513446 }
......@@ -13507,8 +13518,13 @@ fn zirDivExact(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1350713518 } else ok: {
1350813519 const remainder = try block.addBinOp(.rem, casted_lhs, casted_rhs);
1350913520
13521 const scalar_zero = switch (scalar_tag) {
13522 .ComptimeFloat, .Float => Value.float_zero, // TODO migrate to internpool
13523 .ComptimeInt, .Int => try mod.intValue(resolved_type.scalarType(mod), 0),
13524 else => unreachable,
13525 };
1351013526 if (resolved_type.zigTypeTag(mod) == .Vector) {
13511 const zero_val = try Value.Tag.repeated.create(sema.arena, Value.zero);
13527 const zero_val = try Value.Tag.repeated.create(sema.arena, scalar_zero);
1351213528 const zero = try sema.addConstant(resolved_type, zero_val);
1351313529 const eql = try block.addCmpVector(remainder, zero, .eq);
1351413530 break :ok try block.addInst(.{
......@@ -13519,7 +13535,7 @@ fn zirDivExact(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1351913535 } },
1352013536 });
1352113537 } else {
13522 const zero = try sema.addConstant(resolved_type, Value.zero);
13538 const zero = try sema.addConstant(resolved_type, scalar_zero);
1352313539 const is_in_range = try block.addBinOp(.cmp_eq, remainder, zero);
1352413540 break :ok is_in_range;
1352513541 }
......@@ -13592,9 +13608,14 @@ fn zirDivFloor(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1359213608 if (maybe_lhs_val) |lhs_val| {
1359313609 if (!lhs_val.isUndef()) {
1359413610 if (try lhs_val.compareAllWithZeroAdvanced(.eq, sema)) {
13611 const scalar_zero = switch (scalar_tag) {
13612 .ComptimeFloat, .Float => Value.float_zero, // TODO migrate to internpool
13613 .ComptimeInt, .Int => try mod.intValue(resolved_type.scalarType(mod), 0),
13614 else => unreachable,
13615 };
1359513616 const zero_val = if (is_vector) b: {
13596 break :b try Value.Tag.repeated.create(sema.arena, Value.zero);
13597 } else Value.zero;
13617 break :b try Value.Tag.repeated.create(sema.arena, scalar_zero);
13618 } else scalar_zero;
1359813619 return sema.addConstant(resolved_type, zero_val);
1359913620 }
1360013621 }
......@@ -13612,7 +13633,7 @@ fn zirDivFloor(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1361213633 if (lhs_val.isUndef()) {
1361313634 if (lhs_scalar_ty.isSignedInt(mod) and rhs_scalar_ty.isSignedInt(mod)) {
1361413635 if (maybe_rhs_val) |rhs_val| {
13615 if (try sema.compareAll(rhs_val, .neq, Value.negative_one, resolved_type)) {
13636 if (try sema.compareAll(rhs_val, .neq, try mod.intValue(resolved_type, -1), resolved_type)) {
1361613637 return sema.addConstUndef(resolved_type);
1361713638 }
1361813639 }
......@@ -13708,9 +13729,14 @@ fn zirDivTrunc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1370813729 if (maybe_lhs_val) |lhs_val| {
1370913730 if (!lhs_val.isUndef()) {
1371013731 if (try lhs_val.compareAllWithZeroAdvanced(.eq, sema)) {
13732 const scalar_zero = switch (scalar_tag) {
13733 .ComptimeFloat, .Float => Value.float_zero, // TODO migrate to internpool
13734 .ComptimeInt, .Int => try mod.intValue(resolved_type.scalarType(mod), 0),
13735 else => unreachable,
13736 };
1371113737 const zero_val = if (is_vector) b: {
13712 break :b try Value.Tag.repeated.create(sema.arena, Value.zero);
13713 } else Value.zero;
13738 break :b try Value.Tag.repeated.create(sema.arena, scalar_zero);
13739 } else scalar_zero;
1371413740 return sema.addConstant(resolved_type, zero_val);
1371513741 }
1371613742 }
......@@ -13727,7 +13753,7 @@ fn zirDivTrunc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1372713753 if (lhs_val.isUndef()) {
1372813754 if (lhs_scalar_ty.isSignedInt(mod) and rhs_scalar_ty.isSignedInt(mod)) {
1372913755 if (maybe_rhs_val) |rhs_val| {
13730 if (try sema.compareAll(rhs_val, .neq, Value.negative_one, resolved_type)) {
13756 if (try sema.compareAll(rhs_val, .neq, try mod.intValue(resolved_type, -1), resolved_type)) {
1373113757 return sema.addConstUndef(resolved_type);
1373213758 }
1373313759 }
......@@ -13862,8 +13888,9 @@ fn addDivByZeroSafety(
1386213888 if (maybe_rhs_val != null) return;
1386313889
1386413890 const mod = sema.mod;
13891 const scalar_zero = if (is_int) try mod.intValue(resolved_type.scalarType(mod), 0) else Value.float_zero; // TODO migrate to internpool
1386513892 const ok = if (resolved_type.zigTypeTag(mod) == .Vector) ok: {
13866 const zero_val = try Value.Tag.repeated.create(sema.arena, Value.zero);
13893 const zero_val = try Value.Tag.repeated.create(sema.arena, scalar_zero);
1386713894 const zero = try sema.addConstant(resolved_type, zero_val);
1386813895 const ok = try block.addCmpVector(casted_rhs, zero, .neq);
1386913896 break :ok try block.addInst(.{
......@@ -13874,7 +13901,7 @@ fn addDivByZeroSafety(
1387413901 } },
1387513902 });
1387613903 } else ok: {
13877 const zero = try sema.addConstant(resolved_type, Value.zero);
13904 const zero = try sema.addConstant(resolved_type, scalar_zero);
1387813905 break :ok try block.addBinOp(if (is_int) .cmp_neq else .cmp_neq_optimized, casted_rhs, zero);
1387913906 };
1388013907 try sema.addSafetyCheck(block, ok, .divide_by_zero);
......@@ -13946,9 +13973,14 @@ fn zirModRem(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
1394613973 return sema.failWithUseOfUndef(block, lhs_src);
1394713974 }
1394813975 if (try lhs_val.compareAllWithZeroAdvanced(.eq, sema)) {
13976 const scalar_zero = switch (scalar_tag) {
13977 .ComptimeFloat, .Float => Value.float_zero, // TODO migrate to internpool
13978 .ComptimeInt, .Int => try mod.intValue(resolved_type.scalarType(mod), 0),
13979 else => unreachable,
13980 };
1394913981 const zero_val = if (is_vector) b: {
13950 break :b try Value.Tag.repeated.create(sema.arena, Value.zero);
13951 } else Value.zero;
13982 break :b try Value.Tag.repeated.create(sema.arena, scalar_zero);
13983 } else scalar_zero;
1395213984 return sema.addConstant(resolved_type, zero_val);
1395313985 }
1395413986 } else if (lhs_scalar_ty.isSignedInt(mod)) {
......@@ -14325,6 +14357,7 @@ fn zirOverflowArithmetic(
1432514357 wrapped: Value = Value.@"unreachable",
1432614358 overflow_bit: Value,
1432714359 } = result: {
14360 const zero = try mod.intValue(dest_ty.scalarType(mod), 0);
1432814361 switch (zir_tag) {
1432914362 .add_with_overflow => {
1433014363 // If either of the arguments is zero, `false` is returned and the other is stored
......@@ -14332,12 +14365,12 @@ fn zirOverflowArithmetic(
1433214365 // Otherwise, if either of the argument is undefined, undefined is returned.
1433314366 if (maybe_lhs_val) |lhs_val| {
1433414367 if (!lhs_val.isUndef() and (try lhs_val.compareAllWithZeroAdvanced(.eq, sema))) {
14335 break :result .{ .overflow_bit = try maybeRepeated(sema, dest_ty, Value.zero), .inst = rhs };
14368 break :result .{ .overflow_bit = try maybeRepeated(sema, dest_ty, zero), .inst = rhs };
1433614369 }
1433714370 }
1433814371 if (maybe_rhs_val) |rhs_val| {
1433914372 if (!rhs_val.isUndef() and (try rhs_val.compareAllWithZeroAdvanced(.eq, sema))) {
14340 break :result .{ .overflow_bit = try maybeRepeated(sema, dest_ty, Value.zero), .inst = lhs };
14373 break :result .{ .overflow_bit = try maybeRepeated(sema, dest_ty, zero), .inst = lhs };
1434114374 }
1434214375 }
1434314376 if (maybe_lhs_val) |lhs_val| {
......@@ -14358,7 +14391,7 @@ fn zirOverflowArithmetic(
1435814391 if (rhs_val.isUndef()) {
1435914392 break :result .{ .overflow_bit = Value.undef, .wrapped = Value.undef };
1436014393 } else if (try rhs_val.compareAllWithZeroAdvanced(.eq, sema)) {
14361 break :result .{ .overflow_bit = try maybeRepeated(sema, dest_ty, Value.zero), .inst = lhs };
14394 break :result .{ .overflow_bit = try maybeRepeated(sema, dest_ty, zero), .inst = lhs };
1436214395 } else if (maybe_lhs_val) |lhs_val| {
1436314396 if (lhs_val.isUndef()) {
1436414397 break :result .{ .overflow_bit = Value.undef, .wrapped = Value.undef };
......@@ -14373,12 +14406,13 @@ fn zirOverflowArithmetic(
1437314406 // If either of the arguments is zero, the result is zero and no overflow occured.
1437414407 // If either of the arguments is one, the result is the other and no overflow occured.
1437514408 // Otherwise, if either of the arguments is undefined, both results are undefined.
14409 const scalar_one = try mod.intValue(dest_ty.scalarType(mod), 1);
1437614410 if (maybe_lhs_val) |lhs_val| {
1437714411 if (!lhs_val.isUndef()) {
1437814412 if (try lhs_val.compareAllWithZeroAdvanced(.eq, sema)) {
14379 break :result .{ .overflow_bit = try maybeRepeated(sema, dest_ty, Value.zero), .inst = lhs };
14380 } else if (try sema.compareAll(lhs_val, .eq, try maybeRepeated(sema, dest_ty, Value.one), dest_ty)) {
14381 break :result .{ .overflow_bit = try maybeRepeated(sema, dest_ty, Value.zero), .inst = rhs };
14413 break :result .{ .overflow_bit = try maybeRepeated(sema, dest_ty, zero), .inst = lhs };
14414 } else if (try sema.compareAll(lhs_val, .eq, try maybeRepeated(sema, dest_ty, scalar_one), dest_ty)) {
14415 break :result .{ .overflow_bit = try maybeRepeated(sema, dest_ty, zero), .inst = rhs };
1438214416 }
1438314417 }
1438414418 }
......@@ -14386,9 +14420,9 @@ fn zirOverflowArithmetic(
1438614420 if (maybe_rhs_val) |rhs_val| {
1438714421 if (!rhs_val.isUndef()) {
1438814422 if (try rhs_val.compareAllWithZeroAdvanced(.eq, sema)) {
14389 break :result .{ .overflow_bit = try maybeRepeated(sema, dest_ty, Value.zero), .inst = rhs };
14390 } else if (try sema.compareAll(rhs_val, .eq, try maybeRepeated(sema, dest_ty, Value.one), dest_ty)) {
14391 break :result .{ .overflow_bit = try maybeRepeated(sema, dest_ty, Value.zero), .inst = lhs };
14423 break :result .{ .overflow_bit = try maybeRepeated(sema, dest_ty, zero), .inst = rhs };
14424 } else if (try sema.compareAll(rhs_val, .eq, try maybeRepeated(sema, dest_ty, scalar_one), dest_ty)) {
14425 break :result .{ .overflow_bit = try maybeRepeated(sema, dest_ty, zero), .inst = lhs };
1439214426 }
1439314427 }
1439414428 }
......@@ -14410,12 +14444,12 @@ fn zirOverflowArithmetic(
1441014444 // Oterhwise if either of the arguments is undefined, both results are undefined.
1441114445 if (maybe_lhs_val) |lhs_val| {
1441214446 if (!lhs_val.isUndef() and (try lhs_val.compareAllWithZeroAdvanced(.eq, sema))) {
14413 break :result .{ .overflow_bit = try maybeRepeated(sema, dest_ty, Value.zero), .inst = lhs };
14447 break :result .{ .overflow_bit = try maybeRepeated(sema, dest_ty, zero), .inst = lhs };
1441414448 }
1441514449 }
1441614450 if (maybe_rhs_val) |rhs_val| {
1441714451 if (!rhs_val.isUndef() and (try rhs_val.compareAllWithZeroAdvanced(.eq, sema))) {
14418 break :result .{ .overflow_bit = try maybeRepeated(sema, dest_ty, Value.zero), .inst = lhs };
14452 break :result .{ .overflow_bit = try maybeRepeated(sema, dest_ty, zero), .inst = lhs };
1441914453 }
1442014454 }
1442114455 if (maybe_lhs_val) |lhs_val| {
......@@ -14766,6 +14800,11 @@ fn analyzeArithmetic(
1476614800 // If either of the operands are inf, and the other operand is zero,
1476714801 // the result is nan.
1476814802 // If either of the operands are nan, the result is nan.
14803 const scalar_zero = switch (scalar_tag) {
14804 .ComptimeFloat, .Float => Value.float_zero, // TODO migrate to internpool
14805 .ComptimeInt, .Int => try mod.intValue(resolved_type.scalarType(mod), 0),
14806 else => unreachable,
14807 };
1476914808 if (maybe_lhs_val) |lhs_val| {
1477014809 if (!lhs_val.isUndef()) {
1477114810 if (lhs_val.isNan()) {
......@@ -14783,11 +14822,11 @@ fn analyzeArithmetic(
1478314822 break :lz;
1478414823 }
1478514824 const zero_val = if (is_vector) b: {
14786 break :b try Value.Tag.repeated.create(sema.arena, Value.zero);
14787 } else Value.zero;
14825 break :b try Value.Tag.repeated.create(sema.arena, scalar_zero);
14826 } else scalar_zero;
1478814827 return sema.addConstant(resolved_type, zero_val);
1478914828 }
14790 if (try sema.compareAll(lhs_val, .eq, Value.one, resolved_type)) {
14829 if (try sema.compareAll(lhs_val, .eq, try mod.intValue(resolved_type, 1), resolved_type)) {
1479114830 return casted_rhs;
1479214831 }
1479314832 }
......@@ -14813,11 +14852,11 @@ fn analyzeArithmetic(
1481314852 break :rz;
1481414853 }
1481514854 const zero_val = if (is_vector) b: {
14816 break :b try Value.Tag.repeated.create(sema.arena, Value.zero);
14817 } else Value.zero;
14855 break :b try Value.Tag.repeated.create(sema.arena, scalar_zero);
14856 } else scalar_zero;
1481814857 return sema.addConstant(resolved_type, zero_val);
1481914858 }
14820 if (try sema.compareAll(rhs_val, .eq, Value.one, resolved_type)) {
14859 if (try sema.compareAll(rhs_val, .eq, try mod.intValue(resolved_type, 1), resolved_type)) {
1482114860 return casted_lhs;
1482214861 }
1482314862 if (maybe_lhs_val) |lhs_val| {
......@@ -14849,15 +14888,20 @@ fn analyzeArithmetic(
1484914888 // If either of the operands are zero, result is zero.
1485014889 // If either of the operands are one, result is the other operand.
1485114890 // If either of the operands are undefined, result is undefined.
14891 const scalar_zero = switch (scalar_tag) {
14892 .ComptimeFloat, .Float => Value.float_zero, // TODO migrate to internpool
14893 .ComptimeInt, .Int => try mod.intValue(resolved_type.scalarType(mod), 0),
14894 else => unreachable,
14895 };
1485214896 if (maybe_lhs_val) |lhs_val| {
1485314897 if (!lhs_val.isUndef()) {
1485414898 if (try lhs_val.compareAllWithZeroAdvanced(.eq, sema)) {
1485514899 const zero_val = if (is_vector) b: {
14856 break :b try Value.Tag.repeated.create(sema.arena, Value.zero);
14857 } else Value.zero;
14900 break :b try Value.Tag.repeated.create(sema.arena, scalar_zero);
14901 } else scalar_zero;
1485814902 return sema.addConstant(resolved_type, zero_val);
1485914903 }
14860 if (try sema.compareAll(lhs_val, .eq, Value.one, resolved_type)) {
14904 if (try sema.compareAll(lhs_val, .eq, try mod.intValue(resolved_type, 1), resolved_type)) {
1486114905 return casted_rhs;
1486214906 }
1486314907 }
......@@ -14869,11 +14913,11 @@ fn analyzeArithmetic(
1486914913 }
1487014914 if (try rhs_val.compareAllWithZeroAdvanced(.eq, sema)) {
1487114915 const zero_val = if (is_vector) b: {
14872 break :b try Value.Tag.repeated.create(sema.arena, Value.zero);
14873 } else Value.zero;
14916 break :b try Value.Tag.repeated.create(sema.arena, scalar_zero);
14917 } else scalar_zero;
1487414918 return sema.addConstant(resolved_type, zero_val);
1487514919 }
14876 if (try sema.compareAll(rhs_val, .eq, Value.one, resolved_type)) {
14920 if (try sema.compareAll(rhs_val, .eq, try mod.intValue(resolved_type, 1), resolved_type)) {
1487714921 return casted_lhs;
1487814922 }
1487914923 if (maybe_lhs_val) |lhs_val| {
......@@ -14892,15 +14936,20 @@ fn analyzeArithmetic(
1489214936 // If either of the operands are zero, result is zero.
1489314937 // If either of the operands are one, result is the other operand.
1489414938 // If either of the operands are undefined, result is undefined.
14939 const scalar_zero = switch (scalar_tag) {
14940 .ComptimeFloat, .Float => Value.float_zero, // TODO migrate to internpool
14941 .ComptimeInt, .Int => try mod.intValue(resolved_type.scalarType(mod), 0),
14942 else => unreachable,
14943 };
1489514944 if (maybe_lhs_val) |lhs_val| {
1489614945 if (!lhs_val.isUndef()) {
1489714946 if (try lhs_val.compareAllWithZeroAdvanced(.eq, sema)) {
1489814947 const zero_val = if (is_vector) b: {
14899 break :b try Value.Tag.repeated.create(sema.arena, Value.zero);
14900 } else Value.zero;
14948 break :b try Value.Tag.repeated.create(sema.arena, scalar_zero);
14949 } else scalar_zero;
1490114950 return sema.addConstant(resolved_type, zero_val);
1490214951 }
14903 if (try sema.compareAll(lhs_val, .eq, Value.one, resolved_type)) {
14952 if (try sema.compareAll(lhs_val, .eq, try mod.intValue(resolved_type, 1), resolved_type)) {
1490414953 return casted_rhs;
1490514954 }
1490614955 }
......@@ -14911,11 +14960,11 @@ fn analyzeArithmetic(
1491114960 }
1491214961 if (try rhs_val.compareAllWithZeroAdvanced(.eq, sema)) {
1491314962 const zero_val = if (is_vector) b: {
14914 break :b try Value.Tag.repeated.create(sema.arena, Value.zero);
14915 } else Value.zero;
14963 break :b try Value.Tag.repeated.create(sema.arena, scalar_zero);
14964 } else scalar_zero;
1491614965 return sema.addConstant(resolved_type, zero_val);
1491714966 }
14918 if (try sema.compareAll(rhs_val, .eq, Value.one, resolved_type)) {
14967 if (try sema.compareAll(rhs_val, .eq, try mod.intValue(resolved_type, 1), resolved_type)) {
1491914968 return casted_lhs;
1492014969 }
1492114970 if (maybe_lhs_val) |lhs_val| {
......@@ -14968,7 +15017,7 @@ fn analyzeArithmetic(
1496815017 })
1496915018 else
1497015019 ov_bit;
14971 const zero_ov = try sema.addConstant(Type.u1, Value.zero);
15020 const zero_ov = try sema.addConstant(Type.u1, try mod.intValue(Type.u1, 0));
1497215021 const no_ov = try block.addBinOp(.cmp_eq, any_ov_bit, zero_ov);
1497315022
1497415023 try sema.addSafetyCheck(block, no_ov, .integer_overflow);
......@@ -15785,7 +15834,7 @@ fn zirBuiltinSrc(
1578515834 const name = std.mem.span(fn_owner_decl.name);
1578615835 const bytes = try anon_decl.arena().dupe(u8, name[0 .. name.len + 1]);
1578715836 const new_decl = try anon_decl.finish(
15788 try Type.array(anon_decl.arena(), bytes.len - 1, Value.zero, Type.u8, mod),
15837 try Type.array(anon_decl.arena(), bytes.len - 1, try mod.intValue(Type.u8, 0), Type.u8, mod),
1578915838 try Value.Tag.bytes.create(anon_decl.arena(), bytes),
1579015839 0, // default alignment
1579115840 );
......@@ -15798,7 +15847,7 @@ fn zirBuiltinSrc(
1579815847 // The compiler must not call realpath anywhere.
1579915848 const name = try fn_owner_decl.getFileScope().fullPathZ(anon_decl.arena());
1580015849 const new_decl = try anon_decl.finish(
15801 try Type.array(anon_decl.arena(), name.len, Value.zero, Type.u8, mod),
15850 try Type.array(anon_decl.arena(), name.len, try mod.intValue(Type.u8, 0), Type.u8, mod),
1580215851 try Value.Tag.bytes.create(anon_decl.arena(), name[0 .. name.len + 1]),
1580315852 0, // default alignment
1580415853 );
......@@ -16148,7 +16197,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1614816197 defer anon_decl.deinit();
1614916198 const bytes = try anon_decl.arena().dupeZ(u8, name);
1615016199 const new_decl = try anon_decl.finish(
16151 try Type.array(anon_decl.arena(), bytes.len, Value.zero, Type.u8, mod),
16200 try Type.array(anon_decl.arena(), bytes.len, try mod.intValue(Type.u8, 0), Type.u8, mod),
1615216201 try Value.Tag.bytes.create(anon_decl.arena(), bytes[0 .. bytes.len + 1]),
1615316202 0, // default alignment
1615416203 );
......@@ -16256,7 +16305,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1625616305 defer anon_decl.deinit();
1625716306 const bytes = try anon_decl.arena().dupeZ(u8, name);
1625816307 const new_decl = try anon_decl.finish(
16259 try Type.array(anon_decl.arena(), bytes.len, Value.zero, Type.u8, mod),
16308 try Type.array(anon_decl.arena(), bytes.len, try mod.intValue(Type.u8, 0), Type.u8, mod),
1626016309 try Value.Tag.bytes.create(anon_decl.arena(), bytes[0 .. bytes.len + 1]),
1626116310 0, // default alignment
1626216311 );
......@@ -16344,7 +16393,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1634416393 defer anon_decl.deinit();
1634516394 const bytes = try anon_decl.arena().dupeZ(u8, name);
1634616395 const new_decl = try anon_decl.finish(
16347 try Type.array(anon_decl.arena(), bytes.len, Value.zero, Type.u8, mod),
16396 try Type.array(anon_decl.arena(), bytes.len, try mod.intValue(Type.u8, 0), Type.u8, mod),
1634816397 try Value.Tag.bytes.create(anon_decl.arena(), bytes[0 .. bytes.len + 1]),
1634916398 0, // default alignment
1635016399 );
......@@ -16454,7 +16503,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1645416503 else
1645516504 try std.fmt.allocPrintZ(anon_decl.arena(), "{d}", .{i});
1645616505 const new_decl = try anon_decl.finish(
16457 try Type.array(anon_decl.arena(), bytes.len, Value.zero, Type.u8, mod),
16506 try Type.array(anon_decl.arena(), bytes.len, try mod.intValue(Type.u8, 0), Type.u8, mod),
1645816507 try Value.Tag.bytes.create(anon_decl.arena(), bytes[0 .. bytes.len + 1]),
1645916508 0, // default alignment
1646016509 );
......@@ -16496,7 +16545,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1649616545 defer anon_decl.deinit();
1649716546 const bytes = try anon_decl.arena().dupeZ(u8, name);
1649816547 const new_decl = try anon_decl.finish(
16499 try Type.array(anon_decl.arena(), bytes.len, Value.zero, Type.u8, mod),
16548 try Type.array(anon_decl.arena(), bytes.len, try mod.intValue(Type.u8, 0), Type.u8, mod),
1650016549 try Value.Tag.bytes.create(anon_decl.arena(), bytes[0 .. bytes.len + 1]),
1650116550 0, // default alignment
1650216551 );
......@@ -16692,7 +16741,7 @@ fn typeInfoNamespaceDecls(
1669216741 defer anon_decl.deinit();
1669316742 const bytes = try anon_decl.arena().dupeZ(u8, mem.sliceTo(decl.name, 0));
1669416743 const new_decl = try anon_decl.finish(
16695 try Type.array(anon_decl.arena(), bytes.len, Value.zero, Type.u8, mod),
16744 try Type.array(anon_decl.arena(), bytes.len, try mod.intValue(Type.u8, 0), Type.u8, mod),
1669616745 try Value.Tag.bytes.create(anon_decl.arena(), bytes[0 .. bytes.len + 1]),
1669716746 0, // default alignment
1669816747 );
......@@ -17884,7 +17933,7 @@ fn zirStructInit(
1788417933 }
1788517934 found_fields[field_index] = item.data.field_type;
1788617935 field_inits[field_index] = try sema.resolveInst(item.data.init);
17887 if (!is_packed) if (resolved_ty.structFieldValueComptime(mod, field_index)) |default_value| {
17936 if (!is_packed) if (try resolved_ty.structFieldValueComptime(mod, field_index)) |default_value| {
1788817937 const init_val = (try sema.resolveMaybeUndefVal(field_inits[field_index])) orelse {
1788917938 return sema.failWithNeededComptime(block, field_src, "value stored in comptime field must be comptime-known");
1789017939 };
......@@ -18544,8 +18593,8 @@ fn zirBoolToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
1854418593 const operand = try sema.resolveInst(inst_data.operand);
1854518594 if (try sema.resolveMaybeUndefVal(operand)) |val| {
1854618595 if (val.isUndef()) return sema.addConstUndef(Type.u1);
18547 if (val.toBool(mod)) return sema.addConstant(Type.u1, Value.one);
18548 return sema.addConstant(Type.u1, Value.zero);
18596 if (val.toBool(mod)) return sema.addConstant(Type.u1, try mod.intValue(Type.u1, 1));
18597 return sema.addConstant(Type.u1, try mod.intValue(Type.u1, 0));
1854918598 }
1855018599 return block.addUnOp(.bool_to_int, operand);
1855118600}
......@@ -19761,7 +19810,7 @@ fn zirTypeName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1976119810 const bytes = try ty.nameAllocArena(anon_decl.arena(), mod);
1976219811
1976319812 const new_decl = try anon_decl.finish(
19764 try Type.array(anon_decl.arena(), bytes.len, Value.zero, Type.u8, mod),
19813 try Type.array(anon_decl.arena(), bytes.len, try mod.intValue(Type.u8, 0), Type.u8, mod),
1976519814 try Value.Tag.bytes.create(anon_decl.arena(), bytes[0 .. bytes.len + 1]),
1976619815 0, // default alignment
1976719816 );
......@@ -19804,17 +19853,17 @@ fn zirFloatToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
1980419853 try sema.requireRuntimeBlock(block, inst_data.src(), operand_src);
1980519854 if (dest_ty.intInfo(mod).bits == 0) {
1980619855 if (block.wantSafety()) {
19807 const ok = try block.addBinOp(if (block.float_mode == .Optimized) .cmp_eq_optimized else .cmp_eq, operand, try sema.addConstant(operand_ty, Value.zero));
19856 const ok = try block.addBinOp(if (block.float_mode == .Optimized) .cmp_eq_optimized else .cmp_eq, operand, try sema.addConstant(operand_ty, try mod.intValue(operand_ty, 0)));
1980819857 try sema.addSafetyCheck(block, ok, .integer_part_out_of_bounds);
1980919858 }
19810 return sema.addConstant(dest_ty, Value.zero);
19859 return sema.addConstant(dest_ty, try mod.intValue(dest_ty, 0));
1981119860 }
1981219861 const result = try block.addTyOp(if (block.float_mode == .Optimized) .float_to_int_optimized else .float_to_int, dest_ty, operand);
1981319862 if (block.wantSafety()) {
1981419863 const back = try block.addTyOp(.int_to_float, operand_ty, result);
1981519864 const diff = try block.addBinOp(.sub, operand, back);
19816 const ok_pos = try block.addBinOp(if (block.float_mode == .Optimized) .cmp_lt_optimized else .cmp_lt, diff, try sema.addConstant(operand_ty, Value.one));
19817 const ok_neg = try block.addBinOp(if (block.float_mode == .Optimized) .cmp_gt_optimized else .cmp_gt, diff, try sema.addConstant(operand_ty, Value.negative_one));
19865 const ok_pos = try block.addBinOp(if (block.float_mode == .Optimized) .cmp_lt_optimized else .cmp_lt, diff, try sema.addConstant(operand_ty, try mod.intValue(operand_ty, 1)));
19866 const ok_neg = try block.addBinOp(if (block.float_mode == .Optimized) .cmp_gt_optimized else .cmp_gt, diff, try sema.addConstant(operand_ty, try mod.intValue(operand_ty, -1)));
1981819867 const ok = try block.addBinOp(.bool_and, ok_pos, ok_neg);
1981919868 try sema.addSafetyCheck(block, ok, .integer_part_out_of_bounds);
1982019869 }
......@@ -21398,7 +21447,7 @@ fn analyzeShuffle(
2139821447 expand_mask_values[i] = try mod.intValue(Type.comptime_int, i);
2139921448 }
2140021449 while (i < max_len) : (i += 1) {
21401 expand_mask_values[i] = Value.negative_one;
21450 expand_mask_values[i] = try mod.intValue(Type.comptime_int, -1);
2140221451 }
2140321452 const expand_mask = try Value.Tag.aggregate.create(sema.arena, expand_mask_values);
2140421453
......@@ -24504,7 +24553,7 @@ fn finishFieldCallBind(
2450424553
2450524554 const container_ty = ptr_ty.childType(mod);
2450624555 if (container_ty.zigTypeTag(mod) == .Struct) {
24507 if (container_ty.structFieldValueComptime(mod, field_index)) |default_val| {
24556 if (try container_ty.structFieldValueComptime(mod, field_index)) |default_val| {
2450824557 return .{ .direct = try sema.addConstant(field_ty, default_val) };
2450924558 }
2451024559 }
......@@ -24815,7 +24864,7 @@ fn tupleFieldValByIndex(
2481524864 const mod = sema.mod;
2481624865 const field_ty = tuple_ty.structFieldType(field_index);
2481724866
24818 if (tuple_ty.structFieldValueComptime(mod, field_index)) |default_value| {
24867 if (try tuple_ty.structFieldValueComptime(mod, field_index)) |default_value| {
2481924868 return sema.addConstant(field_ty, default_value);
2482024869 }
2482124870
......@@ -24828,7 +24877,7 @@ fn tupleFieldValByIndex(
2482824877 return sema.addConstant(field_ty, field_values[field_index]);
2482924878 }
2483024879
24831 if (tuple_ty.structFieldValueComptime(mod, field_index)) |default_val| {
24880 if (try tuple_ty.structFieldValueComptime(mod, field_index)) |default_val| {
2483224881 return sema.addConstant(field_ty, default_val);
2483324882 }
2483424883
......@@ -25205,7 +25254,7 @@ fn tupleFieldPtr(
2520525254 .@"addrspace" = tuple_ptr_ty.ptrAddressSpace(mod),
2520625255 });
2520725256
25208 if (tuple_ty.structFieldValueComptime(mod, field_index)) |default_val| {
25257 if (try tuple_ty.structFieldValueComptime(mod, field_index)) |default_val| {
2520925258 const val = try Value.Tag.comptime_field_ptr.create(sema.arena, .{
2521025259 .field_ty = field_ty,
2521125260 .field_val = default_val,
......@@ -25256,13 +25305,13 @@ fn tupleField(
2525625305
2525725306 const field_ty = tuple_ty.structFieldType(field_index);
2525825307
25259 if (tuple_ty.structFieldValueComptime(mod, field_index)) |default_value| {
25308 if (try tuple_ty.structFieldValueComptime(mod, field_index)) |default_value| {
2526025309 return sema.addConstant(field_ty, default_value); // comptime field
2526125310 }
2526225311
2526325312 if (try sema.resolveMaybeUndefVal(tuple)) |tuple_val| {
2526425313 if (tuple_val.isUndef()) return sema.addConstUndef(field_ty);
25265 return sema.addConstant(field_ty, tuple_val.fieldValue(tuple_ty, mod, field_index));
25314 return sema.addConstant(field_ty, try tuple_val.fieldValue(tuple_ty, mod, field_index));
2526625315 }
2526725316
2526825317 try sema.validateRuntimeElemAccess(block, field_index_src, field_ty, tuple_ty, tuple_src);
......@@ -25812,7 +25861,7 @@ fn coerceExtra(
2581225861 if (inst_info.size == .Slice) {
2581325862 assert(dest_info.sentinel == null);
2581425863 if (inst_info.sentinel == null or
25815 !inst_info.sentinel.?.eql(Value.zero, dest_info.pointee_type, sema.mod))
25864 !inst_info.sentinel.?.eql(try mod.intValue(dest_info.pointee_type, 0), dest_info.pointee_type, sema.mod))
2581625865 break :p;
2581725866
2581825867 const slice_ptr = try sema.analyzeSlicePtr(block, inst_src, inst, inst_ty);
......@@ -25879,7 +25928,7 @@ fn coerceExtra(
2587925928 try mod.intValue(Type.usize, dest_info.@"align")
2588025929 else
2588125930 try dest_info.pointee_type.lazyAbiAlignment(mod, sema.arena),
25882 .len = Value.zero,
25931 .len = try mod.intValue(Type.usize, 0),
2588325932 });
2588425933 return sema.addConstant(dest_ty, slice_val);
2588525934 }
......@@ -28234,7 +28283,7 @@ fn beginComptimePtrLoad(
2823428283 const field_ty = field_ptr.container_ty.structFieldType(field_index);
2823528284 deref.pointee = TypedValue{
2823628285 .ty = field_ty,
28237 .val = tv.val.fieldValue(tv.ty, mod, field_index),
28286 .val = try tv.val.fieldValue(tv.ty, mod, field_index),
2823828287 };
2823928288 }
2824028289 break :blk deref;
......@@ -32532,9 +32581,9 @@ fn semaUnionFields(mod: *Module, union_obj: *Module.Union) CompileError!void {
3253232581 break :blk try val.copy(decl_arena_allocator);
3253332582 } else blk: {
3253432583 const val = if (last_tag_val) |val|
32535 try sema.intAdd(val, Value.one, int_tag_ty)
32584 try sema.intAdd(val, try mod.intValue(int_tag_ty, 1), int_tag_ty)
3253632585 else
32537 Value.zero;
32586 try mod.intValue(int_tag_ty, 0);
3253832587 last_tag_val = val;
3253932588
3254032589 break :blk try val.copy(decl_arena_allocator);
......@@ -32903,7 +32952,7 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {
3290332952 if (ty.ip_index != .none) switch (mod.intern_pool.indexToKey(ty.ip_index)) {
3290432953 .int_type => |int_type| {
3290532954 if (int_type.bits == 0) {
32906 return Value.zero;
32955 return try mod.intValue(ty, 0);
3290732956 } else {
3290832957 return null;
3290932958 }
......@@ -33049,7 +33098,7 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {
3304933098 }
3305033099 if (enum_obj.fields.count() == 1) {
3305133100 if (enum_obj.values.count() == 0) {
33052 return Value.zero; // auto-numbered
33101 return try mod.intValue(ty, 0); // auto-numbered
3305333102 } else {
3305433103 return enum_obj.values.keys()[0];
3305533104 }
......@@ -33066,7 +33115,7 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {
3306633115 switch (enum_obj.fields.count()) {
3306733116 0 => return Value.@"unreachable",
3306833117 1 => if (enum_obj.values.count() == 0) {
33069 return Value.zero; // auto-numbered
33118 return try mod.intValue(ty, 0); // auto-numbered
3307033119 } else {
3307133120 return enum_obj.values.keys()[0];
3307233121 },
......@@ -33078,14 +33127,14 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {
3307833127 const enum_simple = resolved_ty.castTag(.enum_simple).?.data;
3307933128 switch (enum_simple.fields.count()) {
3308033129 0 => return Value.@"unreachable",
33081 1 => return Value.zero,
33130 1 => return try mod.intValue(ty, 0),
3308233131 else => return null,
3308333132 }
3308433133 },
3308533134 .enum_nonexhaustive => {
3308633135 const tag_ty = ty.castTag(.enum_nonexhaustive).?.data.tag_ty;
3308733136 if (tag_ty.zigTypeTag(mod) != .ComptimeInt and !(try sema.typeHasRuntimeBits(tag_ty))) {
33088 return Value.zero;
33137 return try mod.intValue(ty, 0);
3308933138 } else {
3309033139 return null;
3309133140 }
src/TypedValue.zig+8-5
......@@ -61,7 +61,10 @@ pub fn format(
6161) !void {
6262 _ = options;
6363 comptime std.debug.assert(fmt.len == 0);
64 return ctx.tv.print(writer, 3, ctx.mod);
64 return ctx.tv.print(writer, 3, ctx.mod) catch |err| switch (err) {
65 error.OutOfMemory => @panic("OOM"), // We're not allowed to return this from a format function
66 else => |e| return e,
67 };
6568}
6669
6770/// Prints the Value according to the Type, not according to the Value Tag.
......@@ -70,7 +73,7 @@ pub fn print(
7073 writer: anytype,
7174 level: u8,
7275 mod: *Module,
73) @TypeOf(writer).Error!void {
76) (@TypeOf(writer).Error || Allocator.Error)!void {
7477 var val = tv.val;
7578 var ty = tv.ty;
7679 if (val.isVariable(mod))
......@@ -95,7 +98,7 @@ pub fn print(
9598 }
9699 try print(.{
97100 .ty = ty.structFieldType(i),
98 .val = val.fieldValue(ty, mod, i),
101 .val = try val.fieldValue(ty, mod, i),
99102 }, writer, level - 1, mod);
100103 }
101104 if (ty.structFieldCount() > max_aggregate_items) {
......@@ -112,7 +115,7 @@ pub fn print(
112115
113116 var i: u32 = 0;
114117 while (i < max_len) : (i += 1) {
115 const elem = val.fieldValue(ty, mod, i);
118 const elem = try val.fieldValue(ty, mod, i);
116119 if (elem.isUndef()) break :str;
117120 buf[i] = std.math.cast(u8, elem.toUnsignedInt(mod)) orelse break :str;
118121 }
......@@ -129,7 +132,7 @@ pub fn print(
129132 if (i != 0) try writer.writeAll(", ");
130133 try print(.{
131134 .ty = elem_ty,
132 .val = val.fieldValue(ty, mod, i),
135 .val = try val.fieldValue(ty, mod, i),
133136 }, writer, level - 1, mod);
134137 }
135138 if (len > max_aggregate_items) {
src/arch/aarch64/CodeGen.zig+2-2
......@@ -4311,7 +4311,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
43114311
43124312 // Due to incremental compilation, how function calls are generated depends
43134313 // on linking.
4314 if (self.air.value(callee, mod)) |func_value| {
4314 if (try self.air.value(callee, mod)) |func_value| {
43154315 if (func_value.castTag(.function)) |func_payload| {
43164316 const func = func_payload.data;
43174317
......@@ -6154,7 +6154,7 @@ fn resolveInst(self: *Self, inst: Air.Inst.Ref) InnerError!MCValue {
61546154
61556155 const inst_index = Air.refToIndex(inst) orelse return self.genTypedValue(.{
61566156 .ty = inst_ty,
6157 .val = self.air.value(inst, mod).?,
6157 .val = (try self.air.value(inst, mod)).?,
61586158 });
61596159
61606160 switch (self.air.instructions.items(.tag)[inst_index]) {
src/arch/arm/CodeGen.zig+2-2
......@@ -4291,7 +4291,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
42914291
42924292 // Due to incremental compilation, how function calls are generated depends
42934293 // on linking.
4294 if (self.air.value(callee, mod)) |func_value| {
4294 if (try self.air.value(callee, mod)) |func_value| {
42954295 if (func_value.castTag(.function)) |func_payload| {
42964296 const func = func_payload.data;
42974297
......@@ -6101,7 +6101,7 @@ fn resolveInst(self: *Self, inst: Air.Inst.Ref) InnerError!MCValue {
61016101
61026102 const inst_index = Air.refToIndex(inst) orelse return self.genTypedValue(.{
61036103 .ty = inst_ty,
6104 .val = self.air.value(inst, mod).?,
6104 .val = (try self.air.value(inst, mod)).?,
61056105 });
61066106
61076107 switch (self.air.instructions.items(.tag)[inst_index]) {
src/arch/riscv64/CodeGen.zig+2-2
......@@ -1743,7 +1743,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
17431743 }
17441744 }
17451745
1746 if (self.air.value(callee, mod)) |func_value| {
1746 if (try self.air.value(callee, mod)) |func_value| {
17471747 if (func_value.castTag(.function)) |func_payload| {
17481748 const func = func_payload.data;
17491749 const atom_index = try elf_file.getOrCreateAtomForDecl(func.owner_decl);
......@@ -2551,7 +2551,7 @@ fn resolveInst(self: *Self, inst: Air.Inst.Ref) InnerError!MCValue {
25512551
25522552 const inst_index = Air.refToIndex(inst) orelse return self.genTypedValue(.{
25532553 .ty = inst_ty,
2554 .val = self.air.value(inst, mod).?,
2554 .val = (try self.air.value(inst, mod)).?,
25552555 });
25562556
25572557 switch (self.air.instructions.items(.tag)[inst_index]) {
src/arch/sparc64/CodeGen.zig+2-2
......@@ -1343,7 +1343,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
13431343
13441344 // Due to incremental compilation, how function calls are generated depends
13451345 // on linking.
1346 if (self.air.value(callee, mod)) |func_value| {
1346 if (try self.air.value(callee, mod)) |func_value| {
13471347 if (self.bin_file.tag == link.File.Elf.base_tag) {
13481348 if (func_value.castTag(.function)) |func_payload| {
13491349 const func = func_payload.data;
......@@ -4575,7 +4575,7 @@ fn resolveInst(self: *Self, ref: Air.Inst.Ref) InnerError!MCValue {
45754575
45764576 return self.genTypedValue(.{
45774577 .ty = ty,
4578 .val = self.air.value(ref, mod).?,
4578 .val = (try self.air.value(ref, mod)).?,
45794579 });
45804580}
45814581
src/arch/wasm/CodeGen.zig+5-5
......@@ -789,7 +789,7 @@ fn resolveInst(func: *CodeGen, ref: Air.Inst.Ref) InnerError!WValue {
789789 assert(!gop.found_existing);
790790
791791 const mod = func.bin_file.base.options.module.?;
792 const val = func.air.value(ref, mod).?;
792 const val = (try func.air.value(ref, mod)).?;
793793 const ty = func.typeOf(ref);
794794 if (!ty.hasRuntimeBitsIgnoreComptime(mod) and !ty.isInt(mod) and !ty.isError(mod)) {
795795 gop.value_ptr.* = WValue{ .none = {} };
......@@ -2195,7 +2195,7 @@ fn airCall(func: *CodeGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
21952195 const first_param_sret = firstParamSRet(fn_info.cc, fn_info.return_type, mod);
21962196
21972197 const callee: ?Decl.Index = blk: {
2198 const func_val = func.air.value(pl_op.operand, mod) orelse break :blk null;
2198 const func_val = (try func.air.value(pl_op.operand, mod)) orelse break :blk null;
21992199
22002200 if (func_val.castTag(.function)) |function| {
22012201 _ = try func.bin_file.getOrCreateAtomForDecl(function.data.owner_decl);
......@@ -3138,7 +3138,7 @@ fn lowerConstant(func: *CodeGen, arg_val: Value, ty: Type) InnerError!WValue {
31383138 if (!payload_type.hasRuntimeBitsIgnoreComptime(mod)) {
31393139 // We use the error type directly as the type.
31403140 const is_pl = val.errorUnionIsPayload();
3141 const err_val = if (!is_pl) val else Value.zero;
3141 const err_val = if (!is_pl) val else try mod.intValue(error_type, 0);
31423142 return func.lowerConstant(err_val, error_type);
31433143 }
31443144 return func.fail("Wasm TODO: lowerConstant error union with non-zero-bit payload type", .{});
......@@ -3792,7 +3792,7 @@ fn airSwitchBr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
37923792 errdefer func.gpa.free(values);
37933793
37943794 for (items, 0..) |ref, i| {
3795 const item_val = func.air.value(ref, mod).?;
3795 const item_val = (try func.air.value(ref, mod)).?;
37963796 const int_val = func.valueAsI32(item_val, target_ty);
37973797 if (lowest_maybe == null or int_val < lowest_maybe.?) {
37983798 lowest_maybe = int_val;
......@@ -5048,7 +5048,7 @@ fn airAggregateInit(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
50485048 const result = try func.allocStack(result_ty);
50495049 const offset = try func.buildPointerOffset(result, 0, .new); // pointer to offset
50505050 for (elements, 0..) |elem, elem_index| {
5051 if (result_ty.structFieldValueComptime(mod, elem_index) != null) continue;
5051 if ((try result_ty.structFieldValueComptime(mod, elem_index)) != null) continue;
50525052
50535053 const elem_ty = result_ty.structFieldType(elem_index);
50545054 const elem_size = @intCast(u32, elem_ty.abiSize(mod));
src/arch/x86_64/CodeGen.zig+6-6
......@@ -2768,7 +2768,7 @@ fn airTrunc(self: *Self, inst: Air.Inst.Index) !void {
27682768
27692769 const full_ty = try mod.vectorType(.{
27702770 .len = @intCast(u32, @divExact(@as(u64, if (src_abi_size > 16) 256 else 128), src_info.bits)),
2771 .child = src_ty.childType(mod).ip_index,
2771 .child = elem_ty.ip_index,
27722772 });
27732773 const full_abi_size = @intCast(u32, full_ty.abiSize(mod));
27742774
......@@ -8107,7 +8107,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
81078107
81088108 // Due to incremental compilation, how function calls are generated depends
81098109 // on linking.
8110 if (self.air.value(callee, mod)) |func_value| {
8110 if (try self.air.value(callee, mod)) |func_value| {
81118111 if (if (func_value.castTag(.function)) |func_payload|
81128112 func_payload.data.owner_decl
81138113 else if (func_value.castTag(.decl_ref)) |decl_ref_payload|
......@@ -11265,7 +11265,7 @@ fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void {
1126511265 .{ .immediate = result_ty.abiSize(mod) },
1126611266 );
1126711267 for (elements, 0..) |elem, elem_i| {
11268 if (result_ty.structFieldValueComptime(mod, elem_i) != null) continue;
11268 if ((try result_ty.structFieldValueComptime(mod, elem_i)) != null) continue;
1126911269
1127011270 const elem_ty = result_ty.structFieldType(elem_i);
1127111271 const elem_bit_size = @intCast(u32, elem_ty.bitSize(mod));
......@@ -11337,7 +11337,7 @@ fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void {
1133711337 }
1133811338 }
1133911339 } else for (elements, 0..) |elem, elem_i| {
11340 if (result_ty.structFieldValueComptime(mod, elem_i) != null) continue;
11340 if ((try result_ty.structFieldValueComptime(mod, elem_i)) != null) continue;
1134111341
1134211342 const elem_ty = result_ty.structFieldType(elem_i);
1134311343 const elem_off = @intCast(i32, result_ty.structFieldOffset(elem_i, mod));
......@@ -11601,7 +11601,7 @@ fn resolveInst(self: *Self, ref: Air.Inst.Ref) InnerError!MCValue {
1160111601 const gop = try self.const_tracking.getOrPut(self.gpa, inst);
1160211602 if (!gop.found_existing) gop.value_ptr.* = InstTracking.init(try self.genTypedValue(.{
1160311603 .ty = ty,
11604 .val = self.air.value(ref, mod).?,
11604 .val = (try self.air.value(ref, mod)).?,
1160511605 }));
1160611606 break :tracking gop.value_ptr;
1160711607 },
......@@ -11614,7 +11614,7 @@ fn resolveInst(self: *Self, ref: Air.Inst.Ref) InnerError!MCValue {
1161411614 }
1161511615 }
1161611616
11617 return self.genTypedValue(.{ .ty = ty, .val = self.air.value(ref, mod).? });
11617 return self.genTypedValue(.{ .ty = ty, .val = (try self.air.value(ref, mod)).? });
1161811618}
1161911619
1162011620fn getResolvedInstValue(self: *Self, inst: Air.Inst.Index) *InstTracking {
src/codegen.zig+4-4
......@@ -675,7 +675,7 @@ pub fn generateSymbol(
675675 const is_payload = typed_value.val.errorUnionIsPayload();
676676
677677 if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) {
678 const err_val = if (is_payload) Value.zero else typed_value.val;
678 const err_val = if (is_payload) try mod.intValue(error_ty, 0) else typed_value.val;
679679 return generateSymbol(bin_file, src_loc, .{
680680 .ty = error_ty,
681681 .val = err_val,
......@@ -690,7 +690,7 @@ pub fn generateSymbol(
690690 if (error_align > payload_align) {
691691 switch (try generateSymbol(bin_file, src_loc, .{
692692 .ty = error_ty,
693 .val = if (is_payload) Value.zero else typed_value.val,
693 .val = if (is_payload) try mod.intValue(error_ty, 0) else typed_value.val,
694694 }, code, debug_output, reloc_info)) {
695695 .ok => {},
696696 .fail => |em| return Result{ .fail = em },
......@@ -722,7 +722,7 @@ pub fn generateSymbol(
722722 const begin = code.items.len;
723723 switch (try generateSymbol(bin_file, src_loc, .{
724724 .ty = error_ty,
725 .val = if (is_payload) Value.zero else typed_value.val,
725 .val = if (is_payload) try mod.intValue(error_ty, 0) else typed_value.val,
726726 }, code, debug_output, reloc_info)) {
727727 .ok => {},
728728 .fail => |em| return Result{ .fail = em },
......@@ -1280,7 +1280,7 @@ pub fn genTypedValue(
12801280
12811281 if (!payload_type.hasRuntimeBitsIgnoreComptime(mod)) {
12821282 // We use the error type directly as the type.
1283 const err_val = if (!is_pl) typed_value.val else Value.zero;
1283 const err_val = if (!is_pl) typed_value.val else try mod.intValue(error_type, 0);
12841284 return genTypedValue(bin_file, src_loc, .{
12851285 .ty = error_type,
12861286 .val = err_val,
src/codegen/c.zig+32-32
......@@ -287,7 +287,7 @@ pub const Function = struct {
287287 if (gop.found_existing) return gop.value_ptr.*;
288288
289289 const mod = f.object.dg.module;
290 const val = f.air.value(ref, mod).?;
290 const val = (try f.air.value(ref, mod)).?;
291291 const ty = f.typeOf(ref);
292292
293293 const result: CValue = if (lowersToArray(ty, mod)) result: {
......@@ -356,7 +356,7 @@ pub const Function = struct {
356356 .constant => |inst| {
357357 const mod = f.object.dg.module;
358358 const ty = f.typeOf(inst);
359 const val = f.air.value(inst, mod).?;
359 const val = (try f.air.value(inst, mod)).?;
360360 return f.object.dg.renderValue(w, ty, val, location);
361361 },
362362 .undef => |ty| return f.object.dg.renderValue(w, ty, Value.undef, location),
......@@ -369,7 +369,7 @@ pub const Function = struct {
369369 .constant => |inst| {
370370 const mod = f.object.dg.module;
371371 const ty = f.typeOf(inst);
372 const val = f.air.value(inst, mod).?;
372 const val = (try f.air.value(inst, mod)).?;
373373 try w.writeAll("(*");
374374 try f.object.dg.renderValue(w, ty, val, .Other);
375375 return w.writeByte(')');
......@@ -383,7 +383,7 @@ pub const Function = struct {
383383 .constant => |inst| {
384384 const mod = f.object.dg.module;
385385 const ty = f.typeOf(inst);
386 const val = f.air.value(inst, mod).?;
386 const val = (try f.air.value(inst, mod)).?;
387387 try f.object.dg.renderValue(w, ty, val, .Other);
388388 try w.writeByte('.');
389389 return f.writeCValue(w, member, .Other);
......@@ -397,7 +397,7 @@ pub const Function = struct {
397397 .constant => |inst| {
398398 const mod = f.object.dg.module;
399399 const ty = f.typeOf(inst);
400 const val = f.air.value(inst, mod).?;
400 const val = (try f.air.value(inst, mod)).?;
401401 try w.writeByte('(');
402402 try f.object.dg.renderValue(w, ty, val, .Other);
403403 try w.writeAll(")->");
......@@ -690,7 +690,7 @@ pub const DeclGen = struct {
690690 location,
691691 );
692692 try writer.print(") + {})", .{
693 try dg.fmtIntLiteral(Type.usize, Value.one, .Other),
693 try dg.fmtIntLiteral(Type.usize, try mod.intValue(Type.usize, 1), .Other),
694694 });
695695 },
696696 }
......@@ -1253,7 +1253,7 @@ pub const DeclGen = struct {
12531253 .ErrorUnion => {
12541254 const payload_ty = ty.errorUnionPayload();
12551255 const error_ty = ty.errorUnionSet();
1256 const error_val = if (val.errorUnionIsPayload()) Value.zero else val;
1256 const error_val = if (val.errorUnionIsPayload()) try mod.intValue(Type.anyerror, 0) else val;
12571257
12581258 if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) {
12591259 return dg.renderValue(writer, error_ty, error_val, location);
......@@ -3611,7 +3611,7 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
36113611 const ptr_val = try f.resolveInst(bin_op.lhs);
36123612 const src_ty = f.typeOf(bin_op.rhs);
36133613
3614 const val_is_undef = if (f.air.value(bin_op.rhs, mod)) |v| v.isUndefDeep() else false;
3614 const val_is_undef = if (try f.air.value(bin_op.rhs, mod)) |v| v.isUndefDeep() else false;
36153615
36163616 if (val_is_undef) {
36173617 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
......@@ -4183,7 +4183,7 @@ fn airCall(
41834183 callee: {
41844184 known: {
41854185 const fn_decl = fn_decl: {
4186 const callee_val = f.air.value(pl_op.operand, mod) orelse break :known;
4186 const callee_val = (try f.air.value(pl_op.operand, mod)) orelse break :known;
41874187 break :fn_decl switch (callee_val.tag()) {
41884188 .extern_fn => callee_val.castTag(.extern_fn).?.data.owner_decl,
41894189 .function => callee_val.castTag(.function).?.data.owner_decl,
......@@ -4269,7 +4269,7 @@ fn airDbgVar(f: *Function, inst: Air.Inst.Index) !CValue {
42694269 const mod = f.object.dg.module;
42704270 const pl_op = f.air.instructions.items(.data)[inst].pl_op;
42714271 const name = f.air.nullTerminatedString(pl_op.payload);
4272 const operand_is_undef = if (f.air.value(pl_op.operand, mod)) |v| v.isUndefDeep() else false;
4272 const operand_is_undef = if (try f.air.value(pl_op.operand, mod)) |v| v.isUndefDeep() else false;
42734273 if (!operand_is_undef) _ = try f.resolveInst(pl_op.operand);
42744274
42754275 try reap(f, inst, &.{pl_op.operand});
......@@ -4735,7 +4735,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index) !CValue {
47354735 try f.renderType(writer, Type.usize);
47364736 try writer.writeByte(')');
47374737 }
4738 try f.object.dg.renderValue(writer, condition_ty, f.air.value(item, mod).?, .Other);
4738 try f.object.dg.renderValue(writer, condition_ty, (try f.air.value(item, mod)).?, .Other);
47394739 try writer.writeByte(':');
47404740 }
47414741 try writer.writeByte(' ');
......@@ -5069,7 +5069,7 @@ fn airIsNull(
50695069 // operand is a regular pointer, test `operand !=/== NULL`
50705070 TypedValue{ .ty = optional_ty, .val = Value.null }
50715071 else if (payload_ty.zigTypeTag(mod) == .ErrorSet)
5072 TypedValue{ .ty = payload_ty, .val = Value.zero }
5072 TypedValue{ .ty = payload_ty, .val = try mod.intValue(payload_ty, 0) }
50735073 else if (payload_ty.isSlice(mod) and optional_ty.optionalReprIsPayload(mod)) rhs: {
50745074 try writer.writeAll(".ptr");
50755075 const slice_ptr_ty = payload_ty.slicePtrFieldType(&slice_ptr_buf, mod);
......@@ -5325,7 +5325,7 @@ fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue {
53255325 },
53265326 .end => {
53275327 try f.writeCValue(writer, field_ptr_val, .Other);
5328 try writer.print(" - {}", .{try f.fmtIntLiteral(Type.usize, Value.one)});
5328 try writer.print(" - {}", .{try f.fmtIntLiteral(Type.usize, try mod.intValue(Type.usize, 1))});
53295329 },
53305330 }
53315331
......@@ -5378,7 +5378,7 @@ fn fieldPtr(
53785378 .end => {
53795379 try writer.writeByte('(');
53805380 try f.writeCValue(writer, container_ptr_val, .Other);
5381 try writer.print(" + {})", .{try f.fmtIntLiteral(Type.usize, Value.one)});
5381 try writer.print(" + {})", .{try f.fmtIntLiteral(Type.usize, try mod.intValue(Type.usize, 1))});
53825382 },
53835383 }
53845384
......@@ -5546,7 +5546,7 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {
55465546 else
55475547 try f.writeCValueMember(writer, operand, .{ .identifier = "error" })
55485548 else
5549 try f.object.dg.renderValue(writer, error_ty, Value.zero, .Initializer);
5549 try f.object.dg.renderValue(writer, error_ty, try mod.intValue(error_ty, 0), .Initializer);
55505550 }
55515551 try writer.writeAll(";\n");
55525552 return local;
......@@ -5673,7 +5673,7 @@ fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue {
56735673 if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) {
56745674 try f.writeCValueDeref(writer, operand);
56755675 try writer.writeAll(" = ");
5676 try f.object.dg.renderValue(writer, error_ty, Value.zero, .Other);
5676 try f.object.dg.renderValue(writer, error_ty, try mod.intValue(error_ty, 0), .Other);
56775677 try writer.writeAll(";\n ");
56785678
56795679 return operand;
......@@ -5681,7 +5681,7 @@ fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue {
56815681 try reap(f, inst, &.{ty_op.operand});
56825682 try f.writeCValueDeref(writer, operand);
56835683 try writer.writeAll(".error = ");
5684 try f.object.dg.renderValue(writer, error_ty, Value.zero, .Other);
5684 try f.object.dg.renderValue(writer, error_ty, try mod.intValue(error_ty, 0), .Other);
56855685 try writer.writeAll(";\n");
56865686
56875687 // Then return the payload pointer (only if it is used)
......@@ -5737,7 +5737,7 @@ fn airWrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue {
57375737 else
57385738 try f.writeCValueMember(writer, local, .{ .identifier = "error" });
57395739 try a.assign(f, writer);
5740 try f.object.dg.renderValue(writer, err_ty, Value.zero, .Other);
5740 try f.object.dg.renderValue(writer, err_ty, try mod.intValue(err_ty, 0), .Other);
57415741 try a.end(f, writer);
57425742 }
57435743 return local;
......@@ -5768,11 +5768,11 @@ fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const
57685768 else
57695769 try f.writeCValue(writer, operand, .Other)
57705770 else
5771 try f.object.dg.renderValue(writer, error_ty, Value.zero, .Other);
5771 try f.object.dg.renderValue(writer, error_ty, try mod.intValue(error_ty, 0), .Other);
57725772 try writer.writeByte(' ');
57735773 try writer.writeAll(operator);
57745774 try writer.writeByte(' ');
5775 try f.object.dg.renderValue(writer, error_ty, Value.zero, .Other);
5775 try f.object.dg.renderValue(writer, error_ty, try mod.intValue(error_ty, 0), .Other);
57765776 try writer.writeAll(";\n");
57775777 return local;
57785778}
......@@ -5798,7 +5798,7 @@ fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue {
57985798 } else if (array_ty.hasRuntimeBitsIgnoreComptime(mod)) {
57995799 try writer.writeAll("&(");
58005800 try f.writeCValueDeref(writer, operand);
5801 try writer.print(")[{}]", .{try f.fmtIntLiteral(Type.usize, Value.zero)});
5801 try writer.print(")[{}]", .{try f.fmtIntLiteral(Type.usize, try mod.intValue(Type.usize, 0))});
58025802 } else try f.writeCValue(writer, operand, .Initializer);
58035803 try writer.writeAll("; ");
58045804
......@@ -6022,7 +6022,7 @@ fn airCmpBuiltinCall(
60226022 try writer.writeByte(')');
60236023 if (!ref_ret) try writer.print(" {s} {}", .{
60246024 compareOperatorC(operator),
6025 try f.fmtIntLiteral(Type.i32, Value.zero),
6025 try f.fmtIntLiteral(Type.i32, try mod.intValue(Type.i32, 0)),
60266026 });
60276027 try writer.writeAll(";\n");
60286028 try v.end(f, inst, writer);
......@@ -6278,7 +6278,7 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
62786278 const value = try f.resolveInst(bin_op.rhs);
62796279 const elem_ty = f.typeOf(bin_op.rhs);
62806280 const elem_abi_size = elem_ty.abiSize(mod);
6281 const val_is_undef = if (f.air.value(bin_op.rhs, mod)) |val| val.isUndefDeep() else false;
6281 const val_is_undef = if (try f.air.value(bin_op.rhs, mod)) |val| val.isUndefDeep() else false;
62826282 const writer = f.object.writer();
62836283
62846284 if (val_is_undef) {
......@@ -6326,7 +6326,7 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
63266326 try writer.writeAll("for (");
63276327 try f.writeCValue(writer, index, .Other);
63286328 try writer.writeAll(" = ");
6329 try f.object.dg.renderValue(writer, Type.usize, Value.zero, .Initializer);
6329 try f.object.dg.renderValue(writer, Type.usize, try mod.intValue(Type.usize, 0), .Initializer);
63306330 try writer.writeAll("; ");
63316331 try f.writeCValue(writer, index, .Other);
63326332 try writer.writeAll(" != ");
......@@ -6677,27 +6677,27 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue {
66776677 std.heap.stackFallback(@sizeOf(ExpectedContents), arena.allocator());
66786678
66796679 try f.object.dg.renderValue(writer, scalar_ty, switch (reduce.operation) {
6680 .Or, .Xor, .Add => Value.zero,
6680 .Or, .Xor, .Add => try mod.intValue(scalar_ty, 0),
66816681 .And => switch (scalar_ty.zigTypeTag(mod)) {
6682 .Bool => Value.one,
6682 .Bool => try mod.intValue(Type.comptime_int, 1),
66836683 else => switch (scalar_ty.intInfo(mod).signedness) {
66846684 .unsigned => try scalar_ty.maxIntScalar(mod),
6685 .signed => Value.negative_one,
6685 .signed => try mod.intValue(scalar_ty, -1),
66866686 },
66876687 },
66886688 .Min => switch (scalar_ty.zigTypeTag(mod)) {
6689 .Bool => Value.one,
6689 .Bool => try mod.intValue(Type.comptime_int, 1),
66906690 .Int => try scalar_ty.maxIntScalar(mod),
66916691 .Float => try Value.floatToValue(std.math.nan(f128), stack.get(), scalar_ty, target),
66926692 else => unreachable,
66936693 },
66946694 .Max => switch (scalar_ty.zigTypeTag(mod)) {
6695 .Bool => Value.zero,
6695 .Bool => try mod.intValue(scalar_ty, 0),
66966696 .Int => try scalar_ty.minInt(stack.get(), mod),
66976697 .Float => try Value.floatToValue(std.math.nan(f128), stack.get(), scalar_ty, target),
66986698 else => unreachable,
66996699 },
6700 .Mul => Value.one,
6700 .Mul => try mod.intValue(Type.comptime_int, 1),
67016701 }, .Initializer);
67026702 try writer.writeAll(";\n");
67036703
......@@ -7686,13 +7686,13 @@ const Vectorize = struct {
76867686
76877687 try writer.writeAll("for (");
76887688 try f.writeCValue(writer, local, .Other);
7689 try writer.print(" = {d}; ", .{try f.fmtIntLiteral(Type.usize, Value.zero)});
7689 try writer.print(" = {d}; ", .{try f.fmtIntLiteral(Type.usize, try mod.intValue(Type.usize, 0))});
76907690 try f.writeCValue(writer, local, .Other);
76917691 try writer.print(" < {d}; ", .{
76927692 try f.fmtIntLiteral(Type.usize, len_val),
76937693 });
76947694 try f.writeCValue(writer, local, .Other);
7695 try writer.print(" += {d}) {{\n", .{try f.fmtIntLiteral(Type.usize, Value.one)});
7695 try writer.print(" += {d}) {{\n", .{try f.fmtIntLiteral(Type.usize, try mod.intValue(Type.usize, 1))});
76967696 f.object.indent_writer.pushIndent();
76977697
76987698 break :index .{ .index = local };
src/codegen/llvm.zig+9-9
......@@ -2854,7 +2854,7 @@ pub const DeclGen = struct {
28542854 },
28552855 .Array => {
28562856 const elem_ty = t.childType(mod);
2857 assert(elem_ty.onePossibleValue(mod) == null);
2857 if (std.debug.runtime_safety) assert((try elem_ty.onePossibleValue(mod)) == null);
28582858 const elem_llvm_ty = try dg.lowerType(elem_ty);
28592859 const total_len = t.arrayLen(mod) + @boolToInt(t.sentinel(mod) != null);
28602860 return elem_llvm_ty.arrayType(@intCast(c_uint, total_len));
......@@ -3588,7 +3588,7 @@ pub const DeclGen = struct {
35883588
35893589 if (!payload_type.hasRuntimeBitsIgnoreComptime(mod)) {
35903590 // We use the error type directly as the type.
3591 const err_val = if (!is_pl) tv.val else Value.zero;
3591 const err_val = if (!is_pl) tv.val else try mod.intValue(Type.anyerror, 0);
35923592 return dg.lowerValue(.{ .ty = Type.anyerror, .val = err_val });
35933593 }
35943594
......@@ -3596,7 +3596,7 @@ pub const DeclGen = struct {
35963596 const error_align = Type.anyerror.abiAlignment(mod);
35973597 const llvm_error_value = try dg.lowerValue(.{
35983598 .ty = Type.anyerror,
3599 .val = if (is_pl) Value.zero else tv.val,
3599 .val = if (is_pl) try mod.intValue(Type.anyerror, 0) else tv.val,
36003600 });
36013601 const llvm_payload_value = try dg.lowerValue(.{
36023602 .ty = payload_type,
......@@ -4476,7 +4476,7 @@ pub const FuncGen = struct {
44764476 const mod = self.dg.module;
44774477 const llvm_val = try self.resolveValue(.{
44784478 .ty = self.typeOf(inst),
4479 .val = self.air.value(inst, mod).?,
4479 .val = (try self.air.value(inst, mod)).?,
44804480 });
44814481 gop.value_ptr.* = llvm_val;
44824482 return llvm_val;
......@@ -6873,7 +6873,7 @@ pub const FuncGen = struct {
68736873 const err_union_ty = self.typeOf(ty_op.operand).childType(mod);
68746874
68756875 const payload_ty = err_union_ty.errorUnionPayload();
6876 const non_error_val = try self.dg.lowerValue(.{ .ty = Type.anyerror, .val = Value.zero });
6876 const non_error_val = try self.dg.lowerValue(.{ .ty = Type.anyerror, .val = try mod.intValue(Type.anyerror, 0) });
68776877 if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) {
68786878 _ = self.builder.buildStore(non_error_val, operand);
68796879 return operand;
......@@ -8203,7 +8203,7 @@ pub const FuncGen = struct {
82038203 const ptr_ty = self.typeOf(bin_op.lhs);
82048204 const operand_ty = ptr_ty.childType(mod);
82058205
8206 const val_is_undef = if (self.air.value(bin_op.rhs, mod)) |val| val.isUndefDeep() else false;
8206 const val_is_undef = if (try self.air.value(bin_op.rhs, mod)) |val| val.isUndefDeep() else false;
82078207 if (val_is_undef) {
82088208 // Even if safety is disabled, we still emit a memset to undefined since it conveys
82098209 // extra information to LLVM. However, safety makes the difference between using
......@@ -8494,7 +8494,7 @@ pub const FuncGen = struct {
84948494 const dest_ptr = self.sliceOrArrayPtr(dest_slice, ptr_ty);
84958495 const is_volatile = ptr_ty.isVolatilePtr(mod);
84968496
8497 if (self.air.value(bin_op.rhs, mod)) |elem_val| {
8497 if (try self.air.value(bin_op.rhs, mod)) |elem_val| {
84988498 if (elem_val.isUndefDeep()) {
84998499 // Even if safety is disabled, we still emit a memset to undefined since it conveys
85008500 // extra information to LLVM. However, safety makes the difference between using
......@@ -9323,7 +9323,7 @@ pub const FuncGen = struct {
93239323
93249324 var indices: [2]*llvm.Value = .{ llvm_u32.constNull(), undefined };
93259325 for (elements, 0..) |elem, i| {
9326 if (result_ty.structFieldValueComptime(mod, i) != null) continue;
9326 if ((try result_ty.structFieldValueComptime(mod, i)) != null) continue;
93279327
93289328 const llvm_elem = try self.resolveInst(elem);
93299329 const llvm_i = llvmFieldIndex(result_ty, i, mod, &ptr_ty_buf).?;
......@@ -9344,7 +9344,7 @@ pub const FuncGen = struct {
93449344 } else {
93459345 var result = llvm_result_ty.getUndef();
93469346 for (elements, 0..) |elem, i| {
9347 if (result_ty.structFieldValueComptime(mod, i) != null) continue;
9347 if ((try result_ty.structFieldValueComptime(mod, i)) != null) continue;
93489348
93499349 const llvm_elem = try self.resolveInst(elem);
93509350 const llvm_i = llvmFieldIndex(result_ty, i, mod, &ptr_ty_buf).?;
src/codegen/spirv.zig+5-5
......@@ -232,7 +232,7 @@ pub const DeclGen = struct {
232232 /// Fetch the result-id for a previously generated instruction or constant.
233233 fn resolve(self: *DeclGen, inst: Air.Inst.Ref) !IdRef {
234234 const mod = self.module;
235 if (self.air.value(inst, mod)) |val| {
235 if (try self.air.value(inst, mod)) |val| {
236236 const ty = self.typeOf(inst);
237237 if (ty.zigTypeTag(mod) == .Fn) {
238238 const fn_decl_index = switch (val.tag()) {
......@@ -584,7 +584,7 @@ pub const DeclGen = struct {
584584 // TODO: Properly lower function pointers. For now we are going to hack around it and
585585 // just generate an empty pointer. Function pointers are represented by usize for now,
586586 // though.
587 try self.addInt(Type.usize, Value.zero);
587 try self.addInt(Type.usize, Value.zero_usize);
588588 // TODO: Add dependency
589589 return;
590590 },
......@@ -803,7 +803,7 @@ pub const DeclGen = struct {
803803 .ErrorUnion => {
804804 const payload_ty = ty.errorUnionPayload();
805805 const is_pl = val.errorUnionIsPayload();
806 const error_val = if (!is_pl) val else Value.zero;
806 const error_val = if (!is_pl) val else try mod.intValue(Type.anyerror, 0);
807807
808808 const eu_layout = dg.errorUnionLayout(payload_ty);
809809 if (!eu_layout.payload_has_bits) {
......@@ -2801,7 +2801,7 @@ pub const DeclGen = struct {
28012801 const value = try self.resolve(bin_op.rhs);
28022802 const ptr_ty_ref = try self.resolveType(ptr_ty, .direct);
28032803
2804 const val_is_undef = if (self.air.value(bin_op.rhs, mod)) |val| val.isUndefDeep() else false;
2804 const val_is_undef = if (try self.air.value(bin_op.rhs, mod)) |val| val.isUndefDeep() else false;
28052805 if (val_is_undef) {
28062806 const undef = try self.spv.constUndef(ptr_ty_ref);
28072807 try self.store(ptr_ty, ptr, undef);
......@@ -3141,7 +3141,7 @@ pub const DeclGen = struct {
31413141 const label = IdRef{ .id = first_case_label.id + case_i };
31423142
31433143 for (items) |item| {
3144 const value = self.air.value(item, mod) orelse {
3144 const value = (try self.air.value(item, mod)) orelse {
31453145 return self.todo("switch on runtime value???", .{});
31463146 };
31473147 const int_val = switch (cond_ty.zigTypeTag(mod)) {
src/type.zig+22-22
......@@ -3377,7 +3377,7 @@ pub const Type = struct {
33773377 }
33783378
33793379 /// For vectors, returns the element type. Otherwise returns self.
3380 pub fn scalarType(ty: Type, mod: *const Module) Type {
3380 pub fn scalarType(ty: Type, mod: *Module) Type {
33813381 return switch (ty.zigTypeTag(mod)) {
33823382 .Vector => ty.childType(mod),
33833383 else => ty,
......@@ -3941,13 +3941,13 @@ pub const Type = struct {
39413941
39423942 /// During semantic analysis, instead call `Sema.typeHasOnePossibleValue` which
39433943 /// resolves field types rather than asserting they are already resolved.
3944 pub fn onePossibleValue(starting_type: Type, mod: *const Module) ?Value {
3944 pub fn onePossibleValue(starting_type: Type, mod: *Module) !?Value {
39453945 var ty = starting_type;
39463946
39473947 if (ty.ip_index != .none) switch (mod.intern_pool.indexToKey(ty.ip_index)) {
39483948 .int_type => |int_type| {
39493949 if (int_type.bits == 0) {
3950 return Value.zero;
3950 return try mod.intValue(ty, 0);
39513951 } else {
39523952 return null;
39533953 }
......@@ -3956,13 +3956,13 @@ pub const Type = struct {
39563956 .array_type => |array_type| {
39573957 if (array_type.len == 0)
39583958 return Value.initTag(.empty_array);
3959 if (array_type.child.toType().onePossibleValue(mod) != null)
3959 if ((try array_type.child.toType().onePossibleValue(mod)) != null)
39603960 return Value.initTag(.the_only_possible_value);
39613961 return null;
39623962 },
39633963 .vector_type => |vector_type| {
39643964 if (vector_type.len == 0) return Value.initTag(.empty_array);
3965 if (vector_type.child.toType().onePossibleValue(mod)) |v| return v;
3965 if (try vector_type.child.toType().onePossibleValue(mod)) |v| return v;
39663966 return null;
39673967 },
39683968 .opt_type => |child| {
......@@ -4055,7 +4055,7 @@ pub const Type = struct {
40554055 assert(s.haveFieldTypes());
40564056 for (s.fields.values()) |field| {
40574057 if (field.is_comptime) continue;
4058 if (field.ty.onePossibleValue(mod) != null) continue;
4058 if ((try field.ty.onePossibleValue(mod)) != null) continue;
40594059 return null;
40604060 }
40614061 return Value.initTag(.empty_struct_value);
......@@ -4066,7 +4066,7 @@ pub const Type = struct {
40664066 for (tuple.values, 0..) |val, i| {
40674067 const is_comptime = val.ip_index != .unreachable_value;
40684068 if (is_comptime) continue;
4069 if (tuple.types[i].onePossibleValue(mod) != null) continue;
4069 if ((try tuple.types[i].onePossibleValue(mod)) != null) continue;
40704070 return null;
40714071 }
40724072 return Value.initTag(.empty_struct_value);
......@@ -4089,7 +4089,7 @@ pub const Type = struct {
40894089 switch (enum_full.fields.count()) {
40904090 0 => return Value.@"unreachable",
40914091 1 => if (enum_full.values.count() == 0) {
4092 return Value.zero; // auto-numbered
4092 return try mod.intValue(ty, 0); // auto-numbered
40934093 } else {
40944094 return enum_full.values.keys()[0];
40954095 },
......@@ -4100,24 +4100,24 @@ pub const Type = struct {
41004100 const enum_simple = ty.castTag(.enum_simple).?.data;
41014101 switch (enum_simple.fields.count()) {
41024102 0 => return Value.@"unreachable",
4103 1 => return Value.zero,
4103 1 => return try mod.intValue(ty, 0),
41044104 else => return null,
41054105 }
41064106 },
41074107 .enum_nonexhaustive => {
41084108 const tag_ty = ty.castTag(.enum_nonexhaustive).?.data.tag_ty;
41094109 if (!tag_ty.hasRuntimeBits(mod)) {
4110 return Value.zero;
4110 return try mod.intValue(ty, 0);
41114111 } else {
41124112 return null;
41134113 }
41144114 },
41154115 .@"union", .union_safety_tagged, .union_tagged => {
41164116 const union_obj = ty.cast(Payload.Union).?.data;
4117 const tag_val = union_obj.tag_ty.onePossibleValue(mod) orelse return null;
4117 const tag_val = (try union_obj.tag_ty.onePossibleValue(mod)) orelse return null;
41184118 if (union_obj.fields.count() == 0) return Value.@"unreachable";
41194119 const only_field = union_obj.fields.values()[0];
4120 const val_val = only_field.ty.onePossibleValue(mod) orelse return null;
4120 const val_val = (try only_field.ty.onePossibleValue(mod)) orelse return null;
41214121 _ = tag_val;
41224122 _ = val_val;
41234123 return Value.initTag(.empty_struct_value);
......@@ -4128,7 +4128,7 @@ pub const Type = struct {
41284128 .array => {
41294129 if (ty.arrayLen(mod) == 0)
41304130 return Value.initTag(.empty_array);
4131 if (ty.childType(mod).onePossibleValue(mod) != null)
4131 if ((try ty.childType(mod).onePossibleValue(mod)) != null)
41324132 return Value.initTag(.the_only_possible_value);
41334133 return null;
41344134 },
......@@ -4365,8 +4365,8 @@ pub const Type = struct {
43654365 /// Asserts that the type is an integer.
43664366 pub fn minIntScalar(ty: Type, mod: *Module) !Value {
43674367 const info = ty.intInfo(mod);
4368 if (info.signedness == .unsigned) return Value.zero;
4369 if (info.bits == 0) return Value.negative_one;
4368 if (info.signedness == .unsigned) return mod.intValue(ty, 0);
4369 if (info.bits == 0) return mod.intValue(ty, -1);
43704370
43714371 if (std.math.cast(u6, info.bits - 1)) |shift| {
43724372 const n = @as(i64, std.math.minInt(i64)) >> (63 - shift);
......@@ -4392,17 +4392,17 @@ pub const Type = struct {
43924392 }
43934393
43944394 /// Asserts that the type is an integer.
4395 pub fn maxIntScalar(self: Type, mod: *Module) !Value {
4396 const info = self.intInfo(mod);
4395 pub fn maxIntScalar(ty: Type, mod: *Module) !Value {
4396 const info = ty.intInfo(mod);
43974397
43984398 switch (info.bits) {
43994399 0 => return switch (info.signedness) {
4400 .signed => Value.negative_one,
4401 .unsigned => Value.zero,
4400 .signed => mod.intValue(ty, -1),
4401 .unsigned => mod.intValue(ty, 0),
44024402 },
44034403 1 => return switch (info.signedness) {
4404 .signed => Value.zero,
4405 .unsigned => Value.one,
4404 .signed => mod.intValue(ty, 0),
4405 .unsigned => mod.intValue(ty, 0),
44064406 },
44074407 else => {},
44084408 }
......@@ -4662,7 +4662,7 @@ pub const Type = struct {
46624662 }
46634663 }
46644664
4665 pub fn structFieldValueComptime(ty: Type, mod: *const Module, index: usize) ?Value {
4665 pub fn structFieldValueComptime(ty: Type, mod: *Module, index: usize) !?Value {
46664666 switch (ty.tag()) {
46674667 .@"struct" => {
46684668 const struct_obj = ty.castTag(.@"struct").?.data;
src/value.zig+22-21
......@@ -1022,7 +1022,7 @@ pub const Value = struct {
10221022 if (opt_val) |some| {
10231023 return some.writeToMemory(child, mod, buffer);
10241024 } else {
1025 return writeToMemory(Value.zero, Type.usize, mod, buffer);
1025 return writeToMemory(try mod.intValue(Type.usize, 0), Type.usize, mod, buffer);
10261026 }
10271027 },
10281028 else => return error.Unimplemented,
......@@ -1124,7 +1124,7 @@ pub const Value = struct {
11241124 .Packed => {
11251125 const field_index = ty.unionTagFieldIndex(val.unionTag(), mod);
11261126 const field_type = ty.unionFields().values()[field_index.?].ty;
1127 const field_val = val.fieldValue(field_type, mod, field_index.?);
1127 const field_val = try val.fieldValue(field_type, mod, field_index.?);
11281128
11291129 return field_val.writeToPackedMemory(field_type, mod, buffer, bit_offset);
11301130 },
......@@ -1141,7 +1141,7 @@ pub const Value = struct {
11411141 if (opt_val) |some| {
11421142 return some.writeToPackedMemory(child, mod, buffer, bit_offset);
11431143 } else {
1144 return writeToPackedMemory(Value.zero, Type.usize, mod, buffer, bit_offset);
1144 return writeToPackedMemory(try mod.intValue(Type.usize, 0), Type.usize, mod, buffer, bit_offset);
11451145 }
11461146 },
11471147 else => @panic("TODO implement writeToPackedMemory for more types"),
......@@ -1173,7 +1173,7 @@ pub const Value = struct {
11731173 const int_info = ty.intInfo(mod);
11741174 const bits = int_info.bits;
11751175 const byte_count = (bits + 7) / 8;
1176 if (bits == 0 or buffer.len == 0) return Value.zero;
1176 if (bits == 0 or buffer.len == 0) return mod.intValue(ty, 0);
11771177
11781178 if (bits <= 64) switch (int_info.signedness) { // Fast path for integers <= u64
11791179 .signed => {
......@@ -1290,12 +1290,12 @@ pub const Value = struct {
12901290 }
12911291 },
12921292 .Int, .Enum => {
1293 if (buffer.len == 0) return Value.zero;
1293 if (buffer.len == 0) return mod.intValue(ty, 0);
12941294 const int_info = ty.intInfo(mod);
12951295 const abi_size = @intCast(usize, ty.abiSize(mod));
12961296
12971297 const bits = int_info.bits;
1298 if (bits == 0) return Value.zero;
1298 if (bits == 0) return mod.intValue(ty, 0);
12991299 if (bits <= 64) switch (int_info.signedness) { // Fast path for integers <= u64
13001300 .signed => return mod.intValue(ty, std.mem.readVarPackedInt(i64, buffer, bit_offset, bits, endian, .signed)),
13011301 .unsigned => return mod.intValue(ty, std.mem.readVarPackedInt(u64, buffer, bit_offset, bits, endian, .unsigned)),
......@@ -2091,11 +2091,11 @@ pub const Value = struct {
20912091 // .the_one_possible_value,
20922092 // .aggregate,
20932093 // Note that we already checked above for matching tags, e.g. both .aggregate.
2094 return ty.onePossibleValue(mod) != null;
2094 return (try ty.onePossibleValue(mod)) != null;
20952095 },
20962096 .Union => {
20972097 // Here we have to check for value equality, as-if `a` has been coerced to `ty`.
2098 if (ty.onePossibleValue(mod) != null) {
2098 if ((try ty.onePossibleValue(mod)) != null) {
20992099 return true;
21002100 }
21012101 if (a_ty.castTag(.anon_struct)) |payload| {
......@@ -2604,7 +2604,7 @@ pub const Value = struct {
26042604 if (data.container_ptr.pointerDecl()) |decl_index| {
26052605 const container_decl = mod.declPtr(decl_index);
26062606 const field_type = data.container_ty.structFieldType(data.field_index);
2607 const field_val = container_decl.val.fieldValue(field_type, mod, data.field_index);
2607 const field_val = try container_decl.val.fieldValue(field_type, mod, data.field_index);
26082608 return field_val.elemValue(mod, index);
26092609 } else unreachable;
26102610 },
......@@ -2723,7 +2723,7 @@ pub const Value = struct {
27232723 };
27242724 }
27252725
2726 pub fn fieldValue(val: Value, ty: Type, mod: *const Module, index: usize) Value {
2726 pub fn fieldValue(val: Value, ty: Type, mod: *Module, index: usize) !Value {
27272727 switch (val.ip_index) {
27282728 .undef => return Value.undef,
27292729 .none => switch (val.tag()) {
......@@ -2737,14 +2737,14 @@ pub const Value = struct {
27372737 return payload.val;
27382738 },
27392739
2740 .the_only_possible_value => return ty.onePossibleValue(mod).?,
2740 .the_only_possible_value => return (try ty.onePossibleValue(mod)).?,
27412741
27422742 .empty_struct_value => {
27432743 if (ty.isSimpleTupleOrAnonStruct()) {
27442744 const tuple = ty.tupleFields();
27452745 return tuple.values[index];
27462746 }
2747 if (ty.structFieldValueComptime(mod, index)) |some| {
2747 if (try ty.structFieldValueComptime(mod, index)) |some| {
27482748 return some;
27492749 }
27502750 unreachable;
......@@ -2968,7 +2968,7 @@ pub const Value = struct {
29682968 switch (val.ip_index) {
29692969 .undef => return val,
29702970 .none => switch (val.tag()) {
2971 .the_only_possible_value => return Value.zero, // for i0, u0
2971 .the_only_possible_value => return Value.float_zero, // for i0, u0
29722972 .lazy_align => {
29732973 const ty = val.castTag(.lazy_align).?.data;
29742974 if (opt_sema) |sema| {
......@@ -3402,7 +3402,7 @@ pub const Value = struct {
34023402 if (lhs.isUndef() or rhs.isUndef()) return Value.undef;
34033403
34043404 const anded = try bitwiseAnd(lhs, rhs, ty, arena, mod);
3405 const all_ones = if (ty.isSignedInt(mod)) Value.negative_one else try ty.maxIntScalar(mod);
3405 const all_ones = if (ty.isSignedInt(mod)) try mod.intValue(ty, -1) else try ty.maxIntScalar(mod);
34063406 return bitwiseXor(anded, all_ones, ty, arena, mod);
34073407 }
34083408
......@@ -3803,7 +3803,7 @@ pub const Value = struct {
38033803 bits: u16,
38043804 mod: *Module,
38053805 ) !Value {
3806 if (bits == 0) return Value.zero;
3806 if (bits == 0) return mod.intValue(ty, 0);
38073807
38083808 var val_space: Value.BigIntSpace = undefined;
38093809 const val_bigint = val.toBigInt(&val_space, mod);
......@@ -4011,9 +4011,9 @@ pub const Value = struct {
40114011 // The shift is enough to remove all the bits from the number, which means the
40124012 // result is 0 or -1 depending on the sign.
40134013 if (lhs_bigint.positive) {
4014 return Value.zero;
4014 return mod.intValue(ty, 0);
40154015 } else {
4016 return Value.negative_one;
4016 return mod.intValue(ty, -1);
40174017 }
40184018 }
40194019
......@@ -5151,10 +5151,9 @@ pub const Value = struct {
51515151
51525152 pub const BigIntSpace = InternPool.Key.Int.Storage.BigIntSpace;
51535153
5154 pub const zero: Value = .{ .ip_index = .zero, .legacy = undefined };
5155 pub const one: Value = .{ .ip_index = .one, .legacy = undefined };
5156 pub const negative_one: Value = .{ .ip_index = .negative_one, .legacy = undefined };
5154 pub const zero_usize: Value = .{ .ip_index = .zero_usize, .legacy = undefined };
51575155 pub const undef: Value = .{ .ip_index = .undef, .legacy = undefined };
5156 pub const float_zero: Value = .{ .ip_index = .zero, .legacy = undefined }; // TODO: replace this!
51585157 pub const @"void": Value = .{ .ip_index = .void_value, .legacy = undefined };
51595158 pub const @"null": Value = .{ .ip_index = .null_value, .legacy = undefined };
51605159 pub const @"false": Value = .{ .ip_index = .bool_false, .legacy = undefined };
......@@ -5169,7 +5168,9 @@ pub const Value = struct {
51695168 }
51705169
51715170 pub fn boolToInt(x: bool) Value {
5172 return if (x) Value.one else Value.zero;
5171 const zero: Value = .{ .ip_index = .zero, .legacy = undefined };
5172 const one: Value = .{ .ip_index = .one, .legacy = undefined };
5173 return if (x) one else zero;
51735174 }
51745175
51755176 pub const RuntimeIndex = enum(u32) {