Comment #0 by bearophile_hugs — 2011-08-24T20:50:22Z
This is related to bug 5081 , see there for more context.
I think the behaviours shown by this little program are correct, but I think that error message needs to be improved (completed with more details), because in some cases (like foo1) that's an acceptable operation (dmd 2.055 head):
string foo1() pure {
const(char[]) s2 = ['a'];
return s2;
}
string foo2() pure {
return ['a'];
}
string foo3(immutable char[] s) pure {
return s;
}
string foo4(in char[] s1, immutable char[] s2) pure {
return s2;
}
string foo5(in char[] s) pure {
return s; // Error: cannot implicitly convert expression (s) of type const(char[]) to string
}
void main() {
immutable r1 = foo1(); // OK
immutable r2 = foo2(); // OK
immutable r3 = foo3(['a']); // OK
immutable r4 = foo4(['a'], ['b']); // OK
immutable r5 = foo5(['a']); // Error
}
Good and complete error messages are needed to help the programmer understand her mistake and build a correct model of D semantics in her head.
Comment #1 by andrej.mitrovich — 2012-12-27T17:52:42Z
All of these compile in 2.061. Got any other test-cases or should we close?
Comment #2 by k.hara.pg — 2012-12-27T18:50:09Z
(In reply to comment #1)
> All of these compile in 2.061. Got any other test-cases or should we close?
Wait, the conversion in foo5 should fail correctly. If it is allowed, following code will accidentally break const-correctness.
void main() {
char[] buf = ['a'];
immutable str = foo5(buf);
assert(str[0] == 'a');
buf[0] = 'b';
assert(str[0] == 'a'); // fails!?
}
And this code compiles without errors in current git head.
I think this is a regression. Will open a new issue.
Comment #3 by k.hara.pg — 2012-12-27T19:02:58Z
(In reply to comment #2)
> I think this is a regression. Will open a new issue.
Opened:
Issue 9230 - Incorrect implicit immutable conversion occurs in pure function
Comment #4 by k.hara.pg — 2014-12-11T15:18:04Z
In general, D's type system does not allow implicit conversion from 'const(char[])' to 'string', so I'm not sure the way to provide "better" error diagnostic.
So I close this issue as 'invalid'. If you have concrete example of the compiler output, please comment it and reopen this issue.