Bug 20520 – Runtime segfault when taking typeid of a class instanciated within an enum
Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-01-20T15:04:50Z
Last change time
2023-01-14T14:39:43Z
Keywords
backend, pull, wrong-code
Assigned to
No Owner
Creator
Cédric Picard
Comments
Comment #0 by cpicard — 2020-01-20T15:04:50Z
The following code segfaults at runtime. No warning is displayed during compilation so I assume that the operation should be allowed.
class C {}
enum Foo {
Bar = new C()
}
void main() {
pragma(msg, typeid(Foo.Bar)); // Works fine: typeid(C())
auto t = typeid(Foo.Bar); // Segfault here
}
DMD64 D Compiler v2.090.0, no compilation flag necessary to replicate.
Comment #1 by moonlightsentinel — 2020-01-20T17:00:19Z
Tested with run.dlang.org:
2.064 to 2.068.2: Success and no output
2.069.2 to 2.070.2: Status -6 with output:
-----
dmd: tocsym.c:246: virtual void toSymbol(Dsymbol*)::ToSymbol::visit(VarDeclaration*): Assertion `0' failed.
linkage = 0
-----
2.071.2: Failure with output: --- killed by signal 11
Since 2.072.2: Failure with output: Error: program killed by signal 11
This used to work before https://github.com/dlang/dmd/pull/5161 (found using Digger)
Comment #2 by razvan.nitu1305 — 2022-01-21T12:29:58Z
I think that the operation should not be allowed. Doing `enum Bar = new C()` results in "test.d(10): Error: variable `test.main.C` : Unable to initialize enum with class or pointer to struct. Use static const variable instead.". I assume this should outputted in the case of the bug report also.
Comment #3 by ibuclaw — 2022-12-26T20:01:19Z
GDC and LDC compile and run the test case just fine. It looks like DMD is not generating an initializer for the manifest constant referenced by `typeid()`.
Tagging as backend issue.
Comment #4 by dlang-bot — 2023-01-03T05:24:23Z
@WalterBright created dlang/dmd pull request #14777 "fix Issue 20520 - Runtime segfault when taking typeid of a class inst…" fixing this issue:
- fix Issue 20520 - Runtime segfault when taking typeid of a class instanciated within an enum
https://github.com/dlang/dmd/pull/14777
Comment #5 by bugzilla — 2023-01-03T08:38:21Z
Razvan is right. This should not compile. An enum's value should evaluate to a constant, not a pointer to the heap.
Just like this does not compile:
class C { }
C x = new C();
Comment #6 by ibuclaw — 2023-01-03T11:02:51Z
(In reply to Walter Bright from comment #5)
> Razvan is right. This should not compile. An enum's value should evaluate to
> a constant, not a pointer to the heap.
>
> Just like this does not compile:
>
> class C { }
> C x = new C();
It's an rodata constant, you support it in the same way that
static C x = new C();
is supported.
Comment #7 by dlang-bot — 2023-01-14T13:07:34Z
dlang/dmd pull request #14777 "fix Issue 20520 - Runtime segfault when taking typeid of a class inst…" was merged into stable:
- a8f32669c453dd524fa157382a0be870088bde53 by Walter Bright:
fix Issue 20520 - Runtime segfault when taking typeid of a class instanciated within an enum
https://github.com/dlang/dmd/pull/14777
Comment #8 by dlang-bot — 2023-01-14T13:07:38Z
@dkorpel updated dlang/dmd pull request #14815 "Merge stable" fixing this issue:
- fix Issue 20520 - Runtime segfault when taking typeid of a class instanciated within an enum
https://github.com/dlang/dmd/pull/14815
Comment #9 by dlang-bot — 2023-01-14T14:39:43Z
dlang/dmd pull request #14815 "Merge stable" was merged into master:
- ba04b29d9e88de69e0ac8fb021410f8d509c73f5 by Walter Bright:
fix Issue 20520 - Runtime segfault when taking typeid of a class instanciated within an enum
https://github.com/dlang/dmd/pull/14815