Fix issue 20012 - clear diagnostic for CTFE array cast in UDA context - #23820
Open
usefahmed07 wants to merge 11 commits into
Open
Fix issue 20012 - clear diagnostic for CTFE array cast in UDA context#23820usefahmed07 wants to merge 11 commits into
usefahmed07 wants to merge 11 commits into
Conversation
thewilsonator
approved these changes
Sep 9, 2026
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
force-pushed
the
fix-ctfe-arraycast-uda-msg-20012
branch
from
September 9, 2026 10:23
5441c48 to
7402c95
Compare
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. |
Contributor
|
no, |
dkorpel
requested changes
Sep 9, 2026
dkorpel
left a comment
Contributor
There was a problem hiding this comment.
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.
Contributor
Author
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? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #20012