D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 12065 - Some refused implicit string cast in pure methods
Summary: Some refused implicit string cast in pure methods
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2014-02-02 13:10 UTC by bearophile_hugs
Modified: 2020-03-21 03:56 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2014-02-02 13:10:52 UTC
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 yebblies 2014-02-03 04:07:17 UTC
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
}
Comment 2 basile-z 2019-02-13 02:54:23 UTC
works since 2.072.2. (https://run.dlang.io/is/LeD6kN)