From 0a773259167158f47198ddd3564405604eb03014 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 25 Jun 2019 18:06:03 -0400 Subject: fix several compile error test regressions --- test/compile_errors.zig | 64 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 20 deletions(-) (limited to 'test/compile_errors.zig') diff --git a/test/compile_errors.zig b/test/compile_errors.zig index e85b2f3395..36deec96a5 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -49,16 +49,11 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\const Foo = struct { \\ a: undefined, \\}; - \\const Bar = union { - \\ a: undefined, - \\}; - \\pub fn main() void { + \\export fn entry1() void { \\ const foo: Foo = undefined; - \\ const bar: Bar = undefined; \\} , "tmp.zig:2:8: error: expected type 'type', found '(undefined)'", - "tmp.zig:5:8: error: expected type 'type', found '(undefined)'", ); cases.add( @@ -461,13 +456,25 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\const G = packed struct { \\ x: Enum, \\}; - \\export fn entry() void { + \\export fn entry1() void { \\ var a: A = undefined; + \\} + \\export fn entry2() void { \\ var b: B = undefined; + \\} + \\export fn entry3() void { \\ var r: C = undefined; + \\} + \\export fn entry4() void { \\ var d: D = undefined; + \\} + \\export fn entry5() void { \\ var e: E = undefined; + \\} + \\export fn entry6() void { \\ var f: F = undefined; + \\} + \\export fn entry7() void { \\ var g: G = undefined; \\} \\const S = struct { @@ -489,7 +496,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:14:5: error: non-packed, non-extern struct 'U' not allowed in packed struct; no guaranteed in-memory representation", "tmp.zig:17:5: error: type '?anyerror' not allowed in packed struct; no guaranteed in-memory representation", "tmp.zig:20:5: error: type 'Enum' not allowed in packed struct; no guaranteed in-memory representation", - "tmp.zig:38:14: note: enum declaration does not specify an integer tag type", + "tmp.zig:50:14: note: enum declaration does not specify an integer tag type", ); cases.addCase(x: { @@ -721,7 +728,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ var oops = @bitCast(u7, byte); \\} , - "tmp.zig:2:16: error: destination type 'u7' has 7 bits but source type 'u8' has 8 bits", + "tmp.zig:2:25: error: destination type 'u7' has 7 bits but source type 'u8' has 8 bits", ); cases.add( @@ -1381,7 +1388,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ for (xx) |f| {} \\} , - "tmp.zig:7:15: error: variable of type 'Foo' must be const or comptime", + "tmp.zig:7:5: error: values of type 'Foo' must be comptime known, but index value is runtime known", ); cases.add( @@ -2250,6 +2257,9 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\} \\ \\extern fn bar(x: *void) void { } + \\export fn entry2() void { + \\ bar(&{}); + \\} , "tmp.zig:1:30: error: parameter of type '*void' has 0 bits; not allowed in function with calling convention 'ccc'", "tmp.zig:7:18: error: parameter of type '*void' has 0 bits; not allowed in function with calling convention 'ccc'", @@ -2576,7 +2586,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ \\fn b() void {} , - "tmp.zig:3:5: error: unreachable code", + "tmp.zig:3:6: error: unreachable code", ); cases.add( @@ -2596,7 +2606,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\} , "tmp.zig:3:5: error: use of undeclared identifier 'b'", - "tmp.zig:4:5: error: use of undeclared identifier 'c'", ); cases.add( @@ -2662,7 +2671,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ const a: noreturn = {}; \\} , - "tmp.zig:2:14: error: variable of type 'noreturn' not allowed", + "tmp.zig:2:25: error: expected type 'noreturn', found 'void'", ); cases.add( @@ -2725,9 +2734,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ var bad : bool = undefined; \\ bad[bad] = bad[bad]; \\} + \\export fn g() void { + \\ var bad : bool = undefined; + \\ _ = bad[bad]; + \\} , "tmp.zig:3:8: error: array access of non-array type 'bool'", - "tmp.zig:3:19: error: array access of non-array type 'bool'", + "tmp.zig:7:12: error: array access of non-array type 'bool'", ); cases.add( @@ -2737,9 +2750,14 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ var bad = false; \\ array[bad] = array[bad]; \\} + \\export fn g() void { + \\ var array = "aoeu"; + \\ var bad = false; + \\ _ = array[bad]; + \\} , "tmp.zig:4:11: error: expected type 'usize', found 'bool'", - "tmp.zig:4:24: error: expected type 'usize', found 'bool'", + "tmp.zig:9:15: error: expected type 'usize', found 'bool'", ); cases.add( @@ -2757,12 +2775,14 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "missing else clause", \\fn f(b: bool) void { \\ const x : i32 = if (b) h: { break :h 1; }; + \\} + \\fn g(b: bool) void { \\ const y = if (b) h: { break :h i32(1); }; \\} - \\export fn entry() void { f(true); } + \\export fn entry() void { f(true); g(true); } , "tmp.zig:2:42: error: integer value 1 cannot be implicitly casted to type 'void'", - "tmp.zig:3:15: error: incompatible types: 'i32' and 'void'", + "tmp.zig:5:15: error: incompatible types: 'i32' and 'void'", ); cases.add( @@ -2773,9 +2793,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ a.foo = 1; \\ const y = a.bar; \\} + \\export fn g() void { + \\ var a : A = undefined; + \\ const y = a.bar; + \\} , "tmp.zig:4:6: error: no member named 'foo' in struct 'A'", - "tmp.zig:5:16: error: no member named 'bar' in struct 'A'", + "tmp.zig:9:16: error: no member named 'bar' in struct 'A'", ); cases.add( @@ -2920,7 +2944,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ _ = foo; \\} , - "tmp.zig:1:19: error: type '[3]u16' does not support struct initialization syntax", + "tmp.zig:1:21: error: type '[3]u16' does not support struct initialization syntax", ); cases.add( @@ -3239,7 +3263,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ \\export fn entry() usize { return @sizeOf(@typeOf(Foo)); } , - "tmp.zig:5:25: error: unable to evaluate constant expression", + "tmp.zig:5:18: error: unable to evaluate constant expression", "tmp.zig:2:12: note: called from here", "tmp.zig:2:8: note: called from here", ); -- cgit v1.2.3 From fd4c5f54f05c598c83188409afffea37d4334949 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 25 Jun 2019 19:03:56 -0400 Subject: all compile error tests passing --- src/ir.cpp | 23 ++++++++++++++++++---- test/compile_errors.zig | 51 ++++++++++++++++++++++++++++++------------------- 2 files changed, 50 insertions(+), 24 deletions(-) (limited to 'test/compile_errors.zig') diff --git a/src/ir.cpp b/src/ir.cpp index 4318d612a2..fb2f9f4980 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -8736,7 +8736,16 @@ static ConstExprValue *ir_exec_const_result(CodeGen *codegen, IrExecutable *exec return &codegen->invalid_instruction->value; } return &value->value; - } else if (ir_has_side_effects(instruction) && !instr_is_comptime(instruction)) { + } else if (ir_has_side_effects(instruction)) { + if (instr_is_comptime(instruction)) { + switch (instruction->id) { + case IrInstructionIdUnwrapErrPayload: + case IrInstructionIdUnionFieldPtr: + continue; + default: + break; + } + } exec_add_error_node(codegen, exec, instruction->source_node, buf_sprintf("unable to evaluate constant expression")); return &codegen->invalid_instruction->value; @@ -14498,9 +14507,7 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira, if (type_is_invalid(result_type)) { result_type = ira->codegen->builtin_types.entry_invalid; } else if (result_type->id == ZigTypeIdUnreachable || result_type->id == ZigTypeIdOpaque) { - ir_add_error_node(ira, source_node, - buf_sprintf("variable of type '%s' not allowed", buf_ptr(&result_type->name))); - result_type = ira->codegen->builtin_types.entry_invalid; + zig_unreachable(); } ConstExprValue *init_val = nullptr; @@ -15053,6 +15060,13 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe case ResultLocIdVar: { ResultLocVar *result_loc_var = reinterpret_cast(result_loc); assert(result_loc->source_instruction->id == IrInstructionIdAllocaSrc); + + if (value_type->id == ZigTypeIdUnreachable || value_type->id == ZigTypeIdOpaque) { + ir_add_error(ira, result_loc->source_instruction, + buf_sprintf("variable of type '%s' not allowed", buf_ptr(&value_type->name))); + return ira->codegen->invalid_instruction; + } + IrInstructionAllocaSrc *alloca_src = reinterpret_cast(result_loc->source_instruction); bool force_comptime; @@ -15060,6 +15074,7 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe return ira->codegen->invalid_instruction; bool is_comptime = force_comptime || (value != nullptr && value->value.special != ConstValSpecialRuntime && result_loc_var->var->gen_is_const); + if (alloca_src->base.child == nullptr || is_comptime) { uint32_t align = 0; if (alloca_src->align != nullptr && !ir_resolve_align(ira, alloca_src->align->child, &align)) { diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 36deec96a5..9e92681fdb 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -3880,7 +3880,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ return 2; \\} , - "tmp.zig:2:15: error: values of type 'comptime_int' must be comptime known", + "tmp.zig:5:17: error: cannot store runtime value in type 'comptime_int'", ); cases.add( @@ -5132,7 +5132,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ const array = [2]u8{1, 2, 3}; \\} , - "tmp.zig:2:24: error: expected [2]u8 literal, found [3]u8 literal", + "tmp.zig:2:31: error: index 2 outside array of size 2", ); cases.add( @@ -5149,36 +5149,47 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { cases.add( "non-const variables of things that require const variables", - \\const Opaque = @OpaqueType(); - \\ - \\export fn entry(opaque: *Opaque) void { + \\export fn entry1() void { \\ var m2 = &2; - \\ const y: u32 = m2.*; - \\ + \\} + \\export fn entry2() void { \\ var a = undefined; + \\} + \\export fn entry3() void { \\ var b = 1; + \\} + \\export fn entry4() void { \\ var c = 1.0; + \\} + \\export fn entry5() void { \\ var d = null; + \\} + \\export fn entry6(opaque: *Opaque) void { \\ var e = opaque.*; + \\} + \\export fn entry7() void { \\ var f = i32; + \\} + \\export fn entry8() void { \\ var h = (Foo {}).bar; - \\ + \\} + \\export fn entry9() void { \\ var z: noreturn = return; \\} - \\ + \\const Opaque = @OpaqueType(); \\const Foo = struct { \\ fn bar(self: *const Foo) void {} \\}; , - "tmp.zig:4:4: error: variable of type '*comptime_int' must be const or comptime", - "tmp.zig:7:4: error: variable of type '(undefined)' must be const or comptime", + "tmp.zig:2:4: error: variable of type '*comptime_int' must be const or comptime", + "tmp.zig:5:4: error: variable of type '(undefined)' must be const or comptime", "tmp.zig:8:4: error: variable of type 'comptime_int' must be const or comptime", - "tmp.zig:9:4: error: variable of type 'comptime_float' must be const or comptime", - "tmp.zig:10:4: error: variable of type '(null)' must be const or comptime", - "tmp.zig:11:4: error: variable of type 'Opaque' not allowed", - "tmp.zig:12:4: error: variable of type 'type' must be const or comptime", - "tmp.zig:13:4: error: variable of type '(bound fn(*const Foo) void)' must be const or comptime", - "tmp.zig:15:4: error: unreachable code", + "tmp.zig:11:4: error: variable of type 'comptime_float' must be const or comptime", + "tmp.zig:14:4: error: variable of type '(null)' must be const or comptime", + "tmp.zig:17:4: error: variable of type 'Opaque' not allowed", + "tmp.zig:20:4: error: variable of type 'type' must be const or comptime", + "tmp.zig:23:4: error: variable of type '(bound fn(*const Foo) void)' must be const or comptime", + "tmp.zig:26:4: error: unreachable code", ); cases.add( @@ -5324,7 +5335,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ } \\} , - "tmp.zig:37:16: error: cannot store runtime value in compile time variable", + "tmp.zig:37:29: error: cannot store runtime value in compile time variable", ); cases.add( @@ -5948,7 +5959,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ const foo = Foo { .Bar = x, .Baz = u8 }; \\} , - "tmp.zig:7:30: error: unable to evaluate constant expression", + "tmp.zig:7:23: error: unable to evaluate constant expression", ); cases.add( @@ -5962,7 +5973,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ const foo = Foo { .Bar = x }; \\} , - "tmp.zig:7:30: error: unable to evaluate constant expression", + "tmp.zig:7:23: error: unable to evaluate constant expression", ); cases.addTest( -- cgit v1.2.3