www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 10724] New: Allow slice of string literal to convert to const(char)*

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

           Summary: Allow slice of string literal to convert to
                    const(char)*
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: yebblies gmail.com


--- Comment #0 from yebblies <yebblies gmail.com> 2013-07-28 16:46:27 EST ---
The following code is perfectly safe because the slice happens at compile time,
but the conversion is not allowed.

void main()
{
    const(char)* s = "abc"[0..$-1];
}

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


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull
         AssignedTo|nobody puremagic.com        |yebblies gmail.com


--- Comment #1 from yebblies <yebblies gmail.com> 2013-07-28 16:58:26 EST ---
https://github.com/D-Programming-Language/dmd/pull/2392

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


bearophile_hugs eml.cc changed:

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


--- Comment #2 from bearophile_hugs eml.cc 2013-07-28 03:42:51 PDT ---
Implicit conversions introduce a bit of dangers in a language. They should be
minimized.

Instead of this:
const(char)* s = "abc"[0..$-1];

What about this?
const(char)* s = "abc"[0..$-1].ptr;

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



--- Comment #3 from yebblies <yebblies gmail.com> 2013-07-28 20:52:45 EST ---
(In reply to comment #2)
 Implicit conversions introduce a bit of dangers in a language. They should be
 minimized.
 
 Instead of this:
 const(char)* s = "abc"[0..$-1];
 
 What about this?
 const(char)* s = "abc"[0..$-1].ptr;
The conversion is only safe because the string literal is null-terminated. Explicitly adding .ptr bypasses that, making it unsafe to rely on this. Currently `"abc"[0..$-1]` gets const-folded to (essentially) `cast(string)"ab"`, and not for any reason I can see. It should be just plain "ab", which can convert to a const cstring. The only downside I can see is potential confusion that the above works, but this doesn't: string str = "abc"[0..$-1]; const(char)* s = str; But this is already present with plain string literals, as well as concatenation and probably others. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jul 28 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10724



--- Comment #4 from github-bugzilla puremagic.com 2013-07-28 16:47:38 PDT ---
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/a878588c71ea51e2365cd595dc38413700cc3d8a
Fix Issue 10724 - Allow slice of string literal to convert to const(char)*

https://github.com/D-Programming-Language/dmd/commit/e230276ee0703a3cdf92e16e1b9f388be44e9c54
Merge pull request #2392 from yebblies/issue10724

[DDMD] Fix Issue 10724 - Allow slice of string literal to convert to
const(char)*

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


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla digitalmars.com
         Resolution|                            |FIXED


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


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

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


--- Comment #5 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2013-07-29
08:40:53 PDT ---
Just to clarify:

const(char)* a = "abc";
const(char)* b = "abc"[0..$-1];

Do a and b end up pointing to different memory addresses?

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



--- Comment #6 from yebblies <yebblies gmail.com> 2013-07-30 01:43:28 EST ---
(In reply to comment #5)
 Just to clarify:
 
 const(char)* a = "abc";
 const(char)* b = "abc"[0..$-1];
 
 Do a and b end up pointing to different memory addresses?
Yes. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jul 29 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10724


Luís Marques <luis luismarques.eu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |luis luismarques.eu


--- Comment #7 from Luís Marques <luis luismarques.eu> 2013-10-16 21:13:26 PDT
---
The following used not to work in v2.063.2:

    const a = "a";
    const b = a ~ "b";
    const(char)* output = b;

    $ Error: cannot implicitly convert expression ("ab") of type
const(immutable(char)[]) to const(char)*

Although this worked:

    const b = "a" ~ "b";
    const(char)* output = b;

Now both work. I assume it was the fix for this issue (10724) that also fixed
this? I say fixed because at first glance the old behavior seems wrong, but it
seems such a basic statement that I wonder why this wasn't spotted before, or
if I'm making a mistake. So please confirm this change in behavior was also
desirable.

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



--- Comment #8 from yebblies <yebblies gmail.com> 2013-10-17 15:33:06 EST ---
(In reply to comment #7)
 The following used not to work in v2.063.2:
 
     const a = "a";
     const b = a ~ "b";
     const(char)* output = b;
 
     $ Error: cannot implicitly convert expression ("ab") of type
 const(immutable(char)[]) to const(char)*
 
 Although this worked:
 
     const b = "a" ~ "b";
     const(char)* output = b;
 
 Now both work. I assume it was the fix for this issue (10724) that also fixed
 this? I say fixed because at first glance the old behavior seems wrong, but it
 seems such a basic statement that I wonder why this wasn't spotted before, or
 if I'm making a mistake. So please confirm this change in behavior was also
 desirable.
All this patch changed was the 'committing' behavior of SliceExp constfolding. Without a slice anywhere in there it doesn't look like this patch is the cause. The code does appear to be valid as 'a' and 'b' are const variables with initializers known at compile time, so the const-folder can determine that 'output' is assigned a string literal and allow the conversion. I would expect replacing any of them with a run-time determined value will prevent the conversion. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Oct 16 2013