authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-05-28 10:33:59-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-10 20:47:56-07:00
log3064d2aa7b9a8ea836cb70884b0640fe902ecc29
treea3e315a06c0912e03ddac6b04cbe23205872bd31
parent3b6ca1d35b950d67fff5964f0063dadf01f30e2d

behavior: additional llvm fixes


13 files changed, 160 insertions(+), 137 deletions(-)

src/InternPool.zig+20-15
......@@ -3131,7 +3131,8 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
31313131 },
31323132
31333133 .enum_type => |enum_type| {
3134 assert(enum_type.tag_ty != .none);
3134 assert(enum_type.tag_ty == .noreturn_type or ip.isIntegerType(enum_type.tag_ty));
3135 for (enum_type.values) |value| assert(ip.typeOf(value) == enum_type.tag_ty);
31353136 assert(enum_type.names_map == .none);
31363137 assert(enum_type.values_map == .none);
31373138
......@@ -3622,14 +3623,12 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
36223623 if (bytes.len != len) {
36233624 assert(bytes.len == len_including_sentinel);
36243625 assert(bytes[len] == ip.indexToKey(sentinel).int.storage.u64);
3625 unreachable;
36263626 }
36273627 },
36283628 .elems => |elems| {
36293629 if (elems.len != len) {
36303630 assert(elems.len == len_including_sentinel);
36313631 assert(elems[len] == sentinel);
3632 unreachable;
36333632 }
36343633 },
36353634 .repeated_elem => |elem| {
......@@ -3832,7 +3831,7 @@ pub const IncompleteEnumType = struct {
38323831 values_start: u32,
38333832
38343833 pub fn setTagType(self: @This(), ip: *InternPool, tag_ty: Index) void {
3835 assert(tag_ty != .none);
3834 assert(tag_ty == .noreturn_type or ip.isIntegerType(tag_ty));
38363835 ip.extra.items[self.tag_ty_index] = @enumToInt(tag_ty);
38373836 }
38383837
......@@ -3863,6 +3862,7 @@ pub const IncompleteEnumType = struct {
38633862 gpa: Allocator,
38643863 value: Index,
38653864 ) Allocator.Error!?u32 {
3865 assert(ip.typeOf(value) == @intToEnum(Index, ip.extra.items[self.tag_ty_index]));
38663866 const map = &ip.maps.items[@enumToInt(self.values_map.unwrap().?)];
38673867 const field_index = map.count();
38683868 const indexes = ip.extra.items[self.values_start..][0..field_index];
......@@ -4346,7 +4346,7 @@ pub fn sliceLen(ip: InternPool, i: Index) Index {
43464346/// * ptr <=> ptr
43474347/// * opt ptr <=> ptr
43484348/// * opt ptr <=> opt ptr
4349/// * int => ptr
4349/// * int <=> ptr
43504350/// * null_value => opt
43514351/// * payload => opt
43524352/// * error set <=> error set
......@@ -4386,18 +4386,18 @@ pub fn getCoerced(ip: *InternPool, gpa: Allocator, val: Index, new_ty: Index) Al
43864386 .ty = new_ty,
43874387 .index = func.index,
43884388 } }),
4389 .int => |int| if (ip.isIntegerType(new_ty))
4390 return getCoercedInts(ip, gpa, int, new_ty)
4391 else if (ip.isEnumType(new_ty))
4392 return ip.get(gpa, .{ .enum_tag = .{
4389 .int => |int| switch (ip.indexToKey(new_ty)) {
4390 .enum_type => |enum_type| return ip.get(gpa, .{ .enum_tag = .{
43934391 .ty = new_ty,
4394 .int = val,
4395 } })
4396 else if (ip.isPointerType(new_ty))
4397 return ip.get(gpa, .{ .ptr = .{
4392 .int = try ip.getCoerced(gpa, val, enum_type.tag_ty),
4393 } }),
4394 .ptr_type => return ip.get(gpa, .{ .ptr = .{
43984395 .ty = new_ty,
4399 .addr = .{ .int = val },
4396 .addr = .{ .int = try ip.getCoerced(gpa, val, .usize_type) },
44004397 } }),
4398 else => if (ip.isIntegerType(new_ty))
4399 return getCoercedInts(ip, gpa, int, new_ty),
4400 },
44014401 .enum_tag => |enum_tag| if (ip.isIntegerType(new_ty))
44024402 return getCoercedInts(ip, gpa, ip.indexToKey(enum_tag.int).int, new_ty),
44034403 .enum_literal => |enum_literal| switch (ip.indexToKey(new_ty)) {
......@@ -4421,7 +4421,12 @@ pub fn getCoerced(ip: *InternPool, gpa: Allocator, val: Index, new_ty: Index) Al
44214421 .ty = new_ty,
44224422 .addr = ptr.addr,
44234423 .len = ptr.len,
4424 } }),
4424 } })
4425 else if (ip.isIntegerType(new_ty))
4426 switch (ptr.addr) {
4427 .int => |int| return ip.getCoerced(gpa, int, new_ty),
4428 else => {},
4429 },
44254430 .opt => |opt| if (ip.isPointerType(new_ty))
44264431 return switch (opt.val) {
44274432 .none => try ip.get(gpa, .{ .ptr = .{
src/Module.zig+5-1
......@@ -707,6 +707,10 @@ pub const Decl = struct {
707707 return TypedValue{ .ty = decl.ty, .val = decl.val };
708708 }
709709
710 pub fn internValue(decl: Decl, mod: *Module) Allocator.Error!InternPool.Index {
711 return decl.val.intern(decl.ty, mod);
712 }
713
710714 pub fn isFunction(decl: Decl, mod: *const Module) !bool {
711715 const tv = try decl.typedValue();
712716 return tv.ty.zigTypeTag(mod) == .Fn;
......@@ -7073,7 +7077,7 @@ pub fn atomicPtrAlignment(
70737077
70747078 const int_ty = switch (ty.zigTypeTag(mod)) {
70757079 .Int => ty,
7076 .Enum => try ty.intTagType(mod),
7080 .Enum => ty.intTagType(mod),
70777081 .Float => {
70787082 const bit_count = ty.floatBits(target);
70797083 if (bit_count > max_atomic_bits) {
src/Sema.zig+67-66
......@@ -3291,7 +3291,8 @@ fn zirErrorSetDecl(
32913291 while (extra_index < extra_index_end) : (extra_index += 2) { // +2 to skip over doc_string
32923292 const str_index = sema.code.extra[extra_index];
32933293 const name = sema.code.nullTerminatedString(str_index);
3294 const name_ip = try mod.intern_pool.getOrPutString(gpa, name);
3294 const kv = try mod.getErrorValue(name);
3295 const name_ip = try mod.intern_pool.getOrPutString(gpa, kv.key);
32953296 const result = names.getOrPutAssumeCapacity(name_ip);
32963297 assert(!result.found_existing); // verified in AstGen
32973298 }
......@@ -6409,7 +6410,7 @@ fn zirCall(
64096410
64106411 // Generate args to comptime params in comptime block.
64116412 defer block.is_comptime = parent_comptime;
6412 if (arg_index < fn_params_len and func_ty_info.paramIsComptime(@intCast(u5, arg_index))) {
6413 if (arg_index < @min(fn_params_len, 32) and func_ty_info.paramIsComptime(@intCast(u5, arg_index))) {
64136414 block.is_comptime = true;
64146415 // TODO set comptime_reason
64156416 }
......@@ -7077,14 +7078,13 @@ fn analyzeCall(
70777078 assert(!func_ty_info.is_generic);
70787079
70797080 const args = try sema.arena.alloc(Air.Inst.Ref, uncasted_args.len);
7080 const fn_info = mod.typeToFunc(func_ty).?;
70817081 for (uncasted_args, 0..) |uncasted_arg, i| {
70827082 if (i < fn_params_len) {
70837083 const opts: CoerceOpts = .{ .param_src = .{
70847084 .func_inst = func,
70857085 .param_i = @intCast(u32, i),
70867086 } };
7087 const param_ty = fn_info.param_types[i].toType();
7087 const param_ty = mod.typeToFunc(func_ty).?.param_types[i].toType();
70887088 args[i] = sema.analyzeCallArg(
70897089 block,
70907090 .unneeded,
......@@ -8267,7 +8267,7 @@ fn zirEnumToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
82678267 };
82688268 const enum_tag_ty = sema.typeOf(enum_tag);
82698269
8270 const int_tag_ty = try enum_tag_ty.intTagType(mod);
8270 const int_tag_ty = enum_tag_ty.intTagType(mod);
82718271
82728272 if (try sema.typeHasOnePossibleValue(enum_tag_ty)) |opv| {
82738273 return sema.addConstant(int_tag_ty, try mod.getCoerced(opv, int_tag_ty));
......@@ -8299,12 +8299,9 @@ fn zirIntToEnum(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
82998299
83008300 if (try sema.resolveMaybeUndefVal(operand)) |int_val| {
83018301 if (dest_ty.isNonexhaustiveEnum(mod)) {
8302 const int_tag_ty = try dest_ty.intTagType(mod);
8302 const int_tag_ty = dest_ty.intTagType(mod);
83038303 if (try sema.intFitsInType(int_val, int_tag_ty, null)) {
8304 return sema.addConstant(dest_ty, (try mod.intern(.{ .enum_tag = .{
8305 .ty = dest_ty.toIntern(),
8306 .int = int_val.toIntern(),
8307 } })).toValue());
8304 return sema.addConstant(dest_ty, try mod.getCoerced(int_val, dest_ty));
83088305 }
83098306 const msg = msg: {
83108307 const msg = try sema.errMsg(
......@@ -8336,7 +8333,7 @@ fn zirIntToEnum(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
83368333 };
83378334 return sema.failWithOwnedErrorMsg(msg);
83388335 }
8339 return sema.addConstant(dest_ty, int_val);
8336 return sema.addConstant(dest_ty, try mod.getCoerced(int_val, dest_ty));
83408337 }
83418338
83428339 if (try sema.typeHasOnePossibleValue(dest_ty)) |opv| {
......@@ -9513,7 +9510,7 @@ fn zirPtrToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
95139510 return sema.fail(block, ptr_src, "expected pointer, found '{}'", .{ptr_ty.fmt(sema.mod)});
95149511 }
95159512 if (try sema.resolveMaybeUndefValIntable(ptr)) |ptr_val| {
9516 return sema.addConstant(Type.usize, ptr_val);
9513 return sema.addConstant(Type.usize, try mod.getCoerced(ptr_val, Type.usize));
95179514 }
95189515 try sema.requireRuntimeBlock(block, inst_data.src(), ptr_src);
95199516 return block.addUnOp(.ptrtoint, ptr);
......@@ -9651,7 +9648,7 @@ fn intCast(
96519648 // range shrinkage
96529649 // requirement: int value fits into target type
96539650 if (wanted_value_bits < actual_value_bits) {
9654 const dest_max_val_scalar = try dest_scalar_ty.maxIntScalar(mod, operand_ty);
9651 const dest_max_val_scalar = try dest_scalar_ty.maxIntScalar(mod, operand_scalar_ty);
96559652 const dest_max_val = try sema.splat(operand_ty, dest_max_val_scalar);
96569653 const dest_max = try sema.addConstant(operand_ty, dest_max_val);
96579654 const diff = try block.addBinOp(.subwrap, dest_max, operand);
......@@ -12848,7 +12845,7 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1284812845 if (res_sent_val) |sent_val| {
1284912846 const elem_index = try sema.addIntUnsigned(Type.usize, result_len);
1285012847 const elem_ptr = try block.addPtrElemPtr(alloc, elem_index, elem_ptr_ty);
12851 const init = try sema.addConstant(lhs_info.elem_type, sent_val);
12848 const init = try sema.addConstant(lhs_info.elem_type, try mod.getCoerced(sent_val, lhs_info.elem_type));
1285212849 try sema.storePtr2(block, src, elem_ptr, src, init, lhs_src, .store);
1285312850 }
1285412851
......@@ -19236,7 +19233,8 @@ fn zirReify(
1923619233 const name_val = try elem_val.fieldValue(mod, elem_fields.getIndex("name").?);
1923719234
1923819235 const name_str = try name_val.toAllocatedBytes(Type.slice_const_u8, sema.arena, mod);
19239 const name_ip = try mod.intern_pool.getOrPutString(gpa, name_str);
19236 const kv = try mod.getErrorValue(name_str);
19237 const name_ip = try mod.intern_pool.getOrPutString(gpa, kv.key);
1924019238 const gop = names.getOrPutAssumeCapacity(name_ip);
1924119239 if (gop.found_existing) {
1924219240 return sema.fail(block, src, "duplicate error '{s}'", .{name_str});
......@@ -19346,7 +19344,7 @@ fn zirReify(
1934619344 return sema.failWithOwnedErrorMsg(msg);
1934719345 }
1934819346
19349 if (try incomplete_enum.addFieldValue(&mod.intern_pool, gpa, value_val.toIntern())) |other| {
19347 if (try incomplete_enum.addFieldValue(&mod.intern_pool, gpa, (try mod.getCoerced(value_val, int_tag_ty)).toIntern())) |other| {
1935019348 const msg = msg: {
1935119349 const msg = try sema.errMsg(block, src, "enum tag value {} already taken", .{value_val.fmtValue(Type.comptime_int, mod)});
1935219350 errdefer msg.destroy(gpa);
......@@ -20263,7 +20261,7 @@ fn zirErrSetCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
2026320261 }
2026420262 }
2026520263
20266 return sema.addConstant(dest_ty, val);
20264 return sema.addConstant(dest_ty, try mod.getCoerced(val, dest_ty));
2026720265 }
2026820266
2026920267 try sema.requireRuntimeBlock(block, src, operand_src);
......@@ -20421,7 +20419,7 @@ fn zirConstCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData
2042120419 const dest_ty = try Type.ptr(sema.arena, mod, ptr_info);
2042220420
2042320421 if (try sema.resolveMaybeUndefVal(operand)) |operand_val| {
20424 return sema.addConstant(dest_ty, operand_val);
20422 return sema.addConstant(dest_ty, try mod.getCoerced(operand_val, dest_ty));
2042520423 }
2042620424
2042720425 try sema.requireRuntimeBlock(block, src, null);
......@@ -20624,7 +20622,7 @@ fn zirBitCount(
2062420622 for (elems, 0..) |*elem, i| {
2062520623 const elem_val = try val.elemValue(mod, i);
2062620624 const count = comptimeOp(elem_val, scalar_ty, mod);
20627 elem.* = (try mod.intValue(scalar_ty, count)).toIntern();
20625 elem.* = (try mod.intValue(result_scalar_ty, count)).toIntern();
2062820626 }
2062920627 return sema.addConstant(result_ty, (try mod.intern(.{ .aggregate = .{
2063020628 .ty = result_ty.toIntern(),
......@@ -22385,7 +22383,9 @@ fn analyzeMinMax(
2238522383 if (std.debug.runtime_safety) {
2238622384 assert(try sema.intFitsInType(val, refined_ty, null));
2238722385 }
22388 cur_minmax = try sema.addConstant(refined_ty, try mod.getCoerced(val, refined_ty));
22386 cur_minmax = try sema.addConstant(refined_ty, (try sema.resolveMaybeUndefVal(
22387 try sema.coerceInMemory(block, val, orig_ty, refined_ty, src),
22388 )).?);
2238922389 }
2239022390
2239122391 break :refined refined_ty;
......@@ -23684,7 +23684,7 @@ fn validateExternType(
2368423684 return !target_util.fnCallConvAllowsZigTypes(target, ty.fnCallingConvention(mod));
2368523685 },
2368623686 .Enum => {
23687 return sema.validateExternType(try ty.intTagType(mod), position);
23687 return sema.validateExternType(ty.intTagType(mod), position);
2368823688 },
2368923689 .Struct, .Union => switch (ty.containerLayout(mod)) {
2369023690 .Extern => return true,
......@@ -23762,7 +23762,7 @@ fn explainWhyTypeIsNotExtern(
2376223762 }
2376323763 },
2376423764 .Enum => {
23765 const tag_ty = try ty.intTagType(mod);
23765 const tag_ty = ty.intTagType(mod);
2376623766 try mod.errNoteNonLazy(src_loc, msg, "enum tag type '{}' is not extern compatible", .{tag_ty.fmt(sema.mod)});
2376723767 try sema.explainWhyTypeIsNotExtern(msg, src_loc, tag_ty, position);
2376823768 },
......@@ -25412,7 +25412,8 @@ fn elemVal(
2541225412 const elem_ptr_ty = try sema.elemPtrType(indexable_ty, index);
2541325413 const elem_ptr_val = try indexable_val.elemPtr(elem_ptr_ty, index, mod);
2541425414 if (try sema.pointerDeref(block, indexable_src, elem_ptr_val, elem_ptr_ty)) |elem_val| {
25415 return sema.addConstant(indexable_ty.elemType2(mod), elem_val);
25415 const result_ty = indexable_ty.elemType2(mod);
25416 return sema.addConstant(result_ty, try mod.getCoerced(elem_val, result_ty));
2541625417 }
2541725418 break :rs indexable_src;
2541825419 };
......@@ -26603,6 +26604,10 @@ fn coerceInMemory(
2660326604 .storage = .{ .elems = dest_elems },
2660426605 } })).toValue());
2660526606 },
26607 .float => |float| return sema.addConstant(dst_ty, (try mod.intern(.{ .float = .{
26608 .ty = dst_ty.toIntern(),
26609 .storage = float.storage,
26610 } })).toValue()),
2660626611 else => return sema.addConstant(dst_ty, try mod.getCoerced(val, dst_ty)),
2660726612 }
2660826613}
......@@ -26983,8 +26988,11 @@ fn coerceInMemoryAllowed(
2698326988 if (dest_ty.eql(src_ty, mod))
2698426989 return .ok;
2698526990
26991 const dest_tag = dest_ty.zigTypeTag(mod);
26992 const src_tag = src_ty.zigTypeTag(mod);
26993
2698626994 // Differently-named integers with the same number of bits.
26987 if (dest_ty.zigTypeTag(mod) == .Int and src_ty.zigTypeTag(mod) == .Int) {
26995 if (dest_tag == .Int and src_tag == .Int) {
2698826996 const dest_info = dest_ty.intInfo(mod);
2698926997 const src_info = src_ty.intInfo(mod);
2699026998
......@@ -27009,7 +27017,7 @@ fn coerceInMemoryAllowed(
2700927017 }
2701027018
2701127019 // Differently-named floats with the same number of bits.
27012 if (dest_ty.zigTypeTag(mod) == .Float and src_ty.zigTypeTag(mod) == .Float) {
27020 if (dest_tag == .Float and src_tag == .Float) {
2701327021 const dest_bits = dest_ty.floatBits(target);
2701427022 const src_bits = src_ty.floatBits(target);
2701527023 if (dest_bits == src_bits) {
......@@ -27031,9 +27039,6 @@ fn coerceInMemoryAllowed(
2703127039 return try sema.coerceInMemoryAllowedPtrs(block, dest_ty, src_ty, dest_ty, src_ty, dest_is_mut, target, dest_src, src_src);
2703227040 }
2703327041
27034 const dest_tag = dest_ty.zigTypeTag(mod);
27035 const src_tag = src_ty.zigTypeTag(mod);
27036
2703727042 // Functions
2703827043 if (dest_tag == .Fn and src_tag == .Fn) {
2703927044 return try sema.coerceInMemoryAllowedFns(block, dest_ty, src_ty, target, dest_src, src_src);
......@@ -27808,7 +27813,7 @@ fn beginComptimePtrMutation(
2780827813 .comptime_field => |comptime_field| {
2780927814 const duped = try sema.arena.create(Value);
2781027815 duped.* = comptime_field.toValue();
27811 return sema.beginComptimePtrMutationInner(block, src, mod.intern_pool.typeOf(ptr_val.toIntern()).toType(), duped, ptr_elem_ty, .{
27816 return sema.beginComptimePtrMutationInner(block, src, mod.intern_pool.typeOf(comptime_field).toType(), duped, ptr_elem_ty, .{
2781227817 .decl = undefined,
2781327818 .runtime_index = .comptime_field_ptr,
2781427819 });
......@@ -27864,7 +27869,21 @@ fn beginComptimePtrMutation(
2786427869 .direct => |val_ptr| {
2786527870 const payload_ty = parent.ty.optionalChild(mod);
2786627871 switch (val_ptr.ip_index) {
27867 .undef, .null_value => {
27872 .none => return ComptimePtrMutationKit{
27873 .mut_decl = parent.mut_decl,
27874 .pointee = .{ .direct = &val_ptr.castTag(.opt_payload).?.data },
27875 .ty = payload_ty,
27876 },
27877 else => {
27878 const payload_val = switch (mod.intern_pool.indexToKey(val_ptr.ip_index)) {
27879 .undef => try mod.intern(.{ .undef = payload_ty.toIntern() }),
27880 .opt => |opt| switch (opt.val) {
27881 .none => try mod.intern(.{ .undef = payload_ty.toIntern() }),
27882 else => opt.val,
27883 },
27884 else => unreachable,
27885 };
27886
2786827887 // An optional has been initialized to undefined at comptime and now we
2786927888 // are for the first time setting the payload. We must change the
2787027889 // representation of the optional from `undef` to `opt_payload`.
......@@ -27874,7 +27893,7 @@ fn beginComptimePtrMutation(
2787427893 const payload = try arena.create(Value.Payload.SubValue);
2787527894 payload.* = .{
2787627895 .base = .{ .tag = .opt_payload },
27877 .data = (try mod.intern(.{ .undef = payload_ty.toIntern() })).toValue(),
27896 .data = payload_val.toValue(),
2787827897 };
2787927898
2788027899 val_ptr.* = Value.initPayload(&payload.base);
......@@ -27885,24 +27904,6 @@ fn beginComptimePtrMutation(
2788527904 .ty = payload_ty,
2788627905 };
2788727906 },
27888 .none => switch (val_ptr.tag()) {
27889 .opt_payload => return ComptimePtrMutationKit{
27890 .mut_decl = parent.mut_decl,
27891 .pointee = .{ .direct = &val_ptr.castTag(.opt_payload).?.data },
27892 .ty = payload_ty,
27893 },
27894
27895 else => return ComptimePtrMutationKit{
27896 .mut_decl = parent.mut_decl,
27897 .pointee = .{ .direct = val_ptr },
27898 .ty = payload_ty,
27899 },
27900 },
27901 else => return ComptimePtrMutationKit{
27902 .mut_decl = parent.mut_decl,
27903 .pointee = .{ .direct = val_ptr },
27904 .ty = payload_ty,
27905 },
2790627907 }
2790727908 },
2790827909 .bad_decl_ty, .bad_ptr_ty => return parent,
......@@ -33339,16 +33340,20 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {
3333933340
3334033341 return null;
3334133342 },
33342 .auto, .explicit => switch (enum_type.names.len) {
33343 0 => return Value.@"unreachable",
33344 1 => return try mod.getCoerced((if (enum_type.values.len == 0)
33345 try mod.intern(.{ .int = .{
33346 .ty = enum_type.tag_ty,
33347 .storage = .{ .u64 = 0 },
33348 } })
33349 else
33350 enum_type.values[0]).toValue(), ty),
33351 else => return null,
33343 .auto, .explicit => {
33344 if (enum_type.tag_ty.toType().hasRuntimeBits(mod)) return null;
33345
33346 switch (enum_type.names.len) {
33347 0 => return Value.@"unreachable",
33348 1 => return try mod.getCoerced((if (enum_type.values.len == 0)
33349 try mod.intern(.{ .int = .{
33350 .ty = enum_type.tag_ty,
33351 .storage = .{ .u64 = 0 },
33352 } })
33353 else
33354 enum_type.values[0]).toValue(), ty),
33355 else => return null,
33356 }
3335233357 },
3335333358 },
3335433359
......@@ -34241,13 +34246,9 @@ fn intFitsInType(
3424134246 if (ty.toIntern() == .comptime_int_type) return true;
3424234247 const info = ty.intInfo(mod);
3424334248 switch (val.toIntern()) {
34244 .undef,
34245 .zero,
34246 .zero_usize,
34247 .zero_u8,
34248 => return true,
34249
34249 .zero_usize, .zero_u8 => return true,
3425034250 else => switch (mod.intern_pool.indexToKey(val.toIntern())) {
34251 .undef => return true,
3425134252 .variable, .extern_func, .func, .ptr => {
3425234253 const target = mod.getTarget();
3425334254 const ptr_bits = target.ptrBitWidth();
......@@ -34553,7 +34554,7 @@ fn isNoReturn(sema: *Sema, ref: Air.Inst.Ref) bool {
3455334554 return sema.typeOf(ref).isNoReturn(sema.mod);
3455434555}
3455534556
34556/// Avoids crashing the compiler when asking if inferred allocations are known to be a certain type.
34557/// Avoids crashing the compiler when asking if inferred allocations are known to be a certain zig type.
3455734558fn isKnownZigType(sema: *Sema, ref: Air.Inst.Ref, tag: std.builtin.TypeId) bool {
3455834559 if (Air.refToIndex(ref)) |inst| switch (sema.air_instructions.items(.tag)[inst]) {
3455934560 .inferred_alloc, .inferred_alloc_comptime => return false,
src/TypedValue.zig+8-4
......@@ -302,10 +302,14 @@ fn printAggregate(
302302 var i: u32 = 0;
303303 while (i < max_len) : (i += 1) {
304304 if (i != 0) try writer.writeAll(", ");
305 switch (mod.intern_pool.indexToKey(ty.toIntern())) {
306 .struct_type, .anon_struct_type => try writer.print(".{s} = ", .{ty.structFieldName(i, mod)}),
307 else => {},
308 }
305 if (switch (mod.intern_pool.indexToKey(ty.toIntern())) {
306 .struct_type => |struct_type| mod.structPtrUnwrap(struct_type.index).?.fields.keys()[i],
307 .anon_struct_type => |anon_struct_type| if (anon_struct_type.isTuple())
308 null
309 else
310 mod.intern_pool.stringToSlice(anon_struct_type.names[i]),
311 else => unreachable,
312 }) |field_name| try writer.print(".{s} = ", .{field_name});
309313 try print(.{
310314 .ty = ty.structFieldType(i, mod),
311315 .val = try val.fieldValue(mod, i),
src/arch/aarch64/CodeGen.zig+1-1
......@@ -4527,7 +4527,7 @@ fn cmp(
45274527 }
45284528 },
45294529 .Float => return self.fail("TODO ARM cmp floats", .{}),
4530 .Enum => try lhs_ty.intTagType(mod),
4530 .Enum => lhs_ty.intTagType(mod),
45314531 .Int => lhs_ty,
45324532 .Bool => Type.u1,
45334533 .Pointer => Type.usize,
src/arch/arm/CodeGen.zig+1-1
......@@ -4475,7 +4475,7 @@ fn cmp(
44754475 }
44764476 },
44774477 .Float => return self.fail("TODO ARM cmp floats", .{}),
4478 .Enum => try lhs_ty.intTagType(mod),
4478 .Enum => lhs_ty.intTagType(mod),
44794479 .Int => lhs_ty,
44804480 .Bool => Type.u1,
44814481 .Pointer => Type.usize,
src/arch/sparc64/CodeGen.zig+1-1
......@@ -1435,7 +1435,7 @@ fn airCmp(self: *Self, inst: Air.Inst.Index, op: math.CompareOperator) !void {
14351435
14361436 const int_ty = switch (lhs_ty.zigTypeTag(mod)) {
14371437 .Vector => unreachable, // Handled by cmp_vector.
1438 .Enum => try lhs_ty.intTagType(mod),
1438 .Enum => lhs_ty.intTagType(mod),
14391439 .Int => lhs_ty,
14401440 .Bool => Type.u1,
14411441 .Pointer => Type.usize,
src/arch/wasm/CodeGen.zig+1-1
......@@ -6883,7 +6883,7 @@ fn getTagNameFunction(func: *CodeGen, enum_ty: Type) InnerError!u32 {
68836883 return loc.index;
68846884 }
68856885
6886 const int_tag_ty = try enum_ty.intTagType(mod);
6886 const int_tag_ty = enum_ty.intTagType(mod);
68876887
68886888 if (int_tag_ty.bitSize(mod) > 64) {
68896889 return func.fail("TODO: Implement @tagName for enums with tag size larger than 64 bits", .{});
src/codegen.zig+1-1
......@@ -312,7 +312,7 @@ pub fn generateSymbol(
312312 }
313313 },
314314 .enum_tag => |enum_tag| {
315 const int_tag_ty = try typed_value.ty.intTagType(mod);
315 const int_tag_ty = typed_value.ty.intTagType(mod);
316316 switch (try generateSymbol(bin_file, src_loc, .{
317317 .ty = int_tag_ty,
318318 .val = try mod.getCoerced(enum_tag.int.toValue(), int_tag_ty),
src/codegen/llvm.zig+3-5
......@@ -2773,7 +2773,7 @@ pub const DeclGen = struct {
27732773 return dg.context.intType(info.bits);
27742774 },
27752775 .Enum => {
2776 const int_ty = try t.intTagType(mod);
2776 const int_ty = t.intTagType(mod);
27772777 const bit_count = int_ty.intInfo(mod).bits;
27782778 assert(bit_count != 0);
27792779 return dg.context.intType(bit_count);
......@@ -4148,9 +4148,7 @@ pub const DeclGen = struct {
41484148 const mod = dg.module;
41494149 const int_ty = switch (ty.zigTypeTag(mod)) {
41504150 .Int => ty,
4151 .Enum => ty.intTagType(mod) catch |err| switch (err) {
4152 error.OutOfMemory => @panic("OOM"),
4153 },
4151 .Enum => ty.intTagType(mod),
41544152 .Float => {
41554153 if (!is_rmw_xchg) return null;
41564154 return dg.context.intType(@intCast(c_uint, ty.abiSize(mod) * 8));
......@@ -5100,7 +5098,7 @@ pub const FuncGen = struct {
51005098 const mod = self.dg.module;
51015099 const scalar_ty = operand_ty.scalarType(mod);
51025100 const int_ty = switch (scalar_ty.zigTypeTag(mod)) {
5103 .Enum => try scalar_ty.intTagType(mod),
5101 .Enum => scalar_ty.intTagType(mod),
51045102 .Int, .Bool, .Pointer, .ErrorSet => scalar_ty,
51055103 .Optional => blk: {
51065104 const payload_ty = operand_ty.optionalChild(mod);
src/codegen/spirv.zig+3-3
......@@ -700,7 +700,7 @@ pub const DeclGen = struct {
700700 .enum_tag => {
701701 const int_val = try val.enumToInt(ty, mod);
702702
703 const int_ty = try ty.intTagType(mod);
703 const int_ty = ty.intTagType(mod);
704704
705705 try self.lower(int_ty, int_val);
706706 },
......@@ -1156,7 +1156,7 @@ pub const DeclGen = struct {
11561156 return try self.intType(int_info.signedness, int_info.bits);
11571157 },
11581158 .Enum => {
1159 const tag_ty = try ty.intTagType(mod);
1159 const tag_ty = ty.intTagType(mod);
11601160 return self.resolveType(tag_ty, repr);
11611161 },
11621162 .Float => {
......@@ -3053,7 +3053,7 @@ pub const DeclGen = struct {
30533053 break :blk if (backing_bits <= 32) @as(u32, 1) else 2;
30543054 },
30553055 .Enum => blk: {
3056 const int_ty = try cond_ty.intTagType(mod);
3056 const int_ty = cond_ty.intTagType(mod);
30573057 const int_info = int_ty.intInfo(mod);
30583058 const backing_bits = self.backingIntBits(int_info.bits) orelse {
30593059 return self.todo("implement composite int switch", .{});
src/type.zig+28-26
......@@ -1842,17 +1842,15 @@ pub const Type = struct {
18421842 /// See also `isPtrLikeOptional`.
18431843 pub fn optionalReprIsPayload(ty: Type, mod: *const Module) bool {
18441844 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
1845 .opt_type => |child| switch (child.toType().zigTypeTag(mod)) {
1846 .Pointer => {
1847 const info = child.toType().ptrInfo(mod);
1848 return switch (info.size) {
1849 .C => false,
1850 else => !info.@"allowzero",
1851 };
1845 .opt_type => |child_type| switch (mod.intern_pool.indexToKey(child_type)) {
1846 .ptr_type => |ptr_type| switch (ptr_type.size) {
1847 .C => false,
1848 .Slice, .Many, .One => !ptr_type.is_allowzero,
18521849 },
1853 .ErrorSet => true,
1850 .error_set_type => true,
18541851 else => false,
18551852 },
1853 .ptr_type => |ptr_type| ptr_type.size == .C,
18561854 else => false,
18571855 };
18581856 }
......@@ -2570,23 +2568,27 @@ pub const Type = struct {
25702568
25712569 return null;
25722570 },
2573 .auto, .explicit => switch (enum_type.names.len) {
2574 0 => return Value.@"unreachable",
2575 1 => {
2576 if (enum_type.values.len == 0) {
2577 const only = try mod.intern(.{ .enum_tag = .{
2578 .ty = ty.toIntern(),
2579 .int = try mod.intern(.{ .int = .{
2580 .ty = enum_type.tag_ty,
2581 .storage = .{ .u64 = 0 },
2582 } }),
2583 } });
2584 return only.toValue();
2585 } else {
2586 return enum_type.values[0].toValue();
2587 }
2588 },
2589 else => return null,
2571 .auto, .explicit => {
2572 if (enum_type.tag_ty.toType().hasRuntimeBits(mod)) return null;
2573
2574 switch (enum_type.names.len) {
2575 0 => return Value.@"unreachable",
2576 1 => {
2577 if (enum_type.values.len == 0) {
2578 const only = try mod.intern(.{ .enum_tag = .{
2579 .ty = ty.toIntern(),
2580 .int = try mod.intern(.{ .int = .{
2581 .ty = enum_type.tag_ty,
2582 .storage = .{ .u64 = 0 },
2583 } }),
2584 } });
2585 return only.toValue();
2586 } else {
2587 return enum_type.values[0].toValue();
2588 }
2589 },
2590 else => return null,
2591 }
25902592 },
25912593 },
25922594
......@@ -2887,7 +2889,7 @@ pub const Type = struct {
28872889 }
28882890
28892891 /// Asserts the type is an enum or a union.
2890 pub fn intTagType(ty: Type, mod: *Module) !Type {
2892 pub fn intTagType(ty: Type, mod: *Module) Type {
28912893 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
28922894 .union_type => |union_type| mod.unionPtr(union_type.index).tag_ty.intTagType(mod),
28932895 .enum_type => |enum_type| enum_type.tag_ty.toType(),
src/value.zig+21-12
......@@ -673,7 +673,7 @@ pub const Value = struct {
673673 while (true) switch (mod.intern_pool.indexToKey(check.toIntern())) {
674674 .ptr => |ptr| switch (ptr.addr) {
675675 .decl, .mut_decl, .comptime_field => return true,
676 .eu_payload, .opt_payload => |index| check = index.toValue(),
676 .eu_payload, .opt_payload => |base| check = base.toValue(),
677677 .elem, .field => |base_index| check = base_index.base.toValue(),
678678 else => return false,
679679 },
......@@ -943,22 +943,27 @@ pub const Value = struct {
943943 return Value.true;
944944 }
945945 },
946 .Int, .Enum => {
947 const int_info = ty.intInfo(mod);
946 .Int, .Enum => |ty_tag| {
947 const int_ty = switch (ty_tag) {
948 .Int => ty,
949 .Enum => ty.intTagType(mod),
950 else => unreachable,
951 };
952 const int_info = int_ty.intInfo(mod);
948953 const bits = int_info.bits;
949954 const byte_count = (bits + 7) / 8;
950 if (bits == 0 or buffer.len == 0) return mod.intValue(ty, 0);
955 if (bits == 0 or buffer.len == 0) return mod.getCoerced(try mod.intValue(int_ty, 0), ty);
951956
952957 if (bits <= 64) switch (int_info.signedness) { // Fast path for integers <= u64
953958 .signed => {
954959 const val = std.mem.readVarInt(i64, buffer[0..byte_count], endian);
955960 const result = (val << @intCast(u6, 64 - bits)) >> @intCast(u6, 64 - bits);
956 return mod.intValue(ty, result);
961 return mod.getCoerced(try mod.intValue(int_ty, result), ty);
957962 },
958963 .unsigned => {
959964 const val = std.mem.readVarInt(u64, buffer[0..byte_count], endian);
960965 const result = (val << @intCast(u6, 64 - bits)) >> @intCast(u6, 64 - bits);
961 return mod.intValue(ty, result);
966 return mod.getCoerced(try mod.intValue(int_ty, result), ty);
962967 },
963968 } else { // Slow path, we have to construct a big-int
964969 const Limb = std.math.big.Limb;
......@@ -967,7 +972,7 @@ pub const Value = struct {
967972
968973 var bigint = BigIntMutable.init(limbs_buffer, 0);
969974 bigint.readTwosComplement(buffer[0..byte_count], bits, endian, int_info.signedness);
970 return mod.intValue_big(ty, bigint.toConst());
975 return mod.getCoerced(try mod.intValue_big(int_ty, bigint.toConst()), ty);
971976 }
972977 },
973978 .Float => return (try mod.intern(.{ .float = .{
......@@ -1583,7 +1588,7 @@ pub const Value = struct {
15831588 .Enum => {
15841589 const a_val = try a.enumToInt(ty, mod);
15851590 const b_val = try b.enumToInt(ty, mod);
1586 const int_ty = try ty.intTagType(mod);
1591 const int_ty = ty.intTagType(mod);
15871592 return eqlAdvanced(a_val, int_ty, b_val, int_ty, mod, opt_sema);
15881593 },
15891594 .Array, .Vector => {
......@@ -1835,7 +1840,8 @@ pub const Value = struct {
18351840 })).toValue(),
18361841 .ptr => |ptr| switch (ptr.addr) {
18371842 .decl => |decl| mod.declPtr(decl).val.elemValue(mod, index),
1838 .mut_decl => |mut_decl| mod.declPtr(mut_decl.decl).val.elemValue(mod, index),
1843 .mut_decl => |mut_decl| (try mod.declPtr(mut_decl.decl).internValue(mod))
1844 .toValue().elemValue(mod, index),
18391845 .int, .eu_payload, .opt_payload => unreachable,
18401846 .comptime_field => |field_val| field_val.toValue().elemValue(mod, index),
18411847 .elem => |elem| elem.base.toValue().elemValue(mod, index + elem.index),
......@@ -1946,9 +1952,12 @@ pub const Value = struct {
19461952 return switch (mod.intern_pool.indexToKey(val.toIntern())) {
19471953 .ptr => |ptr| switch (ptr.addr) {
19481954 .decl => |decl| try mod.declPtr(decl).val.sliceArray(mod, arena, start, end),
1949 .mut_decl => |mut_decl| try mod.declPtr(mut_decl.decl).val.sliceArray(mod, arena, start, end),
1950 .comptime_field => |comptime_field| try comptime_field.toValue().sliceArray(mod, arena, start, end),
1951 .elem => |elem| try elem.base.toValue().sliceArray(mod, arena, start + elem.index, end + elem.index),
1955 .mut_decl => |mut_decl| (try mod.declPtr(mut_decl.decl).internValue(mod)).toValue()
1956 .sliceArray(mod, arena, start, end),
1957 .comptime_field => |comptime_field| comptime_field.toValue()
1958 .sliceArray(mod, arena, start, end),
1959 .elem => |elem| elem.base.toValue()
1960 .sliceArray(mod, arena, start + elem.index, end + elem.index),
19521961 else => unreachable,
19531962 },
19541963 .aggregate => |aggregate| (try mod.intern(.{ .aggregate = .{