authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-12-03 17:07:13-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-12-03 17:07:13-07:00
logafaef3619458e6e8c79c15944fa871ca6cc5a58a
tree0266ab1af76bdb24a2821fffedb03476e0652ab1
parentfd18252a7165f04c07289c422eb0b808cc245b05

stage1: compile error for pointer arithmetic on ptr-to-array

See #2018

2 files changed, 11 insertions(+), 1 deletions(-)

src/stage1/ir.cpp+1-1
...@@ -17841,7 +17841,7 @@ static bool is_pointer_arithmetic_allowed(ZigType *lhs_type, IrBinOp op) {...@@ -17841,7 +17841,7 @@ static bool is_pointer_arithmetic_allowed(ZigType *lhs_type, IrBinOp op) {
17841 return false;17841 return false;
17842 switch (lhs_type->data.pointer.ptr_len) {17842 switch (lhs_type->data.pointer.ptr_len) {
17843 case PtrLenSingle:17843 case PtrLenSingle:
17844 return lhs_type->data.pointer.child_type->id == ZigTypeIdArray;17844 return false;
17845 case PtrLenUnknown:17845 case PtrLenUnknown:
17846 case PtrLenC:17846 case PtrLenC:
17847 return true;17847 return true;
test/compile_errors.zig+10
...@@ -2,6 +2,16 @@ const tests = @import("tests.zig");...@@ -2,6 +2,16 @@ const tests = @import("tests.zig");
2const std = @import("std");2const std = @import("std");
33
4pub fn addCases(cases: *tests.CompileErrorContext) void {4pub fn addCases(cases: *tests.CompileErrorContext) void {
5 cases.add("pointer arithmetic on pointer-to-array",
6 \\export fn foo() void {
7 \\ var x: [10]u8 = undefined;
8 \\ var y = &x;
9 \\ var z = y + 1;
10 \\}
11 , &[_][]const u8{
12 "tmp.zig:4:17: error: integer value 1 cannot be coerced to type '*[10]u8'",
13 });
14
5 cases.add("@Type() union payload is undefined",15 cases.add("@Type() union payload is undefined",
6 \\const Foo = @Type(@import("std").builtin.TypeInfo{16 \\const Foo = @Type(@import("std").builtin.TypeInfo{
7 \\ .Struct = undefined,17 \\ .Struct = undefined,