dotnet / dotnet/csharpstandard

Allow non-decimal literal zeroes to be converted to enum types

Open
#289 1 comment 2 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
815
Forks
99
Avg merge
1d 14h
Merged PRs (30d)
16

Description

@GSPP commented on [Fri Mar 25 2016](https://github.com/dotnet/roslyn/issues/10096)

According to 6.1.3 we can convert `0` implicitly to any enum type but not `0x0`. Also not binary literals. Does this make sense? I think not.

The current compiler allows this: http://tryroslyn.azurewebsites.net/#K4Zwlgdg5gBAygTxAFwKYFsDcAoADsAIwBswBjGUogQxBBgGEYBvbGNmVCYdGAUWZgBfVu3zEyMAG4B7MABMYAWQAUASmYj27fqgCMMALwwADDi1b60iCGlFUAOgDqAJzBoAMpFTK9qs+bZNcx0AJkMTAA9TIIsrGzsnVw8vHxC/GPYMth0AZnD9AFoYXX8Ay2tbBxc3VE8Ib1Qc9IDAlr4OABZw43totvL4qqTalNQO5pas9tQAVm6sKYHKxJq6hpmJtmFBIAA=

> An implicit enumeration conversion permits the decimal-integer-literal 0 to be converted to any enum-type and to any nullable-type whose underlying type is an enum-type.

There are many idiosyncrasies around 6.1.3 due to compiler bugs:
- https://blogs.msdn.microsoft.com/ericlippert/2006/03/28/the-root-of-all-evil-part-one/
- https://blogs.msdn.microsoft.com/ericlippert/2006/03/29/the-root-of-all-evil-part-two/

I believe this particular discrepancy can be fixed cleanly, though, by changing the spec.


Also, do we really need to continue supporting `0.0` and `0m`? This should be so rare that it should not cause breakage in the wild. Maybe we can use the language version setting to strengthen language strictness as well. Hopefully, more and more of this deviant behavior can be phased out over time.

---

@gafter commented on [Mon May 23 2016](https://github.com/dotnet/roslyn/issues/10096#issuecomment-221087010)

The compiler also accepts `(1-1)`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.