Bug 12065 – Some refused implicit string cast in pure methods
Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-02-02T13:10:52Z
Last change time
2020-03-21T03:56:37Z
Keywords
rejects-valid
Assigned to
No Owner
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2014-02-02T13:10:52Z
I have tried to understand what's happening here, but I can't tell why some times it accepts the implicit string cast and other times it doesn't:
import std.string: text;
struct Foo1 {
char[][] matrix;
string spam() const pure {
char[] m = text(matrix).dup;
return m; // OK
}
}
struct Foo2 {
enum Bar : char { A }
Bar[][] matrix;
string spam1() const pure {
return text(matrix).dup; // OK
}
string spam2() const pure {
char[] m = text(matrix).dup;
return m; // line 22, Error
}
char[][] matrix2;
string spam3() const pure {
char[] m = text(matrix2).dup;
return m; // line 29, Error.
}
}
void main() {}
dmd 2.065.0b2 gives:
test.d(22): Error: cannot implicitly convert expression (m) of type char[] to string
test.d(29): Error: cannot implicitly convert expression (m) of type char[] to string
If it's not a compiler bug then I think it's not easy for D newbies to learn such behaviours.
Comment #1 by yebblies — 2014-02-03T04:07:17Z
It's a bug, the compiler thinks that `const(Bar[])` could potentially produce a `char[]`.
string func1(in char[] x) pure
{
char[] m = "".dup;
return m; // works, can't get a char[] from const(char[]) without casting
}
enum Bar : char { A }
string func2(in Bar[] x) pure
{
char[] m = "".dup;
return m; // doesn't work, but should
}