Skip to content

Commit afaef36

Browse files
committed
stage1: compile error for pointer arithmetic on ptr-to-array
See #2018
1 parent fd18252 commit afaef36

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/stage1/ir.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17841,7 +17841,7 @@ static bool is_pointer_arithmetic_allowed(ZigType *lhs_type, IrBinOp op) {
1784117841
return false;
1784217842
switch (lhs_type->data.pointer.ptr_len) {
1784317843
case PtrLenSingle:
17844-
return lhs_type->data.pointer.child_type->id == ZigTypeIdArray;
17844+
return false;
1784517845
case PtrLenUnknown:
1784617846
case PtrLenC:
1784717847
return true;

test/compile_errors.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@ const tests = @import("tests.zig");
22
const std = @import("std");
33

44
pub 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+
515
cases.add("@Type() union payload is undefined",
616
\\const Foo = @Type(@import("std").builtin.TypeInfo{
717
\\ .Struct = undefined,

0 commit comments

Comments
 (0)