1
- // FIXME(static_mut_refs): Do not allow `static_mut_refs` lint
2
- #![ allow( static_mut_refs) ]
3
-
4
1
use core:: alloc:: { Allocator , Layout } ;
5
2
use core:: num:: NonZero ;
6
3
use core:: ptr:: NonNull ;
@@ -20,6 +17,8 @@ use std::rc::Rc;
20
17
use std:: sync:: atomic:: { AtomicU32 , Ordering } ;
21
18
use std:: vec:: { Drain , IntoIter } ;
22
19
20
+ use crate :: testing:: macros:: struct_with_counted_drop;
21
+
23
22
struct DropCounter < ' a > {
24
23
count : & ' a mut u32 ,
25
24
}
@@ -548,32 +547,25 @@ fn test_cmp() {
548
547
549
548
#[ test]
550
549
fn 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 ) ;
560
551
561
552
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 ) ;
563
555
v. truncate ( 3 ) ;
564
- assert_eq ! ( unsafe { DROPS } , 2 ) ;
556
+ assert_eq ! ( DROPS . get ( ) , 2 ) ;
565
557
v. truncate ( 0 ) ;
566
- assert_eq ! ( unsafe { DROPS } , 5 ) ;
558
+ assert_eq ! ( DROPS . get ( ) , 5 ) ;
567
559
}
568
560
569
561
#[ test]
570
562
#[ should_panic]
571
563
fn test_vec_truncate_fail ( ) {
572
564
struct BadElem ( i32 ) ;
565
+
573
566
impl Drop for BadElem {
574
567
fn drop ( & mut self ) {
575
- let BadElem ( ref mut x) = * self ;
576
- if * x == 0xbadbeef {
568
+ if let BadElem ( 0xbadbeef ) = self {
577
569
panic ! ( "BadElem panic: 0xbadbeef" )
578
570
}
579
571
}
@@ -812,22 +804,7 @@ fn test_drain_end_overflow() {
812
804
#[ test]
813
805
#[ cfg_attr( not( panic = "unwind" ) , ignore = "test requires unwinding support" ) ]
814
806
fn 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`" ) ; } ) ;
831
808
832
809
let mut v = vec ! [
833
810
D ( 0 , false ) ,
@@ -844,7 +821,7 @@ fn test_drain_leak() {
844
821
} ) )
845
822
. ok ( ) ;
846
823
847
- assert_eq ! ( unsafe { DROPS } , 4 ) ;
824
+ assert_eq ! ( DROPS . get ( ) , 4 ) ;
848
825
assert_eq ! ( v, vec![ D ( 0 , false ) , D ( 1 , false ) , D ( 6 , false ) , ] ) ;
849
826
}
850
827
@@ -1057,27 +1034,13 @@ fn test_into_iter_clone() {
1057
1034
#[ test]
1058
1035
#[ cfg_attr( not( panic = "unwind" ) , ignore = "test requires unwinding support" ) ]
1059
1036
fn 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`" ) ; } ) ;
1075
1038
1076
1039
let v = vec ! [ D ( false ) , D ( true ) , D ( false ) ] ;
1077
1040
1078
1041
catch_unwind ( move || drop ( v. into_iter ( ) ) ) . ok ( ) ;
1079
1042
1080
- assert_eq ! ( unsafe { DROPS } , 3 ) ;
1043
+ assert_eq ! ( DROPS . get ( ) , 3 ) ;
1081
1044
}
1082
1045
1083
1046
#[ test]
@@ -1274,55 +1237,31 @@ fn test_from_iter_specialization_panic_during_iteration_drops() {
1274
1237
1275
1238
#[ test]
1276
1239
#[ 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) ]
1279
1240
fn 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 )
1307
1245
}
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
+ ) ;
1312
1251
1313
1252
let _ = std:: panic:: catch_unwind ( AssertUnwindSafe ( || {
1314
1253
let v = vec ! [ Old ( 0 ) , Old ( 1 ) , Old ( 2 ) , Old ( 3 ) , Old ( 4 ) ] ;
1315
1254
let _ = v. into_iter ( ) . map ( |x| New ( x. 0 ) ) . take ( 2 ) . collect :: < Vec < _ > > ( ) ;
1316
1255
} ) ) ;
1317
1256
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 ) ) ) ;
1323
1262
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 ) ) ) ;
1326
1265
}
1327
1266
1328
1267
// regression test for issue #85322. Peekable previously implemented InPlaceIterable,
0 commit comments