mirror of
https://github.com/ziglang/zig.git
synced 2024-11-27 15:42:49 +00:00
38b83d9d93
This commit removes the `field_call_bind` and `field_call_bind_named` ZIR instructions, replacing them with a `field_call` instruction which does the bind and call in one. `field_call_bind` is an unfortunate instruction. It's tied into one very specific usage pattern - its result can only be used as a callee. This means that it creates a value of a "pseudo-type" of sorts, `bound_fn` - this type used to exist in Zig, but now we just hide it from the user and have AstGen ensure it's only used in one way. This is quite silly - `Type` and `Value` should, as much as possible, reflect real Zig types and values. It makes sense to instead encode the `a.b()` syntax as its own ZIR instruction, so that's what we do here. This commit introduces a new instruction, `field_call`. It's like `call`, but rather than a callee ref, it contains a ref to the object pointer (`&a` in `a.b()`) and the string field name (`b`). This eliminates `bound_fn` from the language, and slightly decreases the size of generated ZIR - stats below. This commit does remove a few usages which used to be allowed: - `@field(a, "b")()` - `@call(.auto, a.b, .{})` - `@call(.auto, @field(a, "b"), .{})` These forms used to work just like `a.b()`, but are no longer allowed. I believe this is the correct choice for a few reasons: - `a.b()` is a purely *syntactic* form; for instance, `(a.b)()` is not valid. This means it is *not* inconsistent to not allow it in these cases; the special case here isn't "a field access as a callee", but rather this exact syntactic form. - The second argument to `@call` looks much more visually distinct from the callee in standard call syntax. To me, this makes it seem strange for that argument to not work like a normal expression in this context. - A more practical argument: it's confusing! `@field` and `@call` are used in very different contexts to standard function calls: the former normally hints at some comptime machinery, and the latter that you want more precise control over parts of a function call. In these contexts, you don't want implicit arguments adding extra confusion: you want to be very explicit about what you're doing. Lastly, some stats. I mentioned before that this change slightly reduces the size of ZIR - this is due to two instructions (`field_call_bind` then `call`) being replaced with one (`field_call`). Here are some numbers: +--------------+----------+----------+--------+ | File | Before | After | Change | +--------------+----------+----------+--------+ | Sema.zig | 4.72M | 4.53M | -4% | | AstGen.zig | 1.52M | 1.48M | -3% | | hash_map.zig | 283.9K | 276.2K | -3% | | math.zig | 312.6K | 305.3K | -2% | +--------------+----------+----------+--------+
104 lines
2.7 KiB
Zig
104 lines
2.7 KiB
Zig
const builtin = @import("builtin");
|
|
const std = @import("std");
|
|
const expect = std.testing.expect;
|
|
|
|
const HasFuncs = struct {
|
|
state: u32,
|
|
func_field: *const fn (u32) u32,
|
|
|
|
fn inc(self: *HasFuncs) void {
|
|
self.state += 1;
|
|
}
|
|
|
|
fn get(self: HasFuncs) u32 {
|
|
return self.state;
|
|
}
|
|
|
|
fn getPtr(self: *const HasFuncs) *const u32 {
|
|
return &self.state;
|
|
}
|
|
|
|
fn one(_: u32) u32 {
|
|
return 1;
|
|
}
|
|
fn two(_: u32) u32 {
|
|
return 2;
|
|
}
|
|
};
|
|
|
|
test "standard field calls" {
|
|
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
|
|
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
|
|
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
|
|
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
|
|
|
|
try expect(HasFuncs.one(0) == 1);
|
|
try expect(HasFuncs.two(0) == 2);
|
|
|
|
var v: HasFuncs = undefined;
|
|
v.state = 0;
|
|
v.func_field = HasFuncs.one;
|
|
|
|
const pv = &v;
|
|
const pcv: *const HasFuncs = pv;
|
|
|
|
try expect(v.get() == 0);
|
|
v.inc();
|
|
try expect(v.state == 1);
|
|
try expect(v.get() == 1);
|
|
|
|
pv.inc();
|
|
try expect(v.state == 2);
|
|
try expect(pv.get() == 2);
|
|
try expect(v.getPtr().* == 2);
|
|
try expect(pcv.get() == 2);
|
|
try expect(pcv.getPtr().* == 2);
|
|
|
|
v.func_field = HasFuncs.one;
|
|
try expect(v.func_field(0) == 1);
|
|
try expect(pv.func_field(0) == 1);
|
|
try expect(pcv.func_field(0) == 1);
|
|
|
|
try expect(pcv.func_field(blk: {
|
|
pv.func_field = HasFuncs.two;
|
|
break :blk 0;
|
|
}) == 1);
|
|
|
|
v.func_field = HasFuncs.two;
|
|
try expect(v.func_field(0) == 2);
|
|
try expect(pv.func_field(0) == 2);
|
|
try expect(pcv.func_field(0) == 2);
|
|
}
|
|
|
|
test "@field field calls" {
|
|
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
|
|
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
|
|
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
|
|
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
|
|
|
|
try expect(@field(HasFuncs, "one")(0) == 1);
|
|
try expect(@field(HasFuncs, "two")(0) == 2);
|
|
|
|
var v: HasFuncs = undefined;
|
|
v.state = 0;
|
|
v.func_field = HasFuncs.one;
|
|
|
|
const pv = &v;
|
|
const pcv: *const HasFuncs = pv;
|
|
|
|
v.func_field = HasFuncs.one;
|
|
try expect(@field(v, "func_field")(0) == 1);
|
|
try expect(@field(pv, "func_field")(0) == 1);
|
|
try expect(@field(pcv, "func_field")(0) == 1);
|
|
|
|
try expect(@field(pcv, "func_field")(blk: {
|
|
pv.func_field = HasFuncs.two;
|
|
break :blk 0;
|
|
}) == 1);
|
|
|
|
v.func_field = HasFuncs.two;
|
|
try expect(@field(v, "func_field")(0) == 2);
|
|
try expect(@field(pv, "func_field")(0) == 2);
|
|
try expect(@field(pcv, "func_field")(0) == 2);
|
|
}
|