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....@@ -15615,7 +15615,7 @@ fn zirSizeOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
15615 => {},15615 => {},
15616 }15616 }
15617 const val = try ty.lazyAbiSize(mod, sema.arena);15617 const val = try ty.lazyAbiSize(mod, sema.arena);
15618 if (val.ip_index == .none and val.tag() == .lazy_size) {15618 if (val.isLazySize()) {
15619 try sema.queueFullTypeResolution(ty);15619 try sema.queueFullTypeResolution(ty);
15620 }15620 }
15621 return sema.addConstant(Type.comptime_int, val);15621 return sema.addConstant(Type.comptime_int, val);
...@@ -17674,6 +17674,10 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air...@@ -17674,6 +17674,10 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
17674 if (ty.isGenericPoison()) return error.GenericPoison;17674 if (ty.isGenericPoison()) return error.GenericPoison;
17675 break :blk ty;17675 break :blk ty;
17676 };17676 };
17677
17678 if (elem_ty.zigTypeTag(mod) == .NoReturn)
17679 return sema.fail(block, elem_ty_src, "pointer to noreturn not allowed", .{});
17680
17677 const target = sema.mod.getTarget();17681 const target = sema.mod.getTarget();
1767817682
17679 var extra_i = extra.end;17683 var extra_i = extra.end;
...@@ -17681,7 +17685,9 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air...@@ -17681,7 +17685,9 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
17681 const sentinel = if (inst_data.flags.has_sentinel) blk: {17685 const sentinel = if (inst_data.flags.has_sentinel) blk: {
17682 const ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_i]);17686 const ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_i]);
17683 extra_i += 1;17687 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;
17685 } else null;17691 } else null;
1768617692
17687 const abi_align: u32 = if (inst_data.flags.has_align) blk: {17693 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...@@ -17725,9 +17731,7 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
17725 return sema.fail(block, bitoffset_src, "bit offset starts after end of host integer", .{});17731 return sema.fail(block, bitoffset_src, "bit offset starts after end of host integer", .{});
17726 }17732 }
1772717733
17728 if (elem_ty.zigTypeTag(mod) == .NoReturn) {17734 if (elem_ty.zigTypeTag(mod) == .Fn) {
17729 return sema.fail(block, elem_ty_src, "pointer to noreturn not allowed", .{});
17730 } else if (elem_ty.zigTypeTag(mod) == .Fn) {
17731 if (inst_data.size != .One) {17735 if (inst_data.size != .One) {
17732 return sema.fail(block, elem_ty_src, "function pointers must be single pointers", .{});17736 return sema.fail(block, elem_ty_src, "function pointers must be single pointers", .{});
17733 }17737 }
...@@ -18580,7 +18584,7 @@ fn zirAlignOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air...@@ -18580,7 +18584,7 @@ fn zirAlignOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
18580 return sema.fail(block, operand_src, "no align available for type '{}'", .{ty.fmt(sema.mod)});18584 return sema.fail(block, operand_src, "no align available for type '{}'", .{ty.fmt(sema.mod)});
18581 }18585 }
18582 const val = try ty.lazyAbiAlignment(mod, sema.arena);18586 const val = try ty.lazyAbiAlignment(mod, sema.arena);
18583 if (val.tag() == .lazy_align) {18587 if (val.isLazyAlign()) {
18584 try sema.queueFullTypeResolution(ty);18588 try sema.queueFullTypeResolution(ty);
18585 }18589 }
18586 return sema.addConstant(Type.comptime_int, val);18590 return sema.addConstant(Type.comptime_int, val);
...@@ -33056,7 +33060,7 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {...@@ -33056,7 +33060,7 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {
33056 const enum_simple = resolved_ty.castTag(.enum_simple).?.data;33060 const enum_simple = resolved_ty.castTag(.enum_simple).?.data;
33057 switch (enum_simple.fields.count()) {33061 switch (enum_simple.fields.count()) {
33058 0 => return Value.@"unreachable",33062 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),
33060 else => return null,33064 else => return null,
33061 }33065 }
33062 },33066 },
src/type.zig+12-4
...@@ -2090,10 +2090,11 @@ pub const Type = struct {...@@ -2090,10 +2090,11 @@ pub const Type = struct {
2090 }2090 }
20912091
2092 /// May capture a reference to `ty`.2092 /// May capture a reference to `ty`.
2093 /// Returned value has type `comptime_int`.
2093 pub fn lazyAbiAlignment(ty: Type, mod: *Module, arena: Allocator) !Value {2094 pub fn lazyAbiAlignment(ty: Type, mod: *Module, arena: Allocator) !Value {
2094 switch (try ty.abiAlignmentAdvanced(mod, .{ .lazy = arena })) {2095 switch (try ty.abiAlignmentAdvanced(mod, .{ .lazy = arena })) {
2095 .val => |val| return val,2096 .val => |val| return val,
2096 .scalar => |x| return mod.intValue(ty, x),2097 .scalar => |x| return mod.intValue(Type.comptime_int, x),
2097 }2098 }
2098 }2099 }
20992100
...@@ -5441,9 +5442,16 @@ pub const Type = struct {...@@ -5441,9 +5442,16 @@ pub const Type = struct {
5441 }5442 }
5442 }5443 }
54435444
5444 if (d.pointee_type.ip_index != .none and5445 ip: {
5445 (d.sentinel == null or d.sentinel.?.ip_index != .none))5446 if (d.pointee_type.ip_index == .none) break :ip;
5446 {5447
5448 if (d.sentinel) |s| {
5449 switch (s.ip_index) {
5450 .none, .null_value => break :ip,
5451 else => {},
5452 }
5453 }
5454
5447 return mod.ptrType(.{5455 return mod.ptrType(.{
5448 .elem_type = d.pointee_type.ip_index,5456 .elem_type = d.pointee_type.ip_index,
5449 .sentinel = if (d.sentinel) |s| s.ip_index else .none,5457 .sentinel = if (d.sentinel) |s| s.ip_index else .none,
src/value.zig+8
...@@ -2637,6 +2637,14 @@ pub const Value = struct {...@@ -2637,6 +2637,14 @@ pub const Value = struct {
2637 }2637 }
2638 }2638 }
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
2640 pub fn isRuntimeValue(val: Value) bool {2648 pub fn isRuntimeValue(val: Value) bool {
2641 return val.ip_index == .none and val.tag() == .runtime_value;2649 return val.ip_index == .none and val.tag() == .runtime_value;
2642 }2650 }