1- // FIXME(static_mut_refs): Do not allow `static_mut_refs` lint
2- #![ allow( static_mut_refs) ]
3-
41use core:: alloc:: { Allocator , Layout } ;
52use core:: num:: NonZero ;
63use core:: ptr:: NonNull ;
@@ -20,6 +17,8 @@ use std::rc::Rc;
2017use std:: sync:: atomic:: { AtomicU32 , Ordering } ;
2118use std:: vec:: { Drain , IntoIter } ;
2219
20+ use crate :: testing:: macros:: struct_with_counted_drop;
21+
2322struct DropCounter < ' a > {
2423 count : & ' a mut u32 ,
2524}
@@ -548,32 +547,25 @@ fn test_cmp() {
548547
549548#[ test]
550549fn test_vec_truncate_drop ( ) {
551- static mut DROPS : u32 = 0 ;
552- struct Elem ( #[ allow( dead_code) ] i32 ) ;
553- impl Drop for Elem {
554- fn drop ( & mut self ) {
555- unsafe {
556- DROPS += 1 ;
557- }
558- }
559- }
550+ struct_with_counted_drop ! ( Elem ( i32 ) , DROPS ) ;
560551
561552 let mut v = vec ! [ Elem ( 1 ) , Elem ( 2 ) , Elem ( 3 ) , Elem ( 4 ) , Elem ( 5 ) ] ;
562- assert_eq ! ( unsafe { DROPS } , 0 ) ;
553+
554+ assert_eq ! ( DROPS . get( ) , 0 ) ;
563555 v. truncate ( 3 ) ;
564- assert_eq ! ( unsafe { DROPS } , 2 ) ;
556+ assert_eq ! ( DROPS . get ( ) , 2 ) ;
565557 v. truncate ( 0 ) ;
566- assert_eq ! ( unsafe { DROPS } , 5 ) ;
558+ assert_eq ! ( DROPS . get ( ) , 5 ) ;
567559}
568560
569561#[ test]
570562#[ should_panic]
571563fn test_vec_truncate_fail ( ) {
572564 struct BadElem ( i32 ) ;
565+
573566 impl Drop for BadElem {
574567 fn drop ( & mut self ) {
575- let BadElem ( ref mut x) = * self ;
576- if * x == 0xbadbeef {
568+ if let BadElem ( 0xbadbeef ) = self {
577569 panic ! ( "BadElem panic: 0xbadbeef" )
578570 }
579571 }
@@ -812,22 +804,7 @@ fn test_drain_end_overflow() {
812804#[ test]
813805#[ cfg_attr( not( panic = "unwind" ) , ignore = "test requires unwinding support" ) ]
814806fn test_drain_leak ( ) {
815- static mut DROPS : i32 = 0 ;
816-
817- #[ derive( Debug , PartialEq ) ]
818- struct D ( u32 , bool ) ;
819-
820- impl Drop for D {
821- fn drop ( & mut self ) {
822- unsafe {
823- DROPS += 1 ;
824- }
825-
826- if self . 1 {
827- panic ! ( "panic in `drop`" ) ;
828- }
829- }
830- }
807+ struct_with_counted_drop ! ( D ( u32 , bool ) , DROPS => |this: & D | if this. 1 { panic!( "panic in `drop`" ) ; } ) ;
831808
832809 let mut v = vec ! [
833810 D ( 0 , false ) ,
@@ -844,7 +821,7 @@ fn test_drain_leak() {
844821 } ) )
845822 . ok ( ) ;
846823
847- assert_eq ! ( unsafe { DROPS } , 4 ) ;
824+ assert_eq ! ( DROPS . get ( ) , 4 ) ;
848825 assert_eq ! ( v, vec![ D ( 0 , false ) , D ( 1 , false ) , D ( 6 , false ) , ] ) ;
849826}
850827
@@ -1057,27 +1034,13 @@ fn test_into_iter_clone() {
10571034#[ test]
10581035#[ cfg_attr( not( panic = "unwind" ) , ignore = "test requires unwinding support" ) ]
10591036fn test_into_iter_leak ( ) {
1060- static mut DROPS : i32 = 0 ;
1061-
1062- struct D ( bool ) ;
1063-
1064- impl Drop for D {
1065- fn drop ( & mut self ) {
1066- unsafe {
1067- DROPS += 1 ;
1068- }
1069-
1070- if self . 0 {
1071- panic ! ( "panic in `drop`" ) ;
1072- }
1073- }
1074- }
1037+ struct_with_counted_drop ! ( D ( bool ) , DROPS => |this: & D | if this. 0 { panic!( "panic in `drop`" ) ; } ) ;
10751038
10761039 let v = vec ! [ D ( false ) , D ( true ) , D ( false ) ] ;
10771040
10781041 catch_unwind ( move || drop ( v. into_iter ( ) ) ) . ok ( ) ;
10791042
1080- assert_eq ! ( unsafe { DROPS } , 3 ) ;
1043+ assert_eq ! ( DROPS . get ( ) , 3 ) ;
10811044}
10821045
10831046#[ test]
@@ -1274,55 +1237,31 @@ fn test_from_iter_specialization_panic_during_iteration_drops() {
12741237
12751238#[ test]
12761239#[ cfg_attr( not( panic = "unwind" ) , ignore = "test requires unwinding support" ) ]
1277- // FIXME(static_mut_refs): Do not allow `static_mut_refs` lint
1278- #[ allow( static_mut_refs) ]
12791240fn test_from_iter_specialization_panic_during_drop_doesnt_leak ( ) {
1280- static mut DROP_COUNTER_OLD : [ usize ; 5 ] = [ 0 ; 5 ] ;
1281- static mut DROP_COUNTER_NEW : [ usize ; 2 ] = [ 0 ; 2 ] ;
1282-
1283- #[ derive( Debug ) ]
1284- struct Old ( usize ) ;
1285-
1286- impl Drop for Old {
1287- fn drop ( & mut self ) {
1288- unsafe {
1289- DROP_COUNTER_OLD [ self . 0 ] += 1 ;
1290- }
1291-
1292- if self . 0 == 3 {
1293- panic ! ( ) ;
1294- }
1295-
1296- println ! ( "Dropped Old: {}" , self . 0 ) ;
1297- }
1298- }
1299-
1300- #[ derive( Debug ) ]
1301- struct New ( usize ) ;
1302-
1303- impl Drop for New {
1304- fn drop ( & mut self ) {
1305- unsafe {
1306- DROP_COUNTER_NEW [ self . 0 ] += 1 ;
1241+ struct_with_counted_drop ! (
1242+ Old ( usize ) , DROP_COUNTER_OLD [ |this: & Old | this. 0 , usize ] =>
1243+ |this: & Old | {
1244+ if this. 0 == 3 { panic!( ) ; } println!( "Dropped Old: {}" , this. 0 )
13071245 }
1308-
1309- println ! ( "Dropped New: {}" , self . 0 ) ;
1310- }
1311- }
1246+ ) ;
1247+ struct_with_counted_drop ! (
1248+ New ( usize ) , DROP_COUNTER_NEW [ |this: & New | this. 0 , usize ] =>
1249+ |this: & New | println!( "Dropped New: {}" , this. 0 )
1250+ ) ;
13121251
13131252 let _ = std:: panic:: catch_unwind ( AssertUnwindSafe ( || {
13141253 let v = vec ! [ Old ( 0 ) , Old ( 1 ) , Old ( 2 ) , Old ( 3 ) , Old ( 4 ) ] ;
13151254 let _ = v. into_iter ( ) . map ( |x| New ( x. 0 ) ) . take ( 2 ) . collect :: < Vec < _ > > ( ) ;
13161255 } ) ) ;
13171256
1318- assert_eq ! ( unsafe { DROP_COUNTER_OLD [ 0 ] } , 1 ) ;
1319- assert_eq ! ( unsafe { DROP_COUNTER_OLD [ 1 ] } , 1 ) ;
1320- assert_eq ! ( unsafe { DROP_COUNTER_OLD [ 2 ] } , 1 ) ;
1321- assert_eq ! ( unsafe { DROP_COUNTER_OLD [ 3 ] } , 1 ) ;
1322- assert_eq ! ( unsafe { DROP_COUNTER_OLD [ 4 ] } , 1 ) ;
1257+ DROP_COUNTER_OLD . with_borrow ( |c| assert_eq ! ( c . get ( & 0 ) , Some ( & 1 ) ) ) ;
1258+ DROP_COUNTER_OLD . with_borrow ( |c| assert_eq ! ( c . get ( & 1 ) , Some ( & 1 ) ) ) ;
1259+ DROP_COUNTER_OLD . with_borrow ( |c| assert_eq ! ( c . get ( & 2 ) , Some ( & 1 ) ) ) ;
1260+ DROP_COUNTER_OLD . with_borrow ( |c| assert_eq ! ( c . get ( & 3 ) , Some ( & 1 ) ) ) ;
1261+ DROP_COUNTER_OLD . with_borrow ( |c| assert_eq ! ( c . get ( & 4 ) , Some ( & 1 ) ) ) ;
13231262
1324- assert_eq ! ( unsafe { DROP_COUNTER_NEW [ 0 ] } , 1 ) ;
1325- assert_eq ! ( unsafe { DROP_COUNTER_NEW [ 1 ] } , 1 ) ;
1263+ DROP_COUNTER_NEW . with_borrow ( |c| assert_eq ! ( c . get ( & 0 ) , Some ( & 1 ) ) ) ;
1264+ DROP_COUNTER_NEW . with_borrow ( |c| assert_eq ! ( c . get ( & 1 ) , Some ( & 1 ) ) ) ;
13261265}
13271266
13281267// regression test for issue #85322. Peekable previously implemented InPlaceIterable,
0 commit comments