diff --git a/src/coreclr/jit/assertionprop.cpp b/src/coreclr/jit/assertionprop.cpp index ffad32a61b200..ef18f2292ec5a 100644 --- a/src/coreclr/jit/assertionprop.cpp +++ b/src/coreclr/jit/assertionprop.cpp @@ -1271,6 +1271,19 @@ AssertionIndex Compiler::optCreateAssertion(GenTree* op1, } toType = op2->CastToType(); + + // Casts to TYP_UINT produce the same ranges as casts to TYP_INT, + // except in overflow cases which we do not yet handle. To avoid + // issues with the propagation code dropping, e. g., CAST_OVF(uint <- int) + // based on an assertion created from CAST(uint <- ulong), normalize the + // type for the range here. Note that TYP_ULONG theoretically has the same + // problem, but we do not create assertions for it. + // TODO-Cleanup: this assertion is not useful - this code exists to preserve + // previous behavior. Refactor it to stop generating such assertions. + if (toType == TYP_UINT) + { + toType = TYP_INT; + } SUBRANGE_COMMON: if ((assertionKind != OAK_SUBRANGE) && (assertionKind != OAK_EQUAL)) { diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_54842/Runtime_54842.cs b/src/tests/JIT/Regression/JitBlue/Runtime_54842/Runtime_54842.cs new file mode 100644 index 0000000000000..8941e1c2d59e8 --- /dev/null +++ b/src/tests/JIT/Regression/JitBlue/Runtime_54842/Runtime_54842.cs @@ -0,0 +1,32 @@ +using System; +using System.Runtime.CompilerServices; + +public class Runtime_54842 +{ + public static int Main() + { + try + { + DoubleCheckedConvert(uint.MaxValue); + } + catch (OverflowException) + { + return 100; + } + + return 101; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static uint DoubleCheckedConvert(ulong a) + { + var b = (int)checked((uint)a); + + // Make sure the importer spills "b" to a local. + Use(b); + + return checked((uint)b); + } + + private static void Use(int value) { } +} diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_54842/Runtime_54842.csproj b/src/tests/JIT/Regression/JitBlue/Runtime_54842/Runtime_54842.csproj new file mode 100644 index 0000000000000..f3e1cbd44b404 --- /dev/null +++ b/src/tests/JIT/Regression/JitBlue/Runtime_54842/Runtime_54842.csproj @@ -0,0 +1,12 @@ + + + Exe + + + None + True + + + + +