might be a bug in the DMD FrontEnd

Daniel Keep daniel.keep.lists at gmail.com
Thu Mar 29 23:39:41 PDT 2007


Davidl wrote:
> i don't see what prevent the following from compiling:

I can see a few things...

> import std.stdio;
> char[] ctfe()
> {
>     wchar[] k=cast(wchar[])"int

What's with the casting?
http://www.digitalmars.com/d/lex.html#StringLiteral -- see the part on
"Postfix" characters.

I'm fairly certain that cast(wchar[]) doesn't do what you *think* it's
doing.  cast(wchar[]) casts an array of chars into an array of wchars...
note that I *did not* say "converts" -- UTF-8 and UTF-16 are very
different encodings, so you can't just cast between them and expect it
to make any sense.

It would be like casting a double pointer to a ushort pointer --
meaningless.

Also, I can't work out why you would want to embed a comment in a mixin
string, but it's not especially problematic :P

> i;/*asdfasf"~cast(wchar[])("adf"~cast(char)100~cast(char)192~cast(char)250)~cast(wchar[])"dsafj*/

cast(char)192, whilst technically valid, is really nasty.  For starters,
'192' isn't a valid character by itself in UTF-8, which means it can't
be printed.  Not to mention the potential byte-order problems.  We have
Unicode escape sequences for a reason.  Again, see the section on string
literals, but basically, we have "\x12" for ASCII characters, "\u1234"
for wide characters, and "\U12345678" for really wide characters.

And, again, that cast doesn't make any sense.

> int j;";
>     char[] jimmy = cast(char[])(k[0..k.length-1]);

Dear lord, why?!  You just spent half your time casting it to a wchar
array, and now you're casting it back?!  char[] is perfectly capable of
storing Unicode text, if that's what you're worried about.

Also, you're cutting off the last character of the string, which means
you're losing that last ";", which means your mixin isn't valid, and
will cause compilation to fail.

>     int i;
>     for(i=0;i<jimmy.length;)
>     {
>         if (jimmy[i]==0)
>             jimmy=jimmy[0..i]~jimmy[i+1..jimmy.length];
>         else
>             i++;
>     }

The only reason I can come up with as to why you're doing the above is
because all that casting above generates a string with null characters
in it... which it wouldn't if you didn't use all the casting.

>     return jimmy;
>     
> }
> void main()
> {
>     mixin(ctfe);
>     char[] k=ctfe;
>         printf ("%s",k.ptr);

Please don't use printf, at least not without passing the string through
toStringz.  writefln works perfectly fine.  I mean, you even imported
std.stdio...

Ok, let's try rewriting this...

import std.stdio;

wchar[] ctfe()
{
    wchar[] k = "int
i;/*asdfasf"w~("adf"w~cast(wchar)'\x64'~cast(wchar)'\u1234')~"dsafj*/
int j;";
    return k;
}

void main()
{
    mixin(ctfe());
    wchar[] k = ctfe();
    writefln("%s", k);
}

The above works perfectly.  Heck, we could get rid of those cast(wchars)
by just using wchar strings: "\x64\u1234"w.

> }
> 
> and by viewing the frontend, i think there might be a bug of slicing
> wchar[] in compile time.
> ilwr, iupr ain't taken care of for wchar[] and dchar[] case
> 
> Regards,
> David Leon

It could well be there's a bug in the frontend.  But this is kind of
like setting your house on fire, and then pointing out there's a
burn-mark on the wall. :P

	-- Daniel

-- 
int getRandomNumber()
{
    return 4; // chosen by fair dice roll.
              // guaranteed to be random.
}

http://xkcd.com/

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/



More information about the Digitalmars-d mailing list