Bug 8509 – Appending a string to an enum string results in another enum string

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-08-04T16:05:27Z
Last change time
2024-12-13T18:00:52Z
Keywords
pull
Assigned to
No Owner
Creator
Jonathan M Davis
Moved to GitHub: dmd#18457 →

Comments

Comment #0 by issues.dlang — 2012-08-04T16:05:27Z
This code enum E : string { a = "hello" } void main() { E e = E.a ~ " world"; E f = E.a ~ 'w'; } gives this compilation error: q.d(6): Error: cannot implicitly convert expression ("hellow") of type string to E So, the compiler correctly recognizes that the result of appending a character to an E results in a string rather than an E. However, it should _also_ fail on appending a string to it. "hello world" is not a valid E, and appending an arbitrary string to an enum of type string is not generally going to result in a valid enum value, so the result should be typed as string, not the enum type. If an operation on an enum type is not _guaranteed_ to result in a valid enum value, then that operation should be illegal, forcing the programmer to cast when they're sure that the result is valid and protecting them from invalid assignments like in the example above.
Comment #1 by github-bugzilla — 2013-05-19T15:46:02Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/d39288edbee92e241c5a086b297f6afe90c3d764 Fixes Issue 8509 - Appending base type to enum should not result in new enum type. https://github.com/D-Programming-Language/dmd/commit/22f300eb07f6e64f36a3535bee6c00b224249e35 Merge pull request #1240 from AndrejMitrovic/Fix8509 Issue 8509 - Appending base type to enum should not result in new enum type
Comment #2 by andrej.mitrovich — 2013-05-19T17:31:26Z
(In reply to comment #1) > Commits pushed to master at https://github.com/D-Programming-Language/dmd > > https://github.com/D-Programming-Language/dmd/commit/d39288edbee92e241c5a086b297f6afe90c3d764 > Fixes Issue 8509 - Appending base type to enum should not result in new enum > type. > > https://github.com/D-Programming-Language/dmd/commit/22f300eb07f6e64f36a3535bee6c00b224249e35 > Merge pull request #1240 from AndrejMitrovic/Fix8509 > > Issue 8509 - Appending base type to enum should not result in new enum type Partially fixed, but this test-case still works: enum En { a = 1 << 1, b = 1 << 2 } En e = En.a | En.b; Here 'e' becomes an invalid enum value -- cast(En)6.
Comment #3 by robert.schadek — 2024-12-13T18:00:52Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18457 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB