Bug 654 – Const string member using implicit type inference gives garbage in certain situation
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2006-12-05T22:05:00Z
Last change time
2014-02-15T13:20:56Z
Assigned to
bugzilla
Creator
chrisp
Comments
Comment #0 by chrisp — 2006-12-05T22:05:59Z
Suppose we have a class (call it A), with a const member called MY_CONST_STRING that is initialised to a char array using implicit type inference. Suppose also that at some point in the code we refer to it as "A.MY_CONST_STRING". Then, if we print "MY_CONST_STRING" from within the class A, garbage will be produced.
However, if we never refer to "A.MY_CONST_STRING" anywhere, then printing "MY_CONST_STRING" will work as expected (without producing garbage).
It seems to be necessary that the member is a char array, and that implicit type inference is used. (Declaring it as static const char[] MY_CONST_... will cause everything to work.)
This is easier to see in code:
---------
testbug.d
---------
import std.stdio;
class A {
static const MY_CONST_STRING = "hello";
this() {
// This will either print garbage or throw a UTF exception.
// But if never_called() is commented out, then it will work.
writefln("%s", MY_CONST_STRING);
}
}
void never_called() {
// This can be anything; there just needs to be a reference to
// A.MY_CONST_STRING somewhere.
writefln("%s", A.MY_CONST_STRING);
}
void main() {
A a = new A();
}
---------
D:\D>dmd -run testbug
♣ Error: 4invalid UTF-8 sequence
D:\D>