aboutsummaryrefslogtreecommitdiff
path: root/test/behavior
diff options
context:
space:
mode:
authorMatthew Lugg <mlugg@mlugg.co.uk>2024-08-27 06:10:56 +0100
committerGitHub <noreply@github.com>2024-08-27 06:10:56 +0100
commitd3c6f7179c7a6086ab9cdbaed231da9a1f0b4dee (patch)
treea75bc8abc129a679fce0673165e04fa7283f2823 /test/behavior
parentd9147b91a601ad2442aaa43f4dba2d01b25d803d (diff)
parent4c0f021c2e4270c7392df7250a5d8f2431dcc54f (diff)
downloadzig-d3c6f7179c7a6086ab9cdbaed231da9a1f0b4dee.tar.gz
zig-d3c6f7179c7a6086ab9cdbaed231da9a1f0b4dee.zip
Merge pull request #21214 from mlugg/branch-hint-and-export
Implement `@branchHint` and new `@export` usage
Diffstat (limited to 'test/behavior')
-rw-r--r--test/behavior/basic.zig89
-rw-r--r--test/behavior/builtin_functions_returning_void_or_noreturn.zig3
-rw-r--r--test/behavior/export_builtin.zig32
-rw-r--r--test/behavior/export_c_keywords.zig24
-rw-r--r--test/behavior/generics.zig2
5 files changed, 103 insertions, 47 deletions
diff --git a/test/behavior/basic.zig b/test/behavior/basic.zig
index 9d34742604..4e5d0d55ab 100644
--- a/test/behavior/basic.zig
+++ b/test/behavior/basic.zig
@@ -107,13 +107,90 @@ test "non const ptr to aliased type" {
try expect(?*int == ?*i32);
}
-test "cold function" {
- thisIsAColdFn();
- comptime thisIsAColdFn();
+test "function branch hints" {
+ const S = struct {
+ fn none() void {
+ @branchHint(.none);
+ }
+ fn likely() void {
+ @branchHint(.likely);
+ }
+ fn unlikely() void {
+ @branchHint(.unlikely);
+ }
+ fn cold() void {
+ @branchHint(.cold);
+ }
+ fn unpredictable() void {
+ @branchHint(.unpredictable);
+ }
+ };
+ S.none();
+ S.likely();
+ S.unlikely();
+ S.cold();
+ S.unpredictable();
+ comptime S.none();
+ comptime S.likely();
+ comptime S.unlikely();
+ comptime S.cold();
+ comptime S.unpredictable();
+}
+
+test "if branch hints" {
+ var t: bool = undefined;
+ t = true;
+ if (t) {
+ @branchHint(.likely);
+ } else {
+ @branchHint(.cold);
+ }
}
-fn thisIsAColdFn() void {
- @setCold(true);
+test "switch branch hints" {
+ var t: bool = undefined;
+ t = true;
+ switch (t) {
+ true => {
+ @branchHint(.likely);
+ },
+ false => {
+ @branchHint(.cold);
+ },
+ }
+}
+
+test "orelse branch hints" {
+ var x: ?u32 = undefined;
+ x = 123;
+ const val = x orelse val: {
+ @branchHint(.cold);
+ break :val 456;
+ };
+ try expect(val == 123);
+}
+
+test "catch branch hints" {
+ var x: error{Bad}!u32 = undefined;
+ x = 123;
+ const val = x catch val: {
+ @branchHint(.cold);
+ break :val 456;
+ };
+ try expect(val == 123);
+}
+
+test "and/or branch hints" {
+ var t: bool = undefined;
+ t = true;
+ try expect(t or b: {
+ @branchHint(.unlikely);
+ break :b false;
+ });
+ try expect(t and b: {
+ @branchHint(.likely);
+ break :b true;
+ });
}
test "unicode escape in character literal" {
@@ -734,7 +811,7 @@ test "extern variable with non-pointer opaque type" {
if (builtin.zig_backend == .stage2_x86_64 and builtin.target.ofmt != .elf and builtin.target.ofmt != .macho) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; // TODO
- @export(var_to_export, .{ .name = "opaque_extern_var" });
+ @export(&var_to_export, .{ .name = "opaque_extern_var" });
try expect(@as(*align(1) u32, @ptrCast(&opaque_extern_var)).* == 42);
}
extern var opaque_extern_var: opaque {};
diff --git a/test/behavior/builtin_functions_returning_void_or_noreturn.zig b/test/behavior/builtin_functions_returning_void_or_noreturn.zig
index 92617ef3a4..01b3062511 100644
--- a/test/behavior/builtin_functions_returning_void_or_noreturn.zig
+++ b/test/behavior/builtin_functions_returning_void_or_noreturn.zig
@@ -15,14 +15,13 @@ test {
var val: u8 = undefined;
try testing.expectEqual({}, @atomicStore(u8, &val, 0, .unordered));
try testing.expectEqual(void, @TypeOf(@breakpoint()));
- try testing.expectEqual({}, @export(x, .{ .name = "x" }));
+ try testing.expectEqual({}, @export(&x, .{ .name = "x" }));
try testing.expectEqual({}, @fence(.acquire));
try testing.expectEqual({}, @memcpy(@as([*]u8, @ptrFromInt(1))[0..0], @as([*]u8, @ptrFromInt(1))[0..0]));
try testing.expectEqual({}, @memset(@as([*]u8, @ptrFromInt(1))[0..0], undefined));
try testing.expectEqual(noreturn, @TypeOf(if (true) @panic("") else {}));
try testing.expectEqual({}, @prefetch(&val, .{}));
try testing.expectEqual({}, @setAlignStack(16));
- try testing.expectEqual({}, @setCold(true));
try testing.expectEqual({}, @setEvalBranchQuota(0));
try testing.expectEqual({}, @setFloatMode(.optimized));
try testing.expectEqual({}, @setRuntimeSafety(true));
diff --git a/test/behavior/export_builtin.zig b/test/behavior/export_builtin.zig
index 25b6e2527e..6ca10376a1 100644
--- a/test/behavior/export_builtin.zig
+++ b/test/behavior/export_builtin.zig
@@ -2,7 +2,7 @@ const builtin = @import("builtin");
const std = @import("std");
const expect = std.testing.expect;
-test "exporting enum type and value" {
+test "exporting enum value" {
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
@@ -10,7 +10,7 @@ test "exporting enum type and value" {
const E = enum(c_int) { one, two };
const e: E = .two;
comptime {
- @export(e, .{ .name = "e" });
+ @export(&e, .{ .name = "e" });
}
};
try expect(S.e == .two);
@@ -23,13 +23,13 @@ test "exporting with internal linkage" {
const S = struct {
fn foo() callconv(.C) void {}
comptime {
- @export(foo, .{ .name = "exporting_with_internal_linkage_foo", .linkage = .internal });
+ @export(&foo, .{ .name = "exporting_with_internal_linkage_foo", .linkage = .internal });
}
};
S.foo();
}
-test "exporting using field access" {
+test "exporting using namespace access" {
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
@@ -38,7 +38,7 @@ test "exporting using field access" {
const x: u32 = 5;
};
comptime {
- @export(Inner.x, .{ .name = "foo", .linkage = .internal });
+ @export(&Inner.x, .{ .name = "foo", .linkage = .internal });
}
};
@@ -57,29 +57,9 @@ test "exporting comptime-known value" {
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
const x: u32 = 10;
- @export(x, .{ .name = "exporting_comptime_known_value_foo" });
+ @export(&x, .{ .name = "exporting_comptime_known_value_foo" });
const S = struct {
extern const exporting_comptime_known_value_foo: u32;
};
try expect(S.exporting_comptime_known_value_foo == 10);
}
-
-test "exporting comptime var" {
- if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
- if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
- if (builtin.zig_backend == .stage2_x86_64 and
- (builtin.target.ofmt != .elf and
- builtin.target.ofmt != .macho and
- builtin.target.ofmt != .coff)) return error.SkipZigTest;
- if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
- if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
- if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
-
- comptime var x: u32 = 5;
- @export(x, .{ .name = "exporting_comptime_var_foo" });
- x = 7; // modifying this now shouldn't change anything
- const S = struct {
- extern const exporting_comptime_var_foo: u32;
- };
- try expect(S.exporting_comptime_var_foo == 5);
-}
diff --git a/test/behavior/export_c_keywords.zig b/test/behavior/export_c_keywords.zig
index a41a9e75d1..53511ddef5 100644
--- a/test/behavior/export_c_keywords.zig
+++ b/test/behavior/export_c_keywords.zig
@@ -24,21 +24,21 @@ export fn some_non_c_keyword_function() Id {
}
comptime {
- @export(int, .{ .name = "long" });
- @export(int, .{ .name = "an_alias_of_int" });
+ @export(&int, .{ .name = "long" });
+ @export(&int, .{ .name = "an_alias_of_int" });
- @export(some_non_c_keyword_variable, .{ .name = "void" });
- @export(some_non_c_keyword_variable, .{ .name = "an_alias_of_some_non_c_keyword_variable" });
+ @export(&some_non_c_keyword_variable, .{ .name = "void" });
+ @export(&some_non_c_keyword_variable, .{ .name = "an_alias_of_some_non_c_keyword_variable" });
- @export(@"if", .{ .name = "else" });
- @export(@"if", .{ .name = "an_alias_of_if" });
+ @export(&@"if", .{ .name = "else" });
+ @export(&@"if", .{ .name = "an_alias_of_if" });
- @export(some_non_c_keyword_constant, .{ .name = "switch" });
- @export(some_non_c_keyword_constant, .{ .name = "an_alias_of_some_non_c_keyword_constant" });
+ @export(&some_non_c_keyword_constant, .{ .name = "switch" });
+ @export(&some_non_c_keyword_constant, .{ .name = "an_alias_of_some_non_c_keyword_constant" });
- @export(float, .{ .name = "double" });
- @export(float, .{ .name = "an_alias_of_float" });
+ @export(&float, .{ .name = "double" });
+ @export(&float, .{ .name = "an_alias_of_float" });
- @export(some_non_c_keyword_function, .{ .name = "break" });
- @export(some_non_c_keyword_function, .{ .name = "an_alias_of_some_non_c_keyword_function" });
+ @export(&some_non_c_keyword_function, .{ .name = "break" });
+ @export(&some_non_c_keyword_function, .{ .name = "an_alias_of_some_non_c_keyword_function" });
}
diff --git a/test/behavior/generics.zig b/test/behavior/generics.zig
index 67ec438d9e..0a1304ae15 100644
--- a/test/behavior/generics.zig
+++ b/test/behavior/generics.zig
@@ -320,7 +320,7 @@ test "generic function instantiation non-duplicates" {
const S = struct {
fn copy(comptime T: type, dest: []T, source: []const T) void {
- @export(foo, .{ .name = "test_generic_instantiation_non_dupe" });
+ @export(&foo, .{ .name = "test_generic_instantiation_non_dupe" });
for (source, 0..) |s, i| dest[i] = s;
}