Wasm: NRE checks inside instance methods need not be generated when the object is this
public static void Main() {
A a = null;
a.x = 1;
}
class A
{
internal int x;
internal void Store(int i)
{
this.x = i;
}
}
In Main attempting to stfld will throw a NRE. In the instance method Store it's impossible isn't it to get in there without an object, so would the compiler be expected to optimise and not put the null check for the stfld here:
.method assembly hidebysig
instance void Store (
int32 i
) cil managed
{
// Method begins at RVA 0x2061
// Code size 8 (0x8)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: stfld int32 C/A::x
IL_0007: ret
} // end of method A::Store
I suppose its not trivial as it would have to know that the first arg is this, but not that hard either to spot at least simple cases like this.
Yes, I think "this is never null" is a fine assumption to make. Technically, one can invoke a method with a null this using reflection (with IL it's also possible without reflection), but many tools make an assumption that a null this is not possible.
Thanks will convert this to an issue.