Skip to content

Does not handle collection expression in call to base constructor properly #3567

@nikitalita

Description

@nikitalita

If a call to a base constructor in a child constructor contains a collection expression, ILSpy will incorrectly attempt to instantiate the collection in the constructor body then make an illegal call to the base constructor's underlying function:

Input Code:

public class TestClass
{
    public readonly float Weight;
    public readonly List<string> Things;
    public TestClass(float weight, List<string> things)
    {
        Weight = weight;
        Things = things;
    }
}

public class ChildClass : TestClass
{
    public ChildClass() : base(1.0f, ["foo", "bar", "baz"]){}
}

Erroneous Output:

public class TestClass
{
    public readonly float Weight;

    public readonly List<string> Things;

    public TestClass(float weight, List<string> things)
    {
        Weight = weight;
        Things = things;
    }
}

public class ChildClass : TestClass
{
    public ChildClass()
    {
        int num = 3;
        List<string> obj = new List<string>(num);
        CollectionsMarshal.SetCount<string>(obj, num);
        global::System.Span<string> span = CollectionsMarshal.AsSpan<string>(obj);
        int num2 = 0;
        span[num2] = "foo";
        num2++;
        span[num2] = "bar";
        span[num2 + 1] = "baz";
        base._002Ector(1f, obj);
    }
}

Details

  • Product in use: ILSpyCmd
  • Version in use: current main @ 61f82d0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions