Skip to content

zig fmt: canonicalize nested cast builtin order #24199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Justus2308
Copy link
Contributor

@Justus2308 Justus2308 commented Jun 16, 2025

Closes #24106

Searches for surrounding tokens tagged with .builtin when a .builtin_... node whose main token ends in "Cast" is encountered and picks which one to render first according to the field order in CastKind. In case of a redundant casting operation (e.g. @ptrCast(@volatileCast(@ptrCast(...)))) no reordering is done for the entire nested chain.

The canonical ordering is the one proposed by mlugg:

@ptrCast(@alignCast(@addrSpaceCast(@constCast(@volatileCast(something)))))

It can be changed by simply rearranging the fields of CastKind (and adjusting the rendering order in translate_c.removeCVQualifiers for @constCast()/@volatileCast() and related tests).

63d9c5d is the actual implementation, 0a86578 is the new ordering applied to affected source files.

@Justus2308 Justus2308 marked this pull request as draft June 16, 2025 18:16
@Justus2308 Justus2308 marked this pull request as ready for review June 16, 2025 21:33
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.

zig fmt fails to canonicalize @ptrCast and @alignCast
1 participant