Thread overview
[Issue 12150] New: Regression (2.063): char[] implicitly converts to string in .dup expression
Feb 13, 2014
Andrej Mitrovic
Feb 13, 2014
Andrej Mitrovic
Feb 13, 2014
Kenji Hara
Feb 13, 2014
Andrej Mitrovic
Feb 13, 2014
Jakob Ovrum
Feb 13, 2014
Kenji Hara
Feb 13, 2014
Andrej Mitrovic
February 13, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12150

           Summary: Regression (2.063): char[] implicitly converts to
                    string in .dup expression
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: accepts-invalid
          Severity: regression
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: andrej.mitrovich@gmail.com


--- Comment #0 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2014-02-13 02:10:17 PST ---
-----
int foo(string arg)
{
    return foo(arg.dup);  // calls itself, stack overflow!
}

void main()
{
    foo("foo");
}
-----

Note that storing the .dup to a variable first avoids the accepts-invalid:

-----
int foo(string arg)
{
    auto duped = arg.dup;
    return foo(duped);  // NG, caught at compile-time
}

void main()
{
    foo("foo");
}
-----

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 13, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12150



--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2014-02-13 02:12:32 PST ---
Here's the slightly reduced example code of where I ran into the issue:

-----
import std.file;

int parseCode(string code)
{
    return parseCode(code.dup);
}

int parseCode(ubyte[] code)
{
    return 0;
}

void main()
{
    parseCode(cast(ubyte[])std.file.read("test.d"));  // ok
    parseCode("foo");  // stack overflow
}
-----

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 13, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12150



--- Comment #2 from Kenji Hara <k.hara.pg@gmail.com> 2014-02-13 07:01:39 PST ---
This is an intended change. See issue 9656.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 13, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12150



--- Comment #3 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2014-02-13 07:28:15 PST ---
But the regression is likely unintended. Did anyone actually request for the feature in Issue 9656? Magical built-in behavior is something we should avoid IMHO, exactly because of issues like this..

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 13, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12150


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs@eml.cc


--- Comment #4 from bearophile_hugs@eml.cc 2014-02-13 07:31:38 PST ---
(In reply to comment #3)
> Magical built-in behavior is something we should avoid IMHO, exactly because of issues like this..

On the other hand the change of Issue 9656 seems useful.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 13, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12150


Jakob Ovrum <jakobovrum@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakobovrum@gmail.com


--- Comment #5 from Jakob Ovrum <jakobovrum@gmail.com> 2014-02-13 07:35:19 PST ---
(In reply to comment #3)
> But the regression is likely unintended. Did anyone actually request for the feature in Issue 9656? Magical built-in behavior is something we should avoid IMHO, exactly because of issues like this..

I might be missing something, but where is the regression? The example you posted doesn't seem to be a regression because char[] isn't implicitly convertible to either `string` or `ubyte[]`, so surely the code would simply not compile.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 13, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12150



--- Comment #6 from Kenji Hara <k.hara.pg@gmail.com> 2014-02-13 07:37:52 PST ---
(In reply to comment #3)
> But the regression is likely unintended. Did anyone actually request for the feature in Issue 9656? Magical built-in behavior is something we should avoid IMHO, exactly because of issues like this..

That is not magic. It is consistent with the implicit conversion for the returned value from pure function.

E[] dup(E)(const(E)[] src) pure
{
    E[] r;
    r.reserve(src.length);
    foreach (ref e; src)
        r ~= e;
    return r;
}
int foo(string arg)
{
    return foo(dup(arg));
    // returned char[] is implicitly convertible to string
}
void main()
{
    foo("foo");
}

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 13, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12150


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID


--- Comment #7 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2014-02-13 07:42:50 PST ---
(In reply to comment #5)
> I might be missing something, but where is the regression? The example you posted doesn't seem to be a regression because char[] isn't implicitly convertible to either `string` or `ubyte[]`, so surely the code would simply not compile.

Ah I thought it would compile, after I added an explicit cast to avoid the recursive call it ended up compiling, but it wouldn't otherwise even though I thought it would. So you're right.

There's no regression here. The below test-case works properly:

-----
int foo(string arg)
{
    return foo(arg.dup);  // calls second overload
}

int foo(char[] b)
{
    assert(0);
}

void main()
{
    auto x = foo("");
}
-----

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