Bug 6815 – Char array is turned into string expression during constant folding

Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
All
Creation time
2011-10-15T20:35:00Z
Last change time
2014-11-06T01:37:37Z
Keywords
CTFE
Assigned to
nobody
Creator
code

Comments

Comment #0 by code — 2011-10-15T20:35:22Z
struct DChars { dchar foo() { return ary[0]; } dchar[] ary; } DChars get() { DChars s; s.ary ~= 'H'; s.ary ~= 'e'; return s; } enum dchars = get().foo(); ---- Which will bark: Error: cannot cast a read-only string literal to mutable in CTFE Cat in constfold.c turns null ~ char into a string expression even though the type of null is not a string but a char array.
Comment #1 by verylonglogin.reg — 2012-02-13T03:44:03Z
Probably the same issue: --- char[] f() { char[] buff = new char[1]; buff[0] = 0; // works buff.ptr[0] = 0; // works *(&buff[0]) = 0; // works char* t = &buff[0]; *t = 0; // error foreach(ref el; buff) el = 0; // error return buff; } static assert(f() == "\0"); --- Where `error` means: `Error: cannot cast a read-only string literal to mutable in CTFE`
Comment #2 by clugdbug — 2012-02-23T02:08:29Z
(In reply to comment #1) > Probably the same issue: Nope, completely different.
Comment #3 by hsteoh — 2014-11-06T01:37:37Z
Tested on git HEAD, Linux/64. Neither test case produces an error now. Looks like the bug has been fixed.