Skip to content
This repository was archived by the owner on Sep 25, 2026. It is now read-only.

Fix merging null and non-null stack slots - #2631

Closed
MichalStrehovsky wants to merge 3 commits into
dotnet:mainfrom
MichalStrehovsky:fix2624
Closed

MichalStrehovsky wants to merge 3 commits into
dotnet:mainfrom
MichalStrehovsky:fix2624

Conversation

@MichalStrehovsky

Copy link
Copy Markdown
Member

Make StackSlot.Value non-nullable. The fix falls out of that.

Fixes #2624.

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));

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah - nullable on structs is... a lie 😉. The default .ctor on struct is just evil - in many ways.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 vitek-karas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot @MichalStrehovsky

private static void PushUnknown (Stack<StackSlot> stack)
{
stack.Push (new StackSlot ());
stack.Push (new StackSlot (UnknownValue.Instance));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

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).

readonly struct StackSlot
{
public MultiValue Value { get; }
/// <summary>
/// True if the value is on the stack as a byref
/// </summary>
public bool IsByRef { get; }
public StackSlot ()
{
Value = new MultiValue (UnknownValue.Instance);
IsByRef = false;
}
public StackSlot (SingleValue value, bool isByRef = false)
{
Value = new MultiValue (value);
IsByRef = isByRef;
}
public StackSlot (MultiValue value, bool isByRef = false)
{
Value = value;
IsByRef = isByRef;
}
}

@MichalStrehovsky

Copy link
Copy Markdown
Member Author

Actually we should probably close this. We're about to merge the damAnalyzer branch where this already works

Makes sense! Learning more about nullable analysis was still a valuable exercise for me.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Merging values on branch ends drops unknowns - underreports warnings

2 participants