-
Notifications
You must be signed in to change notification settings - Fork 4.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
Allow Obsolete attribute on getters and setters #32472
Comments
The ecma specification seems silent on this:
I guess its opinion on this depends on how you define Member |
the C# 6 draft specification is identical: https://github.com/dotnet/csharplang/blob/master/spec/attributes.md#the-obsolete-attribute |
The behaviour of C# and VB w.r.t. these "special" attributes (CLSCompliant, Conditional and Obsolete, according to the docs) differs greatly.
I think VB's behaviour is somewhat better than C#'s.
|
@jaredpar AttributeTargets doesn't contain the value for accessors. |
@jinujoseph As far as I can tell, attributes on property accessors are allowed if the attribute has Though I think this is not really about roslyn/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberMethodSymbol.cs Line 1167 in 9e50109
roslyn/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberMethodSymbol.cs Lines 1211 to 1228 in 9e50109
|
@svick Thanks for the details there. Hopefully the compiler team can shed some light. 😄 |
I have removed the code disallowing the Obsolete Attribute, and confirmed that everything works as expected, without requiring any further changes: https://github.com/YairHalberstadt/roslyn/tree/AllowObsoleteOnPropertyAccessors If this is considered not to require approval from the LDC, I will open a PR. |
based on @svick's analysis, i'm leaning toward this simply being a bug. As far as i can tell, the rest of the compiler consider accessors to just be methods as far as other attributes go. So it seems reasonable to be consistent there with this specific attribute. Barring something in the language spec itself disallowing this (and i don't see anything https://github.com/dotnet/csharplang/blob/master/spec/attributes.md#the-obsolete-attribute that would make me think that exists), then it seems like this makes sense given how we've interpreted |
The language spec says
A property accessor is neither a type nor a member of a type. So the current implementation is as required by the specification. Arguably, the attribute could be permitted on accessors but ignored on uses of the accessor. Either way, changing this would require a change to the specification. Having said that, I doubt such a change would be very controversial. |
In that case I will close the associated pull request. Will you be able to move this issue to csharplang? |
I see you have already |
Hang on the to pull request. Lets check with the language design group and if approved we may be able to use that implementation. |
Should I close the PR if this is championed? |
We'll discuss this on Jan 23. If the LDM likes it we'll review the PR. |
Thanks |
I'd expect Should it fail to compile if you obsolete both accessors, since you should move the obsoletion to the whole property? Valid configurations would be:
|
Note the exact same question arises if the getter is obsoleted and there is no setter, or vice versa. If obsoletion of both accessors was allowed (which I think is pointless, but kind of like from a symmetry standpoint), I would suggest nameof ought to still work on the property, as the property has not been declared obsolete. But this is a relatively trivial detail TBH. Also, I'm guessing this discussion ought to me be to the championed issue. |
This feels like a pitfall to me. Maybe you need a different message between the two, but what's the meaning of obsoleting all accessors versus obsoleting the property versus both? For a getter-only property as well, why should obsoleting just the accessor be legal or desirable? |
What does VB do? |
https://github.com/dotnet/csharplang/blob/master/meetings/2019/LDM-2019-02-27.md It looks like the LDC has supported this proposal. |
The LDM asked to ensure that this works for |
I'm in the middle of cleaning up and finishing off the original pull request. I intend to allow Obsolete and Deprecated on property accessors, but not event accessors. I also will change current behaviour so that an obsolete or deprecated attribute on any accessor (property or event) will suppress warnings from the usage of obsolete/deprecated symbols in that accessor. A new error will be needed to indicate that deprecated and obsolete attributes cannot be used on event accessors but can be used on property accessors. The error for using obsolete/deprecated on an attribute pre C# 8 will be changed to indicate that this will be allowed in C# 8. No work needs to be done to consume these attributes correctly, as they were already legal in VB. |
@YairHalberstadt That all sounds perfect. |
Fixed in #32571 |
Currently in C# the Obsolete attribute is not allowed on property getters and setters.
Has the error
error CS1667: Attribute 'System.ObsoleteAttribute' is not valid on property or event accessors. It is only valid on 'class, struct, enum, constructor, method, property, indexer, field, event, interface, delegate' declarations.
In VB it is allowed, and seems to work as expected.
It would also be useful, for example when I want to start making a property readonly, and so mark usages of the setter obsolete in the meantime. Also a property setter might have to be declared for eg. XmlSerialization, but I don't want anyone else using it, so I would like to mark it obsolete.
Since the obsolete attribute seems to be unique in this regard among attributes, I'm guessing this is either a compiler or a language feature.
I'm not sure which repository is the correct place for this issue, so I'm asking it here, and if necessary I will move it to csharplang.
The text was updated successfully, but these errors were encountered: