Bug 12306 – Struct Enums cannot be read at compile time

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
Linux
Creation time
2014-03-06T13:50:00Z
Last change time
2014-03-14T07:19:13Z
Keywords
pull, rejects-valid
Assigned to
nobody
Creator
jbinero

Attachments

IDFilenameSummaryContent-TypeSize
1337main.dExample Code - "dmd main.d"text/x-dsrc162

Comments

Comment #0 by jbinero — 2014-03-06T13:50:45Z
Created attachment 1337 Example Code - "dmd main.d" When a struct enum is defined like this, it's values cannot be read at compile time. enum Point3D point = {x:1, y:1, z:1}; int testArray[point.x][point.y][point.z]; // z Cannot be read at compile time (will error 6 times) Working (well, technically not working) code in attachment. Tested for both 32 bit and 64 bit on a Linux system.
Comment #1 by jbinero — 2014-03-06T13:54:39Z
(In reply to comment #0) > Created an attachment (id=1337) [details] > Example Code - "dmd main.d" > > When a struct enum is defined like this, it's values cannot be read at compile > time. > enum Point3D point = {x:1, y:1, z:1}; > int testArray[point.x][point.y][point.z]; // z Cannot be read at > compile time (will error 6 times) > > Working (well, technically not working) code in attachment. Tested for both 32 > bit and 64 bit on a Linux system. Probably not important, but it errors 5 times and not 6. With adding and removing to the code I have found inconsistency in how many times it errors, and what variable it picks though.
Comment #2 by dlang-bugzilla — 2014-03-06T15:28:20Z
Looks like the compiler parses the array declaration as an associative array (and conservatively assumes that "point.x" is a type), then when it realizes it is in fact a static array, it does not reparse the whole "point.x" expression correctly (it just looks at the last symbol, x). Making it clear that it is an expression, e.g. adding "+0", also fixes compilation. Fixing this is not trivial as the expression that the compiler sees in the array index is not saved. I think the correct fix would be to switch the parsing order: Assume that the array "index" is an expression, then, if it resolves to a type, reinterpret the whole thing as an associative array.
Comment #3 by k.hara.pg — 2014-03-13T19:59:42Z
Comment #4 by github-bugzilla — 2014-03-14T07:19:02Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/d6de735b9ac60e85b4fdd844f838fa5fb2bc0881 fix Issue 12306 - Struct Enums cannot be read at compile time https://github.com/D-Programming-Language/dmd/commit/0edabb313e4170a62693b4e0f1f5336a5f000c86 Merge pull request #3379 from 9rnsr/fix12306 Issue 12306 - Struct Enums cannot be read at compile time