Skip to content

Chained constructor call emitted as illegal in-body this..ctor(...) when a null-check is hoisted before it #3824

Description

@sailro

Input code

The following compiles fine (Release), but the chained constructor call is not reconstructed when decompiled. The failing member is the CtorChild(string) constructor.

using System;

public class CtorBase
{
    public CtorBase(int a, int b, int c, string s) { }
}

public sealed class CtorChild : CtorBase
{
    public string Value;

    public CtorChild(string value)
        : base(0, value?.Length ?? throw new ArgumentNullException(nameof(value)), value.Length, nameof(value))
    {
        Value = value;
    }
}

Erroneous output

public CtorChild(string value)
{
    if (value == null)
    {
        throw new ArgumentNullException("value");
    }
    base..ctor(0, value.Length, value.Length, "value");
    Value = value;
}

base..ctor(...) is not valid C# (a parse error: base. followed by .ctor), so the output does not compile. The same happens with a : this(...) chain (emitted as this..ctor(...)).

Expected output:

public CtorChild(string value)
    : base(0, value?.Length ?? throw new ArgumentNullException("value"), value.Length, "value")
{
    Value = value;
}

Details

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions