Skip to content

Invalid decompilation of struct initialization with init properties #3383

Description

@mmusu3

Input code

class StructInitPropertiesTest
{
    class TypeA
    {
        public int A { get; set; }
        public int B { get; set; }
    }

    struct TypeB
    {
        public int A { get; set; }
        public int B { get; set; }
    }

    struct TypeC
    {
        public int A { get; init; }
        public int B { get; init; }
    }

    static TypeA TestA()
    {
        return new TypeA {
            A = 1,
            B = 2
        };
    }

    static TypeB TestB()
    {
        return new TypeB {
            A = 1,
            B = 2
        };
    }

    static TypeC TestC()
    {
        return new TypeC {
            A = 1,
            B = 2
        };
    }
}

Erroneous output

internal class StructInitPropertiesTest
{
    private class TypeA
    {
        public int A { get; set; }

        public int B { get; set; }
    }

    private struct TypeB
    {
        public int A { get; set; }

        public int B { get; set; }
    }

    private struct TypeC
    {
        public int A { get; init; }

        public int B { get; init; }
    }

    private static TypeA TestA()
    {
        return new TypeA {
            A = 1,
            B = 2
        };
    }

    private static TypeB TestB()
    {
        // No error but could still be improved
        TypeB result = default(TypeB);
        result.A = 1;
        result.B = 2;
        return result;
    }

    private static TypeC TestC()
    {
        TypeC result = default(TypeC);
        result.A = 1; // Error
        result.B = 2; // Error
        return result;
    }
}

Error message: Init-only property or indexer 'StructInitPropertiesTest.TypeC.A' can only be assigned in an object initializer, or on 'this' or 'base' in an instance constructor or an 'init' accessor.

Details

  • Product in use: e.g. ILSpy 9.0 VS extension

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDecompilerThe decompiler engine itself

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions