op_equality call with null expression as argument on structs #75635
Unanswered
TactiTac0z
asked this question in
Q&A
Replies: 1 comment 1 reply
-
Structs have an implicit conversion to their nullable form, and operators are lifted to their nullable form as well. So I do expect this to work. That said, it seems like a potential case for a warning wave. |
Beta Was this translation helpful? Give feedback.
1 reply
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
-
Today I noticed something I had missed for a couple days ago. Some code was doing a == null on a type that implements a op_equality(SomeStruct, SomeStruct) method, it seems that evaluates to a nop false. at C# compile time.
https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA+ABATARgLABQGAzAATakDCpA3oaQ+WRiqQLIAUAlPY3QY0EA3AIZRSAFxgBnCaQC8pAHYwA7qQAqMiQGUJUAK5gJ3ANy9BpAJYAzUhymyFipQYA2bngMuWMOAJwcAEQahjBBXObePjBu0jAWPgx+gUEAYiJx4ZGJAL6JibKGxpraesVyifxJTOQ4SKTAEBBupBAADjBQIhLQzhxasuVGcm4AFtIANKVD+iOkUBNeNYLyAHykNpnxpqQA9HsAdMdRNSR1DU0tbZ3dveIAhPIDZXMl41Mzum9yi9LLKwY61IDw4H2cCyWp0Y+QIuSAA==
That seems like a pretty easy mistake to make.
TestStruct is not assignable to null nor would it ever be null.
I would have expected the above to give a "<null> is not assignable to TestStruct" error instead of constant 'false'.
Is this some kind of nullable value type conflict of some sorts? If a == operator(TestStruct, object) overload is added you get an error that seem to imply it's trying to evaluate the equality check as if lhs argument is TestStruct?
Surely I would have expected someone to point this out by now, but then why hasn't it been fixed?
Beta Was this translation helpful? Give feedback.
All reactions