From 7ce29fe855030dd49ae671f50c4729815e8b4d76 Mon Sep 17 00:00:00 2001 From: Marisa Heit Date: Mon, 1 Aug 2022 21:51:32 -0500 Subject: [PATCH] Add signed->unsigned promotion for binary operators The C-style rules for integer promotion are that when you have a signed int and an unsigned int, if you can't promote to a wider type, then the signed type is promoted to an unsigned type. --- src/common/scripting/backend/codegen.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/common/scripting/backend/codegen.cpp b/src/common/scripting/backend/codegen.cpp index 175b6206ed..efd3ac2378 100644 --- a/src/common/scripting/backend/codegen.cpp +++ b/src/common/scripting/backend/codegen.cpp @@ -2685,6 +2685,19 @@ bool FxBinary::Promote(FCompileContext &ctx, bool forceint) { ValueType = TypeUInt32; } + // If one side is an unsigned 32-bit int and the other side is a signed 32-bit int, the signed side is implicitly converted to unsigned. + else if (!ctx.FromDecorate && left->ValueType == TypeUInt32 && right->ValueType == TypeSInt32) + { + right = new FxIntCast(right, false, false, true); + right = right->Resolve(ctx); + ValueType = TypeUInt32; + } + else if (!ctx.FromDecorate && left->ValueType == TypeSInt32 && right->ValueType == TypeUInt32) + { + left = new FxIntCast(left, false, false, true); + left = left->Resolve(ctx); + ValueType = TypeUInt32; + } else if (left->IsInteger() && right->IsInteger()) { ValueType = TypeSInt32; // Addition and subtraction forces all integer-derived types to signed int.