Skip to content

Commit bfffb6f

Browse files
committed
Add some tests with references
1 parent 0d69364 commit bfffb6f

File tree

1 file changed

+56
-29
lines changed
  • test_programs/execution_success/hint_black_box/src

1 file changed

+56
-29
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,55 @@
11
use std::hint::black_box;
22

33
fn main(a: u32, b: u32) {
4-
// This version unrolls into a number of additions
5-
assert_eq(loop(5, a), b);
6-
// This version simplifies into a single `constraint 50 == b`
7-
assert_eq(loop(5, 10), b);
8-
// This version should not simplify down to a single constraint,
9-
// it should treat 10 as opaque:
10-
assert_eq(loop(5, black_box(10)), b);
11-
12-
// Check array handling.
13-
let arr = [a, a, a, a, a];
14-
15-
assert_eq(array_sum(arr), b);
16-
assert_eq(array_sum(black_box(arr)), b);
17-
18-
assert_eq(slice_sum(arr.as_slice()), b);
19-
assert_eq(slice_sum(black_box(arr).as_slice()), b);
20-
21-
// This doesn't work because by calling `black_box` on a slice the compiler
22-
// loses track of the length, and then cannot unroll the loop for ACIR.
23-
//assert_eq(slice_sum(black_box(arr.as_slice())), b);
24-
25-
// But we can pass a blackboxed slice to Brillig.
26-
let s = unsafe {
27-
brillig_slice_sum(black_box(arr.as_slice()))
28-
};
29-
assert_eq(s, b);
4+
// // This version unrolls into a number of additions
5+
// assert_eq(loop(5, a), b);
6+
// // This version simplifies into a single `constraint 50 == b`
7+
// assert_eq(loop(5, 10), b);
8+
// // This version should not simplify down to a single constraint,
9+
// // it should treat 10 as opaque:
10+
// assert_eq(loop(5, black_box(10)), b);
11+
12+
// // Check array handling.
13+
// let arr = [a, a, a, a, a];
14+
15+
// assert_eq(array_sum(arr), b);
16+
// assert_eq(array_sum(black_box(arr)), b);
17+
18+
// assert_eq(slice_sum(arr.as_slice()), b);
19+
// assert_eq(slice_sum(black_box(arr).as_slice()), b);
20+
21+
// // This doesn't work because by calling `black_box` on a slice the compiler
22+
// // loses track of the length, and then cannot unroll the loop for ACIR.
23+
// //assert_eq(slice_sum(black_box(arr.as_slice())), b);
24+
25+
// // But we can pass a blackboxed slice to Brillig.
26+
// let s = unsafe {
27+
// brillig_slice_sum(black_box(arr.as_slice()))
28+
// };
29+
// assert_eq(s, b);
30+
31+
let mut d = b;
32+
// This gets completely eliminated:
33+
let mut c = 0;
34+
set_ref(&mut c, &mut d);
35+
assert_eq(c, b);
36+
37+
// This way the constraint is preserved:
38+
let mut c = 0;
39+
set_ref(&mut c, &mut black_box(d));
40+
assert_eq(c, b);
41+
42+
// A reference over the output of black box is not the original variable:
43+
let mut c = 0;
44+
set_ref(&mut black_box(c), &mut d);
45+
assert_eq(c, 0);
46+
47+
// This would cause a causes a crash during SSA passes unless it's a Brillig runtime:
48+
// > Could not resolve some references to the array. All references must be resolved at compile time
49+
// If we use `--force-brillig` then the it doesn't crash but the assertion fails.
50+
// let mut c = 0;
51+
// set_ref(black_box(&mut c), black_box(&mut d));
52+
// assert_eq(c, d);
3053
}
3154

3255
fn loop(n: u32, k: u32) -> u32 {
@@ -47,16 +70,20 @@ fn array_sum<let N: u32>(xs: [u32; N]) -> u32 {
4770

4871
fn slice_sum(xs: [u32]) -> u32 {
4972
let mut sum = 0;
50-
for x in xs{
73+
for x in xs {
5174
sum = sum + x;
5275
}
5376
sum
5477
}
5578

5679
unconstrained fn brillig_slice_sum(xs: [u32]) -> u32 {
5780
let mut sum = 0;
58-
for x in xs{
81+
for x in xs {
5982
sum = sum + x;
6083
}
6184
sum
62-
}
85+
}
86+
87+
fn set_ref(c: &mut u32, b: &mut u32) {
88+
*c = *b;
89+
}

0 commit comments

Comments
 (0)