fix(0129): logical not is truthiness-aware, not a bit flip
The unary .not arm emitted bool_not (LLVM bitwise Not) for every
operand. Correct on i1; on an error binding — an error-set value, u32
tag at the LLVM level — a bitwise not of a nonzero tag stays nonzero,
so 'if !e' held even on a SET error and its branch read the
uninitialized success value (real segfault in the distribution repo's
sqlite tests). Plain integers had the same hole ('!7' was '~7').
Now: bool keeps bool_not; integers and error-set operands lower as the
truthiness complement (cmp_eq against a typed zero); anything else is
diagnosed instead of silently bit-flipped.
Regression: examples/1057 (set error: !e must not hold; success: !e
holds with a real value; integer truthiness) + examples/1171 (!"text"
diagnosed); both FAIL pre-fix. zig build test 426/426;
tests/run_examples.sh 600/600.
This commit is contained in:
@@ -2030,7 +2030,28 @@ pub fn lowerExpr(self: *Lowering, node: *const Node) Ref {
|
||||
self.suppress_int_fit_check = saved_fit;
|
||||
break :blk switch (uop.op) {
|
||||
.negate => self.builder.emit(.{ .neg = .{ .operand = operand } }, self.inferExprType(uop.operand)),
|
||||
.not => self.builder.emit(.{ .bool_not = .{ .operand = operand } }, .bool),
|
||||
// `!` is LOGICAL not. Only a real bool may go through the
|
||||
// bitwise `bool_not` (i1); an integer-backed operand — an
|
||||
// error binding (u32 tag), a plain integer — lowers as the
|
||||
// truthiness complement `operand == 0`: a bitwise not of a
|
||||
// nonzero tag stays nonzero, so `if !e` held even on a set
|
||||
// error (issue 0129). Anything else is diagnosed.
|
||||
.not => blk2: {
|
||||
const oty = self.inferExprType(uop.operand);
|
||||
if (oty == .bool) {
|
||||
break :blk2 self.builder.emit(.{ .bool_not = .{ .operand = operand } }, .bool);
|
||||
}
|
||||
const int_like = self.isIntEx(oty) or
|
||||
(!oty.isBuiltin() and self.module.types.get(oty) == .error_set);
|
||||
if (int_like) {
|
||||
const zero = self.builder.constInt(0, oty);
|
||||
break :blk2 self.builder.emit(.{ .cmp_eq = .{ .lhs = operand, .rhs = zero } }, .bool);
|
||||
}
|
||||
if (self.diagnostics) |d| {
|
||||
d.addFmt(.err, node.span, "'!' needs a bool, integer, or error operand; got '{s}'", .{self.formatTypeName(oty)});
|
||||
}
|
||||
break :blk2 self.builder.constBool(false);
|
||||
},
|
||||
.bit_not => self.builder.emit(.{ .bit_not = .{ .operand = operand } }, self.inferExprType(uop.operand)),
|
||||
.xx => self.lowerXX(operand, uop.operand),
|
||||
.address_of => blk2: {
|
||||
|
||||
Reference in New Issue
Block a user