-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Closed
Labels
bugObserved behavior contradicts documented or intended behaviorObserved behavior contradicts documented or intended behaviorfrontendTokenization, parsing, AstGen, Sema, and Liveness.Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone
Description
Zig Version
0.12.0-dev.1324+5257643d3
Steps to Reproduce and Observed Behavior
Found while debugging a failing test in #17634.
Given the following program:
const std = @import("std");
fn GenericIntApplier(
comptime Context: type,
comptime applyFn: fn (context: Context, arg: u32) void,
) type {
return struct {
context: Context,
const Self = @This();
fn apply(self: Self, arg: u32) void {
self.any().apply(arg);
}
inline fn any(self: *const Self) IntApplier {
return .{
.context = @ptrCast(&self.context),
.applyFn = typeErasedApplyFn,
};
}
fn typeErasedApplyFn(context: *const anyopaque, arg: u32) void {
const ptr: *const Context = @alignCast(@ptrCast(context));
applyFn(ptr.*, arg);
}
};
}
const IntApplier = struct {
context: *const anyopaque,
applyFn: *const fn (context: *const anyopaque, arg: u32) void,
fn apply(ia: IntApplier, arg: u32) void {
ia.applyFn(ia.context, arg);
}
};
const Accumulator = struct {
value: u32,
const Applier = GenericIntApplier(*u32, add);
fn applier(a: *Accumulator) Applier {
return .{ .context = &a.value };
}
fn add(context: *u32, arg: u32) void {
context.* += arg;
}
};
fn s() u32 {
var a: Accumulator = .{ .value = 0 };
const applier = a.applier();
applier.apply(1);
applier.apply(1);
return a.value;
}
test {
try comptime std.testing.expect(s() == 2);
}The test fails. Inserting a @compileLog before returning a.value reveals that the actual value is 1 rather than the expected value of 2.
Expected Behavior
The test should pass.
It seems that the calls to apply (or some part of them) are being incorrectly memoized: as long as applier.apply is called with a different argument each time, the code works as expected, but any subsequent calls with an argument already used will have no effect.
Through bisecting, I've found that this regressed in 405ba26.
This might be further reducible, as there are quite a few moving parts in this example. I'll update this issue later if I'm able to remove any of them.
Metadata
Metadata
Assignees
Labels
bugObserved behavior contradicts documented or intended behaviorObserved behavior contradicts documented or intended behaviorfrontendTokenization, parsing, AstGen, Sema, and Liveness.Tokenization, parsing, AstGen, Sema, and Liveness.