Bug 24223 – __traits(initSymbol) should work for enum types
Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2023-11-03T01:08:35Z
Last change time
2023-11-04T15:05:04Z
Assigned to
No Owner
Creator
Paul Backus
Comments
Comment #0 by snarwin+bugzilla — 2023-11-03T01:08:35Z
An enum type can have its own default initializer, independent of its base type:
---
enum E : int { a = 123 }
static assert(E.init != int.init);
---
This initializer is currently made available through TypeInfo.initializer:
---
enum E : int { a = 123 }
void main()
{
E e;
const(void)[] initializer = typeid(E).initializer;
assert(cast(const(void)[]) (&e)[0 .. 1] == initializer);
}
---
However, it is not available through __traits(initSymbol), which makes it difficult to access without depending on druntime (e.g., in BetterC code).
Comment #1 by snarwin+bugzilla — 2023-11-03T02:24:09Z
Addendum: it turns out that __traits(initSymbol) will compile without error for enum types whose base types are class, struct, or union types. However, it returns the initializer of the base type, not the initializer of the enum type.
Example:
---
struct S { int n; }
enum E : S { a = S(123) }
void main()
{
assert(__traits(initSymbol, E) is __traits(initSymbol, S));
}
---
Obviously, this behavior is incorrect.
Comment #2 by snarwin+bugzilla — 2023-11-04T15:05:04Z
From kinke on the dlang Slack:
> Yeah just checked - no regular init symbol. The TypeInfo.init is a slice to
> an ad-hoc constant emitted together with the TypeInfo; it's not guaranteed to
> be available if the TypeInfo is never referenced, and doesn't have a
> well-defined mangle.
So, it seems like this is probably not feasible. Closing as WONTFIX.