authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-08-05 16:52:19-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-08-05 16:54:50-04:00
logad9f48b74bb5d43b767bdf401002d8bfd57c8813
treeaf4f30fc3ca17ccdcd2824ce23ec859dcc24e191
parent27e4893ee59b965e4c030c43782798db258438ba

fix initializing undefined and crash when casting to invalid type

closes #408

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

src/analyze.cpp+3
...@@ -3704,6 +3704,9 @@ void init_const_undefined(CodeGen *g, ConstExprValue *const_val) {...@@ -3704,6 +3704,9 @@ void init_const_undefined(CodeGen *g, ConstExprValue *const_val) {
3704 const_val->data.x_array.special = ConstArraySpecialUndef;3704 const_val->data.x_array.special = ConstArraySpecialUndef;
3705 } else if (wanted_type->id == TypeTableEntryIdStruct) {3705 } else if (wanted_type->id == TypeTableEntryIdStruct) {
3706 ensure_complete_type(g, wanted_type);3706 ensure_complete_type(g, wanted_type);
3707 if (type_is_invalid(wanted_type)) {
3708 return;
3709 }
37073710
3708 const_val->special = ConstValSpecialStatic;3711 const_val->special = ConstValSpecialStatic;
3709 size_t field_count = wanted_type->data.structure.src_field_count;3712 size_t field_count = wanted_type->data.structure.src_field_count;
src/ir.cpp+4
...@@ -6945,6 +6945,9 @@ IrInstruction *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node...@@ -6945,6 +6945,9 @@ IrInstruction *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node
6945 FnTableEntry *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name,6945 FnTableEntry *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name,
6946 IrExecutable *parent_exec)6946 IrExecutable *parent_exec)
6947{6947{
6948 if (expected_type != nullptr && type_is_invalid(expected_type))
6949 return codegen->invalid_instruction;
6950
6948 IrExecutable ir_executable = {0};6951 IrExecutable ir_executable = {0};
6949 ir_executable.source_node = source_node;6952 ir_executable.source_node = source_node;
6950 ir_executable.parent_exec = parent_exec;6953 ir_executable.parent_exec = parent_exec;
...@@ -14112,6 +14115,7 @@ TypeTableEntry *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutabl...@@ -14112,6 +14115,7 @@ TypeTableEntry *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutabl
14112 TypeTableEntry *expected_type, AstNode *expected_type_source_node)14115 TypeTableEntry *expected_type, AstNode *expected_type_source_node)
14113{14116{
14114 assert(!old_exec->invalid);14117 assert(!old_exec->invalid);
14118 assert(expected_type == nullptr || !type_is_invalid(expected_type));
1411514119
14116 IrAnalyze ir_analyze_data = {};14120 IrAnalyze ir_analyze_data = {};
14117 IrAnalyze *ira = &ir_analyze_data;14121 IrAnalyze *ira = &ir_analyze_data;
test/compile_errors.zig+13
...@@ -1918,4 +1918,17 @@ pub fn addCases(cases: &tests.CompileErrorContext) {...@@ -1918,4 +1918,17 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
1918 ".tmp_source.zig:1:13: error: aoeu",1918 ".tmp_source.zig:1:13: error: aoeu",
1919 ".tmp_source.zig:3:19: note: referenced here",1919 ".tmp_source.zig:3:19: note: referenced here",
1920 ".tmp_source.zig:7:12: note: referenced here");1920 ".tmp_source.zig:7:12: note: referenced here");
1921
1922 cases.add("instantiating an undefined value for an invalid struct that contains itself",
1923 \\const Foo = struct {
1924 \\ x: Foo,
1925 \\};
1926 \\
1927 \\var foo: Foo = undefined;
1928 \\
1929 \\export fn entry() -> usize {
1930 \\ return @sizeOf(@typeOf(foo.x));
1931 \\}
1932 ,
1933 ".tmp_source.zig:1:13: error: struct 'Foo' contains itself");
1921}1934}