From 3e11127d3725a8b9079861b27f66a32119076200 Mon Sep 17 00:00:00 2001 From: Vlad Brezae Date: Fri, 29 Oct 2021 23:02:22 +0300 Subject: [PATCH] [interp] Fix GetType called on ptr constrained to Nullable` We were statically optimizing this call to return the actual constrained class type, which is incorrect for nullables, because boxing of a nullable (as part of the constrained call) actually creates an object with the type of the nullable's value (or null if there is no value). --- src/mono/mono/mini/interp/transform.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/mono/mono/mini/interp/transform.c b/src/mono/mono/mini/interp/transform.c index 6c8d70e68d2111..ee5bc927c125f9 100644 --- a/src/mono/mono/mini/interp/transform.c +++ b/src/mono/mono/mini/interp/transform.c @@ -2454,6 +2454,12 @@ interp_handle_intrinsics (TransformData *td, MonoMethod *target_method, MonoClas *op = MINT_INTRINS_GET_HASHCODE; } else if (!strcmp (tm, "GetType")) { if (constrained_class && m_class_is_valuetype (constrained_class)) { + if (mono_class_is_nullable (constrained_class)) { + // We can't determine the behavior here statically because we don't know if the + // nullable vt has a value or not. If it has a value, the result type is + // m_class_get_cast_class (constrained_class), otherwise GetType should throw NRE. + return FALSE; + } // If constrained_class is valuetype we already know its type. // Resolve GetType to a constant so we can fold type comparisons ERROR_DECL(error);