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(
1625016250 return Air.internedToRef((try pt.aggregateValue(src_loc_ty, &fields)).toIntern());
1625116251}
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.
1625816253fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
1625916254 const pt = sema.pt;
1626016255 const zcu = pt.zcu;
......@@ -16432,12 +16427,17 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1643216427 },
1643316428 .pointer => {
1643416429 const info = ty.ptrInfo(zcu);
16435 const alignment_val = try pt.intValue(.comptime_int, bytes: {
16436 if (info.flags.alignment.toByteUnits()) |b| break :bytes b;
16437 const elem_ty: Type = .fromInterned(info.child);
16438 try sema.ensureLayoutResolved(elem_ty, src, .type_info);
16439 break :bytes elem_ty.abiAlignment(zcu).toByteUnits().?;
16440 });
16430 const alignment_ty = try pt.optionalType(.usize_type);
16431 const alignment_val: Value = val: {
16432 const bytes = info.flags.alignment.toByteUnits() orelse {
16433 break :val try pt.nullValue(alignment_ty);
16434 };
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
1644216442 const addrspace_ty = try sema.getBuiltinType(src, .AddressSpace);
1644316443 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
1645016450 Value.makeBool(info.flags.is_const).toIntern(),
1645116451 // is_volatile: bool,
1645216452 Value.makeBool(info.flags.is_volatile).toIntern(),
16453 // alignment: comptime_int,
16453 // alignment: ?usize,
1645416454 alignment_val.toIntern(),
1645516455 // address_space: AddressSpace
1645616456 (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
1676616766
1676716767 const field_ty: Type = .fromInterned(union_obj.field_types.get(ip)[field_index]);
1676816768
16769 const alignment = switch (layout) {
16770 .auto, .@"extern" => switch (ty.explicitFieldAlignment(field_index, zcu)) {
16771 .none => field_ty.abiAlignment(zcu),
16772 else => |a| a,
16773 },
16774 .@"packed" => .none,
16769 const alignment_ty = try pt.optionalType(.usize_type);
16770 const alignment_val: Value = val: {
16771 const a: Alignment = switch (layout) {
16772 .auto, .@"extern" => ty.explicitFieldAlignment(field_index, zcu),
16773 .@"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 } }));
1677516783 };
1677616784
1677716785 const union_field_fields = .{
......@@ -16779,8 +16787,8 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1677916787 name_val,
1678016788 // type: type,
1678116789 field_ty.toIntern(),
16782 // alignment: comptime_int,
16783 (try pt.intValue(.comptime_int, alignment.toByteUnits() orelse 0)).toIntern(),
16790 // alignment: ?usize,
16791 alignment_val.toIntern(),
1678416792 };
1678516793 field_val.* = (try pt.aggregateValue(union_field_ty, &union_field_fields)).toIntern();
1678616794 }
......@@ -16881,6 +16889,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1688116889 const is_comptime = field_val != .none;
1688216890 const opt_default_val = if (is_comptime) Value.fromInterned(field_val) else null;
1688316891 const default_val_ptr = try sema.optRefValue(opt_default_val);
16892
1688416893 const struct_field_fields = .{
1688516894 // name: [:0]const u8,
1688616895 name_val,
......@@ -16890,8 +16899,8 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1689016899 default_val_ptr.toIntern(),
1689116900 // is_comptime: bool,
1689216901 Value.makeBool(is_comptime).toIntern(),
16893 // alignment: comptime_int,
16894 (try pt.intValue(.comptime_int, Type.fromInterned(field_ty).abiAlignment(zcu).toByteUnits() orelse 0)).toIntern(),
16902 // alignment: ?usize,
16903 (try pt.nullValue(try pt.optionalType(.usize_type))).toIntern(),
1689516904 };
1689616905 struct_field_val.* = (try pt.aggregateValue(struct_field_ty, &struct_field_fields)).toIntern();
1689716906 }
......@@ -16937,12 +16946,21 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1693716946
1693816947 const opt_default_val: ?Value = if (field_default == .none) null else .fromInterned(field_default);
1693916948 const default_val_ptr = try sema.optRefValue(opt_default_val);
16940 const alignment = switch (struct_type.layout) {
16941 .auto, .@"extern" => switch (ty.explicitFieldAlignment(field_index, zcu)) {
16942 .none => field_ty.defaultStructFieldAlignment(struct_type.layout, zcu),
16943 else => |a| a,
16944 },
16945 .@"packed" => .none,
16949
16950 const alignment_ty = try pt.optionalType(.usize_type);
16951 const alignment_val: Value = val: {
16952 const a: Alignment = switch (struct_type.layout) {
16953 .auto, .@"extern" => ty.explicitFieldAlignment(field_index, zcu),
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 } }));
1694616964 };
1694716965
1694816966 const struct_field_fields = .{
......@@ -16954,8 +16972,8 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1695416972 default_val_ptr.toIntern(),
1695516973 // is_comptime: bool,
1695616974 Value.makeBool(field_is_comptime).toIntern(),
16957 // alignment: comptime_int,
16958 (try pt.intValue(.comptime_int, alignment.toByteUnits() orelse 0)).toIntern(),
16975 // alignment: ?usize,
16976 alignment_val.toIntern(),
1695916977 };
1696016978 field_val.* = (try pt.aggregateValue(struct_field_ty, &struct_field_fields)).toIntern();
1696116979 }
......@@ -27434,7 +27452,7 @@ fn coerceExtra(
2743427452 const array_elem_ty = array_ty.childType(zcu);
2743527453 if (array_ty.arrayLen(zcu) != 1) break :single_item;
2743627454 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)) {
2743827456 .ok => {},
2743927457 else => break :single_item,
2744027458 }
......@@ -27452,7 +27470,7 @@ fn coerceExtra(
2745227470 const dest_is_mut = !dest_info.flags.is_const;
2745327471
2745427472 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);
2745627474 switch (elem_res) {
2745727475 .ok => {},
2745827476 else => {
......@@ -27513,7 +27531,7 @@ fn coerceExtra(
2751327531 const src_elem_ty = inst_ty.childType(zcu);
2751427532 const dest_is_mut = !dest_info.flags.is_const;
2751527533 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)) {
2751727535 .ok => {},
2751827536 else => break :src_c_ptr,
2751927537 }
......@@ -27584,7 +27602,7 @@ fn coerceExtra(
2758427602 target,
2758527603 dest_ty_src,
2758627604 inst_src,
27587 maybe_inst_val,
27605 null,
2758827606 )) {
2758927607 .ok => {},
2759027608 else => break :p,
......@@ -27655,7 +27673,7 @@ fn coerceExtra(
2765527673 target,
2765627674 dest_ty_src,
2765727675 inst_src,
27658 maybe_inst_val,
27676 null,
2765927677 )) {
2766027678 .ok => {},
2766127679 else => break :p,
......@@ -27861,7 +27879,7 @@ fn coerceExtra(
2786127879 target,
2786227880 dest_ty_src,
2786327881 inst_src,
27864 maybe_inst_val,
27882 null,
2786527883 )) {
2786627884 break :array_to_array;
2786727885 }
......@@ -27940,7 +27958,7 @@ fn coerceExtra(
2794027958
2794127959 // E!T to T
2794227960 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)
2794427962 {
2794527963 try sema.errNote(inst_src, msg, "cannot convert error union to payload type", .{});
2794627964 try sema.errNote(inst_src, msg, "consider using 'try', 'catch', or 'if'", .{});
......@@ -27948,7 +27966,7 @@ fn coerceExtra(
2794827966
2794927967 // ?T to T
2795027968 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)
2795227970 {
2795327971 try sema.errNote(inst_src, msg, "cannot convert optional to payload type", .{});
2795427972 try sema.errNote(inst_src, msg, "consider using '.?', 'orelse', or 'if'", .{});
......@@ -28395,6 +28413,10 @@ pub fn coerceInMemoryAllowed(
2839528413 const pt = sema.pt;
2839628414 const zcu = pt.zcu;
2839728415
28416 if (src_val) |val| {
28417 assert(val.typeOf(zcu).toIntern() == src_ty.toIntern());
28418 }
28419
2839828420 if (dest_ty.eql(src_ty, zcu))
2839928421 return .ok;
2840028422