authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-05-18 23:30:56-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-05-19 11:05:05-07:00
log447a30299073ce88b7b26d18d060a345beac5276
tree40f114a446af38e91a88cce5c91481c0b3a97cce
parentb61d0debd6d6018203945ae67dc54d474fbc74de

Sema: eliminate `Type.Tag.var_args_param`

This was a special type tag used for hacky stuff in Semantic Analysis. Move the hacky stuff to use a dedicated `Air.Inst.Ref` instead. This way, `var_args_param` is not involved in the type system or intern pool.

4 files changed, 34 insertions(+), 42 deletions(-)

src/Sema.zig+29-22
...@@ -1767,7 +1767,9 @@ pub fn resolveConstString(...@@ -1767,7 +1767,9 @@ pub fn resolveConstString(
1767}1767}
17681768
1769pub fn resolveType(sema: *Sema, block: *Block, src: LazySrcLoc, zir_ref: Zir.Inst.Ref) !Type {1769pub fn resolveType(sema: *Sema, block: *Block, src: LazySrcLoc, zir_ref: Zir.Inst.Ref) !Type {
1770 assert(zir_ref != .var_args_param);
1770 const air_inst = try sema.resolveInst(zir_ref);1771 const air_inst = try sema.resolveInst(zir_ref);
1772 assert(air_inst != .var_args_param);
1771 const ty = try sema.analyzeAsType(block, src, air_inst);1773 const ty = try sema.analyzeAsType(block, src, air_inst);
1772 if (ty.tag() == .generic_poison) return error.GenericPoison;1774 if (ty.tag() == .generic_poison) return error.GenericPoison;
1773 return ty;1775 return ty;
...@@ -6329,12 +6331,6 @@ fn zirCall(...@@ -6329,12 +6331,6 @@ fn zirCall(
6329 const arg_end = sema.code.extra[extra.end + extra_index];6331 const arg_end = sema.code.extra[extra.end + extra_index];
6330 defer arg_start = arg_end;6332 defer arg_start = arg_end;
63316333
6332 const param_ty = if (arg_index >= fn_params_len or
6333 func_ty_info.param_types[arg_index].tag() == .generic_poison)
6334 Type.initTag(.var_args_param)
6335 else
6336 func_ty_info.param_types[arg_index];
6337
6338 // Generate args to comptime params in comptime block.6334 // Generate args to comptime params in comptime block.
6339 defer block.is_comptime = parent_comptime;6335 defer block.is_comptime = parent_comptime;
6340 if (arg_index < fn_params_len and func_ty_info.comptime_params[arg_index]) {6336 if (arg_index < fn_params_len and func_ty_info.comptime_params[arg_index]) {
...@@ -6342,8 +6338,15 @@ fn zirCall(...@@ -6342,8 +6338,15 @@ fn zirCall(
6342 // TODO set comptime_reason6338 // TODO set comptime_reason
6343 }6339 }
63446340
6345 const param_ty_inst = try sema.addType(param_ty);6341 sema.inst_map.putAssumeCapacity(inst, inst: {
6346 sema.inst_map.putAssumeCapacity(inst, param_ty_inst);6342 if (arg_index >= fn_params_len)
6343 break :inst Air.Inst.Ref.var_args_param;
6344
6345 if (func_ty_info.param_types[arg_index].tag() == .generic_poison)
6346 break :inst Air.Inst.Ref.generic_poison_type;
6347
6348 break :inst try sema.addType(func_ty_info.param_types[arg_index]);
6349 });
63476350
6348 const resolved = try sema.resolveBody(block, args_body[arg_start..arg_end], inst);6351 const resolved = try sema.resolveBody(block, args_body[arg_start..arg_end], inst);
6349 const resolved_ty = sema.typeOf(resolved);6352 const resolved_ty = sema.typeOf(resolved);
...@@ -9401,16 +9404,19 @@ fn analyzeAs(...@@ -9401,16 +9404,19 @@ fn analyzeAs(
9401 zir_operand: Zir.Inst.Ref,9404 zir_operand: Zir.Inst.Ref,
9402 no_cast_to_comptime_int: bool,9405 no_cast_to_comptime_int: bool,
9403) CompileError!Air.Inst.Ref {9406) CompileError!Air.Inst.Ref {
9404 const is_ret = if (Zir.refToIndex(zir_dest_type)) |ptr_index|
9405 sema.code.instructions.items(.tag)[ptr_index] == .ret_type
9406 else
9407 false;
9408 const dest_ty = try sema.resolveType(block, src, zir_dest_type);
9409 const operand = try sema.resolveInst(zir_operand);9407 const operand = try sema.resolveInst(zir_operand);
9410 if (dest_ty.tag() == .var_args_param) return operand;9408 if (zir_dest_type == .var_args_param) return operand;
9409 const dest_ty = sema.resolveType(block, src, zir_dest_type) catch |err| switch (err) {
9410 error.GenericPoison => return operand,
9411 else => |e| return e,
9412 };
9411 if (dest_ty.zigTypeTag() == .NoReturn) {9413 if (dest_ty.zigTypeTag() == .NoReturn) {
9412 return sema.fail(block, src, "cannot cast to noreturn", .{});9414 return sema.fail(block, src, "cannot cast to noreturn", .{});
9413 }9415 }
9416 const is_ret = if (Zir.refToIndex(zir_dest_type)) |ptr_index|
9417 sema.code.instructions.items(.tag)[ptr_index] == .ret_type
9418 else
9419 false;
9414 return sema.coerceExtra(block, dest_ty, operand, src, .{ .is_ret = is_ret, .no_cast_to_comptime_int = no_cast_to_comptime_int }) catch |err| switch (err) {9420 return sema.coerceExtra(block, dest_ty, operand, src, .{ .is_ret = is_ret, .no_cast_to_comptime_int = no_cast_to_comptime_int }) catch |err| switch (err) {
9415 error.NotCoercible => unreachable,9421 error.NotCoercible => unreachable,
9416 else => |e| return e,9422 else => |e| return e,
...@@ -18300,8 +18306,14 @@ fn zirFieldType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A...@@ -18300,8 +18306,14 @@ fn zirFieldType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
18300 const extra = sema.code.extraData(Zir.Inst.FieldType, inst_data.payload_index).data;18306 const extra = sema.code.extraData(Zir.Inst.FieldType, inst_data.payload_index).data;
18301 const ty_src = inst_data.src();18307 const ty_src = inst_data.src();
18302 const field_name_src: LazySrcLoc = .{ .node_offset_field_name = inst_data.src_node };18308 const field_name_src: LazySrcLoc = .{ .node_offset_field_name = inst_data.src_node };
18303 const aggregate_ty = try sema.resolveType(block, ty_src, extra.container_type);18309 const aggregate_ty = sema.resolveType(block, ty_src, extra.container_type) catch |err| switch (err) {
18304 if (aggregate_ty.tag() == .var_args_param) return sema.addType(aggregate_ty);18310 // Since this is a ZIR instruction that returns a type, encountering
18311 // generic poison should not result in a failed compilation, but the
18312 // generic poison type. This prevents unnecessary failures when
18313 // constructing types at compile-time.
18314 error.GenericPoison => return Air.Inst.Ref.generic_poison_type,
18315 else => |e| return e,
18316 };
18305 const field_name = sema.code.nullTerminatedString(extra.name_start);18317 const field_name = sema.code.nullTerminatedString(extra.name_start);
18306 return sema.fieldType(block, aggregate_ty, field_name, field_name_src, ty_src);18318 return sema.fieldType(block, aggregate_ty, field_name, field_name_src, ty_src);
18307}18319}
...@@ -24253,8 +24265,7 @@ fn fieldCallBind(...@@ -24253,8 +24265,7 @@ fn fieldCallBind(
24253 const first_param_type = decl_type.fnParamType(0);24265 const first_param_type = decl_type.fnParamType(0);
24254 const first_param_tag = first_param_type.tag();24266 const first_param_tag = first_param_type.tag();
24255 // zig fmt: off24267 // zig fmt: off
24256 if (first_param_tag == .var_args_param or24268 if (first_param_tag == .generic_poison or (
24257 first_param_tag == .generic_poison or (
24258 first_param_type.zigTypeTag() == .Pointer and24269 first_param_type.zigTypeTag() == .Pointer and
24259 (first_param_type.ptrSize() == .One or24270 (first_param_type.ptrSize() == .One or
24260 first_param_type.ptrSize() == .C) and24271 first_param_type.ptrSize() == .C) and
...@@ -25405,7 +25416,6 @@ fn coerceExtra(...@@ -25405,7 +25416,6 @@ fn coerceExtra(
25405 opts: CoerceOpts,25416 opts: CoerceOpts,
25406) CoersionError!Air.Inst.Ref {25417) CoersionError!Air.Inst.Ref {
25407 switch (dest_ty_unresolved.tag()) {25418 switch (dest_ty_unresolved.tag()) {
25408 .var_args_param => return sema.coerceVarArgParam(block, inst, inst_src),
25409 .generic_poison => return inst,25419 .generic_poison => return inst,
25410 else => {},25420 else => {},
25411 }25421 }
...@@ -31269,7 +31279,6 @@ pub fn resolveTypeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool {...@@ -31269,7 +31279,6 @@ pub fn resolveTypeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool {
31269 .function,31279 .function,
31270 => true,31280 => true,
3127131281
31272 .var_args_param => unreachable,
31273 .inferred_alloc_mut => unreachable,31282 .inferred_alloc_mut => unreachable,
31274 .inferred_alloc_const => unreachable,31283 .inferred_alloc_const => unreachable,
31275 .bound_fn => unreachable,31284 .bound_fn => unreachable,
...@@ -32634,7 +32643,6 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {...@@ -32634,7 +32643,6 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {
32634 .anyerror_void_error_union,32643 .anyerror_void_error_union,
32635 .error_set_inferred,32644 .error_set_inferred,
32636 .@"opaque",32645 .@"opaque",
32637 .var_args_param,
32638 .manyptr_u8,32646 .manyptr_u8,
32639 .manyptr_const_u8,32647 .manyptr_const_u8,
32640 .manyptr_const_u8_sentinel_0,32648 .manyptr_const_u8_sentinel_0,
...@@ -33298,7 +33306,6 @@ pub fn typeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool {...@@ -33298,7 +33306,6 @@ pub fn typeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool {
33298 .function,33306 .function,
33299 => true,33307 => true,
3330033308
33301 .var_args_param => unreachable,
33302 .inferred_alloc_mut => unreachable,33309 .inferred_alloc_mut => unreachable,
33303 .inferred_alloc_const => unreachable,33310 .inferred_alloc_const => unreachable,
33304 .bound_fn => unreachable,33311 .bound_fn => unreachable,
src/Zir.zig+5
...@@ -2163,6 +2163,10 @@ pub const Inst = struct {...@@ -2163,6 +2163,10 @@ pub const Inst = struct {
2163 /// Used for generic parameters where the type and value2163 /// Used for generic parameters where the type and value
2164 /// is not known until generic function instantiation.2164 /// is not known until generic function instantiation.
2165 generic_poison,2165 generic_poison,
2166 /// This is a special type for variadic parameters of a function call.
2167 /// Casts to it will validate that the type can be passed to a c
2168 /// calling convention function.
2169 var_args_param,
21662170
2167 _,2171 _,
21682172
...@@ -2474,6 +2478,7 @@ pub const Inst = struct {...@@ -2474,6 +2478,7 @@ pub const Inst = struct {
2474 .ty = Type.initTag(.generic_poison),2478 .ty = Type.initTag(.generic_poison),
2475 .val = Value.initTag(.generic_poison),2479 .val = Value.initTag(.generic_poison),
2476 },2480 },
2481 .var_args_param = undefined,
2477 });2482 });
2478 };2483 };
24792484
src/print_air.zig-1
...@@ -369,7 +369,6 @@ const Writer = struct {...@@ -369,7 +369,6 @@ const Writer = struct {
369 .inferred_alloc_const => try s.writeAll("(inferred_alloc_const)"),369 .inferred_alloc_const => try s.writeAll("(inferred_alloc_const)"),
370 .inferred_alloc_mut => try s.writeAll("(inferred_alloc_mut)"),370 .inferred_alloc_mut => try s.writeAll("(inferred_alloc_mut)"),
371 .generic_poison => try s.writeAll("(generic_poison)"),371 .generic_poison => try s.writeAll("(generic_poison)"),
372 .var_args_param => try s.writeAll("(var_args_param)"),
373 .bound_fn => try s.writeAll("(bound_fn)"),372 .bound_fn => try s.writeAll("(bound_fn)"),
374 else => try ty.print(s, w.module),373 else => try ty.print(s, w.module),
375 }374 }
src/type.zig-19
...@@ -158,7 +158,6 @@ pub const Type = extern union {...@@ -158,7 +158,6 @@ pub const Type = extern union {
158 => return .Union,158 => return .Union,
159159
160 .bound_fn => unreachable,160 .bound_fn => unreachable,
161 .var_args_param => unreachable, // can be any type
162 }161 }
163 }162 }
164163
...@@ -935,7 +934,6 @@ pub const Type = extern union {...@@ -935,7 +934,6 @@ pub const Type = extern union {
935 .type_info => unreachable, // needed to resolve the type before now934 .type_info => unreachable, // needed to resolve the type before now
936935
937 .bound_fn => unreachable,936 .bound_fn => unreachable,
938 .var_args_param => unreachable, // can be any type
939 }937 }
940 }938 }
941939
...@@ -1245,7 +1243,6 @@ pub const Type = extern union {...@@ -1245,7 +1243,6 @@ pub const Type = extern union {
1245 .type_info => unreachable, // needed to resolve the type before now1243 .type_info => unreachable, // needed to resolve the type before now
12461244
1247 .bound_fn => unreachable,1245 .bound_fn => unreachable,
1248 .var_args_param => unreachable, // can be any type
1249 }1246 }
1250 }1247 }
12511248
...@@ -1335,7 +1332,6 @@ pub const Type = extern union {...@@ -1335,7 +1332,6 @@ pub const Type = extern union {
1335 .anyerror_void_error_union,1332 .anyerror_void_error_union,
1336 .inferred_alloc_const,1333 .inferred_alloc_const,
1337 .inferred_alloc_mut,1334 .inferred_alloc_mut,
1338 .var_args_param,
1339 .empty_struct_literal,1335 .empty_struct_literal,
1340 .manyptr_u8,1336 .manyptr_u8,
1341 .manyptr_const_u8,1337 .manyptr_const_u8,
...@@ -1617,7 +1613,6 @@ pub const Type = extern union {...@@ -1617,7 +1613,6 @@ pub const Type = extern union {
1617 .comptime_int,1613 .comptime_int,
1618 .comptime_float,1614 .comptime_float,
1619 .noreturn,1615 .noreturn,
1620 .var_args_param,
1621 .bound_fn,1616 .bound_fn,
1622 => return writer.writeAll(@tagName(t)),1617 => return writer.writeAll(@tagName(t)),
16231618
...@@ -1954,7 +1949,6 @@ pub const Type = extern union {...@@ -1954,7 +1949,6 @@ pub const Type = extern union {
1954 .inferred_alloc_const => unreachable,1949 .inferred_alloc_const => unreachable,
1955 .inferred_alloc_mut => unreachable,1950 .inferred_alloc_mut => unreachable,
1956 .generic_poison => unreachable,1951 .generic_poison => unreachable,
1957 .var_args_param => unreachable,
1958 .bound_fn => unreachable,1952 .bound_fn => unreachable,
19591953
1960 // TODO get rid of these Type.Tag values.1954 // TODO get rid of these Type.Tag values.
...@@ -2595,7 +2589,6 @@ pub const Type = extern union {...@@ -2595,7 +2589,6 @@ pub const Type = extern union {
25952589
2596 .inferred_alloc_const => unreachable,2590 .inferred_alloc_const => unreachable,
2597 .inferred_alloc_mut => unreachable,2591 .inferred_alloc_mut => unreachable,
2598 .var_args_param => unreachable,
2599 .generic_poison => unreachable,2592 .generic_poison => unreachable,
2600 }2593 }
2601 }2594 }
...@@ -2708,7 +2701,6 @@ pub const Type = extern union {...@@ -2708,7 +2701,6 @@ pub const Type = extern union {
2708 .enum_nonexhaustive,2701 .enum_nonexhaustive,
2709 => !ty.cast(Payload.EnumFull).?.data.tag_ty_inferred,2702 => !ty.cast(Payload.EnumFull).?.data.tag_ty_inferred,
27102703
2711 .var_args_param => unreachable,
2712 .inferred_alloc_mut => unreachable,2704 .inferred_alloc_mut => unreachable,
2713 .inferred_alloc_const => unreachable,2705 .inferred_alloc_const => unreachable,
2714 .bound_fn => unreachable,2706 .bound_fn => unreachable,
...@@ -3190,7 +3182,6 @@ pub const Type = extern union {...@@ -3190,7 +3182,6 @@ pub const Type = extern union {
3190 .noreturn,3182 .noreturn,
3191 .inferred_alloc_const,3183 .inferred_alloc_const,
3192 .inferred_alloc_mut,3184 .inferred_alloc_mut,
3193 .var_args_param,
3194 .bound_fn,3185 .bound_fn,
3195 => unreachable,3186 => unreachable,
31963187
...@@ -3295,7 +3286,6 @@ pub const Type = extern union {...@@ -3295,7 +3286,6 @@ pub const Type = extern union {
3295 .noreturn => unreachable,3286 .noreturn => unreachable,
3296 .inferred_alloc_const => unreachable,3287 .inferred_alloc_const => unreachable,
3297 .inferred_alloc_mut => unreachable,3288 .inferred_alloc_mut => unreachable,
3298 .var_args_param => unreachable,
3299 .generic_poison => unreachable,3289 .generic_poison => unreachable,
3300 .modifier => unreachable, // missing call to resolveTypeFields3290 .modifier => unreachable, // missing call to resolveTypeFields
3301 .prefetch_options => unreachable, // missing call to resolveTypeFields3291 .prefetch_options => unreachable, // missing call to resolveTypeFields
...@@ -3639,7 +3629,6 @@ pub const Type = extern union {...@@ -3639,7 +3629,6 @@ pub const Type = extern union {
3639 .inferred_alloc_const => unreachable,3629 .inferred_alloc_const => unreachable,
3640 .inferred_alloc_mut => unreachable,3630 .inferred_alloc_mut => unreachable,
3641 .@"opaque" => unreachable,3631 .@"opaque" => unreachable,
3642 .var_args_param => unreachable,
3643 .generic_poison => unreachable,3632 .generic_poison => unreachable,
3644 .bound_fn => unreachable,3633 .bound_fn => unreachable,
36453634
...@@ -4172,8 +4161,6 @@ pub const Type = extern union {...@@ -4172,8 +4161,6 @@ pub const Type = extern union {
4172 .single_const_pointer_to_comptime_int => Type.initTag(.comptime_int),4161 .single_const_pointer_to_comptime_int => Type.initTag(.comptime_int),
4173 .pointer => ty.castTag(.pointer).?.data.pointee_type,4162 .pointer => ty.castTag(.pointer).?.data.pointee_type,
41744163
4175 .var_args_param => ty,
4176
4177 else => unreachable,4164 else => unreachable,
4178 };4165 };
4179 }4166 }
...@@ -5032,7 +5019,6 @@ pub const Type = extern union {...@@ -5032,7 +5019,6 @@ pub const Type = extern union {
5032 .anyerror_void_error_union,5019 .anyerror_void_error_union,
5033 .error_set_inferred,5020 .error_set_inferred,
5034 .@"opaque",5021 .@"opaque",
5035 .var_args_param,
5036 .manyptr_u8,5022 .manyptr_u8,
5037 .manyptr_const_u8,5023 .manyptr_const_u8,
5038 .manyptr_const_u8_sentinel_0,5024 .manyptr_const_u8_sentinel_0,
...@@ -5257,7 +5243,6 @@ pub const Type = extern union {...@@ -5257,7 +5243,6 @@ pub const Type = extern union {
5257 .function,5243 .function,
5258 => true,5244 => true,
52595245
5260 .var_args_param => unreachable,
5261 .inferred_alloc_mut => unreachable,5246 .inferred_alloc_mut => unreachable,
5262 .inferred_alloc_const => unreachable,5247 .inferred_alloc_const => unreachable,
5263 .bound_fn => unreachable,5248 .bound_fn => unreachable,
...@@ -6088,9 +6073,6 @@ pub const Type = extern union {...@@ -6088,9 +6073,6 @@ pub const Type = extern union {
6088 const_slice_u8_sentinel_0,6073 const_slice_u8_sentinel_0,
6089 anyerror_void_error_union,6074 anyerror_void_error_union,
6090 generic_poison,6075 generic_poison,
6091 /// This is a special type for variadic parameters of a function call.
6092 /// Casts to it will validate that the type can be passed to a c calling convention function.
6093 var_args_param,
6094 /// Same as `empty_struct` except it has an empty namespace.6076 /// Same as `empty_struct` except it has an empty namespace.
6095 empty_struct_literal,6077 empty_struct_literal,
6096 /// This is a special value that tracks a set of types that have been stored6078 /// This is a special value that tracks a set of types that have been stored
...@@ -6201,7 +6183,6 @@ pub const Type = extern union {...@@ -6201,7 +6183,6 @@ pub const Type = extern union {
6201 .generic_poison,6183 .generic_poison,
6202 .inferred_alloc_const,6184 .inferred_alloc_const,
6203 .inferred_alloc_mut,6185 .inferred_alloc_mut,
6204 .var_args_param,
6205 .empty_struct_literal,6186 .empty_struct_literal,
6206 .manyptr_u8,6187 .manyptr_u8,
6207 .manyptr_const_u8,6188 .manyptr_const_u8,