| ... | @@ -897,7 +897,7 @@ fn zirValidateStructInitPtr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Ind | ... | @@ -897,7 +897,7 @@ fn zirValidateStructInitPtr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Ind |
| 897 | try mod.errNoteNonLazy( | 897 | try mod.errNoteNonLazy( |
| 898 | struct_obj.srcLoc(), | 898 | struct_obj.srcLoc(), |
| 899 | msg, | 899 | msg, |
| 900 | "'{s}' declared here", | 900 | "struct '{s}' declared here", |
| 901 | .{fqn}, | 901 | .{fqn}, |
| 902 | ); | 902 | ); |
| 903 | return mod.failWithOwnedErrorMsg(&block.base, msg); | 903 | return mod.failWithOwnedErrorMsg(&block.base, msg); |
| ... | @@ -925,7 +925,7 @@ fn failWithBadFieldAccess( | ... | @@ -925,7 +925,7 @@ fn failWithBadFieldAccess( |
| 925 | .{ field_name, fqn }, | 925 | .{ field_name, fqn }, |
| 926 | ); | 926 | ); |
| 927 | errdefer msg.destroy(gpa); | 927 | errdefer msg.destroy(gpa); |
| 928 | try mod.errNoteNonLazy(struct_obj.srcLoc(), msg, "'{s}' declared here", .{fqn}); | 928 | try mod.errNoteNonLazy(struct_obj.srcLoc(), msg, "struct declared here", .{}); |
| 929 | break :msg msg; | 929 | break :msg msg; |
| 930 | }; | 930 | }; |
| 931 | return mod.failWithOwnedErrorMsg(&block.base, msg); | 931 | return mod.failWithOwnedErrorMsg(&block.base, msg); |
| ... | @@ -4479,21 +4479,24 @@ fn namedFieldPtr( | ... | @@ -4479,21 +4479,24 @@ fn namedFieldPtr( |
| 4479 | return sema.analyzeDeclRef(block, src, decl); | 4479 | return sema.analyzeDeclRef(block, src, decl); |
| 4480 | } | 4480 | } |
| 4481 | } | 4481 | } |
| 4482 | const maybe_field_index: ?usize = switch (child_type.tag()) { | 4482 | const field_index = child_type.enumFieldIndex(field_name) orelse { |
| 4483 | .enum_full, .enum_nonexhaustive => blk: { | 4483 | const msg = msg: { |
| 4484 | const enum_full = child_type.castTag(.enum_full).?.data; | 4484 | const msg = try mod.errMsg( |
| 4485 | break :blk enum_full.fields.getIndex(field_name); | 4485 | &block.base, |
| 4486 | }, | 4486 | src, |
| 4487 | .enum_simple => blk: { | 4487 | "enum '{}' has no member named '{s}'", |
| 4488 | const enum_simple = child_type.castTag(.enum_simple).?.data; | 4488 | .{ child_type, field_name }, |
| 4489 | break :blk enum_simple.fields.getIndex(field_name); | 4489 | ); |
| 4490 | }, | 4490 | errdefer msg.destroy(sema.gpa); |
| 4491 | else => unreachable, | 4491 | try mod.errNoteNonLazy( |
| 4492 | }; | 4492 | child_type.declSrcLoc(), |
| 4493 | const field_index = maybe_field_index orelse { | 4493 | msg, |
| 4494 | return mod.fail(&block.base, src, "enum '{}' has no member named '{s}'", .{ | 4494 | "enum declared here", |
| 4495 | child_type, field_name, | 4495 | .{}, |
| 4496 | }); | 4496 | ); |
| | 4497 | break :msg msg; |
| | 4498 | }; |
| | 4499 | return mod.failWithOwnedErrorMsg(&block.base, msg); |
| 4497 | }; | 4500 | }; |
| 4498 | const field_index_u32 = @intCast(u32, field_index); | 4501 | const field_index_u32 = @intCast(u32, field_index); |
| 4499 | const enum_val = try Value.Tag.enum_field_index.create(arena, field_index_u32); | 4502 | const enum_val = try Value.Tag.enum_field_index.create(arena, field_index_u32); |
| ... | @@ -4593,10 +4596,13 @@ fn coerce( | ... | @@ -4593,10 +4596,13 @@ fn coerce( |
| 4593 | return sema.bitcast(block, dest_type, inst); | 4596 | return sema.bitcast(block, dest_type, inst); |
| 4594 | } | 4597 | } |
| 4595 | | 4598 | |
| | 4599 | const mod = sema.mod; |
| | 4600 | const arena = sema.arena; |
| | 4601 | |
| 4596 | // undefined to anything | 4602 | // undefined to anything |
| 4597 | if (inst.value()) |val| { | 4603 | if (inst.value()) |val| { |
| 4598 | if (val.isUndef() or inst.ty.zigTypeTag() == .Undefined) { | 4604 | if (val.isUndef() or inst.ty.zigTypeTag() == .Undefined) { |
| 4599 | return sema.mod.constInst(sema.arena, inst_src, .{ .ty = dest_type, .val = val }); | 4605 | return mod.constInst(arena, inst_src, .{ .ty = dest_type, .val = val }); |
| 4600 | } | 4606 | } |
| 4601 | } | 4607 | } |
| 4602 | assert(inst.ty.zigTypeTag() != .Undefined); | 4608 | assert(inst.ty.zigTypeTag() != .Undefined); |
| ... | @@ -4610,13 +4616,13 @@ fn coerce( | ... | @@ -4610,13 +4616,13 @@ fn coerce( |
| 4610 | if (try sema.coerceNum(block, dest_type, inst)) |some| | 4616 | if (try sema.coerceNum(block, dest_type, inst)) |some| |
| 4611 | return some; | 4617 | return some; |
| 4612 | | 4618 | |
| 4613 | const target = sema.mod.getTarget(); | 4619 | const target = mod.getTarget(); |
| 4614 | | 4620 | |
| 4615 | switch (dest_type.zigTypeTag()) { | 4621 | switch (dest_type.zigTypeTag()) { |
| 4616 | .Optional => { | 4622 | .Optional => { |
| 4617 | // null to ?T | 4623 | // null to ?T |
| 4618 | if (inst.ty.zigTypeTag() == .Null) { | 4624 | if (inst.ty.zigTypeTag() == .Null) { |
| 4619 | return sema.mod.constInst(sema.arena, inst_src, .{ .ty = dest_type, .val = Value.initTag(.null_value) }); | 4625 | return mod.constInst(arena, inst_src, .{ .ty = dest_type, .val = Value.initTag(.null_value) }); |
| 4620 | } | 4626 | } |
| 4621 | | 4627 | |
| 4622 | // T to ?T | 4628 | // T to ?T |
| ... | @@ -4703,63 +4709,39 @@ fn coerce( | ... | @@ -4703,63 +4709,39 @@ fn coerce( |
| 4703 | } | 4709 | } |
| 4704 | }, | 4710 | }, |
| 4705 | .Enum => { | 4711 | .Enum => { |
| | 4712 | // enum literal to enum |
| 4706 | if (inst.ty.zigTypeTag() == .EnumLiteral) { | 4713 | if (inst.ty.zigTypeTag() == .EnumLiteral) { |
| 4707 | const val = (try sema.resolveDefinedValue(block, inst_src, inst)).?; | 4714 | const val = try sema.resolveConstValue(block, inst_src, inst); |
| 4708 | const bytes = val.castTag(.enum_literal).?.data; | 4715 | const bytes = val.castTag(.enum_literal).?.data; |
| 4709 | switch (dest_type.tag()) { | 4716 | const field_index = dest_type.enumFieldIndex(bytes) orelse { |
| 4710 | .enum_full => { | 4717 | const msg = msg: { |
| 4711 | const enumeration = dest_type.castTag(.enum_full).?.data; | 4718 | const msg = try mod.errMsg( |
| 4712 | const enum_fields = enumeration.fields; | | |
| 4713 | const i = enum_fields.getIndex(bytes) orelse return sema.mod.fail( | | |
| 4714 | &block.base, | 4719 | &block.base, |
| 4715 | inst_src, | 4720 | inst_src, |
| 4716 | "enum '{s}' has no field named '{s}'", | 4721 | "enum '{}' has no field named '{s}'", |
| 4717 | .{ enumeration.owner_decl.name, bytes }, | 4722 | .{ dest_type, bytes }, |
| 4718 | ); | 4723 | ); |
| 4719 | const val_pl = try Value.Tag.enum_field_index.create(sema.arena, @intCast(u32, i)); | 4724 | errdefer msg.destroy(sema.gpa); |
| 4720 | return sema.mod.constInst(sema.arena, inst_src, .{ | 4725 | try mod.errNoteNonLazy( |
| 4721 | .ty = dest_type, | 4726 | dest_type.declSrcLoc(), |
| 4722 | .val = val_pl, | 4727 | msg, |
| 4723 | }); | 4728 | "enum declared here", |
| 4724 | }, | 4729 | .{}, |
| 4725 | .enum_simple => { | | |
| 4726 | const enumeration = dest_type.castTag(.enum_simple).?.data; | | |
| 4727 | const enum_fields = enumeration.fields; | | |
| 4728 | const i = enum_fields.getIndex(bytes) orelse return sema.mod.fail( | | |
| 4729 | &block.base, | | |
| 4730 | inst_src, | | |
| 4731 | "enum '{s}' has no field named '{s}'", | | |
| 4732 | .{ enumeration.owner_decl.name, bytes }, | | |
| 4733 | ); | | |
| 4734 | const val_pl = try Value.Tag.enum_field_index.create(sema.arena, @intCast(u32, i)); | | |
| 4735 | return sema.mod.constInst(sema.arena, inst_src, .{ | | |
| 4736 | .ty = dest_type, | | |
| 4737 | .val = val_pl, | | |
| 4738 | }); | | |
| 4739 | }, | | |
| 4740 | .enum_nonexhaustive => { | | |
| 4741 | const enumeration = dest_type.castTag(.enum_nonexhaustive).?.data; | | |
| 4742 | const enum_fields = enumeration.fields; | | |
| 4743 | const i = enum_fields.getIndex(bytes) orelse return sema.mod.fail( | | |
| 4744 | &block.base, | | |
| 4745 | inst_src, | | |
| 4746 | "enum '{s}' has no field named '{s}'", | | |
| 4747 | .{ enumeration.owner_decl.name, bytes }, | | |
| 4748 | ); | 4730 | ); |
| 4749 | const val_pl = try Value.Tag.enum_field_index.create(sema.arena, @intCast(u32, i)); | 4731 | break :msg msg; |
| 4750 | return sema.mod.constInst(sema.arena, inst_src, .{ | 4732 | }; |
| 4751 | .ty = dest_type, | 4733 | return mod.failWithOwnedErrorMsg(&block.base, msg); |
| 4752 | .val = val_pl, | 4734 | }; |
| 4753 | }); | 4735 | return mod.constInst(arena, inst_src, .{ |
| 4754 | }, | 4736 | .ty = dest_type, |
| 4755 | else => unreachable, | 4737 | .val = try Value.Tag.enum_field_index.create(arena, @intCast(u32, field_index)), |
| 4756 | } | 4738 | }); |
| 4757 | } | 4739 | } |
| 4758 | }, | 4740 | }, |
| 4759 | else => {}, | 4741 | else => {}, |
| 4760 | } | 4742 | } |
| 4761 | | 4743 | |
| 4762 | return sema.mod.fail(&block.base, inst_src, "expected {}, found {}", .{ dest_type, inst.ty }); | 4744 | return mod.fail(&block.base, inst_src, "expected {}, found {}", .{ dest_type, inst.ty }); |
| 4763 | } | 4745 | } |
| 4764 | | 4746 | |
| 4765 | const InMemoryCoercionResult = enum { | 4747 | const InMemoryCoercionResult = enum { |