Bug 21300 – C++ header generation produce nonsense code on enum with enum as parent

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-10-09T19:26:29Z
Last change time
2020-10-25T18:38:32Z
Keywords
C++, pull
Assigned to
No Owner
Creator
Mathias LANG

Comments

Comment #0 by pro.mathias.lang — 2020-10-09T19:26:29Z
Code, extracted from DMD: ``` extern(C++): enum Color : int { Blue, Red, Green, } enum Classification { Deprecation = Color.Blue, Error = Color.Red, Tip = Color.Green, } ``` Using master: * de9057ea6 - (upstream/master, origin/master, origin/HEAD, master) Merge pull request #11805 from MoonlightSentinel/build-from-run (14 hours ago) <The Dlang Bot> ``` // Automatically generated by Digital Mars D Compiler #pragma once #include <stddef.h> #include <stdint.h> enum class Color { Blue = 0, Red = 1, Green = 2, }; namespace Classification { static Color const Deprecation = (Classification)(Color)0; static Color const Error = (Classification)(Color)1; static Color const Tip = (Classification)(Color)2; }; ``` Compiling this code: ``` In file included from foo.cpp:1: ./foobar.h:18:39: error: unexpected namespace name 'Classification': expected expression static Color const Deprecation = (Classification)(Color)0; ^ ./foobar.h:18:55: error: 'Color' does not refer to a value static Color const Deprecation = (Classification)(Color)0; ^ ./foobar.h:9:12: note: declared here enum class Color ^ ./foobar.h:19:33: error: unexpected namespace name 'Classification': expected expression static Color const Error = (Classification)(Color)1; ^ ./foobar.h:19:49: error: 'Color' does not refer to a value static Color const Error = (Classification)(Color)1; ^ ./foobar.h:9:12: note: declared here enum class Color ^ ./foobar.h:20:31: error: unexpected namespace name 'Classification': expected expression static Color const Tip = (Classification)(Color)2; ^ ./foobar.h:20:47: error: 'Color' does not refer to a value static Color const Tip = (Classification)(Color)2; ^ ./foobar.h:9:12: note: declared here enum class Color ^ 6 errors generated. ``` Marking as blocker since this is a pattern used in DMD, and dtoh is now required by the CI.
Comment #1 by moonlightsentinel — 2020-10-25T15:42:35Z
Reduced to normal because the header file is not required to be compilable.
Comment #2 by dlang-bot — 2020-10-25T15:52:50Z
@MoonlightSentinel created dlang/dmd pull request #11906 "dtoh: Don't use enum namespaces as types" fixing this issue: - Fix 21300 - Don't cast to enum emitted as namespace (dtoh) The enum members are declared as variables of the base type, so the cast is redundant anyway. https://github.com/dlang/dmd/pull/11906
Comment #3 by dlang-bot — 2020-10-25T18:38:32Z
dlang/dmd pull request #11906 "dtoh: Don't use enum namespaces as types" was merged into master: - dccaf221697979c011f0f0520f8687a970c1f5ff by MoonlightSentinel: Fix 21300 - Don't cast to enum emitted as namespace (dtoh) The enum members are declared as variables of the base type, so the cast is redundant anyway. https://github.com/dlang/dmd/pull/11906