authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-08-22 15:25:33+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-08-24 11:13:43+03:00
log9589dc4c954c1fd10bdf075c62418aeed42ae96f
tree5ad8adc23a554ed447cf7171d7a72b76c089b960
parent4d7013f7d3e44fc1763011bdf63f5431678ef946

add error checks to `@Type`


2 files changed, 70 insertions(+), 5 deletions(-)

src/ir.cpp+51-5
......@@ -2146,6 +2146,7 @@ static IrInstSrc *ir_build_const_undefined(IrBuilderSrc *irb, Scope *scope, AstN
21462146 IrInstSrcConst *const_instruction = ir_create_instruction<IrInstSrcConst>(irb, scope, source_node);
21472147 ir_instruction_append(irb->current_basic_block, &const_instruction->base);
21482148 const_instruction->value = irb->codegen->intern.for_undefined();
2149 const_instruction->value->special = ConstValSpecialUndef;
21492150 return &const_instruction->base;
21502151}
21512152
......@@ -14916,6 +14917,9 @@ static IrInstGen *ir_analyze_struct_literal_to_struct(IrAnalyze *ira, IrInst* so
1491614917 field_val->parent.data.p_struct.struct_val = const_result->value;
1491714918 field_val->parent.data.p_struct.field_index = dst_field->src_index;
1491814919 field_values[dst_field->src_index] = field_val;
14920 if (field_val->type->id == ZigTypeIdUndefined && dst_field->type_entry->id != ZigTypeIdUndefined) {
14921 field_values[dst_field->src_index]->special = ConstValSpecialUndef;
14922 }
1491914923 } else {
1492014924 is_comptime = false;
1492114925 }
......@@ -15648,7 +15652,7 @@ static IrInstGen *ir_analyze_cast(IrAnalyze *ira, IrInst *source_instr,
1564815652 wanted_type->data.array.len == field_count)
1564915653 {
1565015654 return ir_analyze_struct_literal_to_array(ira, source_instr, value, wanted_type);
15651 } else if (wanted_type->id == ZigTypeIdStruct &&
15655 } else if (wanted_type->id == ZigTypeIdStruct && !is_slice(wanted_type) &&
1565215656 (!is_array_init || field_count == 0))
1565315657 {
1565415658 return ir_analyze_struct_literal_to_struct(ira, source_instr, value, wanted_type);
......@@ -22301,6 +22305,7 @@ static IrInstGen *ir_analyze_container_member_access_inner(IrAnalyze *ira,
2230122305
2230222306static void memoize_field_init_val(CodeGen *codegen, ZigType *container_type, TypeStructField *field) {
2230322307 if (field->init_val != nullptr) return;
22308 if (field->decl_node == nullptr) return;
2230422309 if (field->decl_node->type != NodeTypeStructField) return;
2230522310 AstNode *init_node = field->decl_node->data.struct_field.value;
2230622311 if (init_node == nullptr) return;
......@@ -26016,6 +26021,9 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI
2601626021 assert(payload->special == ConstValSpecialStatic);
2601726022 assert(payload->type == type_info_pointer_type);
2601826023 ZigValue *size_value = get_const_field(ira, source_instr->source_node, payload, "size", 0);
26024 if (size_value == nullptr)
26025 return ira->codegen->invalid_inst_gen->value->type;
26026
2601926027 assert(size_value->type == ir_type_info_get_type(ira, "Size", type_info_pointer_type));
2602026028 BuiltinPtrSize size_enum_index = (BuiltinPtrSize)bigint_as_u32(&size_value->data.x_enum_tag);
2602126029 PtrLen ptr_len = size_enum_index_to_ptr_len(size_enum_index);
......@@ -26099,13 +26107,21 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI
2609926107 assert(payload->special == ConstValSpecialStatic);
2610026108 assert(payload->type == ir_type_info_get_type(ira, "Optional", nullptr));
2610126109 ZigType *child_type = get_const_field_meta_type(ira, source_instr->source_node, payload, "child", 0);
26110 if (type_is_invalid(child_type))
26111 return ira->codegen->invalid_inst_gen->value->type;
2610226112 return get_optional_type(ira->codegen, child_type);
2610326113 }
2610426114 case ZigTypeIdErrorUnion: {
2610526115 assert(payload->special == ConstValSpecialStatic);
2610626116 assert(payload->type == ir_type_info_get_type(ira, "ErrorUnion", nullptr));
2610726117 ZigType *err_set_type = get_const_field_meta_type(ira, source_instr->source_node, payload, "error_set", 0);
26118 if (type_is_invalid(err_set_type))
26119 return ira->codegen->invalid_inst_gen->value->type;
26120
2610826121 ZigType *payload_type = get_const_field_meta_type(ira, source_instr->source_node, payload, "payload", 1);
26122 if (type_is_invalid(payload_type))
26123 return ira->codegen->invalid_inst_gen->value->type;
26124
2610926125 return get_error_union_type(ira->codegen, err_set_type, payload_type);
2611026126 }
2611126127 case ZigTypeIdOpaque: {
......@@ -26119,8 +26135,10 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI
2611926135 assert(payload->special == ConstValSpecialStatic);
2612026136 assert(payload->type == ir_type_info_get_type(ira, "Vector", nullptr));
2612126137 BigInt *len = get_const_field_lit_int(ira, source_instr->source_node, payload, "len", 0);
26138 if (len == nullptr)
26139 return ira->codegen->invalid_inst_gen->value->type;
26140
2612226141 ZigType *child_type = get_const_field_meta_type(ira, source_instr->source_node, payload, "child", 1);
26123 Error err;
2612426142 if ((err = ir_validate_vector_elem_type(ira, source_instr->source_node, child_type))) {
2612526143 return ira->codegen->invalid_inst_gen->value->type;
2612626144 }
......@@ -26130,6 +26148,9 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI
2613026148 assert(payload->special == ConstValSpecialStatic);
2613126149 assert(payload->type == ir_type_info_get_type(ira, "AnyFrame", nullptr));
2613226150 ZigType *child_type = get_const_field_meta_type_optional(ira, source_instr->source_node, payload, "child", 0);
26151 if (child_type != nullptr && type_is_invalid(child_type))
26152 return ira->codegen->invalid_inst_gen->value->type;
26153
2613326154 return get_any_frame_type(ira->codegen, child_type);
2613426155 }
2613526156 case ZigTypeIdEnumLiteral:
......@@ -26138,6 +26159,9 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI
2613826159 assert(payload->special == ConstValSpecialStatic);
2613926160 assert(payload->type == ir_type_info_get_type(ira, "Frame", nullptr));
2614026161 ZigValue *function = get_const_field(ira, source_instr->source_node, payload, "function", 0);
26162 if (function == nullptr)
26163 return ira->codegen->invalid_inst_gen->value->type;
26164
2614126165 assert(function->type->id == ZigTypeIdFn);
2614226166 ZigFn *fn = function->data.x_ptr.data.fn.fn_entry;
2614326167 return get_fn_frame_type(ira->codegen, fn);
......@@ -26172,7 +26196,6 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI
2617226196 assert(error->type == ir_type_info_get_type(ira, "Error", nullptr));
2617326197 ErrorTableEntry *err_entry = heap::c_allocator.create<ErrorTableEntry>();
2617426198 err_entry->decl_node = source_instr->source_node;
26175 Error err;
2617626199 if ((err = get_const_field_buf(ira, source_instr->source_node, error, "name", 0, &err_entry->name)))
2617726200 return ira->codegen->invalid_inst_gen->value->type;
2617826201 auto existing_entry = ira->codegen->error_table.put_unique(&err_entry->name, err_entry);
......@@ -26199,11 +26222,15 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI
2619926222 assert(payload->type == ir_type_info_get_type(ira, "Struct", nullptr));
2620026223
2620126224 ZigValue *layout_value = get_const_field(ira, source_instr->source_node, payload, "layout", 0);
26225 if (layout_value == nullptr)
26226 return ira->codegen->invalid_inst_gen->value->type;
2620226227 assert(layout_value->special == ConstValSpecialStatic);
2620326228 assert(layout_value->type == ir_type_info_get_type(ira, "ContainerLayout", nullptr));
2620426229 ContainerLayout layout = (ContainerLayout)bigint_as_u32(&layout_value->data.x_enum_tag);
2620526230
2620626231 ZigValue *fields_value = get_const_field(ira, source_instr->source_node, payload, "fields", 1);
26232 if (fields_value == nullptr)
26233 return ira->codegen->invalid_inst_gen->value->type;
2620726234 assert(fields_value->special == ConstValSpecialStatic);
2620826235 assert(is_slice(fields_value->type));
2620926236 ZigValue *fields_ptr = fields_value->data.x_struct.fields[slice_ptr_index];
......@@ -26211,6 +26238,8 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI
2621126238 size_t fields_len = bigint_as_usize(&fields_len_value->data.x_bigint);
2621226239
2621326240 ZigValue *decls_value = get_const_field(ira, source_instr->source_node, payload, "decls", 2);
26241 if (decls_value == nullptr)
26242 return ira->codegen->invalid_inst_gen->value->type;
2621426243 assert(decls_value->special == ConstValSpecialStatic);
2621526244 assert(is_slice(decls_value->type));
2621626245 ZigValue *decls_len_value = decls_value->data.x_struct.fields[slice_len_index];
......@@ -26221,7 +26250,8 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI
2622126250 }
2622226251
2622326252 bool is_tuple;
26224 get_const_field_bool(ira, source_instr->source_node, payload, "is_tuple", 3, &is_tuple);
26253 if ((err = get_const_field_bool(ira, source_instr->source_node, payload, "is_tuple", 3, &is_tuple)))
26254 return ira->codegen->invalid_inst_gen->value->type;
2622526255
2622626256 ZigType *entry = new_type_table_entry(ZigTypeIdStruct);
2622726257 buf_init_from_buf(&entry->name,
......@@ -26249,6 +26279,8 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI
2624926279 return ira->codegen->invalid_inst_gen->value->type;
2625026280 field->decl_node = source_instr->source_node;
2625126281 ZigValue *type_value = get_const_field(ira, source_instr->source_node, field_value, "field_type", 1);
26282 if (type_value == nullptr)
26283 return ira->codegen->invalid_inst_gen->value->type;
2625226284 field->type_val = type_value;
2625326285 field->type_entry = type_value->data.x_type;
2625426286 if (entry->data.structure.fields_by_name.put_unique(field->name, field) != nullptr) {
......@@ -26256,6 +26288,8 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI
2625626288 return ira->codegen->invalid_inst_gen->value->type;
2625726289 }
2625826290 ZigValue *default_value = get_const_field(ira, source_instr->source_node, field_value, "default_value", 2);
26291 if (default_value == nullptr)
26292 return ira->codegen->invalid_inst_gen->value->type;
2625926293 if (default_value->type->id == ZigTypeIdNull) {
2626026294 field->init_val = nullptr;
2626126295 } else if (default_value->type->id == ZigTypeIdOptional && default_value->type->data.maybe.child_type == field->type_entry) {
......@@ -26278,6 +26312,9 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI
2627826312 assert(payload->type == ir_type_info_get_type(ira, "Enum", nullptr));
2627926313
2628026314 ZigValue *layout_value = get_const_field(ira, source_instr->source_node, payload, "layout", 0);
26315 if (layout_value == nullptr)
26316 return ira->codegen->invalid_inst_gen->value->type;
26317
2628126318 assert(layout_value->special == ConstValSpecialStatic);
2628226319 assert(layout_value->type == ir_type_info_get_type(ira, "ContainerLayout", nullptr));
2628326320 ContainerLayout layout = (ContainerLayout)bigint_as_u32(&layout_value->data.x_enum_tag);
......@@ -26285,6 +26322,9 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI
2628526322 ZigType *tag_type = get_const_field_meta_type(ira, source_instr->source_node, payload, "tag_type", 1);
2628626323
2628726324 ZigValue *fields_value = get_const_field(ira, source_instr->source_node, payload, "fields", 2);
26325 if (fields_value == nullptr)
26326 return ira->codegen->invalid_inst_gen->value->type;
26327
2628826328 assert(fields_value->special == ConstValSpecialStatic);
2628926329 assert(is_slice(fields_value->type));
2629026330 ZigValue *fields_ptr = fields_value->data.x_struct.fields[slice_ptr_index];
......@@ -26292,6 +26332,9 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI
2629226332 size_t fields_len = bigint_as_usize(&fields_len_value->data.x_bigint);
2629326333
2629426334 ZigValue *decls_value = get_const_field(ira, source_instr->source_node, payload, "decls", 3);
26335 if (decls_value == nullptr)
26336 return ira->codegen->invalid_inst_gen->value->type;
26337
2629526338 assert(decls_value->special == ConstValSpecialStatic);
2629626339 assert(is_slice(decls_value->type));
2629726340 ZigValue *decls_len_value = decls_value->data.x_struct.fields[slice_len_index];
......@@ -26337,7 +26380,10 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI
2633726380 ir_add_error(ira, source_instr, buf_sprintf("duplicate enum field '%s'", buf_ptr(field->name)));
2633826381 return ira->codegen->invalid_inst_gen->value->type;
2633926382 }
26340 field->value = *get_const_field_lit_int(ira, source_instr->source_node, field_value, "value", 1);
26383 BigInt *field_int_value = get_const_field_lit_int(ira, source_instr->source_node, field_value, "value", 1);
26384 if (field_int_value == nullptr)
26385 return ira->codegen->invalid_inst_gen->value->type;
26386 field->value = *field_int_value;
2634126387 }
2634226388
2634326389 return entry;
test/compile_errors.zig+19
......@@ -2,6 +2,25 @@ const tests = @import("tests.zig");
22const std = @import("std");
33
44pub fn addCases(cases: *tests.CompileErrorContext) void {
5 cases.add("@Type with undefined",
6 \\comptime {
7 \\ _ = @Type(.{ .Array = .{ .len = 0, .child = u8, .sentinel = undefined } });
8 \\}
9 \\comptime {
10 \\ _ = @Type(.{
11 \\ .Struct = .{
12 \\ .fields = undefined,
13 \\ .decls = undefined,
14 \\ .is_tuple = false,
15 \\ .layout = .Auto,
16 \\ },
17 \\ });
18 \\}
19 , &[_][]const u8{
20 "tmp.zig:2:16: error: use of undefined value here causes undefined behavior",
21 "tmp.zig:5:16: error: use of undefined value here causes undefined behavior",
22 });
23
524 cases.add("struct with declarations unavailable for @Type",
625 \\export fn entry() void {
726 \\ _ = @Type(@typeInfo(struct { const foo = 1; }));