Skip to content

Conversation

@Adityazzzzz
Copy link
Contributor

fixes Issue #22260 a regression where casting a class instance to void* failed if the class defined a matching opCast.
#22260
Previously, the compiler would prioritize the user-defined opCast over the built-in pointer cast. If opCast returned the object itself (or any non-pointer type), the compiler would error, making it impossible to obtain the raw memory address needed for operations like core.memory.GC.free.

The Fix:

  • Modified visit(CastExp) in expressionsem.d.
  • Added a check to explicitly skip opCast lookup when the target type is void*.
  • This restores the expected behavior: cast(void*) now always yields the raw pointer address, ignoring any user-defined opCast.

@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @Adityazzzzz! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

⚠️⚠️⚠️ Warnings ⚠️⚠️⚠️

  • In preparation for migrating from Bugzilla to GitHub Issues, the issue reference syntax has changed. Please add the word "Bugzilla" to issue references. For example, Fix Bugzilla Issue 12345 or Fix Bugzilla 12345.(Reminder: the edit needs to be done in the Git commit message, not the GitHub pull request.)

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + dmd#22306"

Copy link
Contributor

@thewilsonator thewilsonator left a comment

Choose a reason for hiding this comment

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

Please add a test case for this

@Adityazzzzz
Copy link
Contributor Author

Adityazzzzz commented Dec 29, 2025

I have updated the logic to strictly target the regression. The compiler now only ignores opCast if it returns a class (which causes the recursion bug). Valid opCast implementations that return pointers are now respected.
I've also added a regression test (compiler/test/runnable/issue22260.d) covering both scenarios
@thewilsonator

@rikkimax
Copy link
Contributor

This does not fix the problem.

The cast syntax is doing double duty; it can't do both and work correctly.

@Adityazzzzz
Copy link
Contributor Author

I see the fundamental conflict here.
In my previous iteration, I simply disabled opCast entirely for void* target types. However, @ntrel raised the concern that this would break existing code where users intentionally implement opCast!void* to return a specific address (e.g., to an internal field).
The current patch attempts a middle ground: it respects opCast unless it detects the specific case that causes the infinite recursion bug (returning a class type).
If the consensus is that cast(void*) should strictly be a reinterpret cast (meaning we accept breaking the custom opCast!void* use case), I am happy to revert to the strict behavior. Which direction should we take?

@rikkimax
Copy link
Contributor

It requires language changes to fix properly. There is nothing to do atm.

@Adityazzzzz
Copy link
Contributor Author

It requires language changes to fix properly. There is nothing to do atm.

Understood. Thanks for the explanation.
Since this requires a language-level change rather than a compiler patch, I will pause work on this.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants