Bug 19720 – const structs without explicit initializer are not usable in CTFE expressions

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2019-03-06T10:14:40Z
Last change time
2019-03-06T11:09:49Z
Assigned to
No Owner
Creator
[email protected]

Comments

Comment #0 by johannes.riecken — 2019-03-06T10:14:40Z
Code: dmd -main -run -<<<'struct S {int i;}const S s;pragma(msg, s);' Expected output: S(0) Actual output: __stdin.d(1): Error: static variable s cannot be read at compile time __stdin.d(1): while evaluating pragma(msg, s) Note that initializing explicitly using a struct initializer literal gives the expected result: dmd -main -run -<<<'struct S {int i;}const S s={};pragma(msg, s);' S(0)
Comment #1 by simen.kjaras — 2019-03-06T11:09:49Z
This is intended behavior - a const variable with an initializer has a known value at compile time, while one without is expected to be initialized in a static constructor, and using its value before that point is undefined behavior.