mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 14:23:09 +00:00
Merge pull request #24199 from Justus2308/24106-fmt-casts
zig fmt: canonicalize nested cast builtin order
This commit is contained in:
commit
fd9cfc39f5
@ -256,7 +256,7 @@ fn updateStats() error{OutOfMemory}!void {
|
||||
|
||||
if (recent_coverage_update.items.len == 0) return;
|
||||
|
||||
const hdr: *abi.fuzz.CoverageUpdateHeader = @alignCast(@ptrCast(
|
||||
const hdr: *abi.fuzz.CoverageUpdateHeader = @ptrCast(@alignCast(
|
||||
recent_coverage_update.items[0..@sizeOf(abi.fuzz.CoverageUpdateHeader)],
|
||||
));
|
||||
|
||||
|
||||
@ -110,7 +110,7 @@ inline fn copyForwards(
|
||||
const d = dest + alignment_offset;
|
||||
const s = src + alignment_offset;
|
||||
|
||||
copyBlocksAlignedSource(@ptrCast(d), @alignCast(@ptrCast(s)), n);
|
||||
copyBlocksAlignedSource(@ptrCast(d), @ptrCast(@alignCast(s)), n);
|
||||
|
||||
// copy last `@sizeOf(Element)` bytes unconditionally, since block copy
|
||||
// methods only copy a multiple of `@sizeOf(Element)` bytes.
|
||||
|
||||
@ -157,7 +157,7 @@ inline fn copyForwards(
|
||||
const d = dest + alignment_offset;
|
||||
const s = src + alignment_offset;
|
||||
|
||||
copyBlocksAlignedSource(@ptrCast(d), @alignCast(@ptrCast(s)), n);
|
||||
copyBlocksAlignedSource(@ptrCast(d), @ptrCast(@alignCast(s)), n);
|
||||
|
||||
// copy last `copy_size` bytes unconditionally, since block copy
|
||||
// methods only copy a multiple of `copy_size` bytes.
|
||||
|
||||
@ -289,7 +289,7 @@ pub fn GenericReader(
|
||||
const Self = @This();
|
||||
|
||||
fn typeErasedReadFn(context: *const anyopaque, buffer: []u8) anyerror!usize {
|
||||
const ptr: *const Context = @alignCast(@ptrCast(context));
|
||||
const ptr: *const Context = @ptrCast(@alignCast(context));
|
||||
return readFn(ptr.*, buffer);
|
||||
}
|
||||
|
||||
@ -382,7 +382,7 @@ pub fn GenericWriter(
|
||||
}
|
||||
|
||||
fn typeErasedWriteFn(context: *const anyopaque, bytes: []const u8) anyerror!usize {
|
||||
const ptr: *const Context = @alignCast(@ptrCast(context));
|
||||
const ptr: *const Context = @ptrCast(@alignCast(context));
|
||||
return writeFn(ptr.*, bytes);
|
||||
}
|
||||
|
||||
|
||||
@ -1689,7 +1689,7 @@ pub fn adaptToOldInterface(r: *Reader) std.Io.AnyReader {
|
||||
}
|
||||
|
||||
fn derpRead(context: *const anyopaque, buffer: []u8) anyerror!usize {
|
||||
const r: *Reader = @constCast(@alignCast(@ptrCast(context)));
|
||||
const r: *Reader = @ptrCast(@alignCast(@constCast(context)));
|
||||
return r.readSliceShort(buffer);
|
||||
}
|
||||
|
||||
|
||||
@ -2086,7 +2086,7 @@ const IndexHeader = struct {
|
||||
/// Returns the attached array of indexes. I must match the type
|
||||
/// returned by capacityIndexType.
|
||||
fn indexes(header: *IndexHeader, comptime I: type) []Index(I) {
|
||||
const start_ptr: [*]Index(I) = @alignCast(@ptrCast(@as([*]u8, @ptrCast(header)) + @sizeOf(IndexHeader)));
|
||||
const start_ptr: [*]Index(I) = @ptrCast(@alignCast(@as([*]u8, @ptrCast(header)) + @sizeOf(IndexHeader)));
|
||||
return start_ptr[0..header.length()];
|
||||
}
|
||||
|
||||
@ -2122,7 +2122,7 @@ const IndexHeader = struct {
|
||||
const nbytes = @sizeOf(IndexHeader) + index_size * len;
|
||||
const bytes = try gpa.alignedAlloc(u8, .of(IndexHeader), nbytes);
|
||||
@memset(bytes[@sizeOf(IndexHeader)..], 0xff);
|
||||
const result: *IndexHeader = @alignCast(@ptrCast(bytes.ptr));
|
||||
const result: *IndexHeader = @ptrCast(@alignCast(bytes.ptr));
|
||||
result.* = .{
|
||||
.bit_index = new_bit_index,
|
||||
};
|
||||
|
||||
@ -37,7 +37,7 @@ fn anyTag(self: *Encoder, tag_: Tag, val: anytype) !void {
|
||||
// > The encoding of a set value or sequence value shall not include an encoding for any
|
||||
// > component value which is equal to its default value.
|
||||
const is_default = if (f.is_comptime) false else if (f.default_value_ptr) |v| brk: {
|
||||
const default_val: *const f.type = @alignCast(@ptrCast(v));
|
||||
const default_val: *const f.type = @ptrCast(@alignCast(v));
|
||||
break :brk std.mem.eql(u8, std.mem.asBytes(default_val), std.mem.asBytes(&field_val));
|
||||
} else false;
|
||||
|
||||
|
||||
@ -147,7 +147,7 @@ fn markSecret(ptr: anytype, comptime action: enum { classify, declassify }) void
|
||||
@compileError("A pointer value is always assumed leak information via side channels");
|
||||
},
|
||||
else => {
|
||||
const mem8: *const [@sizeOf(@TypeOf(ptr.*))]u8 = @constCast(@ptrCast(ptr));
|
||||
const mem8: *const [@sizeOf(@TypeOf(ptr.*))]u8 = @ptrCast(@constCast(ptr));
|
||||
if (action == .classify) {
|
||||
std.valgrind.memcheck.makeMemUndefined(mem8);
|
||||
} else {
|
||||
|
||||
@ -569,7 +569,7 @@ pub fn assertReadable(slice: []const volatile u8) void {
|
||||
/// Invokes detectable illegal behavior when the provided array is not aligned
|
||||
/// to the provided amount.
|
||||
pub fn assertAligned(ptr: anytype, comptime alignment: std.mem.Alignment) void {
|
||||
const aligned_ptr: *align(alignment.toByteUnits()) anyopaque = @alignCast(@ptrCast(ptr));
|
||||
const aligned_ptr: *align(alignment.toByteUnits()) anyopaque = @ptrCast(@alignCast(ptr));
|
||||
_ = aligned_ptr;
|
||||
}
|
||||
|
||||
|
||||
@ -836,7 +836,7 @@ pub const WindowsModule = struct {
|
||||
|
||||
pub fn deinit(self: @This()) void {
|
||||
const process_handle = windows.GetCurrentProcess();
|
||||
assert(windows.ntdll.NtUnmapViewOfSection(process_handle, @constCast(@ptrCast(self.section_view.ptr))) == .SUCCESS);
|
||||
assert(windows.ntdll.NtUnmapViewOfSection(process_handle, @ptrCast(@constCast(self.section_view.ptr))) == .SUCCESS);
|
||||
windows.CloseHandle(self.section_handle);
|
||||
self.file.close();
|
||||
}
|
||||
|
||||
@ -1511,7 +1511,7 @@ pub fn HashMapUnmanaged(
|
||||
|
||||
const total_size = std.mem.alignForward(usize, vals_end, max_align);
|
||||
|
||||
const slice = @as([*]align(max_align) u8, @alignCast(@ptrCast(self.header())))[0..total_size];
|
||||
const slice = @as([*]align(max_align) u8, @ptrCast(@alignCast(self.header())))[0..total_size];
|
||||
allocator.free(slice);
|
||||
|
||||
self.metadata = null;
|
||||
|
||||
@ -151,7 +151,7 @@ const CAllocator = struct {
|
||||
};
|
||||
|
||||
fn getHeader(ptr: [*]u8) *[*]u8 {
|
||||
return @alignCast(@ptrCast(ptr - @sizeOf(usize)));
|
||||
return @ptrCast(@alignCast(ptr - @sizeOf(usize)));
|
||||
}
|
||||
|
||||
fn alignedAlloc(len: usize, alignment: Alignment) ?[*]u8 {
|
||||
|
||||
@ -205,7 +205,7 @@ fn free(context: *anyopaque, memory: []u8, alignment: mem.Alignment, ra: usize)
|
||||
return PageAllocator.unmap(@alignCast(memory));
|
||||
}
|
||||
|
||||
const node: *usize = @alignCast(@ptrCast(memory.ptr));
|
||||
const node: *usize = @ptrCast(@alignCast(memory.ptr));
|
||||
|
||||
const t = Thread.lock();
|
||||
defer t.unlock();
|
||||
|
||||
@ -291,7 +291,7 @@ pub fn DebugAllocator(comptime config: Config) type {
|
||||
|
||||
fn usedBits(bucket: *BucketHeader, index: usize) *usize {
|
||||
const ptr: [*]u8 = @ptrCast(bucket);
|
||||
const bits: [*]usize = @alignCast(@ptrCast(ptr + @sizeOf(BucketHeader)));
|
||||
const bits: [*]usize = @ptrCast(@alignCast(ptr + @sizeOf(BucketHeader)));
|
||||
return &bits[index];
|
||||
}
|
||||
|
||||
|
||||
@ -624,7 +624,7 @@ pub const Request = struct {
|
||||
};
|
||||
|
||||
fn read_cl(context: *const anyopaque, buffer: []u8) ReadError!usize {
|
||||
const request: *Request = @constCast(@alignCast(@ptrCast(context)));
|
||||
const request: *Request = @ptrCast(@alignCast(@constCast(context)));
|
||||
const s = request.server;
|
||||
|
||||
const remaining_content_length = &request.reader_state.remaining_content_length;
|
||||
@ -652,7 +652,7 @@ pub const Request = struct {
|
||||
}
|
||||
|
||||
fn read_chunked(context: *const anyopaque, buffer: []u8) ReadError!usize {
|
||||
const request: *Request = @constCast(@alignCast(@ptrCast(context)));
|
||||
const request: *Request = @ptrCast(@alignCast(@constCast(context)));
|
||||
const s = request.server;
|
||||
|
||||
const cp = &request.reader_state.chunk_parser;
|
||||
@ -894,7 +894,7 @@ pub const Response = struct {
|
||||
}
|
||||
|
||||
fn write_cl(context: *const anyopaque, bytes: []const u8) WriteError!usize {
|
||||
const r: *Response = @constCast(@alignCast(@ptrCast(context)));
|
||||
const r: *Response = @ptrCast(@alignCast(@constCast(context)));
|
||||
|
||||
var trash: u64 = std.math.maxInt(u64);
|
||||
const len = switch (r.transfer_encoding) {
|
||||
@ -944,7 +944,7 @@ pub const Response = struct {
|
||||
}
|
||||
|
||||
fn write_chunked(context: *const anyopaque, bytes: []const u8) WriteError!usize {
|
||||
const r: *Response = @constCast(@alignCast(@ptrCast(context)));
|
||||
const r: *Response = @ptrCast(@alignCast(@constCast(context)));
|
||||
assert(r.transfer_encoding == .chunked);
|
||||
|
||||
if (r.elide_body)
|
||||
|
||||
@ -154,7 +154,7 @@ pub const MemoryMapSlice = struct {
|
||||
|
||||
pub fn getUnchecked(self: MemoryMapSlice, index: usize) *MemoryDescriptor {
|
||||
const offset: usize = index * self.info.descriptor_size;
|
||||
return @alignCast(@ptrCast(self.ptr[offset..]));
|
||||
return @ptrCast(@alignCast(self.ptr[offset..]));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@ -353,7 +353,7 @@ pub const RuntimeServices = extern struct {
|
||||
reset_type,
|
||||
reset_status,
|
||||
if (data) |d| d.len else 0,
|
||||
if (data) |d| @alignCast(@ptrCast(d.ptr)) else null,
|
||||
if (data) |d| @ptrCast(@alignCast(d.ptr)) else null,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -202,7 +202,7 @@ pub fn CreatePipe(rd: *HANDLE, wr: *HANDLE, sattr: *const SECURITY_ATTRIBUTES) C
|
||||
const name = UNICODE_STRING{
|
||||
.Length = len,
|
||||
.MaximumLength = len,
|
||||
.Buffer = @constCast(@ptrCast(str)),
|
||||
.Buffer = @ptrCast(@constCast(str)),
|
||||
};
|
||||
const attrs = OBJECT_ATTRIBUTES{
|
||||
.ObjectName = @constCast(&name),
|
||||
|
||||
@ -259,7 +259,7 @@ pub fn relocate(phdrs: []const elf.Phdr) void {
|
||||
|
||||
const rel = sorted_dynv[elf.DT_REL];
|
||||
if (rel != 0) {
|
||||
const rels: []const elf.Rel = @alignCast(@ptrCast(
|
||||
const rels: []const elf.Rel = @ptrCast(@alignCast(
|
||||
@as([*]align(@alignOf(elf.Rel)) const u8, @ptrFromInt(base_addr + rel))[0..sorted_dynv[elf.DT_RELSZ]],
|
||||
));
|
||||
for (rels) |r| {
|
||||
@ -270,7 +270,7 @@ pub fn relocate(phdrs: []const elf.Phdr) void {
|
||||
|
||||
const rela = sorted_dynv[elf.DT_RELA];
|
||||
if (rela != 0) {
|
||||
const relas: []const elf.Rela = @alignCast(@ptrCast(
|
||||
const relas: []const elf.Rela = @ptrCast(@alignCast(
|
||||
@as([*]align(@alignOf(elf.Rela)) const u8, @ptrFromInt(base_addr + rela))[0..sorted_dynv[elf.DT_RELASZ]],
|
||||
));
|
||||
for (relas) |r| {
|
||||
|
||||
@ -783,7 +783,48 @@ fn renderExpression(r: *Render, node: Ast.Node.Index, space: Space) Error!void {
|
||||
=> {
|
||||
var buf: [2]Ast.Node.Index = undefined;
|
||||
const params = tree.builtinCallParams(&buf, node).?;
|
||||
return renderBuiltinCall(r, tree.nodeMainToken(node), params, space);
|
||||
var builtin_token = tree.nodeMainToken(node);
|
||||
|
||||
canonicalize: {
|
||||
if (params.len != 1) break :canonicalize;
|
||||
|
||||
const CastKind = enum {
|
||||
ptrCast,
|
||||
alignCast,
|
||||
addrSpaceCast,
|
||||
constCast,
|
||||
volatileCast,
|
||||
};
|
||||
const kind = meta.stringToEnum(CastKind, tree.tokenSlice(builtin_token)[1..]) orelse break :canonicalize;
|
||||
|
||||
var cast_map = std.EnumMap(CastKind, Ast.TokenIndex).init(.{});
|
||||
cast_map.put(kind, builtin_token);
|
||||
|
||||
var casts_before: usize = 0;
|
||||
if (builtin_token >= 2) {
|
||||
var prev_builtin_token = builtin_token - 2;
|
||||
while (tree.tokenTag(prev_builtin_token) == .builtin) : (prev_builtin_token -= 2) {
|
||||
const prev_kind = meta.stringToEnum(CastKind, tree.tokenSlice(prev_builtin_token)[1..]) orelse break;
|
||||
if (cast_map.contains(prev_kind)) break :canonicalize;
|
||||
cast_map.put(prev_kind, prev_builtin_token);
|
||||
casts_before += 1;
|
||||
}
|
||||
}
|
||||
|
||||
var next_builtin_token = builtin_token + 2;
|
||||
while (tree.tokenTag(next_builtin_token) == .builtin) : (next_builtin_token += 2) {
|
||||
const next_kind = meta.stringToEnum(CastKind, tree.tokenSlice(next_builtin_token)[1..]) orelse break;
|
||||
if (cast_map.contains(next_kind)) break :canonicalize;
|
||||
cast_map.put(next_kind, next_builtin_token);
|
||||
}
|
||||
|
||||
var it = cast_map.iterator();
|
||||
builtin_token = it.next().?.value.*;
|
||||
while (casts_before > 0) : (casts_before -= 1) {
|
||||
builtin_token = it.next().?.value.*;
|
||||
}
|
||||
}
|
||||
return renderBuiltinCall(r, builtin_token, params, space);
|
||||
},
|
||||
|
||||
.fn_proto_simple,
|
||||
|
||||
@ -71,7 +71,7 @@ fn castInt(comptime DestType: type, target: anytype) DestType {
|
||||
}
|
||||
|
||||
fn castPtr(comptime DestType: type, target: anytype) DestType {
|
||||
return @constCast(@volatileCast(@alignCast(@ptrCast(target))));
|
||||
return @ptrCast(@alignCast(@constCast(@volatileCast(target))));
|
||||
}
|
||||
|
||||
fn castToPtr(comptime DestType: type, comptime SourceType: type, target: anytype) DestType {
|
||||
|
||||
@ -58,8 +58,8 @@ const Value = extern struct {
|
||||
}
|
||||
|
||||
return switch (size) {
|
||||
64 => @as(*const u64, @alignCast(@ptrCast(value.handle))).*,
|
||||
128 => @as(*const u128, @alignCast(@ptrCast(value.handle))).*,
|
||||
64 => @as(*const u64, @ptrCast(@alignCast(value.handle))).*,
|
||||
128 => @as(*const u128, @ptrCast(@alignCast(value.handle))).*,
|
||||
else => @trap(),
|
||||
};
|
||||
}
|
||||
@ -74,8 +74,8 @@ const Value = extern struct {
|
||||
return (handle << extra_bits) >> extra_bits;
|
||||
}
|
||||
return switch (size) {
|
||||
64 => @as(*const i64, @alignCast(@ptrCast(value.handle))).*,
|
||||
128 => @as(*const i128, @alignCast(@ptrCast(value.handle))).*,
|
||||
64 => @as(*const i64, @ptrCast(@alignCast(value.handle))).*,
|
||||
128 => @as(*const i128, @ptrCast(@alignCast(value.handle))).*,
|
||||
else => @trap(),
|
||||
};
|
||||
}
|
||||
@ -92,9 +92,9 @@ const Value = extern struct {
|
||||
}, @bitCast(@intFromPtr(value.handle)));
|
||||
}
|
||||
return @floatCast(switch (size) {
|
||||
64 => @as(*const f64, @alignCast(@ptrCast(value.handle))).*,
|
||||
80 => @as(*const f80, @alignCast(@ptrCast(value.handle))).*,
|
||||
128 => @as(*const f128, @alignCast(@ptrCast(value.handle))).*,
|
||||
64 => @as(*const f64, @ptrCast(@alignCast(value.handle))).*,
|
||||
80 => @as(*const f80, @ptrCast(@alignCast(value.handle))).*,
|
||||
128 => @as(*const f128, @ptrCast(@alignCast(value.handle))).*,
|
||||
else => @trap(),
|
||||
});
|
||||
}
|
||||
|
||||
@ -1365,7 +1365,7 @@ const Local = struct {
|
||||
capacity: u32,
|
||||
};
|
||||
fn header(list: ListSelf) *Header {
|
||||
return @alignCast(@ptrCast(list.bytes - bytes_offset));
|
||||
return @ptrCast(@alignCast(list.bytes - bytes_offset));
|
||||
}
|
||||
pub fn view(list: ListSelf) View {
|
||||
const capacity = list.header().capacity;
|
||||
@ -1574,7 +1574,7 @@ const Shard = struct {
|
||||
}
|
||||
};
|
||||
fn header(map: @This()) *Header {
|
||||
return @alignCast(@ptrCast(@as([*]u8, @ptrCast(map.entries)) - entries_offset));
|
||||
return @ptrCast(@alignCast(@as([*]u8, @ptrCast(map.entries)) - entries_offset));
|
||||
}
|
||||
|
||||
const Entry = extern struct {
|
||||
|
||||
@ -898,7 +898,7 @@ const Resource = union(enum) {
|
||||
}
|
||||
|
||||
fn read(context: *const anyopaque, buffer: []u8) anyerror!usize {
|
||||
const resource: *Resource = @constCast(@ptrCast(@alignCast(context)));
|
||||
const resource: *Resource = @ptrCast(@alignCast(@constCast(context)));
|
||||
switch (resource.*) {
|
||||
.file => |*f| return f.read(buffer),
|
||||
.http_request => |*r| return r.read(buffer),
|
||||
|
||||
@ -4008,11 +4008,11 @@ fn transCreateCompoundAssign(
|
||||
}
|
||||
|
||||
fn removeCVQualifiers(c: *Context, dst_type_node: Node, expr: Node) Error!Node {
|
||||
const const_casted = try Tag.const_cast.create(c.arena, expr);
|
||||
const volatile_casted = try Tag.volatile_cast.create(c.arena, const_casted);
|
||||
const volatile_casted = try Tag.volatile_cast.create(c.arena, expr);
|
||||
const const_casted = try Tag.const_cast.create(c.arena, volatile_casted);
|
||||
return Tag.as.create(c.arena, .{
|
||||
.lhs = dst_type_node,
|
||||
.rhs = try Tag.ptr_cast.create(c.arena, volatile_casted),
|
||||
.rhs = try Tag.ptr_cast.create(c.arena, const_casted),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@ -1091,7 +1091,7 @@ test "initialize pointer to anyopaque with reference to empty array initializer"
|
||||
const ptr: *const anyopaque = &.{};
|
||||
// The above acts like an untyped initializer, since the `.{}` has no result type.
|
||||
// So, `ptr` points in memory to an empty tuple (`@TypeOf(.{})`).
|
||||
const casted: *const @TypeOf(.{}) = @alignCast(@ptrCast(ptr));
|
||||
const casted: *const @TypeOf(.{}) = @ptrCast(@alignCast(ptr));
|
||||
const loaded = casted.*;
|
||||
// `val` should be a `@TypeOf(.{})`, as expected.
|
||||
// We can't check the value, but it's zero-bit, so the type matching is good enough.
|
||||
|
||||
@ -477,7 +477,7 @@ fn GenericIntApplier(
|
||||
}
|
||||
|
||||
fn typeErasedApplyFn(context: *const anyopaque, arg: u32) void {
|
||||
const ptr: *const Context = @alignCast(@ptrCast(context));
|
||||
const ptr: *const Context = @ptrCast(@alignCast(context));
|
||||
applyFn(ptr.*, arg);
|
||||
}
|
||||
};
|
||||
|
||||
@ -8,7 +8,7 @@ test "anyopaque extern symbol" {
|
||||
if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
|
||||
|
||||
const a = @extern(*anyopaque, .{ .name = "a_mystery_symbol" });
|
||||
const b: *i32 = @alignCast(@ptrCast(a));
|
||||
const b: *i32 = @ptrCast(@alignCast(a));
|
||||
try expect(b.* == 1234);
|
||||
}
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@ export fn b() void {
|
||||
_ = @constCast(@volatileCast(123));
|
||||
}
|
||||
export fn c() void {
|
||||
const x: ?*f32 = @constCast(@ptrCast(@addrSpaceCast(@volatileCast(p))));
|
||||
const x: ?*f32 = @ptrCast(@addrSpaceCast(@constCast(@volatileCast(p))));
|
||||
_ = x;
|
||||
}
|
||||
|
||||
|
||||
@ -3234,12 +3234,12 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
|
||||
\\pub export fn bar(arg_a: [*c]const c_int) void {
|
||||
\\ var a = arg_a;
|
||||
\\ _ = &a;
|
||||
\\ foo(@as([*c]c_int, @ptrCast(@volatileCast(@constCast(a)))));
|
||||
\\ foo(@as([*c]c_int, @ptrCast(@constCast(@volatileCast(a)))));
|
||||
\\}
|
||||
\\pub export fn baz(arg_a: [*c]volatile c_int) void {
|
||||
\\ var a = arg_a;
|
||||
\\ _ = &a;
|
||||
\\ foo(@as([*c]c_int, @ptrCast(@volatileCast(@constCast(a)))));
|
||||
\\ foo(@as([*c]c_int, @ptrCast(@constCast(@volatileCast(a)))));
|
||||
\\}
|
||||
});
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user