diff --git a/compiler/src/dmd/dcast.d b/compiler/src/dmd/dcast.d index b682c75429a3..ce51c28f6e12 100644 --- a/compiler/src/dmd/dcast.d +++ b/compiler/src/dmd/dcast.d @@ -2336,7 +2336,7 @@ Expression castTo(Expression e, Scope* sc, Type t, Type att = null) Type tb = t.toBasetype(); Type typeb = e.type.toBasetype(); - if (e.hexString && !e.committed) + if (e.hexString && !e.committed && tb.nextOf().isintegral) { const szx = cast(ubyte) tb.nextOf().size(); if (szx != se.sz && (e.len % szx) == 0) diff --git a/compiler/src/dmd/dinterpret.d b/compiler/src/dmd/dinterpret.d index 52520be01df6..967e83106f81 100644 --- a/compiler/src/dmd/dinterpret.d +++ b/compiler/src/dmd/dinterpret.d @@ -6107,7 +6107,7 @@ public: { auto se = e1.isStringExp(); // Allow casting a hex string literal to short[], int[] or long[] - if (se && se.hexString && se.postfix == StringExp.NoPostfix) + if (se && se.hexString && se.postfix == StringExp.NoPostfix && e.to.nextOf().isintegral) { const sz = cast(size_t) e.to.nextOf().size; if ((se.len % sz) != 0) diff --git a/compiler/test/fail_compilation/hexstring.d b/compiler/test/fail_compilation/hexstring.d index 0f23f444389d..edbb4e67bc0d 100644 --- a/compiler/test/fail_compilation/hexstring.d +++ b/compiler/test/fail_compilation/hexstring.d @@ -14,6 +14,7 @@ fail_compilation/hexstring.d(39): perhaps remove postfix `c` from hex str fail_compilation/hexstring.d(40): Error: hex string with `dstring` type needs to be multiple of 4 bytes, not 5 fail_compilation/hexstring.d(41): Error: cannot implicitly convert expression `x"11223344"d` of type `dstring` to `immutable(float[])` fail_compilation/hexstring.d(42): Error: cannot implicitly convert expression `x"1122"w` of type `wstring` to `immutable(ubyte[])` +fail_compilation/hexstring.d(50): Error: array cast from `string` to `S[]` is not supported at compile time fail_compilation/hexstring.d(28): Error: cannot implicitly convert expression `x"123F"` of type `string` to `ubyte[]` --- */ @@ -21,7 +22,6 @@ immutable ubyte[] s0 = x"123F"; static assert(s0[0] == 0x12); static assert(s0[1] == 0x3F); immutable byte[] s1 = x"123F"; - enum E(X) = cast(X[]) x"AABBCCDD"; static assert(E!int[0] == 0xAABBCCDD); @@ -40,3 +40,11 @@ immutable uint[] f11 = cast(immutable uint[]) x"AABBCCDD"c; immutable uint[] f12 = x"1122334455"d; immutable float[] f13 = x"11223344"d; immutable ubyte[] f14 = x"1122"w; + +// https://issues.dlang.org/show_bug.cgi?id=24832 +struct S +{ + ushort l0, l1, l2, l3, l4, l5; +} + +immutable S[] returnValues = cast(S[]) x"FFFFFFFFFFFFFFFFFFFFFFFF";