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

Optional ParameterInfo is lost at runtime for local functions #51518

Closed
halter73 opened this issue Feb 26, 2021 · 2 comments · Fixed by #53402
Closed

Optional ParameterInfo is lost at runtime for local functions #51518

halter73 opened this issue Feb 26, 2021 · 2 comments · Fixed by #53402

Comments

@halter73
Copy link
Member

halter73 commented Feb 26, 2021

Version Used:

master (24 Feb 2021) on sharplab.io

Steps to Reproduce:

View this sharplab.io snippet and observe that this:

using System;
public class C {
    public void M() {
        void TestLocalAction(int a = 2)
        {
        }
        
        MapAction((Action<int>)TestMethodAction);
        MapAction((Action<int>)TestLocalAction);
    }
    
    public void TestMethodAction(int a = 2)
    {
    }
    
    public void MapAction(Delegate action)
    {
    }
}

results in this:

// ...
public class C
{
    public void M()
    {
        MapAction(new Action<int>(TestMethodAction));
        MapAction(new Action<int>(<M>g__TestLocalAction|0_0));
    }

    public void TestMethodAction(int a = 2)
    {
    }

    public void MapAction(Delegate action)
    {
    }

    [CompilerGenerated]
    internal static void <M>g__TestLocalAction|0_0(int a)
    {
    }
}

And that <M>g__TestLocalAction|0_0 does not have a default value unlike TestMethodAction.

Expected Behavior:

<M>g__TestLocalAction|0_0 should have int a = 2 in its parameter list.

Actual Behavior:

<M>g__TestLocalAction|0_0 does not have int a = 2 in its parameter lists. This becomes a problem for MapAction if it wants to call TestLocalAction with the default parameter value. Delegate.Method.GetParameters()[0].HasDefaultValue ends up returning false instead of true.

Thanks @Kahbazi for finding this! https://github.com/dotnet/aspnetcore/pull/30434/files#r583502436

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Feb 26, 2021
@jaredpar jaredpar added Bug Feature - Local Functions Local Functions and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Mar 3, 2021
@jaredpar jaredpar added this to the C# 10 milestone Mar 3, 2021
@Youssef1313
Copy link
Member

@jaredpar Is the breaking change here okay?

@jaredpar
Copy link
Member

Yes. There are a few changes we are taking in this area of making sure that emitted closures / local functions better represent the attributes that were initially placed on them. It's in support of scenarios like MapAction.

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.

3 participants