-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[API Proposal]: RequiresLocationAttribute (supporting ref readonly parameters) #85910
Comments
Tagging subscribers to this area: @dotnet/area-system-runtime-compilerservices Issue DetailsBackground and motivationAs part of the new API Proposalnamespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
public sealed class RequiresLocationAttribute : Attribute
{
}
} API Usagen/a Alternative DesignsNo response RisksNo response
|
We're not going to add such an attribute until it's actually used by the language/compiler. |
Of course. This isn't meant to go to to API review yet 🙂 |
Why is this attributed needed? Won't the compiler be synthesizing the attribute if it doesn't exist, as it does with most of the other attributes it needs? API review has typically rejected such attributes in the past. |
Wouldn't that be the same as with eg. |
It's necessary to differentiate between
Yes. This isn't part of a
This particular API impacts calling conventions. True for the moment it is C# only but seems completely reasonable for other languages to follow suit in the future (for all the same reasons C# is making the change). Given that it, from my chair at least, it seems like that should be in the The compiler does synthesize attributes into If we are never going to put this attribute into the runtime then I feel the Happy to hear others thoughts on this 😄 |
Marked as We can push update the milestone and push forward as/when needed. |
Looks good as proposed. Marking the issue as blocked, though, since it's intentional that this attribute not be added until .NET 9 (to give the compiler feature one version of prototyping/experimentation/etc that might affect the public attribute). namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
public sealed class RequiresLocationAttribute : Attribute
{
}
} |
This is the first I've heard of us holding back for a release to give the compiler time to experiment. When did that policy come into place? The compiler feature is going to RTM in |
As part of #85612, where we decided to add all such attributes, but recognizing that there's sometimes churn in the compiler and its needs and runtime locks down much earlier (e.g. we're going to fork main for .NET 9 in the very near future). So for attributes that are coming in later in the release and where we won't have runway to react to any changes the compiler decides to make around the attribute, we're better off just waiting until the compiler has RTM'd and then we can add the attribute, which means the next release. If you can promise there's absolutely nothing about this attribute that will change in any way after today, whether it be name, or attribute usage, or properties, or arguments, or whatever, we could get it in. I just don't see a big advantage to rushing it. This particular attribute is not expected to have widespread use. |
Ah okay. A deliberate slip between when compiler finishes and when we commit to the shape in runtime makes sense. Based on your comment i would expect that in most cases we ship the attribute and compiler feature in same release. Cases like this where we merge too late would be the rarer case. I read the previous comment as waiting a release being the general policy not the reflection of the specifics of this feature. |
It would be ideal to add this in .NET 8 imo if at all possible (or do the alternative (my preferred solution), which I added in an edit). Using it like so: Let's say with .NET 8 I write an API like so: public void X(delegate* managed<ref readonly int, void> ptr); when .NET 9 happens (assuming we add it to corelib here), I now need to add a type forwarding to maintain binary compatibility, even though this is a fully supported feature in .NET 8 and .NET 9 with C# 12 and 13 respectively. The best solution imo would be to: let roslyn generate it every time if used as a modifier, ignore any public definition in a dependency, unless in corelib (ie. < .NET 8), otherwise use the definition from corelib (if >= .NET 8) & have roslyn generate a type forward if it's used as a modifier. If it's not used as a modifier (ie. no uses in as a function pointer parameter), then it doesn't matter which definition is used as it's not significant for binary compatibility purposes, so there would be no need to generate type forwards or worry about which version we're referencing in this case. The main part of this solution is to add it to corelib asap (that way there will be no issues transitioning from .NET 8 to 9, which are the only "supported" .NET versions for the C# version with this feature), I'm happy to add type forwards myself in a library that targets multiple tfms if this is not something we want to burden roslyn with. Edit (background): public void X(delegate* managed<in int, void> ptr);
public void X(delegate* managed<ref readonly int, void> ptr); unless we want it to be meaningless to ever declare a |
The attribute isn't synthesized for function pointers, they require the attribute to be defined manually somewhere (by user or corelib), otherwise it's an error to declare function pointer signature with So there could be a break if user defines the attribute manually, but that seems expected. Adding some more tests to demonstrate this behavior: dotnet/roslyn#69328 |
Out of curiosity: if we do need to add the attribute to .NET 8 to avoid breaking changes with function pointers (which makes perfect sense), does it still make sense to have Roslyn synthesize the attribute at all if it doesn't exist already? 🤔 |
The compiler won't synthesize the attribute for the Users could still define their own public type, but then they'd never be able to remove it or would have to appropriately type forward in the future, etc. |
Yeah no I get that, but what I'm saying is, if due to the type being used as a (and of course, folks downlevel can just polyfill and type forward as usual) |
The support to synthesize the type is general purpose and it is likely more work (and more risk this late in the cycle) to remove that support than it is to simply keep it. It also allows the feature to be used on older frameworks for the power users that do that (noting its still officially an unsupported scenario; but that's never stopped people in the past). Typically the types that aren't synthesized have some pretty tough restrictions, such as being required to be defined in "corelib" (that is the assembly that defines |
Background and motivation
As part of the new
ref readonly
parameters feature (dotnet/csharplang#6010), Roslyn will emit a new[RequiresLocation]
attribute in some cases, all documented in the spec in that linked issue (as well as in the speclet at dotnet/csharplang#7165). This issue tracks adding the attribute to the runtime as well, so it can be shared (just like eg.[RefSafetyRules]
).API Proposal
The text was updated successfully, but these errors were encountered: