authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-05-02 20:01:32-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-10 20:40:03-07:00
log50f33734c6cec10a0132644c08ee443c2dd224e2
treed5192de4e7f849226b93cf03095a5ac7e54c3782
parent00f82f1c46126f1fc6655c6142ef16e8e5afbf4e

stage2: isGenericPoison InternPool awareness


4 files changed, 47 insertions(+), 31 deletions(-)

src/Module.zig+1-1
......@@ -5728,7 +5728,7 @@ pub fn analyzeFnBody(mod: *Module, func: *Fn, arena: Allocator) SemaError!Air {
57285728 const param_ty = if (func.comptime_args) |comptime_args| t: {
57295729 const arg_tv = comptime_args[total_param_index];
57305730
5731 const arg_val = if (arg_tv.val.tag() != .generic_poison)
5731 const arg_val = if (!arg_tv.val.isGenericPoison())
57325732 arg_tv.val
57335733 else if (arg_tv.ty.onePossibleValue(mod)) |opv|
57345734 opv
src/Sema.zig+22-22
......@@ -300,7 +300,7 @@ pub const Block = struct {
300300 const src_decl = sema.mod.declPtr(rt.block.src_decl);
301301 break :blk rt.func_src.toSrcLoc(src_decl);
302302 };
303 if (rt.return_ty.tag() == .generic_poison) {
303 if (rt.return_ty.isGenericPoison()) {
304304 return sema.mod.errNoteNonLazy(src_loc, parent, prefix ++ "the generic function was instantiated with a comptime-only return type", .{});
305305 }
306306 try sema.mod.errNoteNonLazy(
......@@ -1730,7 +1730,7 @@ pub fn resolveInst(sema: *Sema, zir_ref: Zir.Inst.Ref) !Air.Inst.Ref {
17301730 // The last section of indexes refers to the map of ZIR => AIR.
17311731 const inst = sema.inst_map.get(i - InternPool.static_len).?;
17321732 const ty = sema.typeOf(inst);
1733 if (ty.tag() == .generic_poison) return error.GenericPoison;
1733 if (ty.isGenericPoison()) return error.GenericPoison;
17341734 return inst;
17351735}
17361736
......@@ -1766,7 +1766,7 @@ pub fn resolveType(sema: *Sema, block: *Block, src: LazySrcLoc, zir_ref: Zir.Ins
17661766 const air_inst = try sema.resolveInst(zir_ref);
17671767 assert(air_inst != .var_args_param_type);
17681768 const ty = try sema.analyzeAsType(block, src, air_inst);
1769 if (ty.tag() == .generic_poison) return error.GenericPoison;
1769 if (ty.isGenericPoison()) return error.GenericPoison;
17701770 return ty;
17711771}
17721772
......@@ -1827,7 +1827,7 @@ fn resolveValue(
18271827 reason: []const u8,
18281828) CompileError!Value {
18291829 if (try sema.resolveMaybeUndefValAllowVariables(air_ref)) |val| {
1830 if (val.tag() == .generic_poison) return error.GenericPoison;
1830 if (val.isGenericPoison()) return error.GenericPoison;
18311831 return val;
18321832 }
18331833 return sema.failWithNeededComptime(block, src, reason);
......@@ -6549,8 +6549,8 @@ const GenericCallAdapter = struct {
65496549 const other_comptime_args = other_key.comptime_args.?;
65506550 for (other_comptime_args[0..ctx.func_ty_info.param_types.len], 0..) |other_arg, i| {
65516551 const this_arg = ctx.args[i];
6552 const this_is_comptime = this_arg.val.tag() != .generic_poison;
6553 const other_is_comptime = other_arg.val.tag() != .generic_poison;
6552 const this_is_comptime = !this_arg.val.isGenericPoison();
6553 const other_is_comptime = !other_arg.val.isGenericPoison();
65546554 const this_is_anytype = this_arg.is_anytype;
65556555 const other_is_anytype = other_key.isAnytypeParam(ctx.module, @intCast(u32, i));
65566556
......@@ -7189,7 +7189,7 @@ fn analyzeInlineCallArg(
71897189 const param_body = sema.code.extra[extra.end..][0..extra.data.body_len];
71907190 const param_ty = param_ty: {
71917191 const raw_param_ty = raw_param_types[arg_i.*];
7192 if (raw_param_ty.tag() != .generic_poison) break :param_ty raw_param_ty;
7192 if (!raw_param_ty.isGenericPoison()) break :param_ty raw_param_ty;
71937193 const param_ty_inst = try sema.resolveBody(param_block, param_body, inst);
71947194 break :param_ty try sema.analyzeAsType(param_block, param_src, param_ty_inst);
71957195 };
......@@ -7317,7 +7317,7 @@ fn analyzeGenericCallArg(
73177317 runtime_i: *u32,
73187318) !void {
73197319 const mod = sema.mod;
7320 const is_runtime = comptime_arg.val.tag() == .generic_poison and
7320 const is_runtime = comptime_arg.val.isGenericPoison() and
73217321 comptime_arg.ty.hasRuntimeBits(mod) and
73227322 !(try sema.typeRequiresComptime(comptime_arg.ty));
73237323 if (is_runtime) {
......@@ -8882,7 +8882,7 @@ fn funcCommon(
88828882 const cc_src: LazySrcLoc = .{ .node_offset_fn_type_cc = src_node_offset };
88838883 const func_src = LazySrcLoc.nodeOffset(src_node_offset);
88848884
8885 var is_generic = bare_return_type.tag() == .generic_poison or
8885 var is_generic = bare_return_type.isGenericPoison() or
88868886 alignment == null or
88878887 address_space == null or
88888888 section == .generic or
......@@ -8965,7 +8965,7 @@ fn funcCommon(
89658965 var ret_ty_requires_comptime = false;
89668966 const ret_poison = if (sema.typeRequiresComptime(bare_return_type)) |ret_comptime| rp: {
89678967 ret_ty_requires_comptime = ret_comptime;
8968 break :rp bare_return_type.tag() == .generic_poison;
8968 break :rp bare_return_type.isGenericPoison();
89698969 } else |err| switch (err) {
89708970 error.GenericPoison => rp: {
89718971 is_generic = true;
......@@ -9208,7 +9208,7 @@ fn analyzeParameter(
92089208 const mod = sema.mod;
92099209 const requires_comptime = try sema.typeRequiresComptime(param.ty);
92109210 comptime_params[i] = param.is_comptime or requires_comptime;
9211 const this_generic = param.ty.tag() == .generic_poison;
9211 const this_generic = param.ty.isGenericPoison();
92129212 is_generic.* = is_generic.* or this_generic;
92139213 const target = mod.getTarget();
92149214 if (param.is_comptime and !Type.fnCallingConventionAllowsZigTypes(target, cc)) {
......@@ -15872,7 +15872,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1587215872 const param_vals = try params_anon_decl.arena().alloc(Value, info.param_types.len);
1587315873 for (param_vals, 0..) |*param_val, i| {
1587415874 const param_ty = info.param_types[i];
15875 const is_generic = param_ty.tag() == .generic_poison;
15875 const is_generic = param_ty.isGenericPoison();
1587615876 const param_ty_val = if (is_generic)
1587715877 Value.null
1587815878 else
......@@ -15936,7 +15936,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1593615936 });
1593715937 };
1593815938
15939 const ret_ty_opt = if (info.return_type.tag() != .generic_poison)
15939 const ret_ty_opt = if (!info.return_type.isGenericPoison())
1594015940 try Value.Tag.opt_payload.create(
1594115941 sema.arena,
1594215942 try Value.Tag.ty.create(sema.arena, info.return_type),
......@@ -16713,7 +16713,7 @@ fn zirTypeofBuiltin(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr
1671316713
1671416714 const operand = try sema.resolveBody(&child_block, body, inst);
1671516715 const operand_ty = sema.typeOf(operand);
16716 if (operand_ty.tag() == .generic_poison) return error.GenericPoison;
16716 if (operand_ty.isGenericPoison()) return error.GenericPoison;
1671716717 return sema.addType(operand_ty);
1671816718}
1671916719
......@@ -17589,7 +17589,7 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1758917589 }
1759017590 return err;
1759117591 };
17592 if (ty.tag() == .generic_poison) return error.GenericPoison;
17592 if (ty.isGenericPoison()) return error.GenericPoison;
1759317593 break :blk ty;
1759417594 };
1759517595 const target = sema.mod.getTarget();
......@@ -22575,7 +22575,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
2257522575 extra_index += body.len;
2257622576
2257722577 const val = try sema.resolveGenericBody(block, align_src, body, inst, Type.u29, "alignment must be comptime-known");
22578 if (val.tag() == .generic_poison) {
22578 if (val.isGenericPoison()) {
2257922579 break :blk null;
2258022580 }
2258122581 const alignment = @intCast(u32, val.toUnsignedInt(mod));
......@@ -22611,7 +22611,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
2261122611
2261222612 const addrspace_ty = try sema.getBuiltinType("AddressSpace");
2261322613 const val = try sema.resolveGenericBody(block, addrspace_src, body, inst, addrspace_ty, "addrespace must be comptime-known");
22614 if (val.tag() == .generic_poison) {
22614 if (val.isGenericPoison()) {
2261522615 break :blk null;
2261622616 }
2261722617 break :blk val.toEnum(std.builtin.AddressSpace);
......@@ -22635,7 +22635,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
2263522635
2263622636 const ty = Type.initTag(.const_slice_u8);
2263722637 const val = try sema.resolveGenericBody(block, section_src, body, inst, ty, "linksection must be comptime-known");
22638 if (val.tag() == .generic_poison) {
22638 if (val.isGenericPoison()) {
2263922639 break :blk FuncLinkSection{ .generic = {} };
2264022640 }
2264122641 break :blk FuncLinkSection{ .explicit = try val.toAllocatedBytes(ty, sema.arena, sema.mod) };
......@@ -22659,7 +22659,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
2265922659
2266022660 const cc_ty = try sema.getBuiltinType("CallingConvention");
2266122661 const val = try sema.resolveGenericBody(block, cc_src, body, inst, cc_ty, "calling convention must be comptime-known");
22662 if (val.tag() == .generic_poison) {
22662 if (val.isGenericPoison()) {
2266322663 break :blk null;
2266422664 }
2266522665 break :blk val.toEnum(std.builtin.CallingConvention);
......@@ -31790,7 +31790,7 @@ fn resolveInferredErrorSet(
3179031790 // if ies declared by a inline function with generic return type, the return_type should be generic_poison,
3179131791 // because inline function does not create a new declaration, and the ies has been filled with analyzeCall,
3179231792 // so here we can simply skip this case.
31793 if (ies_func_info.return_type.tag() == .generic_poison) {
31793 if (ies_func_info.return_type.isGenericPoison()) {
3179431794 assert(ies_func_info.cc == .Inline);
3179531795 } else if (ies_func_info.return_type.errorUnionSet().castTag(.error_set_inferred).?.data == ies) {
3179631796 if (ies_func_info.is_generic) {
......@@ -32048,7 +32048,7 @@ fn semaStructFields(mod: *Module, struct_obj: *Module.Struct) CompileError!void
3204832048 else => |e| return e,
3204932049 };
3205032050 };
32051 if (field_ty.tag() == .generic_poison) {
32051 if (field_ty.isGenericPoison()) {
3205232052 return error.GenericPoison;
3205332053 }
3205432054
......@@ -32442,7 +32442,7 @@ fn semaUnionFields(mod: *Module, union_obj: *Module.Union) CompileError!void {
3244232442 else => |e| return e,
3244332443 };
3244432444
32445 if (field_ty.tag() == .generic_poison) {
32445 if (field_ty.isGenericPoison()) {
3244632446 return error.GenericPoison;
3244732447 }
3244832448
src/type.zig+16-8
......@@ -727,8 +727,8 @@ pub const Type = struct {
727727 const a_info = a.fnInfo();
728728 const b_info = b.fnInfo();
729729
730 if (a_info.return_type.tag() != .generic_poison and
731 b_info.return_type.tag() != .generic_poison and
730 if (!a_info.return_type.isGenericPoison() and
731 !b_info.return_type.isGenericPoison() and
732732 !eql(a_info.return_type, b_info.return_type, mod))
733733 return false;
734734
......@@ -758,8 +758,8 @@ pub const Type = struct {
758758 if (a_info.comptime_params[i] != b_info.comptime_params[i])
759759 return false;
760760
761 if (a_param_ty.tag() == .generic_poison) continue;
762 if (b_param_ty.tag() == .generic_poison) continue;
761 if (a_param_ty.isGenericPoison()) continue;
762 if (b_param_ty.isGenericPoison()) continue;
763763
764764 if (!eql(a_param_ty, b_param_ty, mod))
765765 return false;
......@@ -1131,7 +1131,7 @@ pub const Type = struct {
11311131 std.hash.autoHash(hasher, std.builtin.TypeId.Fn);
11321132
11331133 const fn_info = ty.fnInfo();
1134 if (fn_info.return_type.tag() != .generic_poison) {
1134 if (!fn_info.return_type.isGenericPoison()) {
11351135 hashWithHasher(fn_info.return_type, hasher, mod);
11361136 }
11371137 if (!fn_info.align_is_generic) {
......@@ -1148,7 +1148,7 @@ pub const Type = struct {
11481148 std.hash.autoHash(hasher, fn_info.param_types.len);
11491149 for (fn_info.param_types, 0..) |param_ty, i| {
11501150 std.hash.autoHash(hasher, fn_info.paramIsComptime(i));
1151 if (param_ty.tag() == .generic_poison) continue;
1151 if (param_ty.isGenericPoison()) continue;
11521152 hashWithHasher(param_ty, hasher, mod);
11531153 }
11541154 },
......@@ -2154,7 +2154,7 @@ pub const Type = struct {
21542154 if (std.math.cast(u5, i)) |index| if (@truncate(u1, fn_info.noalias_bits >> index) != 0) {
21552155 try writer.writeAll("noalias ");
21562156 };
2157 if (param_ty.tag() == .generic_poison) {
2157 if (param_ty.isGenericPoison()) {
21582158 try writer.writeAll("anytype");
21592159 } else {
21602160 try print(param_ty, writer, mod);
......@@ -2175,7 +2175,7 @@ pub const Type = struct {
21752175 try writer.writeAll(@tagName(fn_info.cc));
21762176 try writer.writeAll(") ");
21772177 }
2178 if (fn_info.return_type.tag() == .generic_poison) {
2178 if (fn_info.return_type.isGenericPoison()) {
21792179 try writer.writeAll("anytype");
21802180 } else {
21812181 try print(fn_info.return_type, writer, mod);
......@@ -6075,6 +6075,14 @@ pub const Type = struct {
60756075 }
60766076 }
60776077
6078 pub fn isGenericPoison(ty: Type) bool {
6079 return switch (ty.ip_index) {
6080 .generic_poison_type => true,
6081 .none => ty.tag() == .generic_poison,
6082 else => false,
6083 };
6084 }
6085
60786086 /// This enum does not directly correspond to `std.builtin.TypeId` because
60796087 /// it has extra enum tags in it, as a way of using less memory. For example,
60806088 /// even though Zig recognizes `*align(10) i32` and `*i32` both as Pointer types
src/value.zig+8
......@@ -5416,6 +5416,14 @@ pub const Value = struct {
54165416 return initPayload(&value_buffer.base);
54175417 }
54185418
5419 pub fn isGenericPoison(val: Value) bool {
5420 return switch (val.ip_index) {
5421 .generic_poison => true,
5422 .none => val.tag() == .generic_poison,
5423 else => false,
5424 };
5425 }
5426
54195427 /// This type is not copyable since it may contain pointers to its inner data.
54205428 pub const Payload = struct {
54215429 tag: Tag,