Skip to content

Assignment of NewObjectExpression of value type to by ref parameter generates invalid code #246

@adrianoc

Description

@adrianoc
struct Bar
{
	public Bar(int i) {}
}

class Foo
{
	static void Main()
	{
		Bar b = new Bar(1); // Works
		M(ref b);
	}

	static void M(ref Bar b) => 
		b = new Bar(2); // Generates invalid code
}

generates something like:

//Parameters of 'static void M(ref Bar b) => ...'
il_M_11.Emit(OpCodes.Ldarg_0);
il_M_11.Emit(OpCodes.Ldc_I4, 2);
il_M_11.Emit(OpCodes.Newobj, ctor_Bar_1);
il_M_11.Emit(OpCodes.Stobj); // <-- missing the type `st_Bar_0`
il_M_11.Emit(OpCodes.Ret);

Metadata

Metadata

Assignees

No one assigned

    Labels

    🐛 bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions