authorgravatar for 14938807+xackus@users.noreply.github.comxackus <14938807+xackus@users.noreply.github.com> 2020-05-07 01:31:18+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-07 16:39:16-04:00
log2c9effc101f2359f6233fefb47eb850001634b42
tree6e1ce64ee454e4ec73527d8530a35478bc03fd46
parent3aa259d119ff57a5026d6bc87663fc569c0beee5

stage1: handle all cases of invalid struct field default value


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

src/ir.cpp+6
...@@ -21697,6 +21697,9 @@ static IrInstGen *ir_analyze_struct_field_ptr(IrAnalyze *ira, IrInst* source_ins...@@ -21697,6 +21697,9 @@ static IrInstGen *ir_analyze_struct_field_ptr(IrAnalyze *ira, IrInst* source_ins
21697 if (field->is_comptime) {21697 if (field->is_comptime) {
21698 IrInstGen *elem = ir_const(ira, source_instr, field_type);21698 IrInstGen *elem = ir_const(ira, source_instr, field_type);
21699 memoize_field_init_val(ira->codegen, struct_type, field);21699 memoize_field_init_val(ira->codegen, struct_type, field);
21700 if(field->init_val != nullptr && type_is_invalid(field->init_val->type)){
21701 return ira->codegen->invalid_inst_gen;
21702 }
21700 copy_const_val(ira->codegen, elem->value, field->init_val);21703 copy_const_val(ira->codegen, elem->value, field->init_val);
21701 return ir_get_ref2(ira, source_instr, elem, field_type, true, false);21704 return ir_get_ref2(ira, source_instr, elem, field_type, true, false);
21702 }21705 }
...@@ -25053,6 +25056,9 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -25053,6 +25056,9 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
25053 inner_fields[3]->type = get_optional_type2(ira->codegen, struct_field->type_entry);25056 inner_fields[3]->type = get_optional_type2(ira->codegen, struct_field->type_entry);
25054 if (inner_fields[3]->type == nullptr) return ErrorSemanticAnalyzeFail;25057 if (inner_fields[3]->type == nullptr) return ErrorSemanticAnalyzeFail;
25055 memoize_field_init_val(ira->codegen, type_entry, struct_field);25058 memoize_field_init_val(ira->codegen, type_entry, struct_field);
25059 if(struct_field->init_val != nullptr && type_is_invalid(struct_field->init_val->type)){
25060 return ErrorSemanticAnalyzeFail;
25061 }
25056 set_optional_payload(inner_fields[3], struct_field->init_val);25062 set_optional_payload(inner_fields[3], struct_field->init_val);
2505725063
25058 ZigValue *name = create_const_str_lit(ira->codegen, struct_field->name)->data.x_ptr.data.ref.pointee;25064 ZigValue *name = create_const_str_lit(ira->codegen, struct_field->name)->data.x_ptr.data.ref.pointee;
test/compile_errors.zig+14
...@@ -7353,4 +7353,18 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -7353,4 +7353,18 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
7353 ":3:18: error: expected type '[*:0]const u8', found '*[64]u8'",7353 ":3:18: error: expected type '[*:0]const u8', found '*[64]u8'",
7354 ":3:18: note: destination pointer requires a terminating '0' sentinel",7354 ":3:18: note: destination pointer requires a terminating '0' sentinel",
7355 });7355 });
7356
7357 cases.add("issue #5221: invalid struct init type referenced by @typeInfo and passed into function",
7358 \\fn ignore(comptime param: var) void {}
7359 \\
7360 \\export fn foo() void {
7361 \\ const MyStruct = struct {
7362 \\ wrong_type: []u8 = "foo",
7363 \\ };
7364 \\
7365 \\ comptime ignore(@typeInfo(MyStruct).Struct.fields[0]);
7366 \\}
7367 , &[_][]const u8{
7368 ":5:28: error: expected type '[]u8', found '*const [3:0]u8'",
7369 });
7356}7370}