authorgravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2020-10-01 11:19:02+03:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-10-01 11:19:02+03:00
logb358c281718a2f6437612fb73590a4992242cf52
tree7381af1f603c28b4fad91bc56694483ee9b3098b
parent4eb390b157fcc047a707ad0a2a522911c2269cd6
parente903b00eecba34295da5490a90eb87aeb984d155
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #6461 from tadeokondrak/type-enum-invalid-tag-type

stage1: Fix @Type(.Enum) with invalid tag_type

2 files changed, 43 insertions(+), 0 deletions(-)

src/stage1/ir.cpp+7
...@@ -26150,6 +26150,13 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI...@@ -26150,6 +26150,13 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI
26150 ContainerLayout layout = (ContainerLayout)bigint_as_u32(&layout_value->data.x_enum_tag);26150 ContainerLayout layout = (ContainerLayout)bigint_as_u32(&layout_value->data.x_enum_tag);
2615126151
26152 ZigType *tag_type = get_const_field_meta_type(ira, source_instr->source_node, payload, "tag_type", 1);26152 ZigType *tag_type = get_const_field_meta_type(ira, source_instr->source_node, payload, "tag_type", 1);
26153 if (type_is_invalid(tag_type))
26154 return ira->codegen->invalid_inst_gen->value->type;
26155 if (tag_type->id != ZigTypeIdInt) {
26156 ir_add_error(ira, source_instr, buf_sprintf(
26157 "TypeInfo.Enum.tag_type must be an integer type, not '%s'", buf_ptr(&tag_type->name)));
26158 return ira->codegen->invalid_inst_gen->value->type;
26159 }
2615326160
26154 ZigValue *fields_value = get_const_field(ira, source_instr->source_node, payload, "fields", 2);26161 ZigValue *fields_value = get_const_field(ira, source_instr->source_node, payload, "fields", 2);
26155 if (fields_value == nullptr)26162 if (fields_value == nullptr)
test/compile_errors.zig+36
...@@ -2,6 +2,42 @@ const tests = @import("tests.zig");...@@ -2,6 +2,42 @@ const tests = @import("tests.zig");
2const std = @import("std");2const std = @import("std");
33
4pub fn addCases(cases: *tests.CompileErrorContext) void {4pub fn addCases(cases: *tests.CompileErrorContext) void {
5 cases.add("@Type for exhaustive enum with undefined tag type",
6 \\const TypeInfo = @import("builtin").TypeInfo;
7 \\const Tag = @Type(.{
8 \\ .Enum = .{
9 \\ .layout = .Auto,
10 \\ .tag_type = undefined,
11 \\ .fields = &[_]TypeInfo.EnumField{},
12 \\ .decls = &[_]TypeInfo.Declaration{},
13 \\ .is_exhaustive = false,
14 \\ },
15 \\});
16 \\export fn entry() void {
17 \\ _ = @intToEnum(Tag, 0);
18 \\}
19 , &[_][]const u8{
20 "tmp.zig:2:20: error: use of undefined value here causes undefined behavior",
21 });
22
23 cases.add("@Type for exhaustive enum with non-integer tag type",
24 \\const TypeInfo = @import("builtin").TypeInfo;
25 \\const Tag = @Type(.{
26 \\ .Enum = .{
27 \\ .layout = .Auto,
28 \\ .tag_type = bool,
29 \\ .fields = &[_]TypeInfo.EnumField{},
30 \\ .decls = &[_]TypeInfo.Declaration{},
31 \\ .is_exhaustive = false,
32 \\ },
33 \\});
34 \\export fn entry() void {
35 \\ _ = @intToEnum(Tag, 0);
36 \\}
37 , &[_][]const u8{
38 "tmp.zig:2:20: error: TypeInfo.Enum.tag_type must be an integer type, not 'bool'",
39 });
40
5 cases.add("slice sentinel mismatch",41 cases.add("slice sentinel mismatch",
6 \\export fn entry() void {42 \\export fn entry() void {
7 \\ const x = @import("std").meta.Vector(3, f32){ 25, 75, 5, 0 };43 \\ const x = @import("std").meta.Vector(3, f32){ 25, 75, 5, 0 };