authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-05-02 01:59:38+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-05-22 07:01:07-07:00
log33809a0c538b77770ef4f80e8208117d6bcb494e
tree73cd9f783fbb3e4fe91af5e1df1d62b85e1a41bc
parent2648e3651eb3025d6ea958e269f4cb8b8786ce07

InternPool: eliminate `var_args_param_type`

This was a "fake" type used to handle C varargs parameters, much like generic poison. In fact, it is treated identically to generic poison in all cases other than one (the final coercion of a call argument), which is trivially special-cased. Thus, it makes sense to remove this special tag and instead use `generic_poison_type` in its place. This fixes several bugs in Sema related to missing handling of this tag. Resolves: #19781

6 files changed, 15 insertions(+), 35 deletions(-)

lib/std/zig/Zir.zig-3
......@@ -2194,9 +2194,6 @@ pub const Inst = struct {
21942194 empty_struct,
21952195 generic_poison,
21962196
2197 /// This tag is here to match Air and InternPool, however it is unused
2198 /// for ZIR purposes.
2199 var_args_param_type = std.math.maxInt(u32) - 1,
22002197 /// This Ref does not correspond to any ZIR instruction or constant
22012198 /// value and may instead be used as a sentinel to indicate null.
22022199 none = std.math.maxInt(u32),
src/Air.zig+2-8
......@@ -891,8 +891,7 @@ pub const Inst = struct {
891891 /// The most-significant bit of the value is a tag bit. This bit is 1 if the value represents an
892892 /// instruction index and 0 if it represents an InternPool index.
893893 ///
894 /// The hardcoded refs `none` and `var_args_param_type` are exceptions to this rule: they have
895 /// their tag bit set but refer to the InternPool.
894 /// The ref `none` is an exception: it has the tag bit set but refers to the InternPool.
896895 pub const Ref = enum(u32) {
897896 u0_type = @intFromEnum(InternPool.Index.u0_type),
898897 i0_type = @intFromEnum(InternPool.Index.i0_type),
......@@ -979,9 +978,6 @@ pub const Inst = struct {
979978 empty_struct = @intFromEnum(InternPool.Index.empty_struct),
980979 generic_poison = @intFromEnum(InternPool.Index.generic_poison),
981980
982 /// This Ref does not correspond to any AIR instruction or constant
983 /// value. It is used to handle argument types of var args functions.
984 var_args_param_type = @intFromEnum(InternPool.Index.var_args_param_type),
985981 /// This Ref does not correspond to any AIR instruction or constant
986982 /// value and may instead be used as a sentinel to indicate null.
987983 none = @intFromEnum(InternPool.Index.none),
......@@ -994,7 +990,6 @@ pub const Inst = struct {
994990
995991 pub fn toInternedAllowNone(ref: Ref) ?InternPool.Index {
996992 return switch (ref) {
997 .var_args_param_type => .var_args_param_type,
998993 .none => .none,
999994 else => if (@intFromEnum(ref) >> 31 == 0)
1000995 @enumFromInt(@as(u31, @truncate(@intFromEnum(ref))))
......@@ -1010,7 +1005,7 @@ pub const Inst = struct {
10101005
10111006 pub fn toIndexAllowNone(ref: Ref) ?Index {
10121007 return switch (ref) {
1013 .var_args_param_type, .none => null,
1008 .none => null,
10141009 else => if (@intFromEnum(ref) >> 31 != 0)
10151010 @enumFromInt(@as(u31, @truncate(@intFromEnum(ref))))
10161011 else
......@@ -1557,7 +1552,6 @@ pub fn deinit(air: *Air, gpa: std.mem.Allocator) void {
15571552
15581553pub fn internedToRef(ip_index: InternPool.Index) Inst.Ref {
15591554 return switch (ip_index) {
1560 .var_args_param_type => .var_args_param_type,
15611555 .none => .none,
15621556 else => {
15631557 assert(@intFromEnum(ip_index) >> 31 == 0);
src/InternPool.zig-4
......@@ -2818,7 +2818,6 @@ pub const Index = enum(u32) {
28182818 generic_poison,
28192819
28202820 /// Used by Air/Sema only.
2821 var_args_param_type = std.math.maxInt(u32) - 1,
28222821 none = std.math.maxInt(u32),
28232822
28242823 _,
......@@ -8938,7 +8937,6 @@ pub fn typeOf(ip: *const InternPool, index: Index) Index {
89388937 .memoized_call => unreachable,
89398938 },
89408939
8941 .var_args_param_type => unreachable,
89428940 .none => unreachable,
89438941 };
89448942}
......@@ -9153,8 +9151,6 @@ pub fn zigTypeTagOrPoison(ip: *const InternPool, index: Index) error{GenericPois
91539151 .empty_struct => unreachable,
91549152 .generic_poison => unreachable,
91559153
9156 .var_args_param_type => unreachable, // special tag
9157
91589154 _ => switch (ip.items.items(.tag)[@intFromEnum(index)]) {
91599155 .removed => unreachable,
91609156
src/Sema.zig+13-18
......@@ -1901,7 +1901,6 @@ pub fn resolveConstStringIntern(
19011901
19021902pub fn resolveType(sema: *Sema, block: *Block, src: LazySrcLoc, zir_ref: Zir.Inst.Ref) !Type {
19031903 const air_inst = try sema.resolveInst(zir_ref);
1904 assert(air_inst != .var_args_param_type);
19051904 const ty = try sema.analyzeAsType(block, src, air_inst);
19061905 if (ty.isGenericPoison()) return error.GenericPoison;
19071906 return ty;
......@@ -4572,12 +4571,10 @@ fn zirValidateRefTy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr
45724571 const src = un_tok.src();
45734572 // In case of GenericPoison, we don't actually have a type, so this will be
45744573 // treated as an untyped address-of operator.
4575 if (un_tok.operand == .var_args_param_type) return;
45764574 const operand_air_inst = sema.resolveInst(un_tok.operand) catch |err| switch (err) {
45774575 error.GenericPoison => return,
45784576 else => |e| return e,
45794577 };
4580 if (operand_air_inst == .var_args_param_type) return;
45814578 const ty_operand = sema.analyzeAsType(block, src, operand_air_inst) catch |err| switch (err) {
45824579 error.GenericPoison => return,
45834580 else => |e| return e,
......@@ -7363,7 +7360,7 @@ const CallArgsInfo = union(enum) {
73637360 }
73647361
73657362 /// Analyzes the arg at `arg_index` and coerces it to `param_ty`.
7366 /// `param_ty` may be `generic_poison` or `var_args_param`.
7363 /// `param_ty` may be `generic_poison`. A value of `null` indicates a varargs parameter.
73677364 /// `func_ty_info` may be the type before instantiation, even if a generic
73687365 /// instantiation has been partially completed.
73697366 fn analyzeArg(
......@@ -7371,16 +7368,16 @@ const CallArgsInfo = union(enum) {
73717368 sema: *Sema,
73727369 block: *Block,
73737370 arg_index: usize,
7374 param_ty: Type,
7371 maybe_param_ty: ?Type,
73757372 func_ty_info: InternPool.Key.FuncType,
73767373 func_inst: Air.Inst.Ref,
73777374 ) CompileError!Air.Inst.Ref {
73787375 const mod = sema.mod;
73797376 const param_count = func_ty_info.param_types.len;
7380 switch (param_ty.toIntern()) {
7381 .generic_poison_type, .var_args_param_type => {},
7377 if (maybe_param_ty) |param_ty| switch (param_ty.toIntern()) {
7378 .generic_poison_type => {},
73827379 else => try sema.queueFullTypeResolution(param_ty),
7383 }
7380 };
73847381 const uncoerced_arg: Air.Inst.Ref = switch (cai) {
73857382 inline .resolved, .call_builtin => |resolved| resolved.args[arg_index],
73867383 .zir_call => |zir_call| arg_val: {
......@@ -7409,7 +7406,8 @@ const CallArgsInfo = union(enum) {
74097406 // TODO set comptime_reason
74107407 }
74117408 // Give the arg its result type
7412 sema.inst_map.putAssumeCapacity(zir_call.call_inst, Air.internedToRef(param_ty.toIntern()));
7409 const provide_param_ty = if (maybe_param_ty) |t| t else Type.generic_poison;
7410 sema.inst_map.putAssumeCapacity(zir_call.call_inst, Air.internedToRef(provide_param_ty.toIntern()));
74137411 // Resolve the arg!
74147412 const uncoerced_arg = try sema.resolveInlineBody(block, arg_body, zir_call.call_inst);
74157413
......@@ -7426,9 +7424,11 @@ const CallArgsInfo = union(enum) {
74267424 break :arg_val uncoerced_arg;
74277425 },
74287426 };
7427 const param_ty = maybe_param_ty orelse {
7428 return sema.coerceVarArgParam(block, uncoerced_arg, cai.argSrc(block, arg_index));
7429 };
74297430 switch (param_ty.toIntern()) {
74307431 .generic_poison_type => return uncoerced_arg,
7431 .var_args_param_type => return sema.coerceVarArgParam(block, uncoerced_arg, cai.argSrc(block, arg_index)),
74327432 else => return sema.coerceExtra(
74337433 block,
74347434 param_ty,
......@@ -7970,10 +7970,10 @@ fn analyzeCall(
79707970 const args = try sema.arena.alloc(Air.Inst.Ref, args_info.count());
79717971 for (args, 0..) |*arg_out, arg_idx| {
79727972 // Non-generic, so param types are already resolved
7973 const param_ty = if (arg_idx < func_ty_info.param_types.len) ty: {
7973 const param_ty: ?Type = if (arg_idx < func_ty_info.param_types.len) ty: {
79747974 break :ty Type.fromInterned(func_ty_info.param_types.get(ip)[arg_idx]);
7975 } else Type.fromInterned(InternPool.Index.var_args_param_type);
7976 assert(!param_ty.isGenericPoison());
7975 } else null;
7976 if (param_ty) |t| assert(!t.isGenericPoison());
79777977 arg_out.* = try args_info.analyzeArg(sema, block, arg_idx, param_ty, func_ty_info, func);
79787978 try sema.validateRuntimeValue(block, args_info.argSrc(block, arg_idx), arg_out.*);
79797979 if (sema.typeOf(arg_out.*).zigTypeTag(mod) == .NoReturn) {
......@@ -10226,12 +10226,10 @@ fn analyzeAs(
1022610226) CompileError!Air.Inst.Ref {
1022710227 const mod = sema.mod;
1022810228 const operand = try sema.resolveInst(zir_operand);
10229 if (zir_dest_type == .var_args_param_type) return operand;
1023010229 const operand_air_inst = sema.resolveInst(zir_dest_type) catch |err| switch (err) {
1023110230 error.GenericPoison => return operand,
1023210231 else => |e| return e,
1023310232 };
10234 if (operand_air_inst == .var_args_param_type) return operand;
1023510233 const dest_ty = sema.analyzeAsType(block, src, operand_air_inst) catch |err| switch (err) {
1023610234 error.GenericPoison => return operand,
1023710235 else => |e| return e,
......@@ -35664,8 +35662,6 @@ pub fn resolveTypeFields(sema: *Sema, ty: Type) CompileError!void {
3566435662 const ty_ip = ty.toIntern();
3566535663
3566635664 switch (ty_ip) {
35667 .var_args_param_type => unreachable,
35668
3566935665 .none => unreachable,
3567035666
3567135667 .u0_type,
......@@ -37184,7 +37180,6 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {
3718437180 .empty_struct,
3718537181 .generic_poison,
3718637182 // invalid
37187 .var_args_param_type,
3718837183 .none,
3718937184 => unreachable,
3719037185
src/codegen/c/Type.zig-1
......@@ -1468,7 +1468,6 @@ pub const Pool = struct {
14681468 .bool_false,
14691469 .empty_struct,
14701470 .generic_poison,
1471 .var_args_param_type,
14721471 .none,
14731472 => unreachable,
14741473
src/codegen/llvm.zig-1
......@@ -3235,7 +3235,6 @@ pub const Object = struct {
32353235 .bool_false,
32363236 .empty_struct,
32373237 .generic_poison,
3238 .var_args_param_type,
32393238 .none,
32403239 => unreachable,
32413240 else => switch (ip.indexToKey(t.toIntern())) {