Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #2531 from JinShil/ArrayCast_improvements
Browse files Browse the repository at this point in the history
Fix object.__ArrayCast error message generation so it works with CTFE
merged-on-behalf-of: Nicholas Wilson <[email protected]>
  • Loading branch information
dlang-bot authored Mar 31, 2019
2 parents 956bb3a + cc14d50 commit cad29f9
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions src/object.d
Original file line number Diff line number Diff line change
Expand Up @@ -4750,31 +4750,38 @@ Params:
private void onArrayCastError()(string fromType, size_t fromSize, string toType, size_t toSize) @trusted
{
import core.internal.string : unsignedToTempString;
import core.stdc.stdlib : alloca;

const(char)[][8] msgComponents =
const(char)[][9] msgComponents =
[
"Cannot cast `"
, fromType
, "` to `"
, toType
, "`; an array of size "
"An array of size "
, unsignedToTempString(fromSize)
, " does not align on an array of size "
, unsignedToTempString(toSize)
, ", so `"
, fromType
, "` cannot be cast to `"
, toType
, "`"
];

// convert discontiguous `msgComponents` to contiguous string on the stack
size_t length = 0;
foreach (m ; msgComponents)
length += m.length;

auto msg = (cast(char*)alloca(length))[0 .. length];
enum msgLength = 2048;
char[msgLength] msg;

size_t index = 0;
foreach (m ; msgComponents)
foreach (m; msgComponents)
{
foreach (c; m)
{
msg[index++] = c;
if (index >= (msgLength - 1))
break;
}

if (index >= (msgLength - 1))
break;
}
msg[index] = '\0'; // null-termination

// first argument must evaluate to `false` at compile-time to maintain memory safety in release builds
assert(false, msg);
Expand Down

0 comments on commit cad29f9

Please sign in to comment.