Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escaped check: store to comptime variable depends on runtime condition #14028

Closed
DerryAlex opened this issue Dec 22, 2022 · 1 comment
Closed
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@DerryAlex
Copy link
Contributor

Zig Version

0.10.0, 0.11.0-dev.923+a52dcdd3c

Steps to Reproduce and Observed Behavior

The example comes from Zorrow.

var cell = RefCell(usize, opaque {}).init(20);
// fixme: Borrow no longer alive!
{
    var mutborrow = cell.borrowMut(opaque {});
    defer mutborrow.release();
    try testing.expectEqual(mutborrow.read(opaque {}), 20);
    mutborrow.write(0, opaque {});
    try testing.expectEqual(mutborrow.read(opaque {}), 0);
}

This should be equate to the following

comptime var borrows: usize = 0;
comptime var mutborrows: usize = 0;
if (borrows > 0 and mutborrows > 0) {
    @compileError("Value has already been unwrapped!");
} else if (borrows > 0 or mutborrows > 0) {
    @compileError("There is a borrow[mut] active!");
}
mutborrows += 1;
var value: usize = 20;
comptime var alive: bool = true;
if (!alive) @compileError("BorrowMut no longer alive!");
testing.expectEqual(value, 20) catch |err| {
    alive = false;
    return err;
}
if (!alive) @compileError("BorrowMut no longer alive!");
value = 0;
if (!alive) @compileError("BorrowMut no longer alive!");
testing.expectEqual(value, 0) catch |err| {
    alive = false;
    return err;
}
alive = false;

Observed Behavior

error: BorrowMut no longer alive!
  @compileError("BorrowMut no longer alive!");

This seems to be

comptime var borrows: usize = 0;
comptime var mutborrows: usize = 0;
if (borrows > 0 and mutborrows > 0) {
    @compileError("Value has already been unwrapped!");
} else if (borrows > 0 or mutborrows > 0) {
    @compileError("There is a borrow[mut] active!");
}
mutborrows += 1;
comptime var alive: bool = true;
if (!alive) @compileError("BorrowMut no longer alive!");
alive = false;
if (!alive) @compileError("BorrowMut no longer alive!"); // compile error here
if (!alive) @compileError("BorrowMut no longer alive!");
alive = false;
alive = false;

Expected Behavior

error: store to comptime variable depends on runtime condition
  defer mutborrow.release();
note: runtime condition here
  try testing.expectEqual(mutborrow.read(opaque {}), 20);
@DerryAlex DerryAlex added the bug Observed behavior contradicts documented or intended behavior label Dec 22, 2022
@Vexu
Copy link
Member

Vexu commented Dec 22, 2022

That library abuses bugs in the Zig compiler (#7396) and is not expected to work.

@Vexu Vexu closed this as completed Dec 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

2 participants