Thread overview
[Issue 7968] New: Failing implicit cast, new in 2.059
Apr 22, 2012
Manu
Apr 22, 2012
timon.gehr@gmx.ch
Apr 23, 2012
Manu
Apr 23, 2012
timon.gehr@gmx.ch
April 22, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7968

           Summary: Failing implicit cast, new in 2.059
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: turkeyman@gmail.com


--- Comment #0 from Manu <turkeyman@gmail.com> 2012-04-22 11:24:21 PDT ---
This just appeared in 2.059:

void failCast(const(char)*[] arg)
{
}

void test(char** ppStr, uint numStrings)
{
    failCast(ppStr[0..numStrings]);
}


SQLiteDB.d(21): Error: function demu.tools.sqlitedb.failCall (const(char)*[]
arg) is not callable using argument types (char*[])
SQLiteDB.d(21): Error: cannot implicitly convert expression
(ppStr[0u..numStrings]) of type char*[] to const(char)*[]

The chaps on IRC seemed to think this was a bug and I should report it.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 22, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7968


timon.gehr@gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |timon.gehr@gmx.ch
         Resolution|                            |INVALID


--- Comment #1 from timon.gehr@gmx.ch 2012-04-22 11:51:45 PDT ---
(In reply to comment #0)
> The chaps on IRC seemed to think this was a bug and I should report it.

They were wrong.

void failCast(const(char)*[] arg) {
    arg[0] = "123".ptr;
}

void main() {
    char*[] x = ["234".dup.ptr];
    failCast(x); // if this passes then x[0] has type char*
    assert(x[0][0..3] == "123");  //  and points into the immutable data
segment
}

Change the signature of failCast to

void failCast(inout(char)*[] arg)

to get the desired semantics.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 23, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7968



--- Comment #2 from Manu <turkeyman@gmail.com> 2012-04-22 17:02:14 PDT ---
(In reply to comment #1)
> (In reply to comment #0)
> > The chaps on IRC seemed to think this was a bug and I should report it.
> 
> They were wrong.
> 
> void failCast(const(char)*[] arg) {
>     arg[0] = "123".ptr;
> }
> 
> void main() {
>     char*[] x = ["234".dup.ptr];
>     failCast(x); // if this passes then x[0] has type char*
>     assert(x[0][0..3] == "123");  //  and points into the immutable data
> segment
> }
> 
> Change the signature of failCast to
> 
> void failCast(inout(char)*[] arg)
> 
> to get the desired semantics.

I have already changed it so that it works similarly to how you describe, but I
still don't follow why the cast isn't valid?
How does the cast I attempted break the const model?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 23, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7968



--- Comment #3 from timon.gehr@gmx.ch 2012-04-23 10:57:19 PDT ---
(In reply to comment #2)
> How does the cast I attempted break the const model?

Having it be valid code allows to get a mutable reference to immutable data. This reference can then be used to (attempt to) modify the immutable data. You can refer to my first post for the counter-example.

It is an instance of the more general rule that mutable field arrays cannot be covariant and type safe without runtime checks on every store to (or load from) one of their array fields.

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