make >> a compile error with any undef arg ; add a bunch of test cases

This commit is contained in:
Justus Klausecker 2025-08-07 02:08:40 +02:00
parent d0586da18e
commit 0ef26d113a
8 changed files with 65 additions and 32 deletions

View File

@ -13826,11 +13826,9 @@ fn zirShr(
else => unreachable,
})).toIntern());
}
if (rhs_val.isUndef(zcu)) switch (air_tag) {
.shr => return pt.undefRef(lhs_ty),
.shr_exact => return sema.failWithUseOfUndef(block, rhs_src, null),
else => unreachable,
};
if (rhs_val.isUndef(zcu)) {
return sema.failWithUseOfUndef(block, rhs_src, null);
}
const bits_val = try pt.intValue(.comptime_int, scalar_ty.intInfo(zcu).bits);
switch (rhs_ty.zigTypeTag(zcu)) {
.int, .comptime_int => {
@ -13849,11 +13847,9 @@ fn zirShr(
var elem_idx: usize = 0;
while (elem_idx < rhs_ty.vectorLen(zcu)) : (elem_idx += 1) {
const rhs_elem = try rhs_val.elemValue(pt, elem_idx);
if (rhs_elem.isUndef(zcu)) switch (air_tag) {
.shr => continue,
.shr_exact => return sema.failWithUseOfUndef(block, rhs_src, elem_idx),
else => unreachable,
};
if (rhs_elem.isUndef(zcu)) {
return sema.failWithUseOfUndef(block, rhs_src, elem_idx);
}
switch (try rhs_elem.orderAgainstZeroSema(pt)) {
.gt => {
if (try rhs_elem.compareHeteroSema(.gte, bits_val, pt)) {
@ -13875,11 +13871,7 @@ fn zirShr(
return sema.fail(block, src, "LHS of shift must be a fixed-width integer type, or RHS must be comptime-known", .{});
}
if (maybe_lhs_val) |lhs_val| {
switch (air_tag) {
.shr => if (lhs_val.isUndef(zcu)) return pt.undefRef(lhs_ty),
.shr_exact => try sema.checkAllScalarsDefined(block, lhs_src, lhs_val),
else => unreachable,
}
try sema.checkAllScalarsDefined(block, lhs_src, lhs_val);
}
}
break :rs rhs_src;

View File

@ -1221,16 +1221,9 @@ fn shrScalar(
const pt = sema.pt;
const zcu = pt.zcu;
switch (op) {
.shr => {
if (lhs_val.isUndef(zcu)) return lhs_val;
if (rhs_val.isUndef(zcu)) return pt.undefValue(lhs_ty);
},
.shr_exact => {
if (lhs_val.isUndef(zcu)) return sema.failWithUseOfUndef(block, lhs_src, vec_idx);
if (rhs_val.isUndef(zcu)) return sema.failWithUseOfUndef(block, rhs_src, vec_idx);
},
}
if (lhs_val.isUndef(zcu)) return sema.failWithUseOfUndef(block, lhs_src, vec_idx);
if (rhs_val.isUndef(zcu)) return sema.failWithUseOfUndef(block, rhs_src, vec_idx);
switch (try rhs_val.orderAgainstZeroSema(pt)) {
.gt => {},
.eq => return lhs_val,

View File

@ -196,10 +196,3 @@ test "Saturating Shift Left" {
try expectEqual(170141183460469231731687303715884105727, S.shlSat(@as(i128, 0x2fe6bc5448c55ce18252e2c9d4477750), 0x31));
try expectEqual(0, S.shlSat(@as(i128, 0), 127));
}
test "shift by partially undef vector" {
comptime {
const a: @Vector(1, u8) = .{undefined};
_ = a >> @splat(4);
}
}

View File

@ -0,0 +1,11 @@
comptime {
var a: i64 = undefined;
var b: u6 = undefined;
_ = &a;
_ = &b;
_ = @shlExact(a, b);
}
// error
//
// :6:19: error: use of undefined value here causes illegal behavior

View File

@ -0,0 +1,11 @@
comptime {
var a: i64 = undefined;
var b: u6 = undefined;
_ = &a;
_ = &b;
_ = a << b;
}
// error
//
// :6:9: error: use of undefined value here causes illegal behavior

View File

@ -0,0 +1,11 @@
comptime {
var a: i64 = undefined;
var b: u6 = undefined;
_ = &a;
_ = &b;
_ = @shlWithOverflow(a, b);
}
// error
//
// :6:26: error: use of undefined value here causes illegal behavior

View File

@ -0,0 +1,11 @@
comptime {
var a: i64 = undefined;
var b: u6 = undefined;
_ = &a;
_ = &b;
_ = @shrExact(a, b);
}
// error
//
// :6:19: error: use of undefined value here causes illegal behavior

View File

@ -0,0 +1,11 @@
comptime {
var a: i64 = undefined;
var b: u6 = undefined;
_ = &a;
_ = &b;
_ = a >> b;
}
// error
//
// :6:9: error: use of undefined value here causes illegal behavior