aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorThomas Ives <tom@trives.co.uk>2021-11-02 22:43:51 +0000
committerAndrew Kelley <andrew@ziglang.org>2021-11-10 12:39:47 -0500
commit51efd553ae94fbf958974284a2f30091e4e1a2fa (patch)
tree84f513a2d2cc57ffd0d1b035dac450d25e35a26c /test
parent91c3206b45074821749f52043937bf6ed6d9a105 (diff)
downloadzig-51efd553ae94fbf958974284a2f30091e4e1a2fa.tar.gz
zig-51efd553ae94fbf958974284a2f30091e4e1a2fa.zip
C backend: Improve lowering of Zig types to C types
1. Changed Zig pointers to functions to be typedef'd so then we can treat them the same as other types. 2. Distinguished between const slices (zig_L prefix) and mut slices (zig_M prefix). 3. Changed lowering of Zig "const pointers" (e.g. *const u8) to to C "pointers to const" (e.g. const char *) rather than C "const pointers" (e.g. char * const) 4. Ensured that all typedefs are "linked" even if the decl doesn't require any forward declarations 5. Added test that exercises function pointer type rendering 6. Changed .slice_ptr instruction to allocate pointer local rather than a uintptr_t local
Diffstat (limited to 'test')
-rw-r--r--test/behavior/basic.zig42
1 files changed, 42 insertions, 0 deletions
diff --git a/test/behavior/basic.zig b/test/behavior/basic.zig
index 93a1e676fe..cca2d63cd7 100644
--- a/test/behavior/basic.zig
+++ b/test/behavior/basic.zig
@@ -250,3 +250,45 @@ test "volatile load and store" {
ptr.* += 1;
try expect(ptr.* == 1235);
}
+
+fn fA() []const u8 {
+ return "a";
+}
+fn fB() []const u8 {
+ return "b";
+}
+
+test "call function pointer in struct" {
+ try expect(mem.eql(u8, f3(true), "a"));
+ try expect(mem.eql(u8, f3(false), "b"));
+}
+
+fn f3(x: bool) []const u8 {
+ var wrapper: FnPtrWrapper = .{
+ .fn_ptr = fB,
+ };
+
+ if (x) {
+ wrapper.fn_ptr = fA;
+ }
+
+ return wrapper.fn_ptr();
+}
+
+const FnPtrWrapper = struct {
+ fn_ptr: fn () []const u8,
+};
+
+test "const ptr from var variable" {
+ var x: u64 = undefined;
+ var y: u64 = undefined;
+
+ x = 78;
+ copy(&x, &y);
+
+ try expect(x == y);
+}
+
+fn copy(src: *const u64, dst: *u64) void {
+ dst.* = src.*;
+}