|
| 1 | +#![deny(disjoint_capture_drop_reorder)] |
| 2 | +//~^ NOTE: the lint level is defined here |
| 3 | + |
| 4 | +// Test cases for types that implement a insignificant drop (stlib defined) |
| 5 | + |
| 6 | +// `t` needs Drop because one of its elements needs drop, |
| 7 | +// therefore precise capture might affect drop ordering |
| 8 | +fn test1_all_need_migration() { |
| 9 | + let t = (String::new(), String::new()); |
| 10 | + let t1 = (String::new(), String::new()); |
| 11 | + let t2 = (String::new(), String::new()); |
| 12 | + |
| 13 | + let c = || { |
| 14 | + //~^ERROR: Drop order affected for closure because of `capture_disjoint_fields` |
| 15 | + //~| NOTE: let (t, t1, t2) = (t, t1, t2); |
| 16 | + let _t = t.0; |
| 17 | + let _t1 = t1.0; |
| 18 | + let _t2 = t2.0; |
| 19 | + }; |
| 20 | + |
| 21 | + c(); |
| 22 | +} |
| 23 | + |
| 24 | +// String implements drop and therefore should be migrated. |
| 25 | +// But in this test cases, `t2` is completely captured and when it is dropped won't be affected |
| 26 | +fn test2_only_precise_paths_need_migration() { |
| 27 | + let t = (String::new(), String::new()); |
| 28 | + let t1 = (String::new(), String::new()); |
| 29 | + let t2 = (String::new(), String::new()); |
| 30 | + |
| 31 | + let c = || { |
| 32 | + //~^ERROR: Drop order affected for closure because of `capture_disjoint_fields` |
| 33 | + //~| NOTE: let (t, t1) = (t, t1); |
| 34 | + let _t = t.0; |
| 35 | + let _t1 = t1.0; |
| 36 | + let _t2 = t2; |
| 37 | + }; |
| 38 | + |
| 39 | + c(); |
| 40 | +} |
| 41 | + |
| 42 | +// If a variable would've not been captured by value then it would've not been |
| 43 | +// dropped with the closure and therefore doesn't need migration. |
| 44 | +fn test3_only_by_value_need_migration() { |
| 45 | + let t = (String::new(), String::new()); |
| 46 | + let t1 = (String::new(), String::new()); |
| 47 | + let c = || { |
| 48 | + //~^ERROR: Drop order affected for closure because of `capture_disjoint_fields` |
| 49 | + //~| NOTE: let (t) = (t); |
| 50 | + let _t = t.0; |
| 51 | + println!("{}", t1.1); |
| 52 | + }; |
| 53 | + |
| 54 | + c(); |
| 55 | +} |
| 56 | + |
| 57 | +// Copy types get copied into the closure instead of move. Therefore we don't need to |
| 58 | +// migrate then as their drop order isn't tied to the closure. |
| 59 | +fn test4_only_non_copy_types_need_migration() { |
| 60 | + let t = (String::new(), String::new()); |
| 61 | + |
| 62 | + // `t1` is Copy because all of its elements are Copy |
| 63 | + let t1 = (0i32, 0i32); |
| 64 | + |
| 65 | + let c = || { |
| 66 | + //~^ERROR: Drop order affected for closure because of `capture_disjoint_fields` |
| 67 | + //~| NOTE: let (t) = (t); |
| 68 | + let _t = t.0; |
| 69 | + let _t1 = t1.0; |
| 70 | + }; |
| 71 | + |
| 72 | + c(); |
| 73 | +} |
| 74 | + |
| 75 | +fn test5_only_drop_types_need_migration() { |
| 76 | + struct S(i32, i32); |
| 77 | + |
| 78 | + let t = (String::new(), String::new()); |
| 79 | + |
| 80 | + // `s` doesn't implement Drop or any elements within it, and doesn't need migration |
| 81 | + let s = S(0i32, 0i32); |
| 82 | + |
| 83 | + let c = || { |
| 84 | + //~^ERROR: Drop order affected for closure because of `capture_disjoint_fields` |
| 85 | + //~| NOTE: let (t) = (t); |
| 86 | + let _t = t.0; |
| 87 | + let _s = s.0; |
| 88 | + }; |
| 89 | + |
| 90 | + c(); |
| 91 | +} |
| 92 | + |
| 93 | +// Since we are using a move closure here, both `t` and `t1` get moved |
| 94 | +// even though they are being used by ref inside the closure. |
| 95 | +fn test6_move_closures_non_copy_types_might_need_migration() { |
| 96 | + let t = (String::new(), String::new()); |
| 97 | + let t1 = (String::new(), String::new()); |
| 98 | + let c = move || { |
| 99 | + //~^ERROR: Drop order affected for closure because of `capture_disjoint_fields` |
| 100 | + //~| NOTE: let (t1, t) = (t1, t); |
| 101 | + println!("{} {}", t1.1, t.1); |
| 102 | + }; |
| 103 | + |
| 104 | + c(); |
| 105 | +} |
| 106 | + |
| 107 | +// Test migration analysis in case of Drop + Non Drop aggregates. |
| 108 | +// Note we need migration here only because the non-copy (because Drop type) is captured, |
| 109 | +// otherwise we won't need to, since we can get away with just by ref capture in that case. |
| 110 | +fn test7_drop_non_drop_aggregate_need_migration() { |
| 111 | + let t = (String::new(), 0i32); |
| 112 | + |
| 113 | + let c = || { |
| 114 | + //~^ERROR: Drop order affected for closure because of `capture_disjoint_fields` |
| 115 | + //~| NOTE: let (t) = (t); |
| 116 | + let _t = t.0; |
| 117 | + }; |
| 118 | + |
| 119 | + c(); |
| 120 | +} |
| 121 | + |
| 122 | +fn main() { |
| 123 | + test1_all_need_migration(); |
| 124 | + test2_only_precise_paths_need_migration(); |
| 125 | + test3_only_by_value_need_migration(); |
| 126 | + test4_only_non_copy_types_need_migration(); |
| 127 | + test5_only_drop_types_need_migration(); |
| 128 | + test6_move_closures_non_copy_types_might_need_migration(); |
| 129 | + test7_drop_non_drop_aggregate_need_migration(); |
| 130 | +} |
0 commit comments