fix initializing undefined and crash when casting to invalid type

closes #408
This commit is contained in:
Andrew Kelley 2017-08-05 16:52:19 -04:00
parent 27e4893ee5
commit ad9f48b74b
3 changed files with 20 additions and 0 deletions

View File

@ -3704,6 +3704,9 @@ void init_const_undefined(CodeGen *g, ConstExprValue *const_val) {
const_val->data.x_array.special = ConstArraySpecialUndef;
} else if (wanted_type->id == TypeTableEntryIdStruct) {
ensure_complete_type(g, wanted_type);
if (type_is_invalid(wanted_type)) {
return;
}
const_val->special = ConstValSpecialStatic;
size_t field_count = wanted_type->data.structure.src_field_count;

View File

@ -6945,6 +6945,9 @@ IrInstruction *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node
FnTableEntry *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name,
IrExecutable *parent_exec)
{
if (expected_type != nullptr && type_is_invalid(expected_type))
return codegen->invalid_instruction;
IrExecutable ir_executable = {0};
ir_executable.source_node = source_node;
ir_executable.parent_exec = parent_exec;
@ -14112,6 +14115,7 @@ TypeTableEntry *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutabl
TypeTableEntry *expected_type, AstNode *expected_type_source_node)
{
assert(!old_exec->invalid);
assert(expected_type == nullptr || !type_is_invalid(expected_type));
IrAnalyze ir_analyze_data = {};
IrAnalyze *ira = &ir_analyze_data;

View File

@ -1918,4 +1918,17 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
".tmp_source.zig:1:13: error: aoeu",
".tmp_source.zig:3:19: note: referenced here",
".tmp_source.zig:7:12: note: referenced here");
cases.add("instantiating an undefined value for an invalid struct that contains itself",
\\const Foo = struct {
\\ x: Foo,
\\};
\\
\\var foo: Foo = undefined;
\\
\\export fn entry() -> usize {
\\ return @sizeOf(@typeOf(foo.x));
\\}
,
".tmp_source.zig:1:13: error: struct 'Foo' contains itself");
}