File tree 2 files changed +55
-0
lines changed 2 files changed +55
-0
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ fn main() {
9
9
struct_ ( ) ;
10
10
replace_vptr ( ) ;
11
11
vtable_nop_cast ( ) ;
12
+ drop_principal ( ) ;
12
13
}
13
14
14
15
fn vtable_nop_cast ( ) {
@@ -430,3 +431,53 @@ fn replace_vptr() {
430
431
let s = S ( 42 ) ;
431
432
invoke_outer ( & s) ;
432
433
}
434
+
435
+ fn drop_principal ( ) {
436
+ use std:: { alloc:: Layout , any:: Any } ;
437
+
438
+ const fn yeet_principal ( x : Box < dyn Any + Send > ) -> Box < dyn Send > {
439
+ x
440
+ }
441
+
442
+ trait Bar : Send + Sync { }
443
+
444
+ impl < T : Send + Sync > Bar for T { }
445
+
446
+ const fn yeet_principal_2 ( x : Box < dyn Bar > ) -> Box < dyn Send > {
447
+ x
448
+ }
449
+
450
+ struct CallMe < F : FnOnce ( ) > ( Option < F > ) ;
451
+
452
+ impl < F : FnOnce ( ) > CallMe < F > {
453
+ fn new ( f : F ) -> Self {
454
+ CallMe ( Some ( f) )
455
+ }
456
+ }
457
+
458
+ impl < F : FnOnce ( ) > Drop for CallMe < F > {
459
+ fn drop ( & mut self ) {
460
+ ( self . 0 . take ( ) . unwrap ( ) ) ( ) ;
461
+ }
462
+ }
463
+
464
+ fn goodbye ( ) {
465
+ println ! ( "goodbye" ) ;
466
+ }
467
+
468
+ let x = Box :: new ( CallMe :: new ( goodbye) ) as Box < dyn Any + Send > ;
469
+ let x_layout = Layout :: for_value ( & * x) ;
470
+ let y = yeet_principal ( x) ;
471
+ let y_layout = Layout :: for_value ( & * y) ;
472
+ assert_eq ! ( x_layout, y_layout) ;
473
+ println ! ( "before" ) ;
474
+ drop ( y) ;
475
+
476
+ let x = Box :: new ( CallMe :: new ( goodbye) ) as Box < dyn Bar > ;
477
+ let x_layout = Layout :: for_value ( & * x) ;
478
+ let y = yeet_principal_2 ( x) ;
479
+ let y_layout = Layout :: for_value ( & * y) ;
480
+ assert_eq ! ( x_layout, y_layout) ;
481
+ println ! ( "before" ) ;
482
+ drop ( y) ;
483
+ }
Original file line number Diff line number Diff line change
1
+ before
2
+ goodbye
3
+ before
4
+ goodbye
You can’t perform that action at this time.
0 commit comments