Skip to content

Commit

Permalink
add error for discarding if/while pointer capture
Browse files Browse the repository at this point in the history
  • Loading branch information
xdBronch authored and DivergentClouds committed Sep 24, 2024
1 parent 070cb24 commit ac5bca6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/std/zig/AstGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6333,8 +6333,10 @@ fn ifExpr(
const token_name_index = payload_token + @intFromBool(payload_is_ref);
const ident_name = try astgen.identAsString(token_name_index);
const token_name_str = tree.tokenSlice(token_name_index);
if (mem.eql(u8, "_", token_name_str))
if (mem.eql(u8, "_", token_name_str)) {
if (payload_is_ref) return astgen.failTok(payload_token, "pointer modifier invalid on discard", .{});
break :s &then_scope.base;
}
try astgen.detectLocalShadowing(&then_scope.base, ident_name, token_name_index, token_name_str, .capture);
payload_val_scope = .{
.parent = &then_scope.base,
Expand All @@ -6357,8 +6359,10 @@ fn ifExpr(
else
.optional_payload_unsafe;
const ident_bytes = tree.tokenSlice(ident_token);
if (mem.eql(u8, "_", ident_bytes))
if (mem.eql(u8, "_", ident_bytes)) {
if (payload_is_ref) return astgen.failTok(payload_token, "pointer modifier invalid on discard", .{});
break :s &then_scope.base;
}
const payload_inst = try then_scope.addUnNode(tag, cond.inst, then_node);
const ident_name = try astgen.identAsString(ident_token);
try astgen.detectLocalShadowing(&then_scope.base, ident_name, ident_token, ident_bytes, .capture);
Expand Down Expand Up @@ -6581,8 +6585,10 @@ fn whileExpr(
opt_payload_inst = payload_inst.toOptional();
const ident_token = payload_token + @intFromBool(payload_is_ref);
const ident_bytes = tree.tokenSlice(ident_token);
if (mem.eql(u8, "_", ident_bytes))
if (mem.eql(u8, "_", ident_bytes)) {
if (payload_is_ref) return astgen.failTok(payload_token, "pointer modifier invalid on discard", .{});
break :s &then_scope.base;
}
const ident_name = try astgen.identAsString(ident_token);
try astgen.detectLocalShadowing(&then_scope.base, ident_name, ident_token, ident_bytes, .capture);
payload_val_scope = .{
Expand Down Expand Up @@ -6611,8 +6617,10 @@ fn whileExpr(
opt_payload_inst = payload_inst.toOptional();
const ident_name = try astgen.identAsString(ident_token);
const ident_bytes = tree.tokenSlice(ident_token);
if (mem.eql(u8, "_", ident_bytes))
if (mem.eql(u8, "_", ident_bytes)) {
if (payload_is_ref) return astgen.failTok(payload_token, "pointer modifier invalid on discard", .{});
break :s &then_scope.base;
}
try astgen.detectLocalShadowing(&then_scope.base, ident_name, ident_token, ident_bytes, .capture);
payload_val_scope = .{
.parent = &then_scope.base,
Expand Down
26 changes: 26 additions & 0 deletions test/cases/compile_errors/capture_by_ref_discard.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export fn a() void {
for (.{}) |*_| {}
}

export fn b() void {
switch (0) {
else => |*_| {},
}
}

export fn c() void {
if (null) |*_| {}
}

export fn d() void {
while (null) |*_| {}
}

// error
// backend=stage2
// target=native
//
// :2:16: error: pointer modifier invalid on discard
// :7:18: error: pointer modifier invalid on discard
// :12:16: error: pointer modifier invalid on discard
// :16:19: error: pointer modifier invalid on discard

0 comments on commit ac5bca6

Please sign in to comment.