Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

struct in using do not box #3089

Open
OwnageIsMagic opened this issue Sep 25, 2023 · 3 comments
Open

struct in using do not box #3089

OwnageIsMagic opened this issue Sep 25, 2023 · 3 comments
Labels
Bug Decompiler The decompiler engine itself

Comments

@OwnageIsMagic
Copy link

Input code

using System;

using (new Str()) ;

struct Str : IDisposable
{
    public void Dispose() {}
}

Erroneous output

Str str = default(Str);
        try
        {
        }
        finally
        {
// this is wrong
            ((IDisposable)str).Dispose(); 
// structs do not box in using pattern
// see corresponding blog post from Eric Lippert
// https://stackoverflow.com/a/2413844/5647513
        }

Details

Sharplab uses v7
https://sharplab.io/#v2:CYLg1APgAgTAjAWAFDKgBgAQAoB2BTAdwwGUAXAJywEoqMBuVTXQjAJTwDMzKb7Ht8RAMIAbAM7VaDFEjEUArgGNSJChhAYAkgBEAlmIAOAezEBDAEYi8yAN7IMDjFADMTgCwY9hk3moYbAL7IQTLknBhy5Eoq7FwUtvaOLu6e+sZivrSBwcioMBiiYupaXukWVglIjk6uUB6lPn7ZSAFAA=

Also tested in ILSpy version 8.1.1.7464.

@OwnageIsMagic OwnageIsMagic added Bug Decompiler The decompiler engine itself labels Sep 25, 2023
@dgrunwald
Copy link
Member

Unfortunately there's no good way to represent a constrained.call in C#.
Imagine the Str.Dispose method was an explicit interface implementation -- the using statement would still be able to call it without boxing, but how would you do the same without a using statement?

@OwnageIsMagic
Copy link
Author

OwnageIsMagic commented Sep 29, 2023

I think since user intentionally opt-ins (using is C# 1 feature -- always on by default) output should prefer correct semantics vs compile-ability. At least it's possible to drop cast for non explicit implementations and add a comment on explicit.

@OwnageIsMagic OwnageIsMagic changed the title struct in using binds by pattern in Roslyn struct in using do not box Sep 29, 2023
@siegfriedpammer
Copy link
Member

I would keep the cast but add a comment that the cast is due to the constrained prefix...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Decompiler The decompiler engine itself
Projects
None yet
Development

No branches or pull requests

3 participants