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) {
37043704 const_val->data.x_array.special = ConstArraySpecialUndef;
37053705 } else if (wanted_type->id == TypeTableEntryIdStruct) {
37063706 ensure_complete_type(g, wanted_type);
3707 if (type_is_invalid(wanted_type)) {
3708 return;
3709 }
37073710
37083711 const_val->special = ConstValSpecialStatic;
37093712 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
69456945 FnTableEntry *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name,
69466946 IrExecutable *parent_exec)
69476947{
6948 if (expected_type != nullptr && type_is_invalid(expected_type))
6949 return codegen->invalid_instruction;
6950
69486951 IrExecutable ir_executable = {0};
69496952 ir_executable.source_node = source_node;
69506953 ir_executable.parent_exec = parent_exec;
......@@ -14112,6 +14115,7 @@ TypeTableEntry *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutabl
1411214115 TypeTableEntry *expected_type, AstNode *expected_type_source_node)
1411314116{
1411414117 assert(!old_exec->invalid);
14118 assert(expected_type == nullptr || !type_is_invalid(expected_type));
1411514119
1411614120 IrAnalyze ir_analyze_data = {};
1411714121 IrAnalyze *ira = &ir_analyze_data;
test/compile_errors.zig+13
......@@ -1918,4 +1918,17 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
19181918 ".tmp_source.zig:1:13: error: aoeu",
19191919 ".tmp_source.zig:3:19: note: referenced here",
19201920 ".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");
19211934}