Bug 1316 – Can't directly reference members of CTFE struct arrays

Status
RESOLVED
Resolution
WONTFIX
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2007-07-05T11:08:00Z
Last change time
2014-02-16T15:22:53Z
Assigned to
bugzilla
Creator
clugdbug

Comments

Comment #0 by clugdbug — 2007-07-05T11:08:37Z
Two variations, with different error messages. ---------------- struct S { int a; } int func() { S [] s = [S(7)]; return s[0].a; // Error: cannot evaluate func() at compile time } void main() { const int x = func(); } -------------- VARIANT 2 (attempted workaround): -------------- struct S { int a; } int func() { S [] s = [S(7)]; return (s[0]).a; // Fails: // bug.d(9): Error: s is used as a type // bug.d(9): Error: no property 'a' for type 'void[0u]' } void main() { const int x = func(); } ---------- Workaround is to index the array first, then access the member. ---------- int func() { S [] s = [S(7)]; auto q = s[0]; return q.a; // OK }
Comment #1 by clugdbug — 2007-07-23T09:54:27Z
The first (and most important) variant seems to have been fixed in DMD 1.019.
Comment #2 by bugzilla — 2007-09-01T03:01:19Z
Variant 1 works fine now. Regarding Variant 2: the parentheses in (s[0]) are redundant. The parser interprets them as an attempt at a C-style cast where s[0] then must be a type. I prefer to leave that in, as it diagnoses attempts to use C-style casts. To fix the code, just remove the redundant parentheses.