Bug 22012 – enum: dotExp lookups allow recursive dereference

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-06-10T11:38:28Z
Last change time
2024-12-13T19:16:56Z
Assigned to
No Owner
Creator
Iain Buclaw
Moved to GitHub: dmd#19943 →

Comments

Comment #0 by ibuclaw — 2021-06-10T11:38:28Z
This is valid because enum members are of their parent type. So whilst, strange to look at, at least it is logical. --- enum Theme { na, batman } auto song = Theme.na.na.na.na.na.na.na.na.na.na.na.na.na.na.na.batman; --- A slight variant on the theme is also valid, because when an ident isn't found, the dotExp is passed on to the base type. --- struct Batman { int batman; } enum Theme { na = Batman(0) } auto song = Theme.na.na.na.na.na.na.na.na.na.na.na.na.na.na.na.batman; --- Where an issue with this happens though, is when an enum member shares the same name as a base type member or property. --- struct S { int hide; } enum E : S { hide = S(42) } pragma(msg, E); // E pragma(msg, E.hide); // S(42) pragma(msg, E.hide); // S(42) pragma(msg, E.hide.hide); // S(42) pragma(msg, E.hide.hide.hide); // S(42) pragma(msg, E.hide.hide.hide.hide); // S(42) pragma(msg, (cast(S)E.hide).hide); // 42 --- Adding an issue for posterity. Though this seems like an amusing quirk to the language, rather than a problem that requires fixing.
Comment #1 by dfj1esp02 — 2021-06-10T18:08:12Z
Reproduction with classes: --- class A { int a; } class B:A { enum a=1; } int f() { B b=new B(); return b.a; } static assert(f==1); ---
Comment #2 by robert.schadek — 2024-12-13T19:16:56Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/19943 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB