-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
base: master
Are you sure you want to change the base?
Conversation
b22a8a0
to
03fea83
Compare
3d1d91f
to
0a86578
Compare
0a86578
to
7fbfc80
Compare
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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
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; |
There was a problem hiding this comment.
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
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; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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; | |
}; |
if (!mem.endsWith(u8, slice, "Cast")) break; | ||
const end = slice.len - "Cast".len; | ||
const prev_kind = meta.stringToEnum(CastKind, slice[1..end]) orelse break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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; |
if (!mem.endsWith(u8, slice, "Cast")) break; | ||
const end = slice.len - "Cast".len; | ||
const next_kind = meta.stringToEnum(CastKind, slice[1..end]) orelse break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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; |
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 inCastKind
. 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:
It can be changed by simply rearranging the fields of
CastKind
(and adjusting the rendering order intranslate_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.