@@ -1111,51 +1111,52 @@ fn test_from_iter_specialization_panic_during_iteration_drops() {
1111
1111
1112
1112
#[ test]
1113
1113
fn test_from_iter_specialization_panic_during_drop_doesnt_leak ( ) {
1114
- static mut DROP_COUNTER_SHOULD_BE_DROPPED : usize = 0 ;
1115
- static mut DROP_COUNTER_DROPPED_TWICE : usize = 0 ;
1114
+ static mut DROP_COUNTER_OLD : [ usize ; 5 ] = [ 0 ; 5 ] ;
1115
+ static mut DROP_COUNTER_NEW : [ usize ; 2 ] = [ 0 ; 2 ] ;
1116
1116
1117
1117
#[ derive( Debug ) ]
1118
- enum Droppable {
1119
- ShouldBeDropped ,
1120
- DroppedTwice ( Box < i32 > ) ,
1121
- PanicOnDrop ,
1118
+ struct Old ( usize ) ;
1119
+
1120
+ impl Drop for Old {
1121
+ fn drop ( & mut self ) {
1122
+ unsafe {
1123
+ DROP_COUNTER_OLD [ self . 0 ] += 1 ;
1124
+ }
1125
+
1126
+ if self . 0 == 3 {
1127
+ panic ! ( ) ;
1128
+ }
1129
+
1130
+ println ! ( "Dropped Old: {}" , self . 0 ) ;
1131
+ }
1122
1132
}
1123
1133
1124
- impl Drop for Droppable {
1134
+ #[ derive( Debug ) ]
1135
+ struct New ( usize ) ;
1136
+
1137
+ impl Drop for New {
1125
1138
fn drop ( & mut self ) {
1126
- match self {
1127
- Droppable :: ShouldBeDropped => {
1128
- unsafe {
1129
- DROP_COUNTER_SHOULD_BE_DROPPED += 1 ;
1130
- }
1131
- println ! ( "Dropping ShouldBeDropped!" )
1132
- }
1133
- Droppable :: DroppedTwice ( _) => {
1134
- unsafe {
1135
- DROP_COUNTER_DROPPED_TWICE += 1 ;
1136
- }
1137
- println ! ( "Dropping DroppedTwice!" )
1138
- }
1139
- Droppable :: PanicOnDrop => {
1140
- if !std:: thread:: panicking ( ) {
1141
- panic ! ( ) ;
1142
- }
1143
- }
1139
+ unsafe {
1140
+ DROP_COUNTER_NEW [ self . 0 ] += 1 ;
1144
1141
}
1142
+
1143
+ println ! ( "Dropped New: {}" , self . 0 ) ;
1145
1144
}
1146
1145
}
1147
1146
1148
1147
let _ = std:: panic:: catch_unwind ( AssertUnwindSafe ( || {
1149
- let v = vec ! [
1150
- Droppable :: ShouldBeDropped ,
1151
- Droppable :: DroppedTwice ( Box :: new( 123 ) ) ,
1152
- Droppable :: PanicOnDrop ,
1153
- ] ;
1154
- let _ = v. into_iter ( ) . take ( 1 ) . collect :: < Vec < _ > > ( ) ;
1148
+ let v = vec ! [ Old ( 0 ) , Old ( 1 ) , Old ( 2 ) , Old ( 3 ) , Old ( 4 ) ] ;
1149
+ let _ = v. into_iter ( ) . map ( |x| New ( x. 0 ) ) . take ( 2 ) . collect :: < Vec < _ > > ( ) ;
1155
1150
} ) ) ;
1156
1151
1157
- assert_eq ! ( unsafe { DROP_COUNTER_SHOULD_BE_DROPPED } , 1 ) ;
1158
- assert_eq ! ( unsafe { DROP_COUNTER_DROPPED_TWICE } , 1 ) ;
1152
+ assert_eq ! ( unsafe { DROP_COUNTER_OLD [ 0 ] } , 1 ) ;
1153
+ assert_eq ! ( unsafe { DROP_COUNTER_OLD [ 1 ] } , 1 ) ;
1154
+ assert_eq ! ( unsafe { DROP_COUNTER_OLD [ 2 ] } , 1 ) ;
1155
+ assert_eq ! ( unsafe { DROP_COUNTER_OLD [ 3 ] } , 1 ) ;
1156
+ assert_eq ! ( unsafe { DROP_COUNTER_OLD [ 4 ] } , 1 ) ;
1157
+
1158
+ assert_eq ! ( unsafe { DROP_COUNTER_NEW [ 0 ] } , 1 ) ;
1159
+ assert_eq ! ( unsafe { DROP_COUNTER_NEW [ 1 ] } , 1 ) ;
1159
1160
}
1160
1161
1161
1162
// regression test for issue #85322. Peekable previously implemented InPlaceIterable,
0 commit comments