diff options
| author | mlugg <mlugg@mlugg.co.uk> | 2023-05-13 17:10:05 +0100 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2023-05-20 12:27:48 -0700 |
| commit | 38b83d9d93db400e8103f02eeb77729040bd3666 (patch) | |
| tree | f88cdbe5603e27aca415071a3f15ef60f4023d2b /src/type.zig | |
| parent | 7077e90b3f8991c844deb08a16ad3f4e0569398f (diff) | |
| download | zig-38b83d9d93db400e8103f02eeb77729040bd3666.tar.gz zig-38b83d9d93db400e8103f02eeb77729040bd3666.zip | |
Zir: eliminate `field_call_bind` and `field_call_bind_named`
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% |
+--------------+----------+----------+--------+
Diffstat (limited to 'src/type.zig')
| -rw-r--r-- | src/type.zig | 18 |
1 files changed, 1 insertions, 17 deletions
diff --git a/src/type.zig b/src/type.zig index 741b173db6..aea2d88571 100644 --- a/src/type.zig +++ b/src/type.zig @@ -156,8 +156,6 @@ pub const Type = extern union { .union_tagged, .type_info, => return .Union, - - .bound_fn => unreachable, } } @@ -933,7 +931,6 @@ pub const Type = extern union { // for example, a was resolved into .union_tagged but b was one of these tags. .type_info => unreachable, // needed to resolve the type before now - .bound_fn => unreachable, } } @@ -1242,7 +1239,6 @@ pub const Type = extern union { // we can't hash these based on tags because they wouldn't match the expanded version. .type_info => unreachable, // needed to resolve the type before now - .bound_fn => unreachable, } } @@ -1349,7 +1345,6 @@ pub const Type = extern union { .type_info, .@"anyframe", .generic_poison, - .bound_fn, => unreachable, .array_u8, @@ -1613,7 +1608,6 @@ pub const Type = extern union { .comptime_int, .comptime_float, .noreturn, - .bound_fn, => return writer.writeAll(@tagName(t)), .enum_literal => return writer.writeAll("@Type(.EnumLiteral)"), @@ -1949,7 +1943,6 @@ pub const Type = extern union { .inferred_alloc_const => unreachable, .inferred_alloc_mut => unreachable, .generic_poison => unreachable, - .bound_fn => unreachable, // TODO get rid of these Type.Tag values. .atomic_order => unreachable, @@ -2468,7 +2461,6 @@ pub const Type = extern union { .enum_literal, .empty_struct, .empty_struct_literal, - .bound_fn, // These are function *bodies*, not pointers. // Special exceptions have to be made when emitting functions due to // this returning false. @@ -2703,7 +2695,6 @@ pub const Type = extern union { .inferred_alloc_mut => unreachable, .inferred_alloc_const => unreachable, - .bound_fn => unreachable, .array, .array_sentinel, @@ -3182,7 +3173,6 @@ pub const Type = extern union { .noreturn, .inferred_alloc_const, .inferred_alloc_mut, - .bound_fn, => unreachable, .generic_poison => unreachable, @@ -3282,7 +3272,6 @@ pub const Type = extern union { .fn_ccc_void_no_args => unreachable, // represents machine code; not a pointer .function => unreachable, // represents machine code; not a pointer .@"opaque" => unreachable, // no size available - .bound_fn => unreachable, .noreturn => unreachable, .inferred_alloc_const => unreachable, .inferred_alloc_mut => unreachable, @@ -3630,7 +3619,6 @@ pub const Type = extern union { .inferred_alloc_mut => unreachable, .@"opaque" => unreachable, .generic_poison => unreachable, - .bound_fn => unreachable, .void => return 0, .bool, .u1 => return 1, @@ -5042,7 +5030,6 @@ pub const Type = extern union { .single_const_pointer, .single_mut_pointer, .pointer, - .bound_fn, => return null, .optional => { @@ -5245,7 +5232,6 @@ pub const Type = extern union { .inferred_alloc_mut => unreachable, .inferred_alloc_const => unreachable, - .bound_fn => unreachable, .array, .array_sentinel, @@ -6081,7 +6067,6 @@ pub const Type = extern union { inferred_alloc_mut, /// Same as `inferred_alloc_mut` but the local is `var` not `const`. inferred_alloc_const, // See last_no_payload_tag below. - bound_fn, // After this, the tag requires a payload. array_u8, @@ -6126,7 +6111,7 @@ pub const Type = extern union { enum_full, enum_nonexhaustive, - pub const last_no_payload_tag = Tag.bound_fn; + pub const last_no_payload_tag = Tag.inferred_alloc_const; pub const no_payload_count = @enumToInt(last_no_payload_tag) + 1; pub fn Type(comptime t: Tag) type { @@ -6199,7 +6184,6 @@ pub const Type = extern union { .extern_options, .type_info, .@"anyframe", - .bound_fn, => @compileError("Type Tag " ++ @tagName(t) ++ " has no payload"), .array_u8, |
