From 2ce5f84c2f2ca5470e34194d853d6950f42cf0e8 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 3 Dec 2020 17:07:13 -0700 Subject: [PATCH] stage1: compile error for pointer arithmetic on ptr-to-array See #2018 --- src/stage1/ir.cpp | 2 +- test/compile_errors.zig | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/stage1/ir.cpp b/src/stage1/ir.cpp index 7b0d83d957..a3600326c4 100644 --- a/src/stage1/ir.cpp +++ b/src/stage1/ir.cpp @@ -17778,7 +17778,7 @@ static bool is_pointer_arithmetic_allowed(ZigType *lhs_type, IrBinOp op) { return false; switch (lhs_type->data.pointer.ptr_len) { case PtrLenSingle: - return lhs_type->data.pointer.child_type->id == ZigTypeIdArray; + return false; case PtrLenUnknown: case PtrLenC: return true; diff --git a/test/compile_errors.zig b/test/compile_errors.zig index f81458ae8b..82cfd39792 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -2,6 +2,16 @@ const tests = @import("tests.zig"); const std = @import("std"); pub fn addCases(cases: *tests.CompileErrorContext) void { + cases.add("pointer arithmetic on pointer-to-array", + \\export fn foo() void { + \\ var x: [10]u8 = undefined; + \\ var y = &x; + \\ var z = y + 1; + \\} + , &[_][]const u8{ + "tmp.zig:4:17: error: integer value 1 cannot be coerced to type '*[10]u8'", + }); + cases.add("@Type() union payload is undefined", \\const Foo = @Type(@import("std").builtin.TypeInfo{ \\ .Struct = undefined,