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).

The first commit is the actual implementation,the second commit is the new ordering applied to affected source files.

@Justus2308 Justus2308 marked this pull request as draft June 16, 2025 18:16
@Justus2308 Justus2308 force-pushed the 24106-fmt-casts branch 2 times, most recently from 3d1d91f to 0a86578 Compare June 16, 2025 21:06
@Justus2308 Justus2308 marked this pull request as ready for review June 16, 2025 21:33
Comment on lines +795 to +797
if (!mem.endsWith(u8, slice, "Cast")) break :canonicalize;
const end = slice.len - "Cast".len;
break :blk meta.stringToEnum(CastKind, slice[1..end]) orelse break :canonicalize;
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't it be easier to just include Cast int the enum field names?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I guess so I probably thought that that checking for 'Cast' first would filter out builtins that don't match more quickly when I wrote this but looking at the list of exsiting builtins again I don't even think that is true

Comment on lines +795 to +797
if (!mem.endsWith(u8, slice, "Cast")) break :canonicalize;
const end = slice.len - "Cast".len;
break :blk meta.stringToEnum(CastKind, slice[1..end]) orelse break :canonicalize;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I guess so I probably thought that that checking for 'Cast' first would filter out builtins that don't match more quickly when I wrote this but looking at the list of exsiting builtins again I don't even think that is true

Comment on lines +786 to +798
const CastKind = enum {
ptr,
@"align",
addrSpace,
@"const",
@"volatile",
};
const kind = blk: {
const slice = tree.tokenSlice(builtin_token);
if (!mem.endsWith(u8, slice, "Cast")) break :canonicalize;
const end = slice.len - "Cast".len;
break :blk meta.stringToEnum(CastKind, slice[1..end]) orelse break :canonicalize;
};
Copy link
Contributor Author

@Justus2308 Justus2308 Jul 12, 2025

Choose a reason for hiding this comment

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

Suggested change
const CastKind = enum {
ptr,
@"align",
addrSpace,
@"const",
@"volatile",
};
const kind = blk: {
const slice = tree.tokenSlice(builtin_token);
if (!mem.endsWith(u8, slice, "Cast")) break :canonicalize;
const end = slice.len - "Cast".len;
break :blk meta.stringToEnum(CastKind, slice[1..end]) orelse break :canonicalize;
};
const CastKind = enum {
ptrCast,
alignCast,
addrSpaceCast,
constCast,
volatileCast,
};
const kind = kind: {
const slice = tree.tokenSlice(builtin_token);
break :kind meta.stringToEnum(CastKind, slice[1..slice.len]) orelse break :canonicalize;
};

Comment on lines +808 to +810
if (!mem.endsWith(u8, slice, "Cast")) break;
const end = slice.len - "Cast".len;
const prev_kind = meta.stringToEnum(CastKind, slice[1..end]) orelse break;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
if (!mem.endsWith(u8, slice, "Cast")) break;
const end = slice.len - "Cast".len;
const prev_kind = meta.stringToEnum(CastKind, slice[1..end]) orelse break;
const prev_kind = meta.stringToEnum(CastKind, slice[1..slice.len]) orelse break;

Comment on lines +820 to +822
if (!mem.endsWith(u8, slice, "Cast")) break;
const end = slice.len - "Cast".len;
const next_kind = meta.stringToEnum(CastKind, slice[1..end]) orelse break;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
if (!mem.endsWith(u8, slice, "Cast")) break;
const end = slice.len - "Cast".len;
const next_kind = meta.stringToEnum(CastKind, slice[1..end]) orelse break;
const next_kind = meta.stringToEnum(CastKind, slice[1..slice.len]) orelse break;

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
2 participants