Repository navigation
Closed
Description
Currently the Zig type system has a "BoundFn" which can be observed thusly:
test "example" {
var a: A = .{};
@compileLog(@TypeOf(a.b));
}
const A = struct {
fn b() void {}
};This type is to be removed from the language. Instead, method call syntax will be explicitly recognized in the parser. Here's an example which currently passes but will become a compile error:
test "example" {
var a1: A = .{};
var a2: A = .{};
(if (true) a1.b else a2.b)();
}
const A = struct {
fn b(a: A) void {
_ = a;
}
};After this change, this will give a compile error: struct A has no field named b.
Related: #7942
Here's a diff to get you started:
diff --git a/lib/std/builtin.zig b/lib/std/builtin.zig
index 9d432a3a0..f4daf5354 100644
--- a/lib/std/builtin.zig
+++ b/lib/std/builtin.zig
@@ -204,7 +204,6 @@ pub const TypeInfo = union(enum) {
Enum: Enum,
Union: Union,
Fn: Fn,
- BoundFn: Fn,
Opaque: Opaque,
Frame: Frame,
AnyFrame: AnyFrame,
diff --git a/src/stage1/all_types.hpp b/src/stage1/all_types.hpp
index dfebc66cf..d36396c68 100644
--- a/src/stage1/all_types.hpp
+++ b/src/stage1/all_types.hpp
@@ -310,12 +310,6 @@ struct ConstErrValue {
ZigValue *payload;
};
-struct ConstBoundFnValue {
- ZigFn *fn;
- Stage1AirInst *first_arg;
- AstNode *first_arg_src;
-};
-
struct ConstArgTuple {
size_t start_index;
size_t end_index;
@@ -510,7 +504,6 @@ struct ZigValue {
double x_f64;
float128_t x_f128;
bool x_bool;
- ConstBoundFnValue x_bound_fn;
ZigType *x_type;
ZigValue *x_optional;
ConstErrValue x_err_union;
@@ -1492,10 +1485,6 @@ struct ZigTypeFn {
ZigType *bound_fn_parent;
};
-struct ZigTypeBoundFn {
- ZigType *fn_type;
-};
-
// Needs to have the same memory layout as ZigTypeArray
struct ZigTypeVector {
// The type must be a pointer, integer, bool, or float
@@ -1528,7 +1517,6 @@ enum ZigTypeId {
ZigTypeIdEnum,
ZigTypeIdUnion,
ZigTypeIdFn,
- ZigTypeIdBoundFn,
ZigTypeIdOpaque,
ZigTypeIdFnFrame,
ZigTypeIdAnyFrame,
@@ -1587,7 +1575,6 @@ struct ZigType {
ZigTypeEnum enumeration;
ZigTypeUnion unionation;
ZigTypeFn fn;
- ZigTypeBoundFn bound_fn;
ZigTypeVector vector;
ZigTypeOpaque opaque;
ZigTypeFnFrame frame;Reacted by Felix Queißner, Komu Wairagu, Danyil Diabin and Ellen Emilia Anna Zscheile
Activity
zigazeljko commentedon Aug 2, 2021
What is the expected implementation here?
NodeTypeFieldAccessExpr+NodeTypeFnCallExprcombo.NodeTypeFieldAccessExpr+NodeTypeFnCallExprcombo inside astgen.andrewrk commentedon Aug 24, 2021
@zigazeljko apologies for not seeing your question earlier.
You'll need to change the parser as noted here:
edt-xx commentedon Sep 27, 2021
Probably a stupid question, but how will zig handle a 'class fn' in a struct? eg usually you want:
dacjames commentedon Nov 8, 2021
Is anyone working on this? I've been looking for something for a first time contribution that touches the language itself (since that's my interest area).
Aside from overall simplification, what is the motivation for removing
BoundFn, @andrewrk ?fogti commentedon Jan 28, 2022
it is normal that I arrive at the following when applying the patch in the first comment?
compiled with zig version 0.10.0 fbe5336
with first-comment patch applied on top 0e6d218
code snippet:
Eliminate `BoundFn` type from the language
Eliminate `BoundFn` type from the language
Eliminate `BoundFn` type from the language
Eliminate `BoundFn` type from the language
Eliminate `BoundFn` type from the language