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


--- Comment #0 from bearophile_hugs eml.cc 2010-07-31 04:26:35 PDT ---
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


--- Comment #1 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2010-07-31
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



--- Comment #2 from bearophile_hugs eml.cc 2010-07-31 08:48:12 PDT ---
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



--- Comment #3 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2010-07-31
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


--- Comment #4 from Don <clugdbug yahoo.com.au> 2010-07-31 17:30:23 PDT ---
(In reply to comment #3)
 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


--- Comment #5 from nfxjfg gmail.com 2010-07-31 18:43:05 PDT ---
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



--- Comment #6 from bearophile_hugs eml.cc 2010-10-29 09:55:19 PDT ---
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


--- Comment #7 from Kenji Hara <k.hara.pg gmail.com> 2011-04-27 04:16:32 PDT ---
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


--- Comment #8 from kennytm gmail.com 2011-04-29 07:04:05 PDT ---
*** 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


--- Comment #9 from Walter Bright <bugzilla digitalmars.com> 2011-06-04
22:55:03 PDT ---
(In reply to comment #7)
 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



--- Comment #10 from Kenji Hara <k.hara.pg gmail.com> 2011-06-04 23:19:38 PDT
---
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.

Patch comment #7 fixes this behavior, and this change also refuse string
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


--- Comment #11 from yebblies <yebblies gmail.com> 2011-07-01 23:36:13 EST ---
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



--- Comment #12 from yebblies <yebblies gmail.com> 2011-09-05 16:30:28 EST ---
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


--- Comment #13 from yebblies <yebblies gmail.com> 2011-12-13 16:35:53 EST ---
*** 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


--- Comment #14 from Denis <verylonglogin.reg gmail.com> 2011-12-25 13:49:33
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



--- Comment #15 from Kenji Hara <k.hara.pg gmail.com> 2011-12-25 07:29:50 PST
---
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



--- Comment #16 from github-bugzilla puremagic.com 2012-01-29 12:21:34 PST ---
Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/7f33ed71d8897ec0d03828c41063213af0283d02
Merge pull request #164 from 9rnsr/fix4539

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



--- Comment #17 from bearophile_hugs eml.cc 2012-01-29 14:36:02 PST ---
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



--- Comment #18 from Kenji Hara <k.hara.pg gmail.com> 2012-01-29 15:20:53 PST
---
(In reply to comment #17)
 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



--- Comment #19 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2012-01-29
20:51:39 PST ---
(In reply to comment #17)
 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



--- Comment #20 from bearophile_hugs eml.cc 2012-01-31 17:09:46 PST ---
(In reply to comment #18)

 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



--- Comment #21 from bearophile_hugs eml.cc 2012-01-31 17:24:32 PST ---
(In reply to comment #20)

 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