diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2017-10-06 12:41:14 -0400 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2017-10-06 12:41:14 -0400 |
| commit | b4e42042cf27177479f448248601b9be92ca6345 (patch) | |
| tree | c01e6aef384cd51ece2baa1c437272eb2c3a2c5a | |
| parent | 569cf286ff79a10126b9f20f39fa8c64df9b8b25 (diff) | |
| download | zig-b4e42042cf27177479f448248601b9be92ca6345.tar.gz zig-b4e42042cf27177479f448248601b9be92ca6345.zip | |
fix compiler crash when invalid value used
closes #527
| -rw-r--r-- | src/ir.cpp | 5 | ||||
| -rw-r--r-- | test/compile_errors.zig | 45 |
2 files changed, 50 insertions, 0 deletions
diff --git a/src/ir.cpp b/src/ir.cpp index 570783872f..2c2a8d282d 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -11314,6 +11314,8 @@ static TypeTableEntry *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field if (ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr) { ConstExprValue *struct_val = const_ptr_pointee(ira->codegen, ptr_val); + if (type_is_invalid(struct_val->type)) + return ira->codegen->builtin_types.entry_invalid; ConstExprValue *field_val = &struct_val->data.x_struct.fields[field->src_index]; TypeTableEntry *ptr_type = get_pointer_to_type_extra(ira->codegen, field_val->type, is_const, is_volatile, align_bytes, @@ -11732,6 +11734,9 @@ static TypeTableEntry *ir_analyze_instruction_store_ptr(IrAnalyze *ira, IrInstru } ir_add_error(ira, &store_ptr_instruction->base, buf_sprintf("cannot store runtime value in compile time variable")); + ConstExprValue *dest_val = const_ptr_pointee(ira->codegen, &ptr->value); + dest_val->type = ira->codegen->builtin_types.entry_invalid; + return ira->codegen->builtin_types.entry_invalid; } } diff --git a/test/compile_errors.zig b/test/compile_errors.zig index cbde0e9b99..0ed02b70ca 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -2186,4 +2186,49 @@ pub fn addCases(cases: &tests.CompileErrorContext) { , ".tmp_source.zig:3:5: error: alignstack set twice", ".tmp_source.zig:2:5: note: first set here"); + + cases.add("storing runtime value in compile time variable then using it", + \\const Mode = @import("builtin").Mode; + \\ + \\fn Free(comptime filename: []const u8) -> TestCase { + \\ TestCase { + \\ .filename = filename, + \\ .problem_type = ProblemType.Free, + \\ } + \\} + \\ + \\fn LibC(comptime filename: []const u8) -> TestCase { + \\ TestCase { + \\ .filename = filename, + \\ .problem_type = ProblemType.LinkLibC, + \\ } + \\} + \\ + \\const TestCase = struct { + \\ filename: []const u8, + \\ problem_type: ProblemType, + \\}; + \\ + \\const ProblemType = enum { + \\ Free, + \\ LinkLibC, + \\}; + \\ + \\export fn entry() { + \\ const tests = []TestCase { + \\ Free("001"), + \\ Free("002"), + \\ LibC("078"), + \\ Free("116"), + \\ Free("117"), + \\ }; + \\ + \\ for ([]Mode { Mode.Debug, Mode.ReleaseSafe, Mode.ReleaseFast }) |mode| { + \\ inline for (tests) |test_case| { + \\ const foo = test_case.filename ++ ".zig"; + \\ } + \\ } + \\} + , + ".tmp_source.zig:37:16: error: cannot store runtime value in compile time variable"); } |
