Bug 51 – String cast overrides the char type of decorated string literals.
Status
RESOLVED
Resolution
FIXED
Severity
minor
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2006-03-14T14:42:00Z
Last change time
2014-02-15T02:10:06Z
Assigned to
bugzilla
Creator
bruno.do.medeiros+deebugz
Comments
Comment #0 by bruno.do.medeiros+deebugz — 2006-03-14T14:42:59Z
Observe the following code:
char[6] cstr = "123456"c;
auto str1 = cast(wchar[3])(cstr);
auto str2 = cast(wchar[3])("123456"c);
writefln("str1: ", (cast(char[])str1).length , " : ", (cast(char[])str1));
// prints: str1: 6 : 123456
writefln("str2: ", (cast(char[])str2).length , " : ", (cast(char[])str2));
// prints: str2: 6 : ?1?2?3
One would expect the same behaviour from str1 and str2, however the cast(wchar[3]) in str2 overrides the string decorator 'c', and makes the string literal "123456" (i.e. the string "instance" data) be of type wchar[6] instead of char[6].
Comment #1 by thomas-dloop — 2006-03-19T01:45:25Z
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
[email protected] schrieb am 2006-03-14:
> Observe the following code:
>
> char[6] cstr = "123456"c;
> auto str1 = cast(wchar[3])(cstr);
> auto str2 = cast(wchar[3])("123456"c);
>
> writefln("str1: ", (cast(char[])str1).length , " : ", (cast(char[])str1));
> // prints: str1: 6 : 123456
> writefln("str2: ", (cast(char[])str2).length , " : ", (cast(char[])str2));
> // prints: str2: 6 : ?1?2?3
>
> One would expect the same behaviour from str1 and str2, however the
> cast(wchar[3]) in str2 overrides the string decorator 'c', and makes the string
> literal "123456" (i.e. the string "instance" data) be of type wchar[6] instead
> of char[6].
This might seem confusing, but is the correct behaviour.
http://www.digitalmars.com/d/arrays.html
# The type of a string is determined by the semantic phase of
# compilation. The type is one of: char[], wchar[], dchar[], and is
# determined by implicit conversion rules. If there are two equally
# applicable implicit conversions, the result is an error. To
# disambiguate these cases, a cast is appropriate:
#
# cast(wchar [])"abc" // this is an array of wchar characters
Thomas
-----BEGIN PGP SIGNATURE-----
iD8DBQFEHRi13w+/yD4P9tIRAmgpAJ9F/KNd1JSBTBOp1QME7RA6Lwja9wCfT7Gx
SlkT9jiEQ1rxtIl/cc7wT7s=
=aqz7
-----END PGP SIGNATURE-----
Comment #2 by bruno.do.medeiros+deebugz — 2006-03-19T04:30:19Z
(In reply to comment #1)
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> This might seem confusing, but is the correct behaviour.
> http://www.digitalmars.com/d/arrays.html
> # The type of a string is determined by the semantic phase of
> # compilation. The type is one of: char[], wchar[], dchar[], and is
> # determined by implicit conversion rules. If there are two equally
> # applicable implicit conversions, the result is an error. To
> # disambiguate these cases, a cast is appropriate:
> #
> # cast(wchar [])"abc" // this is an array of wchar characters
> Thomas
> -----BEGIN PGP SIGNATURE-----
> iD8DBQFEHRi13w+/yD4P9tIRAmgpAJ9F/KNd1JSBTBOp1QME7RA6Lwja9wCfT7Gx
> SlkT9jiEQ1rxtIl/cc7wT7s=
> =aqz7
> -----END PGP SIGNATURE-----
That behaviour is true for undecorated string literals, however for decorated string literals (i.e. those with postfix 'c', 'd', or 'w') it is not so. In http://www.digitalmars.com/d/lex.html#stringliteral it is said: "The optional Postfix character gives a specific type to the string, rather than it being inferred from the context. ..."
Thus postfix-decorated string literals should not be vulnerable to type change due to casts or other context influences, no?
Comment #3 by bugzilla — 2006-06-20T13:06:01Z
Fixed DMD 0.161 according to Bruno's analysis.
Comment #4 by thomas-dloop — 2006-07-12T06:05:25Z
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
[email protected] schrieb am 2006-03-14:
> http://d.puremagic.com/bugzilla/show_bug.cgi?id=51
> Observe the following code:
>
> char[6] cstr = "123456"c;
> auto str1 = cast(wchar[3])(cstr);
> auto str2 = cast(wchar[3])("123456"c);
>
> writefln("str1: ", (cast(char[])str1).length , " : ", (cast(char[])str1));
> // prints: str1: 6 : 123456
> writefln("str2: ", (cast(char[])str2).length , " : ", (cast(char[])str2));
> // prints: str2: 6 : ?1?2?3
>
> One would expect the same behaviour from str1 and str2, however the
> cast(wchar[3]) in str2 overrides the string decorator 'c', and makes the string
> literal "123456" (i.e. the string "instance" data) be of type wchar[6] instead
> of char[6].
Added to DStress as
http://dstress.kuehne.cn/run/s/string_postfix_07_A.dhttp://dstress.kuehne.cn/run/s/string_postfix_07_B.d
Thomas
-----BEGIN PGP SIGNATURE-----
iD8DBQFEtOHcLK5blCcjpWoRAgakAKCOhY7VG1AkTF8IFOz1Ur067Dk+dQCeIlbj
h9EvDdFmTjEL3U2/pdpgnqk=
=rrCR
-----END PGP SIGNATURE-----