Bug 2682 – (D1 only) const struct initialized with struct literal recreates value on stack when used
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
All
OS
All
Creation time
2009-02-22T07:05:05Z
Last change time
2019-06-26T11:40:08Z
Keywords
wrong-code
Assigned to
No Owner
Creator
Christian Kamm
Comments
Comment #0 by kamm-removethis — 2009-02-22T07:05:05Z
As the following code shows the const S s isn't really initialized with a constant that's put into the data segment, but works more like "alias S(5) s;".
---
struct S { int i; }
const S s = S(5);
const S u = { 5 };
void foo(int num) {
printf("%p %p\n", &s, &u);
if (num > 0)
foo(num-1);
}
void main() {
foo(2);
}
---
output:
0xbfc1774c 0x80601a0
0xbfc17738 0x80601a0
0xbfc17724 0x80601a0
Comment #1 by smjg — 2009-02-22T09:08:05Z
This reminds me of bug 2414 ... related?
Comment #2 by tomas — 2009-03-02T21:46:33Z
I fixed this in LDC by disabling the Initializer -> Expression optimization for StructInitializerS:
--- a/dmd/optimize.c Tue Mar 03 02:51:21 2009 +0100
+++ b/dmd/optimize.c Tue Mar 03 04:38:49 2009 +0100
@@ -46,7 +46,7 @@
if (e1->op == TOKvar)
{ VarExp *ve = (VarExp *)e1;
VarDeclaration *v = ve->var->isVarDeclaration();
- if (v && v->isConst() && v->init)
+ if (v && v->isConst() && v->init && !v->init->isStructInitializer())
{ Expression *ei = v->init->toExpression();
if (ei && ei->type)
e1 = ei;
Comment #3 by kamm-removethis — 2009-03-29T04:50:34Z
Removing that optimization lead to constant folding issues. Instead we have now demoted struct literals to rvalues in LDC. It does not create any regressions in our testsuite - please let us know if you expect this to be trouble.
Comment #4 by pro.mathias.lang — 2019-06-26T11:40:08Z