Skip to content

Fix issue 20012 - clear diagnostic for CTFE array cast in UDA context - #23820

Open
usefahmed07 wants to merge 11 commits into
dlang:masterfrom
usefahmed07:fix-ctfe-arraycast-uda-msg-20012
Open

Fix issue 20012 - clear diagnostic for CTFE array cast in UDA context#23820
usefahmed07 wants to merge 11 commits into
dlang:masterfrom
usefahmed07:fix-ctfe-arraycast-uda-msg-20012

Conversation

@usefahmed07

Copy link
Copy Markdown
Contributor

Fixes #20012

When an array cast that changes element size (e.g. cast(dstring) from
a string literal) appeared in a context requiring compile-time
evaluation but going through the codegen-lowering path (such as a
UDA), the compiler rewrote it to a call to object.__ArrayCast!(...).
CTFE then had to interpret that call's body, including
onArrayCastError()'s use of pureMalloc for building the error
message - which cannot be evaluated at compile time. This surfaced a
confusing, unrelated error (e.g. `fakePureErrno` cannot be
interpreted at compile time) instead of a clear message, unlike the
equivalent enum case which hits dinterpret.d's CTFE-aware array cast
check directly and gives a clear message.

Added a __ctfe branch in onArrayCastError() (druntime) that builds
the same error message using plain string concatenation instead of
pureMalloc, since that's evaluable during CTFE. The non-CTFE path is
untouched. Added test/fail_compilation/fail20012.d covering the
original UDA repro from the issue. Verified casting.d's existing
unittests (covering the ordinary runtime path) still pass, and the
full fail_compilation/compilable suites pass with no regressions.
@usefahmed07
usefahmed07 force-pushed the fix-ctfe-arraycast-uda-msg-20012 branch from 5441c48 to 7402c95 Compare September 9, 2026 10:23
@usefahmed07

Copy link
Copy Markdown
Contributor Author

@thewilsonator This CI failure looks unrelated to this PR - it's in a different file and fails on nearly every platform, so it seems like a pre-existing issue on master.

@thewilsonator

Copy link
Copy Markdown
Contributor

no, runnable/betterc.d fails every time on every platform, it looks very much related.

@dkorpel dkorpel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still printing multiple redundant messages

The CTFE branch of onArrayCastError used ~ and .idup, which are
heap-allocating operations. Since __ArrayCast is @nogc, the D
compiler's attribute inference marked onArrayCastError as non-@nogc
based on this branch, breaking the @nogc call from __ArrayCast and
producing a confusing secondary 'CTFE failed because of previous
errors' message alongside the real one.

Build the CTFE error message on a stack-allocated char[2048] buffer
instead, with no ~ or .idup, so attribute inference keeps the
function @nogc. This also fixes the same issue under -betterC.

Fixes https://issues.dlang.org/show_bug.cgi?id=22546
The expected error message hardcodes '4LU' for the toElemSize
argument printed by the compiler's call-site diagnostics. This
literal suffix reflects size_t being ulong on 64-bit platforms;
on 32-bit platforms size_t is uint, so the compiler prints '4u'
instead, causing a text mismatch.

Disable the test on 32-bit linux/windows targets rather than
attempt to match both formats, consistent with existing tests
like chkformat_clong_smalllong.d that hit the same size_t
size-dependent formatting issue.
Adding the DISABLED: linux32 win32 line shifted the source code
down by one line, so the expected error line numbers (10, 11)
no longer matched the actual output (11, 12). Update the
expected TEST_OUTPUT line numbers accordingly.
Previous CI failure was an unrelated apt-get/Google Chrome mirror
hash mismatch during runner setup, unrelated to this change.
Previous CI failure was an unrelated apt-get/Google Chrome mirror
hash mismatch during runner setup, unrelated to this change.
Previous CI failure was an unrelated apt-get/Google Chrome mirror
hash mismatch during runner setup, unrelated to this change.
Previous CI failure was an unrelated apt-get/Google Chrome mirror
hash mismatch during runner setup, unrelated to this change.
Previous CI failure was an unrelated Google Chrome apt mirror
hash mismatch during runner setup, unrelated to this change.
@usefahmed07

Copy link
Copy Markdown
Contributor Author

no, runnable/betterc.d fails every time on every platform, it looks very much related.

I'm not sure what's causing it it passes for me locally on the latest commit. Could you share the CI failure log so I can dig into it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bad diagnostic for CTFE array cast of string in UDA

3 participants