-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Comments
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 |
@bbarry That is not part of this proposal. |
FYI @rynowak (from Razor) mentioned interest in this feature as well and helpfully provided a use case to illustrate: |
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() { }
}
} |
This change allows the PerformanceSensitiveAttribute to be applied to the method. See dotnet/csharplang#1888.
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. |
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:
|
It would also need to be a separate feature request. |
Opened discussion regarding Conditional attribute here. #4193 |
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.The text was updated successfully, but these errors were encountered: