authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-05-07 22:25:50-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-10 20:42:30-07:00
log8587e510e46f98e321fbad30bb235e5eed33f1ba
tree5eb31158113d74473f3f895ec76eaaab1cc59efd
parent3116477dcc5e85d8fe7b2be2f332796e1425f956

stage2: more InternPool related fixes

* make Sema.zirPtrType coerce the sentinel value against the element type * fix lazyAbiAlignment wrong result type * typeHasOnePossibleValue no longer tries to create interned enum tag value with integer zero, instead uses enum_field_index * Type.ptr avoids trying to store typed null values into the intern pool

3 files changed, 31 insertions(+), 11 deletions(-)

src/Sema.zig+11-7
......@@ -15615,7 +15615,7 @@ fn zirSizeOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
1561515615 => {},
1561615616 }
1561715617 const val = try ty.lazyAbiSize(mod, sema.arena);
15618 if (val.ip_index == .none and val.tag() == .lazy_size) {
15618 if (val.isLazySize()) {
1561915619 try sema.queueFullTypeResolution(ty);
1562015620 }
1562115621 return sema.addConstant(Type.comptime_int, val);
......@@ -17674,6 +17674,10 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1767417674 if (ty.isGenericPoison()) return error.GenericPoison;
1767517675 break :blk ty;
1767617676 };
17677
17678 if (elem_ty.zigTypeTag(mod) == .NoReturn)
17679 return sema.fail(block, elem_ty_src, "pointer to noreturn not allowed", .{});
17680
1767717681 const target = sema.mod.getTarget();
1767817682
1767917683 var extra_i = extra.end;
......@@ -17681,7 +17685,9 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1768117685 const sentinel = if (inst_data.flags.has_sentinel) blk: {
1768217686 const ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_i]);
1768317687 extra_i += 1;
17684 break :blk (try sema.resolveInstConst(block, sentinel_src, ref, "pointer sentinel value must be comptime-known")).val;
17688 const coerced = try sema.coerce(block, elem_ty, try sema.resolveInst(ref), sentinel_src);
17689 const val = try sema.resolveConstValue(block, sentinel_src, coerced, "pointer sentinel value must be comptime-known");
17690 break :blk val;
1768517691 } else null;
1768617692
1768717693 const abi_align: u32 = if (inst_data.flags.has_align) blk: {
......@@ -17725,9 +17731,7 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1772517731 return sema.fail(block, bitoffset_src, "bit offset starts after end of host integer", .{});
1772617732 }
1772717733
17728 if (elem_ty.zigTypeTag(mod) == .NoReturn) {
17729 return sema.fail(block, elem_ty_src, "pointer to noreturn not allowed", .{});
17730 } else if (elem_ty.zigTypeTag(mod) == .Fn) {
17734 if (elem_ty.zigTypeTag(mod) == .Fn) {
1773117735 if (inst_data.size != .One) {
1773217736 return sema.fail(block, elem_ty_src, "function pointers must be single pointers", .{});
1773317737 }
......@@ -18580,7 +18584,7 @@ fn zirAlignOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1858018584 return sema.fail(block, operand_src, "no align available for type '{}'", .{ty.fmt(sema.mod)});
1858118585 }
1858218586 const val = try ty.lazyAbiAlignment(mod, sema.arena);
18583 if (val.tag() == .lazy_align) {
18587 if (val.isLazyAlign()) {
1858418588 try sema.queueFullTypeResolution(ty);
1858518589 }
1858618590 return sema.addConstant(Type.comptime_int, val);
......@@ -33056,7 +33060,7 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {
3305633060 const enum_simple = resolved_ty.castTag(.enum_simple).?.data;
3305733061 switch (enum_simple.fields.count()) {
3305833062 0 => return Value.@"unreachable",
33059 1 => return try mod.intValue(ty, 0),
33063 1 => return try Value.Tag.enum_field_index.create(sema.arena, 0),
3306033064 else => return null,
3306133065 }
3306233066 },
src/type.zig+12-4
......@@ -2090,10 +2090,11 @@ pub const Type = struct {
20902090 }
20912091
20922092 /// May capture a reference to `ty`.
2093 /// Returned value has type `comptime_int`.
20932094 pub fn lazyAbiAlignment(ty: Type, mod: *Module, arena: Allocator) !Value {
20942095 switch (try ty.abiAlignmentAdvanced(mod, .{ .lazy = arena })) {
20952096 .val => |val| return val,
2096 .scalar => |x| return mod.intValue(ty, x),
2097 .scalar => |x| return mod.intValue(Type.comptime_int, x),
20972098 }
20982099 }
20992100
......@@ -5441,9 +5442,16 @@ pub const Type = struct {
54415442 }
54425443 }
54435444
5444 if (d.pointee_type.ip_index != .none and
5445 (d.sentinel == null or d.sentinel.?.ip_index != .none))
5446 {
5445 ip: {
5446 if (d.pointee_type.ip_index == .none) break :ip;
5447
5448 if (d.sentinel) |s| {
5449 switch (s.ip_index) {
5450 .none, .null_value => break :ip,
5451 else => {},
5452 }
5453 }
5454
54475455 return mod.ptrType(.{
54485456 .elem_type = d.pointee_type.ip_index,
54495457 .sentinel = if (d.sentinel) |s| s.ip_index else .none,
src/value.zig+8
......@@ -2637,6 +2637,14 @@ pub const Value = struct {
26372637 }
26382638 }
26392639
2640 pub fn isLazyAlign(val: Value) bool {
2641 return val.ip_index == .none and val.tag() == .lazy_align;
2642 }
2643
2644 pub fn isLazySize(val: Value) bool {
2645 return val.ip_index == .none and val.tag() == .lazy_size;
2646 }
2647
26402648 pub fn isRuntimeValue(val: Value) bool {
26412649 return val.ip_index == .none and val.tag() == .runtime_value;
26422650 }