www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 12065] New: Some refused implicit string cast in pure methods

reply d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12065

           Summary: Some refused implicit string cast in pure methods
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



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.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 02 2014
parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12065


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |yebblies gmail.com
           Platform|x86                         |All
         OS/Version|Windows                     |All



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
}

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 03 2014