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

C#7 local function generated structures shouldn't be declared as static #13191

Closed
ndepend opened this issue Aug 16, 2016 · 3 comments
Closed
Assignees
Labels
Area-Compilers Community The pull request was submitted by a contributor who is not a Microsoft employee. New Language Feature - Local Functions Local Functions

Comments

@ndepend
Copy link

ndepend commented Aug 16, 2016

Version Used: dev15 preview 3

Steps to Reproduce: I compile code from this blog post.

private static void Main(string[] args)
{
    int localVar = 42;
    int LocalFunction(int arg)
    {
        return localVar * arg;
    }
    Console.WriteLine(LocalFunction(1));
}

Actual Behavior: The structure generated under the wood by the compiler to handle localVar is declared as static (and the field localVar is a non-static field)

Expected Behavior: Such generated structure shouldn't be declared as static.

As the developer of a static analyzer that churns IL code this matters. Unless there is a real technical reason for declaring such structure as static, it'd be cleaner to not do so.


Note: Reflector see the structure as a static class that derives from value type (it is a Reflector bug that it doesn't tell it is a structure).

capture

Our static analyzer that relies on Cecil for IL read, shows that the generated structure is both abstract and sealed (which means static).

capture

@svick
Copy link
Contributor

svick commented Aug 16, 2016

As far as I can tell, the tools are confused, because the type is marked as abstract. Somehow, CLR (and peverify) doesn't mind and treats it as a normal struct.

I think this is a bug, so I have submitted a PR to fix it.

@gafter gafter added Bug Area-Compilers Community The pull request was submitted by a contributor who is not a Microsoft employee. New Language Feature - Local Functions Local Functions labels Aug 17, 2016
@gafter gafter added this to the 2.0 (Preview 5) milestone Aug 17, 2016
@gafter
Copy link
Member

gafter commented Aug 17, 2016

@agocke a community member has submitted a PR to fix this.

@ndepend
Copy link
Author

ndepend commented Dec 9, 2016

FYI I tested with VS2017 RC and the bug is indeed fixed, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Community The pull request was submitted by a contributor who is not a Microsoft employee. New Language Feature - Local Functions Local Functions
Projects
None yet
Development

No branches or pull requests

5 participants