@@ -1111,51 +1111,52 @@ fn test_from_iter_specialization_panic_during_iteration_drops() {
11111111
11121112#[ test]
11131113fn 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 ] ;
11161116
11171117 #[ 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+ }
11221132 }
11231133
1124- impl Drop for Droppable {
1134+ #[ derive( Debug ) ]
1135+ struct New ( usize ) ;
1136+
1137+ impl Drop for New {
11251138 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 ;
11441141 }
1142+
1143+ println ! ( "Dropped New: {}" , self . 0 ) ;
11451144 }
11461145 }
11471146
11481147 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 < _ > > ( ) ;
11551150 } ) ) ;
11561151
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 ) ;
11591160}
11601161
11611162// regression test for issue #85322. Peekable previously implemented InPlaceIterable,
0 commit comments