authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-10-26 12:03:15-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-10-26 12:03:15-07:00
log6df26a37d13d21be061a1cccd39dd17e46a81322
tree968733ee0b37a9335cf2a9c4d8fe26874618c671
parent25012ab3d12ac7bcd4e53a90367afc8e97d91c36

Sema: fix coercion from union to its own tag

I had reversed the tag type / union type arguments.

2 files changed, 22 insertions(+), 14 deletions(-)

src/Sema.zig+9-14
......@@ -8253,7 +8253,7 @@ fn analyzeCmpUnionTag(
82538253 tag_src: LazySrcLoc,
82548254 op: std.math.CompareOperator,
82558255) CompileError!Air.Inst.Ref {
8256 const union_ty = sema.typeOf(un);
8256 const union_ty = try sema.resolveTypeFields(block, un_src, sema.typeOf(un));
82578257 const union_tag_ty = union_ty.unionTagType() orelse {
82588258 // TODO note at declaration site that says "union foo is not tagged"
82598259 return sema.fail(block, un_src, "comparison of union and enum literal is only valid for tagged union types", .{});
......@@ -9553,7 +9553,7 @@ fn zirTagName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
95539553fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
95549554 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
95559555 const src = inst_data.src();
9556 const type_info_ty = try sema.getBuiltinType(block, src, "TypeInfo");
9556 const type_info_ty = try sema.resolveBuiltinTypeFields(block, src, "TypeInfo");
95579557 const uncasted_operand = sema.resolveInst(inst_data.operand);
95589558 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
95599559 const type_info = try sema.coerce(block, type_info_ty, uncasted_operand, operand_src);
......@@ -11984,8 +11984,7 @@ fn coerce(
1198411984 }
1198511985 const dest_ty_src = inst_src; // TODO better source location
1198611986 const dest_ty = try sema.resolveTypeFields(block, dest_ty_src, dest_ty_unresolved);
11987
11988 const inst_ty = sema.typeOf(inst);
11987 const inst_ty = try sema.resolveTypeFields(block, inst_src, sema.typeOf(inst));
1198911988 // If the types are the same, we can return the operand.
1199011989 if (dest_ty.eql(inst_ty))
1199111990 return inst;
......@@ -12167,18 +12166,17 @@ fn coerce(
1216712166 // enum literal to enum
1216812167 const val = try sema.resolveConstValue(block, inst_src, inst);
1216912168 const bytes = val.castTag(.enum_literal).?.data;
12170 const resolved_dest_type = try sema.resolveTypeFields(block, inst_src, dest_ty);
12171 const field_index = resolved_dest_type.enumFieldIndex(bytes) orelse {
12169 const field_index = dest_ty.enumFieldIndex(bytes) orelse {
1217212170 const msg = msg: {
1217312171 const msg = try sema.errMsg(
1217412172 block,
1217512173 inst_src,
1217612174 "enum '{}' has no field named '{s}'",
12177 .{ resolved_dest_type, bytes },
12175 .{ dest_ty, bytes },
1217812176 );
1217912177 errdefer msg.destroy(sema.gpa);
1218012178 try sema.mod.errNoteNonLazy(
12181 resolved_dest_type.declSrcLoc(),
12179 dest_ty.declSrcLoc(),
1218212180 msg,
1218312181 "enum declared here",
1218412182 .{},
......@@ -12188,7 +12186,7 @@ fn coerce(
1218812186 return sema.failWithOwnedErrorMsg(msg);
1218912187 };
1219012188 return sema.addConstant(
12191 resolved_dest_type,
12189 dest_ty,
1219212190 try Value.Tag.enum_field_index.create(arena, @intCast(u32, field_index)),
1219312191 );
1219412192 },
......@@ -12196,7 +12194,7 @@ fn coerce(
1219612194 // union to its own tag type
1219712195 const union_tag_ty = inst_ty.unionTagType() orelse break :blk;
1219812196 if (union_tag_ty.eql(dest_ty)) {
12199 return sema.unionToTag(block, dest_ty, inst, inst_src);
12197 return sema.unionToTag(block, inst_ty, inst, inst_src);
1220012198 }
1220112199 },
1220212200 else => {},
......@@ -14108,10 +14106,7 @@ fn semaStructFields(
1410814106 }
1410914107}
1411014108
14111fn semaUnionFields(
14112 mod: *Module,
14113 union_obj: *Module.Union,
14114) CompileError!void {
14109fn semaUnionFields(mod: *Module, union_obj: *Module.Union) CompileError!void {
1411514110 const tracy = trace(@src());
1411614111 defer tracy.end();
1411714112
src/type.zig+13
......@@ -2573,6 +2573,19 @@ pub const Type = extern union {
25732573 pub fn unionTagType(ty: Type) ?Type {
25742574 return switch (ty.tag()) {
25752575 .union_tagged => ty.castTag(.union_tagged).?.data.tag_ty,
2576
2577 .atomic_order,
2578 .atomic_rmw_op,
2579 .calling_convention,
2580 .address_space,
2581 .float_mode,
2582 .reduce_op,
2583 .call_options,
2584 .export_options,
2585 .extern_options,
2586 .type_info,
2587 => unreachable, // needed to call resolveTypeFields first
2588
25762589 else => null,
25772590 };
25782591 }