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

Champion "Permit attributes on local functions" (VS 16.8, .NET 5) #1888

Open
3 of 5 tasks
Tracked by #829
gafter opened this issue Sep 25, 2018 · 8 comments
Open
3 of 5 tasks
Tracked by #829

Champion "Permit attributes on local functions" (VS 16.8, .NET 5) #1888

gafter opened this issue Sep 25, 2018 · 8 comments
Assignees
Labels
Implemented Needs ECMA Spec This feature has been implemented in C#, but still needs to be merged into the ECMA specification Proposal champion
Milestone

Comments

@gafter
Copy link
Member

gafter commented Sep 25, 2018

The idea is to permit attributes to be part of the declaration of a local function.

From discussion in LDM today (4/29/2019), we would like to allow attributes on local function parameters, which would help with async-iterator local functions that want to use [EnumeratorCancellation].

Speclet: https://github.com/dotnet/csharplang/blob/master/proposals/csharp-9.0/local-function-attributes.md

Note: we did include support for extern as part of this work after all.

@gafter gafter self-assigned this Sep 25, 2018
@bbarry
Copy link
Contributor

bbarry commented Sep 25, 2018

In addition to permitting attributes we would need to also be able to declare a local function without a body (CS8112). Perhaps only if the function is also extern?

@gafter
Copy link
Member Author

gafter commented Sep 26, 2018

@bbarry That is not part of this proposal.

@agocke agocke self-assigned this Sep 26, 2018
@jcouv
Copy link
Member

jcouv commented Mar 14, 2019

FYI @rynowak (from Razor) mentioned interest in this feature as well and helpfully provided a use case to illustrate:
https://gist.github.com/rynowak/4d4738a57fb482952056ca67573f1d50#file-attributes_for_local_functions-md

@gafter gafter added this to the 8.X candidate milestone Apr 29, 2019
@0xd4d
Copy link

0xd4d commented May 24, 2019

Another use case: Nullable reference types feature sometimes requires attributes on parameters. The following code currently fails to compile with C#8 preview (VS 16.1) but should compile:

using System;
using System.Runtime.CompilerServices;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            // Attributes can't be used here, but are sometimes required when nullable reference types is enabled
            //  CS8205 Attributes are not allowed on local function parameters or type parameters
            static bool LocalFunc([NotNullWhenTrue] out string? result)
            {
                result = null;
                return false;
            }
        }
    }
}

namespace System.Runtime.CompilerServices
{
    [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)]
    public class NotNullWhenTrueAttribute : Attribute
    {
        public NotNullWhenTrueAttribute() { }
    }
}

sharwell added a commit to sharwell/roslyn that referenced this issue Jun 6, 2019
This change allows the PerformanceSensitiveAttribute to be applied to
the method. See dotnet/csharplang#1888.
@gafter gafter modified the milestones: 8.X candidate, 9.0 candidate Aug 28, 2019
@gafter gafter removed their assignment Sep 13, 2019
@jcouv jcouv changed the title Champion "Permit attributes on local functions" Champion "Permit attributes on local functions" (VS 16.8, .NET 5) Sep 1, 2020
@MadsTorgersen MadsTorgersen removed this from the 9.0 candidate milestone Sep 9, 2020
@MadsTorgersen MadsTorgersen added this to the 9.0 milestone Sep 9, 2020
@333fred 333fred added the Implemented Needs ECMA Spec This feature has been implemented in C#, but still needs to be merged into the ECMA specification label Oct 16, 2020
@HobbsCode
Copy link

Local functions attributed with Conditional("DEBUG") are currently required to be static. Why? If they only capture 'this', there is no need for the restriction.

@RikkiGibson
Copy link
Contributor

RikkiGibson commented Dec 3, 2020

Conditional local functions were only considered late in the development in the feature, and for expediency we decided to come up with a simple/coarse rule which could be potentially relaxed later.

If we were to relax this, I think we'd want to consider:

  1. Can we allow usages which just read variables from the enclosing scope?
  2. Can we allow variable writes, but somehow avoid transitioning variables from unassigned to definitely assigned when the local function is conditional?

@333fred
Copy link
Member

333fred commented Dec 3, 2020

It would also need to be a separate feature request.

@HobbsCode
Copy link

Opened discussion regarding Conditional attribute here. #4193

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Implemented Needs ECMA Spec This feature has been implemented in C#, but still needs to be merged into the ECMA specification Proposal champion
Projects
None yet
Development

No branches or pull requests

8 participants