stage1: handle all cases of invalid struct field default value

master
xackus 2020-05-07 01:31:18 +02:00 committed by Andrew Kelley
parent 3aa259d119
commit 2c9effc101
2 changed files with 20 additions and 0 deletions

View File

@ -21697,6 +21697,9 @@ static IrInstGen *ir_analyze_struct_field_ptr(IrAnalyze *ira, IrInst* source_ins
if (field->is_comptime) {
IrInstGen *elem = ir_const(ira, source_instr, field_type);
memoize_field_init_val(ira->codegen, struct_type, field);
if(field->init_val != nullptr && type_is_invalid(field->init_val->type)){
return ira->codegen->invalid_inst_gen;
}
copy_const_val(ira->codegen, elem->value, field->init_val);
return ir_get_ref2(ira, source_instr, elem, field_type, true, false);
}
@ -25053,6 +25056,9 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
inner_fields[3]->type = get_optional_type2(ira->codegen, struct_field->type_entry);
if (inner_fields[3]->type == nullptr) return ErrorSemanticAnalyzeFail;
memoize_field_init_val(ira->codegen, type_entry, struct_field);
if(struct_field->init_val != nullptr && type_is_invalid(struct_field->init_val->type)){
return ErrorSemanticAnalyzeFail;
}
set_optional_payload(inner_fields[3], struct_field->init_val);
ZigValue *name = create_const_str_lit(ira->codegen, struct_field->name)->data.x_ptr.data.ref.pointee;

View File

@ -7353,4 +7353,18 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
":3:18: error: expected type '[*:0]const u8', found '*[64]u8'",
":3:18: note: destination pointer requires a terminating '0' sentinel",
});
cases.add("issue #5221: invalid struct init type referenced by @typeInfo and passed into function",
\\fn ignore(comptime param: var) void {}
\\
\\export fn foo() void {
\\ const MyStruct = struct {
\\ wrong_type: []u8 = "foo",
\\ };
\\
\\ comptime ignore(@typeInfo(MyStruct).Struct.fields[0]);
\\}
, &[_][]const u8{
":5:28: error: expected type '[]u8', found '*const [3:0]u8'",
});
}