| ... | ... | @@ -16250,11 +16250,6 @@ fn zirBuiltinSrc( |
| 16250 | 16250 | return Air.internedToRef((try pt.aggregateValue(src_loc_ty, &fields)).toIntern()); |
| 16251 | 16251 | } |
| 16252 | 16252 | |
| 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. |
| 16258 | 16253 | fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 16259 | 16254 | const pt = sema.pt; |
| 16260 | 16255 | const zcu = pt.zcu; |
| ... | ... | @@ -16432,12 +16427,17 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 16432 | 16427 | }, |
| 16433 | 16428 | .pointer => { |
| 16434 | 16429 | 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 | }; |
| 16441 | 16441 | |
| 16442 | 16442 | const addrspace_ty = try sema.getBuiltinType(src, .AddressSpace); |
| 16443 | 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 | 16450 | Value.makeBool(info.flags.is_const).toIntern(), |
| 16451 | 16451 | // is_volatile: bool, |
| 16452 | 16452 | Value.makeBool(info.flags.is_volatile).toIntern(), |
| 16453 | | // alignment: comptime_int, |
| 16453 | // alignment: ?usize, |
| 16454 | 16454 | alignment_val.toIntern(), |
| 16455 | 16455 | // address_space: AddressSpace |
| 16456 | 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 | 16766 | |
| 16767 | 16767 | const field_ty: Type = .fromInterned(union_obj.field_types.get(ip)[field_index]); |
| 16768 | 16768 | |
| 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 | } })); |
| 16775 | 16783 | }; |
| 16776 | 16784 | |
| 16777 | 16785 | const union_field_fields = .{ |
| ... | ... | @@ -16779,8 +16787,8 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 16779 | 16787 | name_val, |
| 16780 | 16788 | // type: type, |
| 16781 | 16789 | 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(), |
| 16784 | 16792 | }; |
| 16785 | 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 | 16889 | const is_comptime = field_val != .none; |
| 16882 | 16890 | const opt_default_val = if (is_comptime) Value.fromInterned(field_val) else null; |
| 16883 | 16891 | const default_val_ptr = try sema.optRefValue(opt_default_val); |
| 16892 | |
| 16884 | 16893 | const struct_field_fields = .{ |
| 16885 | 16894 | // name: [:0]const u8, |
| 16886 | 16895 | name_val, |
| ... | ... | @@ -16890,8 +16899,8 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 16890 | 16899 | default_val_ptr.toIntern(), |
| 16891 | 16900 | // is_comptime: bool, |
| 16892 | 16901 | 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(), |
| 16895 | 16904 | }; |
| 16896 | 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 | 16946 | |
| 16938 | 16947 | const opt_default_val: ?Value = if (field_default == .none) null else .fromInterned(field_default); |
| 16939 | 16948 | 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 | } })); |
| 16946 | 16964 | }; |
| 16947 | 16965 | |
| 16948 | 16966 | const struct_field_fields = .{ |
| ... | ... | @@ -16954,8 +16972,8 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 16954 | 16972 | default_val_ptr.toIntern(), |
| 16955 | 16973 | // is_comptime: bool, |
| 16956 | 16974 | 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(), |
| 16959 | 16977 | }; |
| 16960 | 16978 | field_val.* = (try pt.aggregateValue(struct_field_ty, &struct_field_fields)).toIntern(); |
| 16961 | 16979 | } |
| ... | ... | @@ -27434,7 +27452,7 @@ fn coerceExtra( |
| 27434 | 27452 | const array_elem_ty = array_ty.childType(zcu); |
| 27435 | 27453 | if (array_ty.arrayLen(zcu) != 1) break :single_item; |
| 27436 | 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 | 27456 | .ok => {}, |
| 27439 | 27457 | else => break :single_item, |
| 27440 | 27458 | } |
| ... | ... | @@ -27452,7 +27470,7 @@ fn coerceExtra( |
| 27452 | 27470 | const dest_is_mut = !dest_info.flags.is_const; |
| 27453 | 27471 | |
| 27454 | 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 | 27474 | switch (elem_res) { |
| 27457 | 27475 | .ok => {}, |
| 27458 | 27476 | else => { |
| ... | ... | @@ -27513,7 +27531,7 @@ fn coerceExtra( |
| 27513 | 27531 | const src_elem_ty = inst_ty.childType(zcu); |
| 27514 | 27532 | const dest_is_mut = !dest_info.flags.is_const; |
| 27515 | 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 | 27535 | .ok => {}, |
| 27518 | 27536 | else => break :src_c_ptr, |
| 27519 | 27537 | } |
| ... | ... | @@ -27584,7 +27602,7 @@ fn coerceExtra( |
| 27584 | 27602 | target, |
| 27585 | 27603 | dest_ty_src, |
| 27586 | 27604 | inst_src, |
| 27587 | | maybe_inst_val, |
| 27605 | null, |
| 27588 | 27606 | )) { |
| 27589 | 27607 | .ok => {}, |
| 27590 | 27608 | else => break :p, |
| ... | ... | @@ -27655,7 +27673,7 @@ fn coerceExtra( |
| 27655 | 27673 | target, |
| 27656 | 27674 | dest_ty_src, |
| 27657 | 27675 | inst_src, |
| 27658 | | maybe_inst_val, |
| 27676 | null, |
| 27659 | 27677 | )) { |
| 27660 | 27678 | .ok => {}, |
| 27661 | 27679 | else => break :p, |
| ... | ... | @@ -27861,7 +27879,7 @@ fn coerceExtra( |
| 27861 | 27879 | target, |
| 27862 | 27880 | dest_ty_src, |
| 27863 | 27881 | inst_src, |
| 27864 | | maybe_inst_val, |
| 27882 | null, |
| 27865 | 27883 | )) { |
| 27866 | 27884 | break :array_to_array; |
| 27867 | 27885 | } |
| ... | ... | @@ -27940,7 +27958,7 @@ fn coerceExtra( |
| 27940 | 27958 | |
| 27941 | 27959 | // E!T to T |
| 27942 | 27960 | 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 | 27963 | try sema.errNote(inst_src, msg, "cannot convert error union to payload type", .{}); |
| 27946 | 27964 | try sema.errNote(inst_src, msg, "consider using 'try', 'catch', or 'if'", .{}); |
| ... | ... | @@ -27948,7 +27966,7 @@ fn coerceExtra( |
| 27948 | 27966 | |
| 27949 | 27967 | // ?T to T |
| 27950 | 27968 | 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 | 27971 | try sema.errNote(inst_src, msg, "cannot convert optional to payload type", .{}); |
| 27954 | 27972 | try sema.errNote(inst_src, msg, "consider using '.?', 'orelse', or 'if'", .{}); |
| ... | ... | @@ -28395,6 +28413,10 @@ pub fn coerceInMemoryAllowed( |
| 28395 | 28413 | const pt = sema.pt; |
| 28396 | 28414 | const zcu = pt.zcu; |
| 28397 | 28415 | |
| 28416 | if (src_val) |val| { |
| 28417 | assert(val.typeOf(zcu).toIntern() == src_ty.toIntern()); |
| 28418 | } |
| 28419 | |
| 28398 | 28420 | if (dest_ty.eql(src_ty, zcu)) |
| 28399 | 28421 | return .ok; |
| 28400 | 28422 | |