Skip to content

Commit 325a18c

Browse files
committed
behavior,cases: add @branchHint test coverage
1 parent df413fb commit 325a18c

File tree

2 files changed

+123
-5
lines changed

2 files changed

+123
-5
lines changed

test/behavior/basic.zig

+82-5
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,90 @@ test "non const ptr to aliased type" {
107107
try expect(?*int == ?*i32);
108108
}
109109

110-
test "cold function" {
111-
thisIsAColdFn();
112-
comptime thisIsAColdFn();
110+
test "function branch hints" {
111+
const S = struct {
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+
140+
test "if branch hints" {
141+
var t: bool = undefined;
142+
t = true;
143+
if (t) {
144+
@branchHint(.likely);
145+
} else {
146+
@branchHint(.cold);
147+
}
113148
}
114149

115-
fn thisIsAColdFn() void {
116-
@branchHint(.cold);
150+
test "switch branch hints" {
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+
163+
test "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+
173+
test "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+
183+
test "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+
});
117194
}
118195

119196
test "unicode escape in character literal" {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const globl = g: {
2+
@branchHint(.none);
3+
break :g {};
4+
};
5+
6+
comptime {
7+
@branchHint(.none);
8+
}
9+
10+
test {
11+
@branchHint(.none);
12+
}
13+
14+
export fn foo() void {
15+
{
16+
@branchHint(.none);
17+
}
18+
}
19+
20+
export fn bar() void {
21+
_ = (b: {
22+
@branchHint(.none);
23+
break :b true;
24+
}) or true;
25+
}
26+
27+
export 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

0 commit comments

Comments
 (0)