motiejus/zig

fork of https://codeberg.org/ziglang/zig
git clone https://git.jakstys.lt/motiejus/zig.git
Log | Tree | Refs | README | LICENSE

commit b358c281718a2f6437612fb73590a4992242cf52 (tree)
parent 4eb390b157fcc047a707ad0a2a522911c2269cd6
Author: Alexandros Naskos <alex_naskos@hotmail.com>
Date:   Thu,  1 Oct 2020 11:19:02 +0300

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

stage1: Fix @Type(.Enum) with invalid tag_type
Diffstat:
Msrc/stage1/ir.cpp | 7+++++++
Mtest/compile_errors.zig | 36++++++++++++++++++++++++++++++++++++
2 files changed, 43 insertions(+), 0 deletions(-)

diff --git a/src/stage1/ir.cpp b/src/stage1/ir.cpp @@ -26150,6 +26150,13 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI ContainerLayout layout = (ContainerLayout)bigint_as_u32(&layout_value->data.x_enum_tag); ZigType *tag_type = get_const_field_meta_type(ira, source_instr->source_node, payload, "tag_type", 1); + if (type_is_invalid(tag_type)) + return ira->codegen->invalid_inst_gen->value->type; + if (tag_type->id != ZigTypeIdInt) { + ir_add_error(ira, source_instr, buf_sprintf( + "TypeInfo.Enum.tag_type must be an integer type, not '%s'", buf_ptr(&tag_type->name))); + return ira->codegen->invalid_inst_gen->value->type; + } ZigValue *fields_value = get_const_field(ira, source_instr->source_node, payload, "fields", 2); if (fields_value == nullptr) diff --git a/test/compile_errors.zig b/test/compile_errors.zig @@ -2,6 +2,42 @@ const tests = @import("tests.zig"); const std = @import("std"); pub fn addCases(cases: *tests.CompileErrorContext) void { + cases.add("@Type for exhaustive enum with undefined tag type", + \\const TypeInfo = @import("builtin").TypeInfo; + \\const Tag = @Type(.{ + \\ .Enum = .{ + \\ .layout = .Auto, + \\ .tag_type = undefined, + \\ .fields = &[_]TypeInfo.EnumField{}, + \\ .decls = &[_]TypeInfo.Declaration{}, + \\ .is_exhaustive = false, + \\ }, + \\}); + \\export fn entry() void { + \\ _ = @intToEnum(Tag, 0); + \\} + , &[_][]const u8{ + "tmp.zig:2:20: error: use of undefined value here causes undefined behavior", + }); + + cases.add("@Type for exhaustive enum with non-integer tag type", + \\const TypeInfo = @import("builtin").TypeInfo; + \\const Tag = @Type(.{ + \\ .Enum = .{ + \\ .layout = .Auto, + \\ .tag_type = bool, + \\ .fields = &[_]TypeInfo.EnumField{}, + \\ .decls = &[_]TypeInfo.Declaration{}, + \\ .is_exhaustive = false, + \\ }, + \\}); + \\export fn entry() void { + \\ _ = @intToEnum(Tag, 0); + \\} + , &[_][]const u8{ + "tmp.zig:2:20: error: TypeInfo.Enum.tag_type must be an integer type, not 'bool'", + }); + cases.add("slice sentinel mismatch", \\export fn entry() void { \\ const x = @import("std").meta.Vector(3, f32){ 25, 75, 5, 0 };