compiler bug? readonly. prefix opcode not generated for calls to T::Address()  
Author Message
John Perks





PostPosted: Visual C++ Language, compiler bug? readonly. prefix opcode not generated for calls to T::Address() Top

(Apologies if this is known, but I didn't find it on a search.)

The following methods

generic<typename T>
static void Ref(array<T, 2>^ x) { x[0, 0]->GetType(); }

public static void Ref<T>(T[,] x) { x[0,0].GetType(); }

both result in the following IL:


method public hidebysig static void Ref<T>(!!T[0...,0...] x) cil managed
{
.maxstack 3
L_0000: ldarg.0
L_0001: ldc.i4.0
L_0002: ldc.i4.0
L_0003: call instance !!0& !!T[0...,0...]::Address(int32, int32)
L_0008: constrained !!T
L_000e: callvirt instance [mscorlib]System.Type object::GetType()
L_0013: pop
L_0014: ret
}

The call to Address() is not prefixed with readonly, so when called by:

Ref<Object^>(gcnew array<String^, 2>(1, 1) { { "" } });

Ref<object>(new string[1, 1] { { "" } });

an ArrayTypeMismatchException is thrown.This is because it compiler does not try the weaker condition of obtaining a controlled-mutability managed pointer, but rather tries an exact-match type-check.



Visual C++10  
 
 
John Perks





PostPosted: Visual C++ Language, compiler bug? readonly. prefix opcode not generated for calls to T::Address() Top

...and this problem arises with C++ and C#; the VB compiler properly puts the readonly. prfeix in place.

 
 
Brian Kramer





PostPosted: Visual C++ Language, compiler bug? readonly. prefix opcode not generated for calls to T::Address() Top

I think the best thing to do is to submit a bug under Product Feedback on this.
 
 
John Perks





PostPosted: Visual C++ Language, compiler bug? readonly. prefix opcode not generated for calls to T::Address() Top

Thanks. I just wanted to be sure that it was an actual bug first.

 
 
Brian Kramer





PostPosted: Visual C++ Language, compiler bug? readonly. prefix opcode not generated for calls to T::Address() Top

Well, you made a great argument that's its a bug. It's probably obscure enough to prevent someone in this group to want to go in and analyze. Opening bugs not only forces an investigation, but it ensures that it gets triaged for a fix.