Fix Issue #22752: Force semantic analysis of fields before is() conversion check#22760
Fix Issue #22752: Force semantic analysis of fields before is() conversion check#22760dkorpel merged 1 commit intodlang:masterfrom
Conversation
|
Please add the test case to the test suite |
Added the requested runnable test case to the test suite and amended the commit. |
|
Can you make it a compilable test? Runnable tests are significantly slower, and the main function doesn't do anything here. |
|
done |
|
Also it should be the exact code that caused the issue, the current test case already worked before. I think you can copy-paste the snippet from the issue into the compilable folder. |
|
I have updated implicitConvToWithoutAliasThis in dmd.typesem to call from.sym.size(Loc.initial). this forces the compiler to finish field analysis and determine the struct's layout before checking the implicit conversion, which fixes the issues with is() expressions on forward-referenced structs. |
compiler/src/dmd/typesem.d
Outdated
| import dmd.dsymbolsem : size; | ||
| from.sym.size(Loc.initial); |
There was a problem hiding this comment.
Shouldn't the return result be checked in case an error occurred and it returns invalid size?
There was a problem hiding this comment.
good catch. I've updated the code to check the return value if size() returns SIZE_INVALID, we now return MATCH.nomatch early
|
The two failing checks are both macOS 14 x64 runners. All other platforms (Windows x86/x64, Ubuntu x86/x64, FreeBSD, Alpine, CircleCI, Buildkite) passed successfully. This looks like a macOS CI infrastructure issue unrelated to my change. Could a maintainer re-run the macOS jobs? |
Fix Issue #22752:
is() expressions should force semantic analysis on forward-referenced structs before conversion checks.
The Problem: Currently, is(const(S) : S) can incorrectly return true if it is evaluated before S's fields have been populated (while the struct is still "opaque" or in a forward-reference state). In this state, the compiler has not yet "seen" any fields, so it assumes the struct is empty and decides that an implicit conversion is valid. This leads to static assert failures later in the compilation when the same condition is re-evaluated after the fields are actually read.
The Fix: I have updated implicitConvToWithoutAliasThis in dmd.typesem to explicitly call from.sym.size(Loc.initial). This forces the compiler to perform field analysis and determine the struct's layout before making a final decision on the implicit conversion.
Verification:
Bug Reproduction: The test case provided in the issue now compiles and passes successfully.
Internal Consistency: All modules passed dmd-unittest, ensuring no regressions in the compiler's core logic.
ABI Safety: High. This change only affects the internal reasoning of the compiler during semantic analysis and does not change the generated mangling or binary layout.