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 {...@@ -2584,9 +2584,9 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
25842584
2585 .extern_func => @panic("TODO"),2585 .extern_func => @panic("TODO"),
25862586
2587 .ptr => |ptr| switch (ip.items.items(.tag)[@enumToInt(ptr.ty)]) {2587 .ptr => |ptr| switch (ptr.len) {
2588 .type_pointer => {2588 .none => {
2589 assert(ptr.len == .none);2589 assert(ip.indexToKey(ptr.ty).ptr_type.size != .Slice);
2590 switch (ptr.addr) {2590 switch (ptr.addr) {
2591 .@"var" => |@"var"| ip.items.appendAssumeCapacity(.{2591 .@"var" => |@"var"| ip.items.appendAssumeCapacity(.{
2592 .tag = .ptr_var,2592 .tag = .ptr_var,
...@@ -2626,11 +2626,12 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {...@@ -2626,11 +2626,12 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
2626 }),2626 }),
2627 }2627 }
2628 },2628 },
2629 .type_slice => {2629 else => {
2630 assert(ptr.len != .none);2630 assert(ip.indexToKey(ptr.ty).ptr_type.size == .Slice);
2631 var new_key = key;2631 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);
2633 new_key.ptr.len = .none;2633 new_key.ptr.len = .none;
2634 assert(ip.indexToKey(new_key.ptr.ty).ptr_type.size == .Many);
2634 const ptr_index = try get(ip, gpa, new_key);2635 const ptr_index = try get(ip, gpa, new_key);
2635 try ip.items.ensureUnusedCapacity(gpa, 1);2636 try ip.items.ensureUnusedCapacity(gpa, 1);
2636 ip.items.appendAssumeCapacity(.{2637 ip.items.appendAssumeCapacity(.{
...@@ -2641,7 +2642,6 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {...@@ -2641,7 +2642,6 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
2641 }),2642 }),
2642 });2643 });
2643 },2644 },
2644 else => unreachable,
2645 },2645 },
26462646
2647 .opt => |opt| {2647 .opt => |opt| {
...@@ -3465,10 +3465,12 @@ pub fn sliceLen(ip: InternPool, i: Index) Index {...@@ -3465,10 +3465,12 @@ pub fn sliceLen(ip: InternPool, i: Index) Index {
34653465
3466/// Given an existing value, returns the same value but with the supplied type.3466/// Given an existing value, returns the same value but with the supplied type.
3467/// Only some combinations are allowed:3467/// Only some combinations are allowed:
3468/// * identity coercion
3468/// * int <=> int3469/// * int <=> int
3469/// * int <=> enum3470/// * int <=> enum
3470/// * ptr <=> ptr3471/// * ptr <=> ptr
3471pub fn getCoerced(ip: *InternPool, gpa: Allocator, val: Index, new_ty: Index) Allocator.Error!Index {3472pub fn getCoerced(ip: *InternPool, gpa: Allocator, val: Index, new_ty: Index) Allocator.Error!Index {
3473 if (ip.typeOf(val) == new_ty) return val;
3472 switch (ip.indexToKey(val)) {3474 switch (ip.indexToKey(val)) {
3473 .int => |int| switch (ip.indexToKey(new_ty)) {3475 .int => |int| switch (ip.indexToKey(new_ty)) {
3474 .enum_type => return ip.get(gpa, .{ .enum_tag = .{3476 .enum_type => return ip.get(gpa, .{ .enum_tag = .{
src/Sema.zig+2-2
...@@ -7836,7 +7836,7 @@ fn resolveGenericInstantiationType(...@@ -7836,7 +7836,7 @@ fn resolveGenericInstantiationType(
7836 const arg_val = (child_sema.resolveMaybeUndefValAllowVariables(arg) catch unreachable).?;7836 const arg_val = (child_sema.resolveMaybeUndefValAllowVariables(arg) catch unreachable).?;
7837 child_sema.comptime_args[arg_i] = .{7837 child_sema.comptime_args[arg_i] = .{
7838 .ty = arg_ty,7838 .ty = arg_ty,
7839 .val = try arg_val.copy(new_decl_arena_allocator),7839 .val = (try arg_val.intern(arg_ty, mod)).toValue(),
7840 };7840 };
7841 } else {7841 } else {
7842 child_sema.comptime_args[arg_i] = .{7842 child_sema.comptime_args[arg_i] = .{
...@@ -16537,7 +16537,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -16537,7 +16537,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
16537 try std.fmt.allocPrintZ(anon_decl.arena(), "{d}", .{i});16537 try std.fmt.allocPrintZ(anon_decl.arena(), "{d}", .{i});
16538 const new_decl = try anon_decl.finish(16538 const new_decl = try anon_decl.finish(
16539 try Type.array(anon_decl.arena(), bytes.len, Value.zero_u8, Type.u8, mod),16539 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]),
16541 0, // default alignment16541 0, // default alignment
16542 );16542 );
16543 break :v try Value.Tag.slice.create(fields_anon_decl.arena(), .{16543 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 {...@@ -3357,7 +3357,7 @@ pub const DeclGen = struct {
3357 }),3357 }),
3358 },3358 },
3359 else => switch (mod.intern_pool.indexToKey(tv.val.ip_index)) {3359 else => switch (mod.intern_pool.indexToKey(tv.val.ip_index)) {
3360 .int => |int| return lowerIntAsPtr(dg, int),3360 .int => |int| return dg.lowerIntAsPtr(int),
3361 .ptr => |ptr| {3361 .ptr => |ptr| {
3362 const ptr_val = switch (ptr.addr) {3362 const ptr_val = switch (ptr.addr) {
3363 .@"var" => |@"var"| ptr: {3363 .@"var" => |@"var"| ptr: {
...@@ -3376,7 +3376,7 @@ pub const DeclGen = struct {...@@ -3376,7 +3376,7 @@ pub const DeclGen = struct {
3376 },3376 },
3377 .decl => |decl| try lowerDeclRefValue(dg, tv, decl),3377 .decl => |decl| try lowerDeclRefValue(dg, tv, decl),
3378 .mut_decl => |mut_decl| try lowerDeclRefValue(dg, tv, mut_decl.decl),3378 .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),
3380 };3380 };
3381 switch (ptr.len) {3381 switch (ptr.len) {
3382 .none => return ptr_val,3382 .none => return ptr_val,
...@@ -4084,8 +4084,14 @@ pub const DeclGen = struct {...@@ -4084,8 +4084,14 @@ pub const DeclGen = struct {
4084 fn lowerParentPtr(dg: *DeclGen, ptr_val: Value, byte_aligned: bool) Error!*llvm.Value {4084 fn lowerParentPtr(dg: *DeclGen, ptr_val: Value, byte_aligned: bool) Error!*llvm.Value {
4085 const mod = dg.module;4085 const mod = dg.module;
4086 const target = mod.getTarget();4086 const target = mod.getTarget();
4087 if (ptr_val.ip_index != .none) switch (mod.intern_pool.indexToKey(ptr_val.ip_index)) {4087 if (ptr_val.ip_index != .none) return switch (mod.intern_pool.indexToKey(ptr_val.ip_index)) {
4088 .int => |int| return lowerIntAsPtr(dg, int),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 },
4089 else => unreachable,4095 else => unreachable,
4090 };4096 };
4091 switch (ptr_val.tag()) {4097 switch (ptr_val.tag()) {
src/value.zig+13-4
...@@ -603,7 +603,7 @@ pub const Value = struct {...@@ -603,7 +603,7 @@ pub const Value = struct {
603 }603 }
604604
605 pub fn intern(val: Value, ty: Type, mod: *Module) Allocator.Error!InternPool.Index {605 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);
607 switch (val.tag()) {607 switch (val.tag()) {
608 .slice => {608 .slice => {
609 const pl = val.castTag(.slice).?.data;609 const pl = val.castTag(.slice).?.data;
...@@ -2769,9 +2769,18 @@ pub const Value = struct {...@@ -2769,9 +2769,18 @@ pub const Value = struct {
2769 mod: *Module,2769 mod: *Module,
2770 ) Allocator.Error!Value {2770 ) Allocator.Error!Value {
2771 const elem_ty = ty.elemType2(mod);2771 const elem_ty = ty.elemType2(mod);
2772 const ptr_val = switch (val.tag()) {2772 const ptr_val = switch (val.ip_index) {
2773 .slice => val.castTag(.slice).?.data.ptr,2773 .none => switch (val.tag()) {
2774 else => val,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 },
2775 };2784 };
27762785
2777 if (ptr_val.ip_index == .none and ptr_val.tag() == .elem_ptr) {2786 if (ptr_val.ip_index == .none and ptr_val.tag() == .elem_ptr) {