authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-11-11 13:52:42+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-11-11 19:32:30-07:00
log8c62733927b0b0785b6ed951a2a239102c6ca95b
tree0eaeb98c39b8d7bada7d14818f29de062ef6e246
parentaa6fc29744de4008e97faf0f54ccb52ebbdf5ca2

ensure TypeInfo payload is not undefined


2 files changed, 35 insertions(+), 10 deletions(-)

src/stage1/ir.cpp+26-10
...@@ -26022,6 +26022,32 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI...@@ -26022,6 +26022,32 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI
26022 return ira->codegen->builtin_types.entry_bool;26022 return ira->codegen->builtin_types.entry_bool;
26023 case ZigTypeIdUnreachable:26023 case ZigTypeIdUnreachable:
26024 return ira->codegen->builtin_types.entry_unreachable;26024 return ira->codegen->builtin_types.entry_unreachable;
26025 case ZigTypeIdComptimeFloat:
26026 return ira->codegen->builtin_types.entry_num_lit_float;
26027 case ZigTypeIdComptimeInt:
26028 return ira->codegen->builtin_types.entry_num_lit_int;
26029 case ZigTypeIdUndefined:
26030 return ira->codegen->builtin_types.entry_undef;
26031 case ZigTypeIdNull:
26032 return ira->codegen->builtin_types.entry_null;
26033 case ZigTypeIdEnumLiteral:
26034 return ira->codegen->builtin_types.entry_enum_literal;
26035 default:
26036 if ((err = ir_resolve_const_val(ira->codegen, ira->new_irb.exec, source_instr->source_node, payload, UndefBad)))
26037 return ira->codegen->invalid_inst_gen->value->type;
26038 }
26039 switch (tagTypeId) {
26040 case ZigTypeIdInvalid:
26041 case ZigTypeIdMetaType:
26042 case ZigTypeIdVoid:
26043 case ZigTypeIdBool:
26044 case ZigTypeIdUnreachable:
26045 case ZigTypeIdComptimeFloat:
26046 case ZigTypeIdComptimeInt:
26047 case ZigTypeIdUndefined:
26048 case ZigTypeIdNull:
26049 case ZigTypeIdEnumLiteral:
26050 zig_unreachable();
26025 case ZigTypeIdInt: {26051 case ZigTypeIdInt: {
26026 assert(payload->special == ConstValSpecialStatic);26052 assert(payload->special == ConstValSpecialStatic);
26027 assert(payload->type == ir_type_info_get_type(ira, "Int", nullptr));26053 assert(payload->type == ir_type_info_get_type(ira, "Int", nullptr));
...@@ -26131,14 +26157,6 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI...@@ -26131,14 +26157,6 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI
26131 return ira->codegen->invalid_inst_gen->value->type;26157 return ira->codegen->invalid_inst_gen->value->type;
26132 return get_array_type(ira->codegen, elem_type, bigint_as_u64(bi), sentinel);26158 return get_array_type(ira->codegen, elem_type, bigint_as_u64(bi), sentinel);
26133 }26159 }
26134 case ZigTypeIdComptimeFloat:
26135 return ira->codegen->builtin_types.entry_num_lit_float;
26136 case ZigTypeIdComptimeInt:
26137 return ira->codegen->builtin_types.entry_num_lit_int;
26138 case ZigTypeIdUndefined:
26139 return ira->codegen->builtin_types.entry_undef;
26140 case ZigTypeIdNull:
26141 return ira->codegen->builtin_types.entry_null;
26142 case ZigTypeIdOptional: {26160 case ZigTypeIdOptional: {
26143 assert(payload->special == ConstValSpecialStatic);26161 assert(payload->special == ConstValSpecialStatic);
26144 assert(payload->type == ir_type_info_get_type(ira, "Optional", nullptr));26162 assert(payload->type == ir_type_info_get_type(ira, "Optional", nullptr));
...@@ -26204,8 +26222,6 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI...@@ -26204,8 +26222,6 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI
2620426222
26205 return get_any_frame_type(ira->codegen, child_type);26223 return get_any_frame_type(ira->codegen, child_type);
26206 }26224 }
26207 case ZigTypeIdEnumLiteral:
26208 return ira->codegen->builtin_types.entry_enum_literal;
26209 case ZigTypeIdFnFrame: {26225 case ZigTypeIdFnFrame: {
26210 assert(payload->special == ConstValSpecialStatic);26226 assert(payload->special == ConstValSpecialStatic);
26211 assert(payload->type == ir_type_info_get_type(ira, "Frame", nullptr));26227 assert(payload->type == ir_type_info_get_type(ira, "Frame", nullptr));
test/compile_errors.zig+9
...@@ -2,6 +2,15 @@ const tests = @import("tests.zig");...@@ -2,6 +2,15 @@ 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() union payload is undefined",
6 \\const Foo = @Type(@import("std").builtin.TypeInfo{
7 \\ .Struct = undefined,
8 \\});
9 \\comptime { _ = Foo; }
10 , &[_][]const u8{
11 "tmp.zig:1:50: error: use of undefined value here causes undefined behavior",
12 });
13
5 cases.add("union with too small explicit signed tag type",14 cases.add("union with too small explicit signed tag type",
6 \\const U = union(enum(i2)) {15 \\const U = union(enum(i2)) {
7 \\ A: u8,16 \\ A: u8,