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

static Local Functions are not being emitted as static #38143

Closed
333fred opened this issue Aug 20, 2019 · 7 comments · Fixed by #38147
Closed

static Local Functions are not being emitted as static #38143

333fred opened this issue Aug 20, 2019 · 7 comments · Fixed by #38147
Assignees
Milestone

Comments

@333fred
Copy link
Member

333fred commented Aug 20, 2019

using System;
public class C {
	static void M()
	{
  		static void localFunction() { }
        Action a = localFunction;
	}
}

Capturing the local function in an Action is causing it to be emitted as an instance function on an inner class, instead of being a static method.
https://sharplab.io/#v2:EYLgtghgzgLgpgJwDQxASwDZICYgNQA+AAgEwCMAsAFBEDMABKfQML0De1AkEWQGyMAWegFkAFAEouHKvXqdufQfQwB7AMYQMRAKww0KgHYT29AL7VZlyzxL0I9ALzL1mnXsMBuLuaqmgA==

@333fred
Copy link
Member Author

333fred commented Aug 20, 2019

/cc @cston

@cston cston self-assigned this Aug 20, 2019
@cston cston added this to the 16.4.P1 milestone Aug 20, 2019
@gafter
Copy link
Member

gafter commented Aug 20, 2019

@333fred I can see that this would be unexpected, but it implements a tradeoff (the inner class must be created, but invocations are faster). Why do you classify it as a bug?

@ZacharyPatten
Copy link

I was asking about this code (C# 8.0; .NET Core preview 8), and @333fred concluded there was a bug:

static void Main(string[] args)
{
  static void localFunction() { }
  Action action = localFunction;
  Console.WriteLine(action.Method.IsStatic);
  // Output: False
}

I wasn't sure if that output was intended or not. If that output is intended, that is going to confuse people, and maybe "static" wasn't a very appropriate keyword. :P

Question (since @gafter implied this might not be a bug): If it is intended that "static" local functions be compiled to instance methods instead of a static methods, would the MethodInfo.IsStatic property ever be altered for "static" local functions?

@gafter
Copy link
Member

gafter commented Aug 21, 2019

The C# language won't say anything about the MethodInfo's IsStatic behavior for a delegate created from a local function. Nor when it is created from a lambda. Local functions are not methods. It is possible that this is a bug but it is also likely it is intentional.

@333fred
Copy link
Member Author

333fred commented Aug 21, 2019

@gafter it's a bug because future work intends to allow static local functions to be used for native interop. This would break that scenario, and at least mine and @agocke's understanding was that static local functions would be emitted as static members.

@agocke
Copy link
Member

agocke commented Aug 21, 2019

@gafter This behavior is a bug. The proposal has a mention of the intent here: dotnet/csharplang#1565 (comment)

@tannergooding
Copy link
Member

This was also hit for some interop code being contributed to a personal project of mine. Currently validating delegates don't have a Target so that code can be cleanly converted to use function pointers once they are out.

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

Successfully merging a pull request may close this issue.

6 participants