Thread overview
[Issue 7161] New: Passing string literal by ref changes its bounds forever
Dec 24, 2011
Denis
Dec 24, 2011
Denis
Dec 24, 2011
Andrej Mitrovic
Dec 24, 2011
Andrej Mitrovic
Dec 24, 2011
Denis
Dec 24, 2011
Jonathan M Davis
Dec 25, 2011
Denis
December 24, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=7161

           Summary: Passing string literal by ref changes its bounds
                    forever
           Product: D
           Version: D1 & D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: verylonglogin.reg@gmail.com


--- Comment #0 from Denis <verylonglogin.reg@gmail.com> 2011-12-24 15:41:24 MSK ---
---
import std.stdio;

void f(ref string s) {
    writeln(s);
    if(s.length)
        --s.length;
    else
        s = "def";
}

void g1() { f("abc"); }
void g2() { f("abc"); }

void main()
{
    g1();     // prints "abc"
    g2();     // prints "ab"
    f("abc"); // prints "a"
    f("abc"); // prints ""
    f("abc"); // prints "def"
}
---

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 24, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=7161


Denis <verylonglogin.reg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code


--- Comment #1 from Denis <verylonglogin.reg@gmail.com> 2011-12-24 15:54:03 MSK ---
Workaround:
wrap every passed string `f("abc")` like `f(s("abc"))` and `f("11", "22",
"33")` like `f(s("11"), s!1("22"), s!2("33"))` where `s` is:
---
ref string s(size_t n = 0)(string s) {
    static string ss;
    ss = s;
    return ss;
}
---

It does have an easy workaround but is very annoying.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 24, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=7161


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich@gmail.com


--- Comment #2 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2011-12-24 05:21:38 PST ---
I'll do you one better:

void g1() { f("abc".s); }
void g2() { f("abc".s); }

ref string s_impl()(string s)
{
    static string ss;
    ss = s;
    return ss;
}

ref string s(string s)
{
    return s_impl!()(s);
}

void main()
{
    g1();
    g2();
    f("abc".s);
    f("abc".s);
    f("abc".s);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 24, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=7161



--- Comment #3 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2011-12-24 05:27:20 PST ---
Actually s_impl isn't needed, s() can have that implementation it just needs a
set of empty parens:
ref string s()(string s)
{
    static string ss;
    ss = s;
    return ss;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 24, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=7161



--- Comment #4 from Denis <verylonglogin.reg@gmail.com> 2011-12-24 17:03:56 MSK ---
And a better one (CTFE friendly, no need to specify param number):
---
private ref string wrap(string s) {
    static struct GCString { string str; }
    auto gcStr = new GCString();
    gcStr.str = s;
    return gcStr.str;
}
---
Yes, hello Java-like arrays!

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 24, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=7161


Jonathan M Davis <jmdavisProg@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jmdavisProg@gmx.com


--- Comment #5 from Jonathan M Davis <jmdavisProg@gmx.com> 2011-12-24 13:11:00 PST ---
This code shouldn't even be legal. A string literal isn't an lvalue and can't be passed by reference. It's bug that that compiles at all. This looks like a duplicate of bug# 4539.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 25, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=7161


Denis <verylonglogin.reg@gmail.com> changed:

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


--- Comment #6 from Denis <verylonglogin.reg@gmail.com> 2011-12-25 13:49:32 MSK ---
*** This issue has been marked as a duplicate of issue 4539 ***

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