Skip to content

Commit bbe4da3

Browse files
steveisokCopilotlewing
authored
[mono][interp] Preserve nullable boxing for GetType (#132732)
Fixes #132461. The interpreter optimized `box T` followed by `Object.GetType()` into a constant type lookup. For `Nullable<T>`, this bypassed nullable boxing semantics and returned `typeof(Nullable<T>)` instead of the underlying type. Exclude `Nullable<T>` from this optimization so it follows the normal boxing path: - A populated nullable returns the underlying type. - A null nullable throws `NullReferenceException`. Adds regression coverage for the direct `box Nullable<T>; call Object.GetType()` IL pattern under the Mono interpreter. > [!NOTE] > This pull request description was generated by GitHub Copilot. Co-authored-by: Copilot App <[email protected]> Co-authored-by: Larry Ewing <[email protected]>
1 parent cad631f commit bbe4da3

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

‎src/mono/mono/mini/interp/transform.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5063,7 +5063,8 @@ interp_handle_box_patterns (TransformData *td, MonoClass *box_class, const unsig
50635063
return FALSE;
50645064
MonoMethod *method = td->inlined_method ? td->inlined_method : td->method;
50655065
MonoMethod *cmethod;
5066-
if (*next_ip == CEE_CALL &&
5066+
if (!mono_class_is_nullable (box_class) &&
5067+
*next_ip == CEE_CALL &&
50675068
(cmethod = interp_get_method (method, read32 (next_ip + 1), image, generic_context, error)) &&
50685069
(cmethod->klass == mono_defaults.object_class) &&
50695070
(strcmp (cmethod->name, "GetType") == 0)) {

‎src/tests/JIT/Directed/nullabletypes/gettype.cs‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
namespace JitTest_Directed_nullabletypes_gettype;
55

66
using System;
7-
using System.Linq;
87
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Runtime.CompilerServices;
910
using Xunit;
1011

1112
class C<T>
@@ -22,6 +23,16 @@ public bool Check()
2223

2324
public class P
2425
{
26+
[MethodImpl(MethodImplOptions.NoInlining)]
27+
private static Type GetNullableType(bool? value) => value.GetType();
28+
29+
[Fact]
30+
public static void BoxedNullableGetType()
31+
{
32+
Assert.Same(typeof(bool), GetNullableType(true));
33+
Assert.Throws<NullReferenceException>(() => GetNullableType(null));
34+
}
35+
2536
[OuterLoop]
2637
[Fact]
2738
public static int TestEntryPoint()
@@ -50,4 +61,3 @@ public static int TestEntryPoint()
5061
return 100;
5162
}
5263
}
53-

0 commit comments

Comments
 (0)