diff --git a/src/coreclr/jit/morph.cpp b/src/coreclr/jit/morph.cpp index 7f6cd2cb3af97e..150989bddca910 100644 --- a/src/coreclr/jit/morph.cpp +++ b/src/coreclr/jit/morph.cpp @@ -577,6 +577,14 @@ GenTree* Compiler::fgMorphExpandCast(GenTreeCast* tree) // we fix this by copying the GC pointer to a non-gc pointer temp. noway_assert(!varTypeIsGC(dstType) && "How can we have a cast to a GCRef here?"); + // we can just retype byref constants + if (oper->IsCnsIntOrI() && oper->TypeIs(TYP_BYREF) && varTypeIsI(dstType)) + { + assert(srcType == TYP_BYREF); + oper->gtType = dstType; + return fgMorphTree(oper); + } + // We generate a store to an int and then do the cast from an int. With this we avoid // the gc problem and we allow casts to bytes, longs, etc... unsigned lclNum = lvaGrabTemp(true DEBUGARG("Cast away GC"));