-
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
Addition of a synthesized override of object.GetHashCode() in records not aligned with the latest design #45602
Comments
It is correct in a sense that the synthesized GetHashCode cannot override object.GetHashCode() due to shadowing. Perhaps the error is not perfect, but this is an error scenario and shouldn't be compiler successfully.
Similarly by design.
This behavior follows the current design and is not related to GetHashCode at all - https://github.com/dotnet/csharplang/blob/master/meetings/2020/LDM-2020-06-22.md#inheritance-between-records-and-classes. I will amend the specification shortly |
Shadowing doesn't affect whether or not it overrides |
I understand your point of view. However, my interpretation of specification is different and I do not think the intent was to generate IL in the way that regular language rules do not support. I agree, the specification is somewhat unclear in this regard (thank you for pointing this out), and I am planning to submit clarifications for the specification. |
As far as I can tell, the specification is clear and unambiguous on this point. But of course it should change if it is not expressing the intent. |
The spec has been updated |
The spec change doesn't really make any sense:
The "example" isn't an example of anything implied by the language specification (either the base language specification or any modifications to it in the records specification). It is confusing because it suggests that the specification implies something that the specification doesn't imply. If you want hiding (or sealing, etc) of |
The specification clearly says: "It is an error if the method doesn't override object.Equals(object? obj)". It really doesn't matter what the reason is, and I don't believe the specification should specify them. The examples are given for the benefit of the reader. If you have suggestions about improving the specification, please open a spec bug and clearly state what do you think the spec should say. As it stands right now, I believe the implementation is in line with the specification. |
I am not writing the specification, I am reading it. This is your spec issue. To be clear, a synthesized method doesn't override anything unless the specification says it does (there is no other text in the base language specification or the record specification to make it do so). So there is your first suggestion: say it does. If you want certain other situations to be an error, identify them and say they are an error. Since you removed the part about it overriding |
Note the |
According to the specification this member is synthesized and not declared. So it doesn’t have a “declaration”. |
Could you help me to clarify the specification? I think you've got the idea what behavior we want it to specify. Everybody is different and three members of the team, which signed off on my PR for the specification change, felt that the specification reflects their understanding for the feature behavior. I'll be happy to clarify it more. |
There are two approaches you can use to clarify the specification.
You should probably pick one of these approaches and use it wherever possible, rather than switching between them arbitrarily. |
The following test, which should compile without any warnings or errors according to the records specification, nevertheless compiles with errors
The first error,
ERR_DoesNotOverrideMethodFromObject
, is incorrect because according to the specification "The record type includes a synthesized override ofobject.GetHashCode()
. ". The next warning is also therefore incorrect. Finally, the errorERR_BadRecordBase
is not supported by the specification, which permits other class types to be used as a base class.The text was updated successfully, but these errors were encountered: