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
87368736 return &codegen->invalid_instruction->value;
87378737 }
87388738 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 }
87408749 exec_add_error_node(codegen, exec, instruction->source_node,
87418750 buf_sprintf("unable to evaluate constant expression"));
87428751 return &codegen->invalid_instruction->value;
......@@ -14498,9 +14507,7 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira,
1449814507 if (type_is_invalid(result_type)) {
1449914508 result_type = ira->codegen->builtin_types.entry_invalid;
1450014509 } else if (result_type->id == ZigTypeIdUnreachable || result_type->id == ZigTypeIdOpaque) {
14501 ir_add_error_node(ira, source_node,
14502 buf_sprintf("variable of type '%s' not allowed", buf_ptr(&result_type->name)));
14503 result_type = ira->codegen->builtin_types.entry_invalid;
14510 zig_unreachable();
1450414511 }
1450514512
1450614513 ConstExprValue *init_val = nullptr;
......@@ -15053,6 +15060,13 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
1505315060 case ResultLocIdVar: {
1505415061 ResultLocVar *result_loc_var = reinterpret_cast<ResultLocVar *>(result_loc);
1505515062 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
1505615070 IrInstructionAllocaSrc *alloca_src =
1505715071 reinterpret_cast<IrInstructionAllocaSrc *>(result_loc->source_instruction);
1505815072 bool force_comptime;
......@@ -15060,6 +15074,7 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
1506015074 return ira->codegen->invalid_instruction;
1506115075 bool is_comptime = force_comptime || (value != nullptr &&
1506215076 value->value.special != ConstValSpecialRuntime && result_loc_var->var->gen_is_const);
15077
1506315078 if (alloca_src->base.child == nullptr || is_comptime) {
1506415079 uint32_t align = 0;
1506515080 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 {
38803880 \\ return 2;
38813881 \\}
38823882 ,
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'",
38843884 );
38853885
38863886 cases.add(
......@@ -5132,7 +5132,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
51325132 \\ const array = [2]u8{1, 2, 3};
51335133 \\}
51345134 ,
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",
51365136 );
51375137
51385138 cases.add(
......@@ -5149,36 +5149,47 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
51495149
51505150 cases.add(
51515151 "non-const variables of things that require const variables",
5152 \\const Opaque = @OpaqueType();
5153 \\
5154 \\export fn entry(opaque: *Opaque) void {
5152 \\export fn entry1() void {
51555153 \\ var m2 = &2;
5156 \\ const y: u32 = m2.*;
5157 \\
5154 \\}
5155 \\export fn entry2() void {
51585156 \\ var a = undefined;
5157 \\}
5158 \\export fn entry3() void {
51595159 \\ var b = 1;
5160 \\}
5161 \\export fn entry4() void {
51605162 \\ var c = 1.0;
5163 \\}
5164 \\export fn entry5() void {
51615165 \\ var d = null;
5166 \\}
5167 \\export fn entry6(opaque: *Opaque) void {
51625168 \\ var e = opaque.*;
5169 \\}
5170 \\export fn entry7() void {
51635171 \\ var f = i32;
5172 \\}
5173 \\export fn entry8() void {
51645174 \\ var h = (Foo {}).bar;
5165 \\
5175 \\}
5176 \\export fn entry9() void {
51665177 \\ var z: noreturn = return;
51675178 \\}
5168 \\
5179 \\const Opaque = @OpaqueType();
51695180 \\const Foo = struct {
51705181 \\ fn bar(self: *const Foo) void {}
51715182 \\};
51725183 ,
5173 "tmp.zig:4: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",
5184 "tmp.zig:2:4: error: variable of type '*comptime_int' must be const or comptime",
5185 "tmp.zig:5:4: error: variable of type '(undefined)' must be const or comptime",
51755186 "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",
5177 "tmp.zig:10:4: error: variable of type '(null)' must be const or comptime",
5178 "tmp.zig:11:4: error: variable of type 'Opaque' not allowed",
5179 "tmp.zig:12: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",
5181 "tmp.zig:15:4: error: unreachable code",
5187 "tmp.zig:11:4: error: variable of type 'comptime_float' must be const or comptime",
5188 "tmp.zig:14:4: error: variable of type '(null)' must be const or comptime",
5189 "tmp.zig:17:4: error: variable of type 'Opaque' not allowed",
5190 "tmp.zig:20:4: error: variable of type 'type' must be const or comptime",
5191 "tmp.zig:23:4: error: variable of type '(bound fn(*const Foo) void)' must be const or comptime",
5192 "tmp.zig:26:4: error: unreachable code",
51825193 );
51835194
51845195 cases.add(
......@@ -5324,7 +5335,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
53245335 \\ }
53255336 \\}
53265337 ,
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",
53285339 );
53295340
53305341 cases.add(
......@@ -5948,7 +5959,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
59485959 \\ const foo = Foo { .Bar = x, .Baz = u8 };
59495960 \\}
59505961 ,
5951 "tmp.zig:7:30: error: unable to evaluate constant expression",
5962 "tmp.zig:7:23: error: unable to evaluate constant expression",
59525963 );
59535964
59545965 cases.add(
......@@ -5962,7 +5973,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
59625973 \\ const foo = Foo { .Bar = x };
59635974 \\}
59645975 ,
5965 "tmp.zig:7:30: error: unable to evaluate constant expression",
5976 "tmp.zig:7:23: error: unable to evaluate constant expression",
59665977 );
59675978
59685979 cases.addTest(