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(
17671767}
17681768
17691769pub fn resolveType(sema: *Sema, block: *Block, src: LazySrcLoc, zir_ref: Zir.Inst.Ref) !Type {
1770 assert(zir_ref != .var_args_param);
17701771 const air_inst = try sema.resolveInst(zir_ref);
1772 assert(air_inst != .var_args_param);
17711773 const ty = try sema.analyzeAsType(block, src, air_inst);
17721774 if (ty.tag() == .generic_poison) return error.GenericPoison;
17731775 return ty;
......@@ -6329,12 +6331,6 @@ fn zirCall(
63296331 const arg_end = sema.code.extra[extra.end + extra_index];
63306332 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
63386334 // Generate args to comptime params in comptime block.
63396335 defer block.is_comptime = parent_comptime;
63406336 if (arg_index < fn_params_len and func_ty_info.comptime_params[arg_index]) {
......@@ -6342,8 +6338,15 @@ fn zirCall(
63426338 // TODO set comptime_reason
63436339 }
63446340
6345 const param_ty_inst = try sema.addType(param_ty);
6346 sema.inst_map.putAssumeCapacity(inst, param_ty_inst);
6341 sema.inst_map.putAssumeCapacity(inst, 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
63486351 const resolved = try sema.resolveBody(block, args_body[arg_start..arg_end], inst);
63496352 const resolved_ty = sema.typeOf(resolved);
......@@ -9401,16 +9404,19 @@ fn analyzeAs(
94019404 zir_operand: Zir.Inst.Ref,
94029405 no_cast_to_comptime_int: bool,
94039406) 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);
94099407 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 };
94119413 if (dest_ty.zigTypeTag() == .NoReturn) {
94129414 return sema.fail(block, src, "cannot cast to noreturn", .{});
94139415 }
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;
94149420 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) {
94159421 error.NotCoercible => unreachable,
94169422 else => |e| return e,
......@@ -18300,8 +18306,14 @@ fn zirFieldType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
1830018306 const extra = sema.code.extraData(Zir.Inst.FieldType, inst_data.payload_index).data;
1830118307 const ty_src = inst_data.src();
1830218308 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);
18304 if (aggregate_ty.tag() == .var_args_param) return sema.addType(aggregate_ty);
18309 const aggregate_ty = sema.resolveType(block, ty_src, extra.container_type) catch |err| switch (err) {
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 };
1830518317 const field_name = sema.code.nullTerminatedString(extra.name_start);
1830618318 return sema.fieldType(block, aggregate_ty, field_name, field_name_src, ty_src);
1830718319}
......@@ -24253,8 +24265,7 @@ fn fieldCallBind(
2425324265 const first_param_type = decl_type.fnParamType(0);
2425424266 const first_param_tag = first_param_type.tag();
2425524267 // zig fmt: off
24256 if (first_param_tag == .var_args_param or
24257 first_param_tag == .generic_poison or (
24268 if (first_param_tag == .generic_poison or (
2425824269 first_param_type.zigTypeTag() == .Pointer and
2425924270 (first_param_type.ptrSize() == .One or
2426024271 first_param_type.ptrSize() == .C) and
......@@ -25405,7 +25416,6 @@ fn coerceExtra(
2540525416 opts: CoerceOpts,
2540625417) CoersionError!Air.Inst.Ref {
2540725418 switch (dest_ty_unresolved.tag()) {
25408 .var_args_param => return sema.coerceVarArgParam(block, inst, inst_src),
2540925419 .generic_poison => return inst,
2541025420 else => {},
2541125421 }
......@@ -31269,7 +31279,6 @@ pub fn resolveTypeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool {
3126931279 .function,
3127031280 => true,
3127131281
31272 .var_args_param => unreachable,
3127331282 .inferred_alloc_mut => unreachable,
3127431283 .inferred_alloc_const => unreachable,
3127531284 .bound_fn => unreachable,
......@@ -32634,7 +32643,6 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {
3263432643 .anyerror_void_error_union,
3263532644 .error_set_inferred,
3263632645 .@"opaque",
32637 .var_args_param,
3263832646 .manyptr_u8,
3263932647 .manyptr_const_u8,
3264032648 .manyptr_const_u8_sentinel_0,
......@@ -33298,7 +33306,6 @@ pub fn typeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool {
3329833306 .function,
3329933307 => true,
3330033308
33301 .var_args_param => unreachable,
3330233309 .inferred_alloc_mut => unreachable,
3330333310 .inferred_alloc_const => unreachable,
3330433311 .bound_fn => unreachable,
src/Zir.zig+5
......@@ -2163,6 +2163,10 @@ pub const Inst = struct {
21632163 /// Used for generic parameters where the type and value
21642164 /// is not known until generic function instantiation.
21652165 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
21672171 _,
21682172
......@@ -2474,6 +2478,7 @@ pub const Inst = struct {
24742478 .ty = Type.initTag(.generic_poison),
24752479 .val = Value.initTag(.generic_poison),
24762480 },
2481 .var_args_param = undefined,
24772482 });
24782483 };
24792484
src/print_air.zig-1
......@@ -369,7 +369,6 @@ const Writer = struct {
369369 .inferred_alloc_const => try s.writeAll("(inferred_alloc_const)"),
370370 .inferred_alloc_mut => try s.writeAll("(inferred_alloc_mut)"),
371371 .generic_poison => try s.writeAll("(generic_poison)"),
372 .var_args_param => try s.writeAll("(var_args_param)"),
373372 .bound_fn => try s.writeAll("(bound_fn)"),
374373 else => try ty.print(s, w.module),
375374 }
src/type.zig-19
......@@ -158,7 +158,6 @@ pub const Type = extern union {
158158 => return .Union,
159159
160160 .bound_fn => unreachable,
161 .var_args_param => unreachable, // can be any type
162161 }
163162 }
164163
......@@ -935,7 +934,6 @@ pub const Type = extern union {
935934 .type_info => unreachable, // needed to resolve the type before now
936935
937936 .bound_fn => unreachable,
938 .var_args_param => unreachable, // can be any type
939937 }
940938 }
941939
......@@ -1245,7 +1243,6 @@ pub const Type = extern union {
12451243 .type_info => unreachable, // needed to resolve the type before now
12461244
12471245 .bound_fn => unreachable,
1248 .var_args_param => unreachable, // can be any type
12491246 }
12501247 }
12511248
......@@ -1335,7 +1332,6 @@ pub const Type = extern union {
13351332 .anyerror_void_error_union,
13361333 .inferred_alloc_const,
13371334 .inferred_alloc_mut,
1338 .var_args_param,
13391335 .empty_struct_literal,
13401336 .manyptr_u8,
13411337 .manyptr_const_u8,
......@@ -1617,7 +1613,6 @@ pub const Type = extern union {
16171613 .comptime_int,
16181614 .comptime_float,
16191615 .noreturn,
1620 .var_args_param,
16211616 .bound_fn,
16221617 => return writer.writeAll(@tagName(t)),
16231618
......@@ -1954,7 +1949,6 @@ pub const Type = extern union {
19541949 .inferred_alloc_const => unreachable,
19551950 .inferred_alloc_mut => unreachable,
19561951 .generic_poison => unreachable,
1957 .var_args_param => unreachable,
19581952 .bound_fn => unreachable,
19591953
19601954 // TODO get rid of these Type.Tag values.
......@@ -2595,7 +2589,6 @@ pub const Type = extern union {
25952589
25962590 .inferred_alloc_const => unreachable,
25972591 .inferred_alloc_mut => unreachable,
2598 .var_args_param => unreachable,
25992592 .generic_poison => unreachable,
26002593 }
26012594 }
......@@ -2708,7 +2701,6 @@ pub const Type = extern union {
27082701 .enum_nonexhaustive,
27092702 => !ty.cast(Payload.EnumFull).?.data.tag_ty_inferred,
27102703
2711 .var_args_param => unreachable,
27122704 .inferred_alloc_mut => unreachable,
27132705 .inferred_alloc_const => unreachable,
27142706 .bound_fn => unreachable,
......@@ -3190,7 +3182,6 @@ pub const Type = extern union {
31903182 .noreturn,
31913183 .inferred_alloc_const,
31923184 .inferred_alloc_mut,
3193 .var_args_param,
31943185 .bound_fn,
31953186 => unreachable,
31963187
......@@ -3295,7 +3286,6 @@ pub const Type = extern union {
32953286 .noreturn => unreachable,
32963287 .inferred_alloc_const => unreachable,
32973288 .inferred_alloc_mut => unreachable,
3298 .var_args_param => unreachable,
32993289 .generic_poison => unreachable,
33003290 .modifier => unreachable, // missing call to resolveTypeFields
33013291 .prefetch_options => unreachable, // missing call to resolveTypeFields
......@@ -3639,7 +3629,6 @@ pub const Type = extern union {
36393629 .inferred_alloc_const => unreachable,
36403630 .inferred_alloc_mut => unreachable,
36413631 .@"opaque" => unreachable,
3642 .var_args_param => unreachable,
36433632 .generic_poison => unreachable,
36443633 .bound_fn => unreachable,
36453634
......@@ -4172,8 +4161,6 @@ pub const Type = extern union {
41724161 .single_const_pointer_to_comptime_int => Type.initTag(.comptime_int),
41734162 .pointer => ty.castTag(.pointer).?.data.pointee_type,
41744163
4175 .var_args_param => ty,
4176
41774164 else => unreachable,
41784165 };
41794166 }
......@@ -5032,7 +5019,6 @@ pub const Type = extern union {
50325019 .anyerror_void_error_union,
50335020 .error_set_inferred,
50345021 .@"opaque",
5035 .var_args_param,
50365022 .manyptr_u8,
50375023 .manyptr_const_u8,
50385024 .manyptr_const_u8_sentinel_0,
......@@ -5257,7 +5243,6 @@ pub const Type = extern union {
52575243 .function,
52585244 => true,
52595245
5260 .var_args_param => unreachable,
52615246 .inferred_alloc_mut => unreachable,
52625247 .inferred_alloc_const => unreachable,
52635248 .bound_fn => unreachable,
......@@ -6088,9 +6073,6 @@ pub const Type = extern union {
60886073 const_slice_u8_sentinel_0,
60896074 anyerror_void_error_union,
60906075 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,
60946076 /// Same as `empty_struct` except it has an empty namespace.
60956077 empty_struct_literal,
60966078 /// This is a special value that tracks a set of types that have been stored
......@@ -6201,7 +6183,6 @@ pub const Type = extern union {
62016183 .generic_poison,
62026184 .inferred_alloc_const,
62036185 .inferred_alloc_mut,
6204 .var_args_param,
62056186 .empty_struct_literal,
62066187 .manyptr_u8,
62076188 .manyptr_const_u8,