authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-25 19:03:56-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-25 19:03:56-04:00
logfd4c5f54f05c598c83188409afffea37d4334949
treeb4febf2c70fbd7320c8a87c76aa08c97f752f177
parent0a773259167158f47198ddd3564405604eb03014
signaturelock-open Commit is signed but in an unrecognized format.

all compile error tests passing


2 files changed, 50 insertions(+), 24 deletions(-)

src/ir.cpp+19-4
...@@ -8736,7 +8736,16 @@ static ConstExprValue *ir_exec_const_result(CodeGen *codegen, IrExecutable *exec...@@ -8736,7 +8736,16 @@ static ConstExprValue *ir_exec_const_result(CodeGen *codegen, IrExecutable *exec
8736 return &codegen->invalid_instruction->value;8736 return &codegen->invalid_instruction->value;
8737 }8737 }
8738 return &value->value;8738 return &value->value;
8739 } else if (ir_has_side_effects(instruction) && !instr_is_comptime(instruction)) {8739 } else if (ir_has_side_effects(instruction)) {
8740 if (instr_is_comptime(instruction)) {
8741 switch (instruction->id) {
8742 case IrInstructionIdUnwrapErrPayload:
8743 case IrInstructionIdUnionFieldPtr:
8744 continue;
8745 default:
8746 break;
8747 }
8748 }
8740 exec_add_error_node(codegen, exec, instruction->source_node,8749 exec_add_error_node(codegen, exec, instruction->source_node,
8741 buf_sprintf("unable to evaluate constant expression"));8750 buf_sprintf("unable to evaluate constant expression"));
8742 return &codegen->invalid_instruction->value;8751 return &codegen->invalid_instruction->value;
...@@ -14498,9 +14507,7 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira,...@@ -14498,9 +14507,7 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira,
14498 if (type_is_invalid(result_type)) {14507 if (type_is_invalid(result_type)) {
14499 result_type = ira->codegen->builtin_types.entry_invalid;14508 result_type = ira->codegen->builtin_types.entry_invalid;
14500 } else if (result_type->id == ZigTypeIdUnreachable || result_type->id == ZigTypeIdOpaque) {14509 } else if (result_type->id == ZigTypeIdUnreachable || result_type->id == ZigTypeIdOpaque) {
14501 ir_add_error_node(ira, source_node,14510 zig_unreachable();
14502 buf_sprintf("variable of type '%s' not allowed", buf_ptr(&result_type->name)));
14503 result_type = ira->codegen->builtin_types.entry_invalid;
14504 }14511 }
1450514512
14506 ConstExprValue *init_val = nullptr;14513 ConstExprValue *init_val = nullptr;
...@@ -15053,6 +15060,13 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe...@@ -15053,6 +15060,13 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
15053 case ResultLocIdVar: {15060 case ResultLocIdVar: {
15054 ResultLocVar *result_loc_var = reinterpret_cast<ResultLocVar *>(result_loc);15061 ResultLocVar *result_loc_var = reinterpret_cast<ResultLocVar *>(result_loc);
15055 assert(result_loc->source_instruction->id == IrInstructionIdAllocaSrc);15062 assert(result_loc->source_instruction->id == IrInstructionIdAllocaSrc);
15063
15064 if (value_type->id == ZigTypeIdUnreachable || value_type->id == ZigTypeIdOpaque) {
15065 ir_add_error(ira, result_loc->source_instruction,
15066 buf_sprintf("variable of type '%s' not allowed", buf_ptr(&value_type->name)));
15067 return ira->codegen->invalid_instruction;
15068 }
15069
15056 IrInstructionAllocaSrc *alloca_src =15070 IrInstructionAllocaSrc *alloca_src =
15057 reinterpret_cast<IrInstructionAllocaSrc *>(result_loc->source_instruction);15071 reinterpret_cast<IrInstructionAllocaSrc *>(result_loc->source_instruction);
15058 bool force_comptime;15072 bool force_comptime;
...@@ -15060,6 +15074,7 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe...@@ -15060,6 +15074,7 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
15060 return ira->codegen->invalid_instruction;15074 return ira->codegen->invalid_instruction;
15061 bool is_comptime = force_comptime || (value != nullptr &&15075 bool is_comptime = force_comptime || (value != nullptr &&
15062 value->value.special != ConstValSpecialRuntime && result_loc_var->var->gen_is_const);15076 value->value.special != ConstValSpecialRuntime && result_loc_var->var->gen_is_const);
15077
15063 if (alloca_src->base.child == nullptr || is_comptime) {15078 if (alloca_src->base.child == nullptr || is_comptime) {
15064 uint32_t align = 0;15079 uint32_t align = 0;
15065 if (alloca_src->align != nullptr && !ir_resolve_align(ira, alloca_src->align->child, &align)) {15080 if (alloca_src->align != nullptr && !ir_resolve_align(ira, alloca_src->align->child, &align)) {
test/compile_errors.zig+31-20
...@@ -3880,7 +3880,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -3880,7 +3880,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
3880 \\ return 2;3880 \\ return 2;
3881 \\}3881 \\}
3882 ,3882 ,
3883 "tmp.zig:2:15: error: values of type 'comptime_int' must be comptime known",3883 "tmp.zig:5:17: error: cannot store runtime value in type 'comptime_int'",
3884 );3884 );
38853885
3886 cases.add(3886 cases.add(
...@@ -5132,7 +5132,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -5132,7 +5132,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
5132 \\ const array = [2]u8{1, 2, 3};5132 \\ const array = [2]u8{1, 2, 3};
5133 \\}5133 \\}
5134 ,5134 ,
5135 "tmp.zig:2:24: error: expected [2]u8 literal, found [3]u8 literal",5135 "tmp.zig:2:31: error: index 2 outside array of size 2",
5136 );5136 );
51375137
5138 cases.add(5138 cases.add(
...@@ -5149,36 +5149,47 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -5149,36 +5149,47 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
51495149
5150 cases.add(5150 cases.add(
5151 "non-const variables of things that require const variables",5151 "non-const variables of things that require const variables",
5152 \\const Opaque = @OpaqueType();5152 \\export fn entry1() void {
5153 \\
5154 \\export fn entry(opaque: *Opaque) void {
5155 \\ var m2 = &2;5153 \\ var m2 = &2;
5156 \\ const y: u32 = m2.*;5154 \\}
5157 \\5155 \\export fn entry2() void {
5158 \\ var a = undefined;5156 \\ var a = undefined;
5157 \\}
5158 \\export fn entry3() void {
5159 \\ var b = 1;5159 \\ var b = 1;
5160 \\}
5161 \\export fn entry4() void {
5160 \\ var c = 1.0;5162 \\ var c = 1.0;
5163 \\}
5164 \\export fn entry5() void {
5161 \\ var d = null;5165 \\ var d = null;
5166 \\}
5167 \\export fn entry6(opaque: *Opaque) void {
5162 \\ var e = opaque.*;5168 \\ var e = opaque.*;
5169 \\}
5170 \\export fn entry7() void {
5163 \\ var f = i32;5171 \\ var f = i32;
5172 \\}
5173 \\export fn entry8() void {
5164 \\ var h = (Foo {}).bar;5174 \\ var h = (Foo {}).bar;
5165 \\5175 \\}
5176 \\export fn entry9() void {
5166 \\ var z: noreturn = return;5177 \\ var z: noreturn = return;
5167 \\}5178 \\}
5168 \\5179 \\const Opaque = @OpaqueType();
5169 \\const Foo = struct {5180 \\const Foo = struct {
5170 \\ fn bar(self: *const Foo) void {}5181 \\ fn bar(self: *const Foo) void {}
5171 \\};5182 \\};
5172 ,5183 ,
5173 "tmp.zig:4:4: error: variable of type '*comptime_int' must be const or comptime",5184 "tmp.zig:2:4: error: variable of type '*comptime_int' must be const or comptime",
5174 "tmp.zig:7:4: error: variable of type '(undefined)' must be const or comptime",5185 "tmp.zig:5:4: error: variable of type '(undefined)' must be const or comptime",
5175 "tmp.zig:8:4: error: variable of type 'comptime_int' must be const or comptime",5186 "tmp.zig:8:4: error: variable of type 'comptime_int' must be const or comptime",
5176 "tmp.zig:9:4: error: variable of type 'comptime_float' must be const or comptime",5187 "tmp.zig:11:4: error: variable of type 'comptime_float' must be const or comptime",
5177 "tmp.zig:10:4: error: variable of type '(null)' must be const or comptime",5188 "tmp.zig:14:4: error: variable of type '(null)' must be const or comptime",
5178 "tmp.zig:11:4: error: variable of type 'Opaque' not allowed",5189 "tmp.zig:17:4: error: variable of type 'Opaque' not allowed",
5179 "tmp.zig:12:4: error: variable of type 'type' must be const or comptime",5190 "tmp.zig:20:4: error: variable of type 'type' must be const or comptime",
5180 "tmp.zig:13:4: error: variable of type '(bound fn(*const Foo) void)' must be const or comptime",5191 "tmp.zig:23:4: error: variable of type '(bound fn(*const Foo) void)' must be const or comptime",
5181 "tmp.zig:15:4: error: unreachable code",5192 "tmp.zig:26:4: error: unreachable code",
5182 );5193 );
51835194
5184 cases.add(5195 cases.add(
...@@ -5324,7 +5335,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -5324,7 +5335,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
5324 \\ }5335 \\ }
5325 \\}5336 \\}
5326 ,5337 ,
5327 "tmp.zig:37:16: error: cannot store runtime value in compile time variable",5338 "tmp.zig:37:29: error: cannot store runtime value in compile time variable",
5328 );5339 );
53295340
5330 cases.add(5341 cases.add(
...@@ -5948,7 +5959,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -5948,7 +5959,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
5948 \\ const foo = Foo { .Bar = x, .Baz = u8 };5959 \\ const foo = Foo { .Bar = x, .Baz = u8 };
5949 \\}5960 \\}
5950 ,5961 ,
5951 "tmp.zig:7:30: error: unable to evaluate constant expression",5962 "tmp.zig:7:23: error: unable to evaluate constant expression",
5952 );5963 );
59535964
5954 cases.add(5965 cases.add(
...@@ -5962,7 +5973,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -5962,7 +5973,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
5962 \\ const foo = Foo { .Bar = x };5973 \\ const foo = Foo { .Bar = x };
5963 \\}5974 \\}
5964 ,5975 ,
5965 "tmp.zig:7:30: error: unable to evaluate constant expression",5976 "tmp.zig:7:23: error: unable to evaluate constant expression",
5966 );5977 );
59675978
5968 cases.addTest(5979 cases.addTest(