authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-03-04 16:31:39+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-03-10 10:26:15+00:00
log4a310ce7c13c9aa485200a2b712562ba20854272
treee52bc1325d10ade706c9f70547c3a24dd255473f
parenta5219cd288c322f5f5bda89d608af5b243e9fd6f
signaturelock-open Commit is signed but in an unrecognized format.

Sema: expect 'alignment' fields in 'std.builtin.Type' to be '?usize'

This is a language change which works nicely alongside some of the other changes in this branch. It will help to resolve the remaining couple of failures in the std and behavior tests. The actual std.builtin change is not in this commit, because we must update zig1.wasm first.

1 files changed, 60 insertions(+), 38 deletions(-)

src/Sema.zig+60-38
...@@ -16250,11 +16250,6 @@ fn zirBuiltinSrc(...@@ -16250,11 +16250,6 @@ fn zirBuiltinSrc(
16250 return Air.internedToRef((try pt.aggregateValue(src_loc_ty, &fields)).toIntern());16250 return Air.internedToRef((try pt.aggregateValue(src_loc_ty, &fields)).toIntern());
16251}16251}
1625216252
16253/// MLUGG TODO: once this branch is in a more stable state, I need to make a language change so that
16254/// `std.builtin.Type` makes all `alignment` fields `?usize` instead of `comptime_int`, to prevent
16255/// explicit alignment annotations from sneaking in without the user requesting any; but doing that
16256/// right now would be really annoying because it would break the base compiler. I need to have the
16257/// compiler more-or-less fully migrated first.
16258fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {16253fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
16259 const pt = sema.pt;16254 const pt = sema.pt;
16260 const zcu = pt.zcu;16255 const zcu = pt.zcu;
...@@ -16432,12 +16427,17 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -16432,12 +16427,17 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
16432 },16427 },
16433 .pointer => {16428 .pointer => {
16434 const info = ty.ptrInfo(zcu);16429 const info = ty.ptrInfo(zcu);
16435 const alignment_val = try pt.intValue(.comptime_int, bytes: {16430 const alignment_ty = try pt.optionalType(.usize_type);
16436 if (info.flags.alignment.toByteUnits()) |b| break :bytes b;16431 const alignment_val: Value = val: {
16437 const elem_ty: Type = .fromInterned(info.child);16432 const bytes = info.flags.alignment.toByteUnits() orelse {
16438 try sema.ensureLayoutResolved(elem_ty, src, .type_info);16433 break :val try pt.nullValue(alignment_ty);
16439 break :bytes elem_ty.abiAlignment(zcu).toByteUnits().?;16434 };
16440 });16435 const int_val = try pt.intValue(.usize, bytes);
16436 break :val .fromInterned(try pt.intern(.{ .opt = .{
16437 .ty = alignment_ty.toIntern(),
16438 .val = int_val.toIntern(),
16439 } }));
16440 };
1644116441
16442 const addrspace_ty = try sema.getBuiltinType(src, .AddressSpace);16442 const addrspace_ty = try sema.getBuiltinType(src, .AddressSpace);
16443 const pointer_ty = try sema.getBuiltinType(src, .@"Type.Pointer");16443 const pointer_ty = try sema.getBuiltinType(src, .@"Type.Pointer");
...@@ -16450,7 +16450,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -16450,7 +16450,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
16450 Value.makeBool(info.flags.is_const).toIntern(),16450 Value.makeBool(info.flags.is_const).toIntern(),
16451 // is_volatile: bool,16451 // is_volatile: bool,
16452 Value.makeBool(info.flags.is_volatile).toIntern(),16452 Value.makeBool(info.flags.is_volatile).toIntern(),
16453 // alignment: comptime_int,16453 // alignment: ?usize,
16454 alignment_val.toIntern(),16454 alignment_val.toIntern(),
16455 // address_space: AddressSpace16455 // address_space: AddressSpace
16456 (try pt.enumValueFieldIndex(addrspace_ty, @intFromEnum(info.flags.address_space))).toIntern(),16456 (try pt.enumValueFieldIndex(addrspace_ty, @intFromEnum(info.flags.address_space))).toIntern(),
...@@ -16766,12 +16766,20 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -16766,12 +16766,20 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1676616766
16767 const field_ty: Type = .fromInterned(union_obj.field_types.get(ip)[field_index]);16767 const field_ty: Type = .fromInterned(union_obj.field_types.get(ip)[field_index]);
1676816768
16769 const alignment = switch (layout) {16769 const alignment_ty = try pt.optionalType(.usize_type);
16770 .auto, .@"extern" => switch (ty.explicitFieldAlignment(field_index, zcu)) {16770 const alignment_val: Value = val: {
16771 .none => field_ty.abiAlignment(zcu),16771 const a: Alignment = switch (layout) {
16772 else => |a| a,16772 .auto, .@"extern" => ty.explicitFieldAlignment(field_index, zcu),
16773 },16773 .@"packed" => .none,
16774 .@"packed" => .none,16774 };
16775 const bytes = a.toByteUnits() orelse {
16776 break :val try pt.nullValue(alignment_ty);
16777 };
16778 const int_val = try pt.intValue(.usize, bytes);
16779 break :val .fromInterned(try pt.intern(.{ .opt = .{
16780 .ty = alignment_ty.toIntern(),
16781 .val = int_val.toIntern(),
16782 } }));
16775 };16783 };
1677616784
16777 const union_field_fields = .{16785 const union_field_fields = .{
...@@ -16779,8 +16787,8 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -16779,8 +16787,8 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
16779 name_val,16787 name_val,
16780 // type: type,16788 // type: type,
16781 field_ty.toIntern(),16789 field_ty.toIntern(),
16782 // alignment: comptime_int,16790 // alignment: ?usize,
16783 (try pt.intValue(.comptime_int, alignment.toByteUnits() orelse 0)).toIntern(),16791 alignment_val.toIntern(),
16784 };16792 };
16785 field_val.* = (try pt.aggregateValue(union_field_ty, &union_field_fields)).toIntern();16793 field_val.* = (try pt.aggregateValue(union_field_ty, &union_field_fields)).toIntern();
16786 }16794 }
...@@ -16881,6 +16889,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -16881,6 +16889,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
16881 const is_comptime = field_val != .none;16889 const is_comptime = field_val != .none;
16882 const opt_default_val = if (is_comptime) Value.fromInterned(field_val) else null;16890 const opt_default_val = if (is_comptime) Value.fromInterned(field_val) else null;
16883 const default_val_ptr = try sema.optRefValue(opt_default_val);16891 const default_val_ptr = try sema.optRefValue(opt_default_val);
16892
16884 const struct_field_fields = .{16893 const struct_field_fields = .{
16885 // name: [:0]const u8,16894 // name: [:0]const u8,
16886 name_val,16895 name_val,
...@@ -16890,8 +16899,8 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -16890,8 +16899,8 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
16890 default_val_ptr.toIntern(),16899 default_val_ptr.toIntern(),
16891 // is_comptime: bool,16900 // is_comptime: bool,
16892 Value.makeBool(is_comptime).toIntern(),16901 Value.makeBool(is_comptime).toIntern(),
16893 // alignment: comptime_int,16902 // alignment: ?usize,
16894 (try pt.intValue(.comptime_int, Type.fromInterned(field_ty).abiAlignment(zcu).toByteUnits() orelse 0)).toIntern(),16903 (try pt.nullValue(try pt.optionalType(.usize_type))).toIntern(),
16895 };16904 };
16896 struct_field_val.* = (try pt.aggregateValue(struct_field_ty, &struct_field_fields)).toIntern();16905 struct_field_val.* = (try pt.aggregateValue(struct_field_ty, &struct_field_fields)).toIntern();
16897 }16906 }
...@@ -16937,12 +16946,21 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -16937,12 +16946,21 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1693716946
16938 const opt_default_val: ?Value = if (field_default == .none) null else .fromInterned(field_default);16947 const opt_default_val: ?Value = if (field_default == .none) null else .fromInterned(field_default);
16939 const default_val_ptr = try sema.optRefValue(opt_default_val);16948 const default_val_ptr = try sema.optRefValue(opt_default_val);
16940 const alignment = switch (struct_type.layout) {16949
16941 .auto, .@"extern" => switch (ty.explicitFieldAlignment(field_index, zcu)) {16950 const alignment_ty = try pt.optionalType(.usize_type);
16942 .none => field_ty.defaultStructFieldAlignment(struct_type.layout, zcu),16951 const alignment_val: Value = val: {
16943 else => |a| a,16952 const a: Alignment = switch (struct_type.layout) {
16944 },16953 .auto, .@"extern" => ty.explicitFieldAlignment(field_index, zcu),
16945 .@"packed" => .none,16954 .@"packed" => .none,
16955 };
16956 const bytes = a.toByteUnits() orelse {
16957 break :val try pt.nullValue(alignment_ty);
16958 };
16959 const int_val = try pt.intValue(.usize, bytes);
16960 break :val .fromInterned(try pt.intern(.{ .opt = .{
16961 .ty = alignment_ty.toIntern(),
16962 .val = int_val.toIntern(),
16963 } }));
16946 };16964 };
1694716965
16948 const struct_field_fields = .{16966 const struct_field_fields = .{
...@@ -16954,8 +16972,8 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -16954,8 +16972,8 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
16954 default_val_ptr.toIntern(),16972 default_val_ptr.toIntern(),
16955 // is_comptime: bool,16973 // is_comptime: bool,
16956 Value.makeBool(field_is_comptime).toIntern(),16974 Value.makeBool(field_is_comptime).toIntern(),
16957 // alignment: comptime_int,16975 // alignment: ?usize,
16958 (try pt.intValue(.comptime_int, alignment.toByteUnits() orelse 0)).toIntern(),16976 alignment_val.toIntern(),
16959 };16977 };
16960 field_val.* = (try pt.aggregateValue(struct_field_ty, &struct_field_fields)).toIntern();16978 field_val.* = (try pt.aggregateValue(struct_field_ty, &struct_field_fields)).toIntern();
16961 }16979 }
...@@ -27434,7 +27452,7 @@ fn coerceExtra(...@@ -27434,7 +27452,7 @@ fn coerceExtra(
27434 const array_elem_ty = array_ty.childType(zcu);27452 const array_elem_ty = array_ty.childType(zcu);
27435 if (array_ty.arrayLen(zcu) != 1) break :single_item;27453 if (array_ty.arrayLen(zcu) != 1) break :single_item;
27436 const dest_is_mut = !dest_info.flags.is_const;27454 const dest_is_mut = !dest_info.flags.is_const;
27437 switch (try sema.coerceInMemoryAllowed(block, array_elem_ty, ptr_elem_ty, dest_is_mut, target, dest_ty_src, inst_src, maybe_inst_val)) {27455 switch (try sema.coerceInMemoryAllowed(block, array_elem_ty, ptr_elem_ty, dest_is_mut, target, dest_ty_src, inst_src, null)) {
27438 .ok => {},27456 .ok => {},
27439 else => break :single_item,27457 else => break :single_item,
27440 }27458 }
...@@ -27452,7 +27470,7 @@ fn coerceExtra(...@@ -27452,7 +27470,7 @@ fn coerceExtra(
27452 const dest_is_mut = !dest_info.flags.is_const;27470 const dest_is_mut = !dest_info.flags.is_const;
2745327471
27454 const dst_elem_type: Type = .fromInterned(dest_info.child);27472 const dst_elem_type: Type = .fromInterned(dest_info.child);
27455 const elem_res = try sema.coerceInMemoryAllowed(block, dst_elem_type, array_elem_type, dest_is_mut, target, dest_ty_src, inst_src, maybe_inst_val);27473 const elem_res = try sema.coerceInMemoryAllowed(block, dst_elem_type, array_elem_type, dest_is_mut, target, dest_ty_src, inst_src, null);
27456 switch (elem_res) {27474 switch (elem_res) {
27457 .ok => {},27475 .ok => {},
27458 else => {27476 else => {
...@@ -27513,7 +27531,7 @@ fn coerceExtra(...@@ -27513,7 +27531,7 @@ fn coerceExtra(
27513 const src_elem_ty = inst_ty.childType(zcu);27531 const src_elem_ty = inst_ty.childType(zcu);
27514 const dest_is_mut = !dest_info.flags.is_const;27532 const dest_is_mut = !dest_info.flags.is_const;
27515 const dst_elem_type: Type = .fromInterned(dest_info.child);27533 const dst_elem_type: Type = .fromInterned(dest_info.child);
27516 switch (try sema.coerceInMemoryAllowed(block, dst_elem_type, src_elem_ty, dest_is_mut, target, dest_ty_src, inst_src, maybe_inst_val)) {27534 switch (try sema.coerceInMemoryAllowed(block, dst_elem_type, src_elem_ty, dest_is_mut, target, dest_ty_src, inst_src, null)) {
27517 .ok => {},27535 .ok => {},
27518 else => break :src_c_ptr,27536 else => break :src_c_ptr,
27519 }27537 }
...@@ -27584,7 +27602,7 @@ fn coerceExtra(...@@ -27584,7 +27602,7 @@ fn coerceExtra(
27584 target,27602 target,
27585 dest_ty_src,27603 dest_ty_src,
27586 inst_src,27604 inst_src,
27587 maybe_inst_val,27605 null,
27588 )) {27606 )) {
27589 .ok => {},27607 .ok => {},
27590 else => break :p,27608 else => break :p,
...@@ -27655,7 +27673,7 @@ fn coerceExtra(...@@ -27655,7 +27673,7 @@ fn coerceExtra(
27655 target,27673 target,
27656 dest_ty_src,27674 dest_ty_src,
27657 inst_src,27675 inst_src,
27658 maybe_inst_val,27676 null,
27659 )) {27677 )) {
27660 .ok => {},27678 .ok => {},
27661 else => break :p,27679 else => break :p,
...@@ -27861,7 +27879,7 @@ fn coerceExtra(...@@ -27861,7 +27879,7 @@ fn coerceExtra(
27861 target,27879 target,
27862 dest_ty_src,27880 dest_ty_src,
27863 inst_src,27881 inst_src,
27864 maybe_inst_val,27882 null,
27865 )) {27883 )) {
27866 break :array_to_array;27884 break :array_to_array;
27867 }27885 }
...@@ -27940,7 +27958,7 @@ fn coerceExtra(...@@ -27940,7 +27958,7 @@ fn coerceExtra(
2794027958
27941 // E!T to T27959 // E!T to T
27942 if (inst_ty.zigTypeTag(zcu) == .error_union and27960 if (inst_ty.zigTypeTag(zcu) == .error_union and
27943 (try sema.coerceInMemoryAllowed(block, inst_ty.errorUnionPayload(zcu), dest_ty, false, target, dest_ty_src, inst_src, maybe_inst_val)) == .ok)27961 (try sema.coerceInMemoryAllowed(block, inst_ty.errorUnionPayload(zcu), dest_ty, false, target, dest_ty_src, inst_src, null)) == .ok)
27944 {27962 {
27945 try sema.errNote(inst_src, msg, "cannot convert error union to payload type", .{});27963 try sema.errNote(inst_src, msg, "cannot convert error union to payload type", .{});
27946 try sema.errNote(inst_src, msg, "consider using 'try', 'catch', or 'if'", .{});27964 try sema.errNote(inst_src, msg, "consider using 'try', 'catch', or 'if'", .{});
...@@ -27948,7 +27966,7 @@ fn coerceExtra(...@@ -27948,7 +27966,7 @@ fn coerceExtra(
2794827966
27949 // ?T to T27967 // ?T to T
27950 if (inst_ty.zigTypeTag(zcu) == .optional and27968 if (inst_ty.zigTypeTag(zcu) == .optional and
27951 (try sema.coerceInMemoryAllowed(block, inst_ty.optionalChild(zcu), dest_ty, false, target, dest_ty_src, inst_src, maybe_inst_val)) == .ok)27969 (try sema.coerceInMemoryAllowed(block, inst_ty.optionalChild(zcu), dest_ty, false, target, dest_ty_src, inst_src, null)) == .ok)
27952 {27970 {
27953 try sema.errNote(inst_src, msg, "cannot convert optional to payload type", .{});27971 try sema.errNote(inst_src, msg, "cannot convert optional to payload type", .{});
27954 try sema.errNote(inst_src, msg, "consider using '.?', 'orelse', or 'if'", .{});27972 try sema.errNote(inst_src, msg, "consider using '.?', 'orelse', or 'if'", .{});
...@@ -28395,6 +28413,10 @@ pub fn coerceInMemoryAllowed(...@@ -28395,6 +28413,10 @@ pub fn coerceInMemoryAllowed(
28395 const pt = sema.pt;28413 const pt = sema.pt;
28396 const zcu = pt.zcu;28414 const zcu = pt.zcu;
2839728415
28416 if (src_val) |val| {
28417 assert(val.typeOf(zcu).toIntern() == src_ty.toIntern());
28418 }
28419
28398 if (dest_ty.eql(src_ty, zcu))28420 if (dest_ty.eql(src_ty, zcu))
28399 return .ok;28421 return .ok;
2840028422