Repository navigation
Fix merging null and non-null stack slots - #2631
MichalStrehovsky wants to merge 3 commits into
Conversation
Make StackSlot.Value non-nullable. The fix falls out of that. Fixes dotnet#2624.
| private static void PushUnknown (Stack<StackSlot> stack) | ||
| { | ||
| stack.Push (new StackSlot ()); | ||
| stack.Push (new StackSlot (UnknownValue.Instance)); |
There was a problem hiding this comment.
One would think that the nullable analysis helped me to find these spots, but today I learned that:
Foo f = new();
f.Obj.ToString();
struct Foo
{
public object Obj { get; }
public Foo(object obj) => Obj = obj;
}will crash with a NullReferenceException, #nullable enabled be damned.
There was a problem hiding this comment.
Yeah - nullable on structs is... a lie 😉. The default .ctor on struct is just evil - in many ways.
There was a problem hiding this comment.
We could declare a parameterless .ctor on the struct and make it throw. It doesn't catch all cases (the default initialization will not invoke it), but it would catch all the new StackSlot() calls.
vitek-karas
left a comment
There was a problem hiding this comment.
Thanks a lot @MichalStrehovsky
| private static void PushUnknown (Stack<StackSlot> stack) | ||
| { | ||
| stack.Push (new StackSlot ()); | ||
| stack.Push (new StackSlot (UnknownValue.Instance)); |
There was a problem hiding this comment.
We could declare a parameterless .ctor on the struct and make it throw. It doesn't catch all cases (the default initialization will not invoke it), but it would catch all the new StackSlot() calls.
|
Actually we should probably close this. We're about to merge the damAnalyzer branch where this already works. As I was refactoring linker I "accidentaly" fixed this - added the default .ctor to StackValue and it initialized with UnknownValue.Instance. And I modified the .ctors to not allow null values (just like you do here). linker/src/linker/Linker.Dataflow/MethodBodyScanner.cs Lines 20 to 46 in f678247 |
Makes sense! Learning more about nullable analysis was still a valuable exercise for me. |
Make StackSlot.Value non-nullable. The fix falls out of that.
Fixes #2624.