Bug 12480 – static assert should print out the string representation of a value it can interpret

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-03-27T04:44:00Z
Last change time
2014-04-21T09:20:11Z
Keywords
diagnostic
Assigned to
andrej.mitrovich
Creator
andrej.mitrovich

Comments

Comment #0 by andrej.mitrovich — 2014-03-27T04:44:04Z
----- enum chars = ["oops", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]; static assert(chars.length == 12); ----- $ dmd test.d > Error: static assert (13u == 12u) is false ----- static immutable chars = ["oops", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]; static assert(chars.length == 12); ----- $ dmd test.d > Error: static assert (chars.length == 12u) is false
Comment #1 by github-bugzilla — 2014-04-20T22:06:12Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/7ec47e158601f18a2dad86d7c594628da753a58a Fix Issue 12480 - Implement better static assert diagnostics by doing CTFE interpretation when optimizing static immutable arrays. https://github.com/D-Programming-Language/dmd/commit/8903f3f8342f9476721470e19c53a10eb31bc31b Merge pull request #3474 from AndrejMitrovic/Fix12480 Issue 12480 - Implement better static assert diagnostics for static immutable arrays.
Comment #2 by per.nordlow — 2014-04-21T07:33:57Z
Is there pending work on a dynamic variant for this?
Comment #3 by andrej.mitrovich — 2014-04-21T08:58:41Z
(In reply to Per Nordlöw from comment #2) > Is there pending work on a dynamic variant for this? Can you elaborate?
Comment #4 by per.nordlow — 2014-04-21T09:15:49Z
Elaboration from my previous post: I'v been wanting to have a similar dynamic behaviour for assert(EXPR) where EXPR is of the form x OP y where OP typically is a logical comparison operator ==, !=, <, >, <=, >=. For example if assert(x == y) fails it should in some useful way explain to the user why it failed. For small structures (for example builtin scalar types) it could just print them. For larger structures/ranges it would be more useful to print the first (index, value) of x and y where the assertion failed. This should probably apply recursively, for example Exception: assert(x == y) failed for i:th element of j:th element of ... x being X and ... of y being Y For large structures this may result in a significant increase in code generation (and compilation time) for all the to!string instantiations the needs to be made. Maybe a compiler flag is motivated in this case.
Comment #5 by andrej.mitrovich — 2014-04-21T09:20:11Z
(In reply to Per Nordlöw from comment #4) > I've been wanting to have a similar dynamic behaviour Ah you mean runtime. There is some related work on improving assert itself, although it doesn't go as far as you've talked about. See https://github.com/D-Programming-Language/dmd/pull/1426