Make snegate / unegate overflow handling consistent with other builtins. (#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.
This commit is contained in:
Richard Smith
2024-12-26 22:07:47 +00:00
committed by GitHub
parent 724fc7623e
commit 7d8d59cb7e
4 changed files with 469 additions and 276 deletions
+1 -1
View File
@@ -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,