authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-05-20 18:24:42-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-10 20:47:54-07:00
logcbf304d8c3f7f1e1746a98dcad979ecf79ed16b5
tree73a9f0ed31ee0f1df66215d494f8d32cb640e828
parent25cd4bb3c9220e308cae9956dc4f579c66bf175a

InternPool: fix coersion issues


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

src/InternPool.zig+9-7
......@@ -2584,9 +2584,9 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
25842584
25852585 .extern_func => @panic("TODO"),
25862586
2587 .ptr => |ptr| switch (ip.items.items(.tag)[@enumToInt(ptr.ty)]) {
2588 .type_pointer => {
2589 assert(ptr.len == .none);
2587 .ptr => |ptr| switch (ptr.len) {
2588 .none => {
2589 assert(ip.indexToKey(ptr.ty).ptr_type.size != .Slice);
25902590 switch (ptr.addr) {
25912591 .@"var" => |@"var"| ip.items.appendAssumeCapacity(.{
25922592 .tag = .ptr_var,
......@@ -2626,11 +2626,12 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
26262626 }),
26272627 }
26282628 },
2629 .type_slice => {
2630 assert(ptr.len != .none);
2629 else => {
2630 assert(ip.indexToKey(ptr.ty).ptr_type.size == .Slice);
26312631 var new_key = key;
2632 new_key.ptr.ty = @intToEnum(Index, ip.items.items(.data)[@enumToInt(ptr.ty)]);
2632 new_key.ptr.ty = ip.slicePtrType(ptr.ty);
26332633 new_key.ptr.len = .none;
2634 assert(ip.indexToKey(new_key.ptr.ty).ptr_type.size == .Many);
26342635 const ptr_index = try get(ip, gpa, new_key);
26352636 try ip.items.ensureUnusedCapacity(gpa, 1);
26362637 ip.items.appendAssumeCapacity(.{
......@@ -2641,7 +2642,6 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
26412642 }),
26422643 });
26432644 },
2644 else => unreachable,
26452645 },
26462646
26472647 .opt => |opt| {
......@@ -3465,10 +3465,12 @@ pub fn sliceLen(ip: InternPool, i: Index) Index {
34653465
34663466/// Given an existing value, returns the same value but with the supplied type.
34673467/// Only some combinations are allowed:
3468/// * identity coercion
34683469/// * int <=> int
34693470/// * int <=> enum
34703471/// * ptr <=> ptr
34713472pub fn getCoerced(ip: *InternPool, gpa: Allocator, val: Index, new_ty: Index) Allocator.Error!Index {
3473 if (ip.typeOf(val) == new_ty) return val;
34723474 switch (ip.indexToKey(val)) {
34733475 .int => |int| switch (ip.indexToKey(new_ty)) {
34743476 .enum_type => return ip.get(gpa, .{ .enum_tag = .{
src/Sema.zig+2-2
......@@ -7836,7 +7836,7 @@ fn resolveGenericInstantiationType(
78367836 const arg_val = (child_sema.resolveMaybeUndefValAllowVariables(arg) catch unreachable).?;
78377837 child_sema.comptime_args[arg_i] = .{
78387838 .ty = arg_ty,
7839 .val = try arg_val.copy(new_decl_arena_allocator),
7839 .val = (try arg_val.intern(arg_ty, mod)).toValue(),
78407840 };
78417841 } else {
78427842 child_sema.comptime_args[arg_i] = .{
......@@ -16537,7 +16537,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1653716537 try std.fmt.allocPrintZ(anon_decl.arena(), "{d}", .{i});
1653816538 const new_decl = try anon_decl.finish(
1653916539 try Type.array(anon_decl.arena(), bytes.len, Value.zero_u8, Type.u8, mod),
16540 try Value.Tag.bytes.create(anon_decl.arena(), bytes[0 .. bytes.len + 1]),
16540 try Value.Tag.bytes.create(anon_decl.arena(), bytes.ptr[0 .. bytes.len + 1]),
1654116541 0, // default alignment
1654216542 );
1654316543 break :v try Value.Tag.slice.create(fields_anon_decl.arena(), .{
src/codegen/llvm.zig+10-4
......@@ -3357,7 +3357,7 @@ pub const DeclGen = struct {
33573357 }),
33583358 },
33593359 else => switch (mod.intern_pool.indexToKey(tv.val.ip_index)) {
3360 .int => |int| return lowerIntAsPtr(dg, int),
3360 .int => |int| return dg.lowerIntAsPtr(int),
33613361 .ptr => |ptr| {
33623362 const ptr_val = switch (ptr.addr) {
33633363 .@"var" => |@"var"| ptr: {
......@@ -3376,7 +3376,7 @@ pub const DeclGen = struct {
33763376 },
33773377 .decl => |decl| try lowerDeclRefValue(dg, tv, decl),
33783378 .mut_decl => |mut_decl| try lowerDeclRefValue(dg, tv, mut_decl.decl),
3379 .int => |int| lowerIntAsPtr(dg, mod.intern_pool.indexToKey(int).int),
3379 .int => |int| dg.lowerIntAsPtr(mod.intern_pool.indexToKey(int).int),
33803380 };
33813381 switch (ptr.len) {
33823382 .none => return ptr_val,
......@@ -4084,8 +4084,14 @@ pub const DeclGen = struct {
40844084 fn lowerParentPtr(dg: *DeclGen, ptr_val: Value, byte_aligned: bool) Error!*llvm.Value {
40854085 const mod = dg.module;
40864086 const target = mod.getTarget();
4087 if (ptr_val.ip_index != .none) switch (mod.intern_pool.indexToKey(ptr_val.ip_index)) {
4088 .int => |int| return lowerIntAsPtr(dg, int),
4087 if (ptr_val.ip_index != .none) return switch (mod.intern_pool.indexToKey(ptr_val.ip_index)) {
4088 .int => |int| dg.lowerIntAsPtr(int),
4089 .ptr => |ptr| switch (ptr.addr) {
4090 .@"var" => |@"var"| dg.lowerParentPtrDecl(ptr_val, @"var".owner_decl),
4091 .decl => |decl| dg.lowerParentPtrDecl(ptr_val, decl),
4092 .mut_decl => |mut_decl| dg.lowerParentPtrDecl(ptr_val, mut_decl.decl),
4093 .int => |int| dg.lowerIntAsPtr(mod.intern_pool.indexToKey(int).int),
4094 },
40894095 else => unreachable,
40904096 };
40914097 switch (ptr_val.tag()) {
src/value.zig+13-4
......@@ -603,7 +603,7 @@ pub const Value = struct {
603603 }
604604
605605 pub fn intern(val: Value, ty: Type, mod: *Module) Allocator.Error!InternPool.Index {
606 if (val.ip_index != .none) return val.ip_index;
606 if (val.ip_index != .none) return mod.intern_pool.getCoerced(mod.gpa, val.ip_index, ty.ip_index);
607607 switch (val.tag()) {
608608 .slice => {
609609 const pl = val.castTag(.slice).?.data;
......@@ -2769,9 +2769,18 @@ pub const Value = struct {
27692769 mod: *Module,
27702770 ) Allocator.Error!Value {
27712771 const elem_ty = ty.elemType2(mod);
2772 const ptr_val = switch (val.tag()) {
2773 .slice => val.castTag(.slice).?.data.ptr,
2774 else => val,
2772 const ptr_val = switch (val.ip_index) {
2773 .none => switch (val.tag()) {
2774 .slice => val.castTag(.slice).?.data.ptr,
2775 else => val,
2776 },
2777 else => switch (mod.intern_pool.indexToKey(val.ip_index)) {
2778 .ptr => |ptr| switch (ptr.len) {
2779 .none => val,
2780 else => val.slicePtr(mod),
2781 },
2782 else => val,
2783 },
27752784 };
27762785
27772786 if (ptr_val.ip_index == .none and ptr_val.tag() == .elem_ptr) {