Skip to content

Commit

Permalink
Make snegate / unegate overflow handling consistent with other builti…
Browse files Browse the repository at this point in the history
…ns. (#4744)

Make `int.snegate` ignore the signedness of its operand and
unconditionally check for signed overflow like all the other `int.s*`
builtins do. Fix the prelude implementation of unary `-` for `Core.UInt`
to use `int.unegate` instead of `int.snegate`.

Fix the test for unsigned negate to actually test negating unsigned
integers, and add some tests that unary `-` also works.
  • Loading branch information
zygoloid authored Dec 26, 2024
1 parent 724fc76 commit 7d8d59c
Show file tree
Hide file tree
Showing 4 changed files with 469 additions and 276 deletions.
2 changes: 1 addition & 1 deletion core/prelude/types/uint.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl forall [N:! IntLiteral()] UInt(N) as Mul {
}

impl forall [N:! IntLiteral()] UInt(N) as Negate {
fn Op[self: Self]() -> Self = "int.snegate";
fn Op[self: Self]() -> Self = "int.unegate";
}

impl forall [N:! IntLiteral()] UInt(N) as Sub {
Expand Down
2 changes: 1 addition & 1 deletion toolchain/check/eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ static auto PerformBuiltinUnaryIntOp(Context& context, SemIRLoc loc,

switch (builtin_kind) {
case SemIR::BuiltinFunctionKind::IntSNegate:
if (is_signed && op_val.isMinSignedValue()) {
if (op_val.isMinSignedValue()) {
CARBON_DIAGNOSTIC(CompileTimeIntegerNegateOverflow, Error,
"integer overflow in negation of {0}", TypedInt);
context.emitter().Emit(loc, CompileTimeIntegerNegateOverflow,
Expand Down
Loading

0 comments on commit 7d8d59c

Please sign in to comment.