Thread overview
[Bug 34] New: C style cast illegal
Mar 11, 2006
d-bugmail
Mar 11, 2006
d-bugmail
Mar 15, 2006
d-bugmail
March 11, 2006
http://d.puremagic.com/bugzilla/show_bug.cgi?id=34

           Summary: C style cast illegal
           Product: D
           Version: 0.149
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: braddr@puremagic.com
        ReportedBy: benoit@tionex.de


I think this should be no error?

void main()
{
    uint[10] arr1;
    ulong idx = 3;
    //uint[] arr2 = arr1[ idx .. idx + 3 ]; // Error if with compiler switch -w
    uint[] arr3 = arr1[ cast(int)(idx) .. (cast(int) idx) + 3  ]; // OK
    uint[] arr4 = arr1[ cast(int) idx  ..  cast(int) idx  + 3  ]; // OK
    uint[] arr5 = arr1[ cast(int)(idx) ..  cast(int)(idx) + 3  ]; // C style
cast illegal, use cast(idx)+3
    uint[] arr6 = arr1[ cast(int)(idx) ..  cast(int)(idx  + 3) ]; // OK
}


-- 

March 11, 2006
http://d.puremagic.com/bugzilla/show_bug.cgi?id=34


bugzilla@digitalmars.com changed:

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




------- Comment #1 from bugzilla@digitalmars.com  2006-03-11 13:34 -------
(idx)+3 can be parsed as a C-style cast: (idx)(+3). These are marked as illegal
in order to catch errors in transliterating C code into D code.

This is deliberate behavior, not a bug.


-- 

March 15, 2006
http://d.puremagic.com/bugzilla/show_bug.cgi?id=34


smjg@iname.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |smjg@iname.com
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |




------- Comment #2 from smjg@iname.com  2006-03-15 09:39 -------
(In reply to comment #1)
> (idx)+3 can be parsed as a C-style cast: (idx)(+3).

Not by any compliant D code parser.

The spec doesn't allow

    cast(int)(idx) + 3

to be parsed as anything but an AddExpression.  Indeed, I thought the whole point of removing the old syntax was to remove the ambiguity from the grammar. It also specifically states

    D introduces the cast keyword:

    cast(foo) -p;    // cast (-p) to type foo
    (foo) - p;       // subtract p from foo

> These are marked as illegal
> in order to catch errors in transliterating C code into D code.

Even if the parser is fixed to match the spec, it will still throw an error during semantic analysis if idx turns out to be a type, which is the only situation in which it is necessary to catch the error.

An error message tailored to this special case is nice, but not necessary.  But if you still want it....

Does the compiler still remember that idx was in brackets when it's time to do semantic analysis?  If so, you could still do it by looking out for the form

    ( Type ) + ...

and similarly for -, ~ and *.


--