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
2602226022 return ira->codegen->builtin_types.entry_bool;
2602326023 case ZigTypeIdUnreachable:
2602426024 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();
2602526051 case ZigTypeIdInt: {
2602626052 assert(payload->special == ConstValSpecialStatic);
2602726053 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
2613126157 return ira->codegen->invalid_inst_gen->value->type;
2613226158 return get_array_type(ira->codegen, elem_type, bigint_as_u64(bi), sentinel);
2613326159 }
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;
2614226160 case ZigTypeIdOptional: {
2614326161 assert(payload->special == ConstValSpecialStatic);
2614426162 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
2620426222
2620526223 return get_any_frame_type(ira->codegen, child_type);
2620626224 }
26207 case ZigTypeIdEnumLiteral:
26208 return ira->codegen->builtin_types.entry_enum_literal;
2620926225 case ZigTypeIdFnFrame: {
2621026226 assert(payload->special == ConstValSpecialStatic);
2621126227 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");
22const std = @import("std");
33
44pub 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
514 cases.add("union with too small explicit signed tag type",
615 \\const U = union(enum(i2)) {
716 \\ A: u8,