authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-08-24 16:51:05+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-08-27 00:44:35+01:00
logf47f6d766e236c75af3c26cc9dec07a46725dba0
tree5880459f92a544d475e31e9f258d5617b55abe02
parent6808ce27bdca14d3876ac607c94f75ea054db7b8
signaturelock-open Commit is signed but in an unrecognized format.

behavior,cases: add `@branchHint` test coverage


2 files changed, 123 insertions(+), 5 deletions(-)

test/behavior/basic.zig+82-5
...@@ -107,13 +107,90 @@ test "non const ptr to aliased type" {...@@ -107,13 +107,90 @@ test "non const ptr to aliased type" {
107 try expect(?*int == ?*i32);107 try expect(?*int == ?*i32);
108}108}
109109
110test "cold function" {110test "function branch hints" {
111 thisIsAColdFn();111 const S = struct {
112 comptime thisIsAColdFn();112 fn none() void {
113 @branchHint(.none);
114 }
115 fn likely() void {
116 @branchHint(.likely);
117 }
118 fn unlikely() void {
119 @branchHint(.unlikely);
120 }
121 fn cold() void {
122 @branchHint(.cold);
123 }
124 fn unpredictable() void {
125 @branchHint(.unpredictable);
126 }
127 };
128 S.none();
129 S.likely();
130 S.unlikely();
131 S.cold();
132 S.unpredictable();
133 comptime S.none();
134 comptime S.likely();
135 comptime S.unlikely();
136 comptime S.cold();
137 comptime S.unpredictable();
138}
139
140test "if branch hints" {
141 var t: bool = undefined;
142 t = true;
143 if (t) {
144 @branchHint(.likely);
145 } else {
146 @branchHint(.cold);
147 }
113}148}
114149
115fn thisIsAColdFn() void {150test "switch branch hints" {
116 @branchHint(.cold);151 var t: bool = undefined;
152 t = true;
153 switch (t) {
154 true => {
155 @branchHint(.likely);
156 },
157 false => {
158 @branchHint(.cold);
159 },
160 }
161}
162
163test "orelse branch hints" {
164 var x: ?u32 = undefined;
165 x = 123;
166 const val = x orelse val: {
167 @branchHint(.cold);
168 break :val 456;
169 };
170 try expect(val == 123);
171}
172
173test "catch branch hints" {
174 var x: error{Bad}!u32 = undefined;
175 x = 123;
176 const val = x catch val: {
177 @branchHint(.cold);
178 break :val 456;
179 };
180 try expect(val == 123);
181}
182
183test "and/or branch hints" {
184 var t: bool = undefined;
185 t = true;
186 try expect(t or b: {
187 @branchHint(.unlikely);
188 break :b false;
189 });
190 try expect(t and b: {
191 @branchHint(.likely);
192 break :b true;
193 });
117}194}
118195
119test "unicode escape in character literal" {196test "unicode escape in character literal" {
test/cases/compile_errors/invalid_branch_hint.zig created+41
...@@ -0,0 +1,41 @@
1const globl = g: {
2 @branchHint(.none);
3 break :g {};
4};
5
6comptime {
7 @branchHint(.none);
8}
9
10test {
11 @branchHint(.none);
12}
13
14export fn foo() void {
15 {
16 @branchHint(.none);
17 }
18}
19
20export fn bar() void {
21 _ = (b: {
22 @branchHint(.none);
23 break :b true;
24 }) or true;
25}
26
27export fn qux() void {
28 (b: {
29 @branchHint(.none);
30 break :b @as(?void, {});
31 }) orelse unreachable;
32}
33
34// error
35//
36// :2:5: error: '@branchHint' outside function scope
37// :7:5: error: '@branchHint' outside function scope
38// :11:5: error: '@branchHint' must appear as the first statement in a function or conditional branch
39// :16:9: error: '@branchHint' must appear as the first statement in a function or conditional branch
40// :22:9: error: '@branchHint' must appear as the first statement in a function or conditional branch
41// :29:9: error: '@branchHint' must appear as the first statement in a function or conditional branch