www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4539] New: Refuse assignment to string literal

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

           Summary: Refuse assignment to string literal
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: accepts-invalid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



dmd 2.047 compiles this code, but string literals can't be lvalues:

void main() {
    "hello" = "red";
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 31 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4539


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

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



07:27:57 PDT ---
I just found this by accident on the compiler docs page:

http://www.digitalmars.com/d/2.0/dmd-windows.html

"
Differences between Windows and Linux versions

    * String literals are read-only under Linux. Attempting to write to them
will cause a segment violation.
"

Maybe this is relevant?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 31 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4539




I think that's not relevant, I think that refers to changing the content of a
variable initialized with a string literal (plus cast to mutable).

But the code I have shown is meaningless, because a string can't be a lvalue.
It's like doing:

void main() {
    5 = 7;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 31 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4539




09:18:53 PDT ---
Yeah, it does not make sense at all. But it gets worse:

import std.stdio;

void main() {
    "hello" = "red";
    string x = "hello";
    writeln(x);
}

Writes "red"

That's not good. :)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 31 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4539


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug yahoo.com.au
           Severity|normal                      |major




 Yeah, it does not make sense at all. But it gets worse:
 
 import std.stdio;
 
 void main() {
     "hello" = "red";
     string x = "hello";
     writeln(x);
 }
 
 Writes "red"
 
 That's not good. :)
Ouch! -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jul 31 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4539


nfxjfg gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nfxjfg gmail.com



This segfaults on the far superior Linux (dmd v2.046), so yes, it's probably
because OPTLINK can't do read-only sections.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 31 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4539




A related bug found by denis spir:

auto p = &"hello";

String literals aren't lvalues, so you can't take their address. Just as you
can't tale the address of a decimal literal:

auto q = &(1);

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 29 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4539


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
                 CC|                            |k.hara.pg gmail.com



Patch posted:
https://github.com/D-Programming-Language/dmd/pull/46

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 27 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4539


kennytm gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla kyllingen.net



*** Issue 4309 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 29 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4539


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla digitalmars.com



22:55:03 PDT ---

 Patch posted:
 https://github.com/D-Programming-Language/dmd/pull/46
See github for problems with the patch. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jun 04 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4539




---
I think the main problem is that dmd treats string literal as lvalue.
Lvalue can appear on left hand side of assignment, so string literal now
asignable.


literal on ref parameter as a side effect.

It seems to me that it is correct refusing string literal on ref parameter.
So this change breaks existing code.

(If D specification treats string literal specially, my think is mistaken.
But it might be not.)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 04 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4539


yebblies <yebblies gmail.com> changed:

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



A new patch that reapplies the old one and fixes the failing tests:
https://github.com/D-Programming-Language/dmd/pull/188

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 01 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4539




The current patch for this issue is
https://github.com/D-Programming-Language/dmd/issues/164

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 04 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4539


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jens.k.mueller gmx.de



*** Issue 6882 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 12 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4539


Denis <verylonglogin.reg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |verylonglogin.reg gmail.com



MSK ---
*** Issue 7161 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 25 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4539




---
Updated patch.
https://github.com/D-Programming-Language/dmd/pull/164

A string literal should be able to bind a reference to static array type.
In the context of ref binding, string literal should work as like static array
value.

Example:

void main()
{
    void foo1(ref string s){}         // ref slice
    void foo2(ref const char[10] s){} // difference of length
    void foo3(ref char[5] s){}        // mutable element

    void foo4(ref const char[5] s)
    {
        assert(s[0] == 'h');
        assert(s[4] == 'o');
    }
    void foo5(ref const ubyte[5] s)
    {
        assert(s[0] == 0xc3);
        assert(s[4] == 0x61);
    }

    static assert(!__traits(compiles, foo1("hello")));
    static assert(!__traits(compiles, foo2("hello")));
    static assert(!__traits(compiles, foo3("hello")));
    foo4("hello");
    foo5(cast(ubyte[5])x"c3fcd3d761");

    import std.conv;
    static assert(!__traits(compiles, parse!int("10") == 10));
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 25 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4539




Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/7f33ed71d8897ec0d03828c41063213af0283d02


Retry to fix issue 4539 - Refuse assignment to string literal

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 29 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4539


Walter Bright <bugzilla digitalmars.com> changed:

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


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 29 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4539




With the latest DMD2.058head this code:


void foo(immutable ref string) {}
void main() {
    foo("hello");
}


Gives:

test.d(3): Error: function test.foo (ref immutable(char[]) _param_0) is not
callable using argument types (string)

Is this expected?

(Also note the function signature is (ref immutable(char[]) _param_0) while I
have specified an immutable ref).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 29 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4539




---

 With the latest DMD2.058head this code:
 
 
 void foo(immutable ref string) {}
 void main() {
     foo("hello");
 }
 
 
 Gives:
 
 test.d(3): Error: function test.foo (ref immutable(char[]) _param_0) is not
 callable using argument types (string)
 
 Is this expected?
 
 (Also note the function signature is (ref immutable(char[]) _param_0) while I
 have specified an immutable ref).
Yes, it is expected behavior. 'ref' storage class requires lvalue slice in this case, but string literal DOESN'T have slice, because it has only content. Instead, you can get a reference to string literal by `ref immutable(char[5])` or `ref const(char[5])` (5 == "hello".length), they bind just only content. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 29 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4539




20:51:39 PST ---

 void foo(immutable ref string) {}
I'm curious, what does `immutable ref string` buy you compared to just immutable string? Can't the compiler pass by ref automatically if it's an immutable type? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 29 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4539






 Yes, it is expected behavior.
I have just seen that some of my code that used to compile now gives a problem. This is a reduced version: void foo(ref string t) {} void main() { immutable string s; foo(s); } DMD 2.058head gives: test.d(4): Error: function test.foo (ref string t) is not callable using argument types (immutable(char[])) Is this correct? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 31 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4539






 Is this correct?
Yes, it's correct, sorry. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 31 2012