You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
static constructors throw NRE if having struct fields and invoked via TypeInitializer #1748
The following code throws NullReferenceException on .NET Core (doesn't throw on .NET Framework).
It this a bug?
usingSystem;structS{}classA{staticTimeSpanx=default;// throws NRE// FYI, generated il is: ldsflda, initobj}classB{staticSx=default;// throws NRE}classC{staticSx;staticC()=>x=default;// throws NRE}classD{// no exception if static fiealds are primitives or reference types// generated il is: ldnull/ldc.i4, stsfldstaticstringx=null;staticinty=0;}staticclassProgram{staticvoidMain(){Console.WriteLine("----");try{typeof(A).TypeInitializer.Invoke(null,null);}catch{Console.WriteLine("A");}// Atry{typeof(B).TypeInitializer.Invoke(null,null);}catch{Console.WriteLine("B");}// Btry{typeof(C).TypeInitializer.Invoke(null,null);}catch{Console.WriteLine("C");}// Ctry{typeof(D).TypeInitializer.Invoke(null,null);}catch{Console.WriteLine("D");}// (no excpetion)Console.WriteLine("----");Aa=newA();Bb=newB();Cc=newC();try{typeof(A).TypeInitializer.Invoke(null,null);}catch{Console.WriteLine("A");}// Atry{typeof(B).TypeInitializer.Invoke(null,null);}catch{Console.WriteLine("B");}// Btry{typeof(C).TypeInitializer.Invoke(null,null);}catch{Console.WriteLine("C");}// (no excpetion)try{typeof(D).TypeInitializer.Invoke(null,null);}catch{Console.WriteLine("D");}// (no excpetion)}}
changed the title [-]static constructors throw NRT if having struct fields and invoked via TypeInitializer[/-][+]static constructors throw NRE if having struct fields and invoked via TypeInitializer[/+]on Jan 15, 2020
Activity
[-]static constructors throw NRT if having struct fields and invoked via TypeInitializer[/-][+]static constructors throw NRE if having struct fields and invoked via TypeInitializer[/+]jkotas commentedon Jan 15, 2020
Yes, this looks like a bug.
jkotas commentedon Jan 15, 2020
We are missing allocation of boxed statics on this path (look for
HasBoxedRegularStatics).