Skip to content

Missing detection of using statement with implicit type cast #3385

Description

@mmusu3

Input code

class UsingTest
{
    struct TypeA : IDisposable
    {
        public void Dispose() { }

        public static implicit operator TypeB(in TypeA a) => new();
    }

    struct TypeB { }

    static void Test()
    {
        using (TypeA a = new())
            Empty(a);
    }

    static void Empty(TypeB b) { }
}

Erroneous output

internal class UsingTest
{
    [StructLayout(LayoutKind.Sequential, Size = 1)]
    private struct TypeA : IDisposable
    {
        public void Dispose()
        {
        }

        public static implicit operator TypeB(in TypeA a)
        {
            return default(TypeB);
        }
    }

    [StructLayout(LayoutKind.Sequential, Size = 1)]
    private struct TypeB
    {
    }

    private static void Test()
    {
        TypeA a = default(TypeA);

        try
        {
            Empty(a);
        }
        finally
        {
            ((IDisposable)a/*cast due to .constrained prefix*/).Dispose();
        }
    }

    private static void Empty(TypeB b)
    {
    }
}

This also happens when the types are classes.

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