|
| 1 | +//@ only-cdb |
| 2 | +//@ compile-flags:-g |
| 3 | + |
| 4 | +// === CDB TESTS =================================================================================== |
| 5 | +// Generic functions cause ambigious breakpoints. |
| 6 | +// cdb-command:dx @$debuggerRootNamespace.Debugger.Settings.EngineInitialization.ResolveAmbiguousBreakpoints = true; |
| 7 | +// cdb-command:bp `closures.rs:57` |
| 8 | +// cdb-command:g |
| 9 | +// cdb-command:dx add_closure |
| 10 | +// cdb-check:add_closure [Type: closures::main::closure_env$0] |
| 11 | +// cdb-check: [+0x[...]] _ref__base_value : 0x[...] : 42 [Type: int *] |
| 12 | +// cdb-command:dx increment |
| 13 | +// cdb-check:increment [Type: closures::main::closure_env$1] |
| 14 | +// cdb-check: [+0x[...]] _ref__count : 0x[...] : 2 [Type: int *] |
| 15 | +// cdb-command:dx consume_closure |
| 16 | +// cdb-check:consume_closure [Type: closures::main::closure_env$2] |
| 17 | +// cdb-check: [+0x[...]] x : [...] [Type: alloc::string::String] |
| 18 | +// cdb-check: [+0x[...]] _ref__base_value : 0x[...] : 42 [Type: int *] |
| 19 | +// cdb-command:dx simple_closure |
| 20 | +// cdb-checksimple_closure [Type: closures::main::closure_env$5] |
| 21 | +// cdb-check: [+0x[...]] _ref__base_value : 0x[...] : 42 [Type: int *] |
| 22 | +// cdb-command:g |
| 23 | +// cdb-command:dx first_closure |
| 24 | +// cdb-check:first_closure [Type: closures::main::closure_env$6] |
| 25 | +// cdb-check: [+0x[...]] _ref__variable : 0x[...] : 1 [Type: __int64 *] |
| 26 | +// cdb-check: [+0x[...]] _ref__constant : 0x[...] : 2 [Type: __int64 *] |
| 27 | +// cdb-check: [+0x[...]] _ref__a_struct : 0x[...] [Type: closures::Struct *] |
| 28 | +// cdb-check: [+0x[...]] _ref__struct_ref : 0x[...] [Type: closures::Struct * *] |
| 29 | +// cdb-check: [+0x[...]] _ref__owned_value : 0x[...] [Type: __int64 * *] |
| 30 | +// cdb-command:g |
| 31 | +// cdb-command:dx many_param_closure |
| 32 | +// cdb-check:many_param_closure [Type: closures::main::closure_env$7] |
| 33 | +// cdb-check: [+0x[...]] _ref__base_value : 0x[...] : 42 [Type: int *] |
| 34 | +// cdb-command:g |
| 35 | +// cdb-command:dv |
| 36 | +// cdb-command:dx generic_closure |
| 37 | +// cdb-check:generic_closure [Type: closures::generic_func::closure_env$0<i32>] |
| 38 | +// cdb-check: [+0x[...]] _ref__x : 0x[...] : 42 [Type: int *] |
| 39 | +// cdb-command:g |
| 40 | +// cdb-command:dx generic_closure |
| 41 | +// cdb-check:generic_closure [Type: closures::generic_func::closure_env$0<ref$<str$> >] |
| 42 | +// cdb-check: [+0x000] _ref__x : 0x[...] : "base_value" [Type: ref$<str$> *] |
| 43 | +// cdb-command:g |
| 44 | +// cdb-command:dx second_closure |
| 45 | +// cdb-check:second_closure [Type: closures::main::closure_env$8] |
| 46 | +// cdb-check: [+0x[...]] _ref__variable : 0x[...] : 2 [Type: __int64 *] |
| 47 | +// cdb-check: [+0x[...]] _ref__constant : 0x[...] : 2 [Type: __int64 *] |
| 48 | +// cdb-check: [+0x[...]] _ref__a_struct : 0x[...] [Type: closures::Struct *] |
| 49 | +// cdb-check: [+0x[...]] _ref__struct_ref : 0x[...] [Type: closures::Struct * *] |
| 50 | +// cdb-check: [+0x[...]] _ref__owned_value : 0x[...] [Type: __int64 * *] |
| 51 | + |
| 52 | +#[inline(never)] |
| 53 | +fn generic_func<Tfunc: std::fmt::Debug>(x: Tfunc) { |
| 54 | + let generic_closure = |a: u32| { |
| 55 | + println!("{:?}", x); |
| 56 | + }; |
| 57 | + |
| 58 | + _zzz(); // #break |
| 59 | + |
| 60 | + // rustc really wants to inline this closure, so we use black_box instead of calling it |
| 61 | + std::hint::black_box(generic_closure); |
| 62 | +} |
| 63 | + |
| 64 | +struct Struct { |
| 65 | + a: isize, |
| 66 | + b: f64, |
| 67 | + c: usize, |
| 68 | +} |
| 69 | + |
| 70 | +fn main() { |
| 71 | + let base_value = 42; |
| 72 | + let mut count = 0; |
| 73 | + |
| 74 | + let add_closure = |a: i32, b: i32| a + b + base_value; |
| 75 | + |
| 76 | + add_closure(40, 2); |
| 77 | + |
| 78 | + let mut increment = || { |
| 79 | + count += 1; |
| 80 | + }; |
| 81 | + |
| 82 | + increment(); // count: 1 |
| 83 | + increment(); // count: 2 |
| 84 | + |
| 85 | + let x = String::from("hello"); |
| 86 | + |
| 87 | + // Define a closure that consumes the captured variable `x` |
| 88 | + let consume_closure = move || { |
| 89 | + drop(x); |
| 90 | + base_value + 1 |
| 91 | + }; |
| 92 | + |
| 93 | + consume_closure(); |
| 94 | + |
| 95 | + let paramless_closure = || 42; |
| 96 | + |
| 97 | + let void_closure = |a: i32| { |
| 98 | + println!("Closure with arg: {:?}", a); |
| 99 | + }; |
| 100 | + |
| 101 | + let simple_closure = || { |
| 102 | + let incremented_value = base_value + 1; |
| 103 | + incremented_value |
| 104 | + }; |
| 105 | + |
| 106 | + let result = /*42; */ add_closure(40, 2); |
| 107 | + println!("Result: {:?}", result); |
| 108 | + void_closure(result); |
| 109 | + let result = simple_closure(); |
| 110 | + println!("Result: {:?}", result); |
| 111 | + |
| 112 | + let mut variable = 1; |
| 113 | + let constant = 2; |
| 114 | + |
| 115 | + let a_struct = Struct { a: -3, b: 4.5, c: 5 }; |
| 116 | + |
| 117 | + _zzz(); // #break |
| 118 | + |
| 119 | + let struct_ref = &a_struct; |
| 120 | + let owned_value: Box<_> = Box::new(6); |
| 121 | + |
| 122 | + { |
| 123 | + let mut first_closure = || { |
| 124 | + variable = constant + a_struct.a + struct_ref.a + *owned_value; |
| 125 | + }; |
| 126 | + |
| 127 | + _zzz(); // #break |
| 128 | + |
| 129 | + first_closure(); |
| 130 | + } |
| 131 | + |
| 132 | + let many_param_closure = |
| 133 | + |a: i32, b: f64, c: usize, d: Struct| base_value + a + b as i32 + c as i32 + d.c as i32; |
| 134 | + |
| 135 | + _zzz(); // #break |
| 136 | + |
| 137 | + many_param_closure(1, 2.0, 3, Struct { a: 4, b: 5.0, c: 6 }); |
| 138 | + |
| 139 | + generic_func(42); |
| 140 | + generic_func("base_value"); |
| 141 | + |
| 142 | + { |
| 143 | + let mut second_closure = || { |
| 144 | + variable = constant + a_struct.a + struct_ref.a + *owned_value; |
| 145 | + }; |
| 146 | + |
| 147 | + _zzz(); // #break |
| 148 | + |
| 149 | + second_closure(); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +fn _zzz() { |
| 154 | + () |
| 155 | +} |
0 commit comments