-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Proposal: Flow attributes on primary constructor parameters to generated fields #73899
Comments
More precisely, I think the proposal is to copy the attributes if compatible with the AttributeTargets. My understanding is that AttributeTargets aren't actually enforced by the runtime, so it is up to the compiler whether or not to respect them.
I think the proposal is broader than just primary constructors, right? From broader context, I think the goal would be, for any "compiler-hoisted variable", if the source is a legal attribute target, and the destination is a legal attribute target, and the |
Yes, I agree it makes sense to extend the proposal to all cases like you described. I don't know all the cases, but I can at least think of the following:
|
I agree we need a broader proposal here vs a specific one for primary constructors. Otherwise we will be back here for closures, records, etc ... Think the better path forward is a broader proposal that lets the IL linker get the info it needs without constraining the compiler from evolving its emit approach |
For comparison, F# encodes the mapping itself in attributes (sharplab.io) although it annotates the properties instead of fields. |
@jaredpar Should we make this issue a draft spec, or would you prefer something in |
I just realized C# already supports Are the implicit type parameters of nested types considered an implementation detail, or behavior specified by the language? For example, this prints G<int>.N.M();
class G<T> {
public class N {
public static void M() {
foreach (var p in typeof(N).GetGenericArguments())
Console.WriteLine(p);
}
}
} |
I opened a broader version of the proposal at #73920. Happy to move it elsewhere if desired. |
Closing this - we can continue discussion in the broader proposal that's a superset of this one: #73920. |
In dotnet/runtime#101861 we encountered a scenario where ILLink produces false positive warnings because annotations on a primary constructor parameter aren't present on a generated field. It sounds like we're open to findind a balance between adjusting implementation details to help out ILLink, and not overly constraining the emit strategy. #46646 discusses a similar issue for annotations on generic parameters.
Here's the most straightforward proposal I could think of: we simply copy the annotation from a primary constructor parameter to the generated field/
propertyif possible. Specifically:For record types, a primary ctor parameter annotation gets copied to the generated property only if the attribute type supportsedit: removed this because record primary ctors supportAttributeTargets.Property
.[parameter: ...]
attribute specifications.AttributeTargets.Field
. If there's no generated field, nothing changes.My hope is that this simple proposal solves the problem without needing any other indication that an annotation should or should not be copied to the field/property. But I don't know if that will cause problems, so I wanted to start a discussion.
The main open questions for me are:
AttributeTargets.Parameter | AttributeTargets.Field
(orAttributeTargets.Parameter | AttributeTargets.Property
) where the semantics are meaningfully different for parameters vs fields (or parameters vs properties)?@agocke @MichalStrehovsky @jaredpar
The text was updated successfully, but these errors were encountered: