@@ -388,6 +388,115 @@ fn offset_of() {
388
388
assert ! ( offset_of!( ( u8 , u16 ) , 1 ) <= size_of:: <( u8 , u16 ) >( ) - 2 ) ;
389
389
}
390
390
391
+ #[ test]
392
+ #[ cfg( not( bootstrap) ) ]
393
+ fn offset_of_union ( ) {
394
+ #[ repr( C ) ]
395
+ union Foo {
396
+ x : u8 ,
397
+ y : u16 ,
398
+ z : Bar ,
399
+ }
400
+
401
+ #[ repr( C ) ]
402
+ #[ derive( Copy , Clone ) ]
403
+ struct Bar ( u8 , u8 ) ;
404
+
405
+ assert_eq ! ( offset_of!( Foo , x) , 0 ) ;
406
+ assert_eq ! ( offset_of!( Foo , y) , 0 ) ;
407
+ assert_eq ! ( offset_of!( Foo , z. 0 ) , 0 ) ;
408
+ assert_eq ! ( offset_of!( Foo , z. 1 ) , 1 ) ;
409
+ }
410
+
411
+ #[ test]
412
+ #[ cfg( not( bootstrap) ) ]
413
+ fn offset_of_dst ( ) {
414
+ #[ repr( C ) ]
415
+ struct Alpha {
416
+ x : u8 ,
417
+ y : u16 ,
418
+ z : [ u8 ] ,
419
+ }
420
+
421
+ trait Trait { }
422
+
423
+ #[ repr( C ) ]
424
+ struct Beta {
425
+ x : u8 ,
426
+ y : u16 ,
427
+ z : dyn Trait ,
428
+ }
429
+
430
+ extern "C" {
431
+ type Extern ;
432
+ }
433
+
434
+ #[ repr( C ) ]
435
+ struct Gamma {
436
+ x : u8 ,
437
+ y : u16 ,
438
+ z : Extern ,
439
+ }
440
+
441
+ assert_eq ! ( offset_of!( Alpha , x) , 0 ) ;
442
+ assert_eq ! ( offset_of!( Alpha , y) , 2 ) ;
443
+
444
+ assert_eq ! ( offset_of!( Beta , x) , 0 ) ;
445
+ assert_eq ! ( offset_of!( Beta , y) , 2 ) ;
446
+
447
+ assert_eq ! ( offset_of!( Gamma , x) , 0 ) ;
448
+ assert_eq ! ( offset_of!( Gamma , y) , 2 ) ;
449
+ }
450
+
451
+ #[ test]
452
+ #[ cfg( not( bootstrap) ) ]
453
+ fn offset_of_packed ( ) {
454
+ #[ repr( C , packed) ]
455
+ struct Foo {
456
+ x : u8 ,
457
+ y : u16 ,
458
+ }
459
+
460
+ assert_eq ! ( offset_of!( Foo , x) , 0 ) ;
461
+ assert_eq ! ( offset_of!( Foo , y) , 1 ) ;
462
+ }
463
+
464
+ #[ test]
465
+ #[ cfg( not( bootstrap) ) ]
466
+ fn offset_of_projection ( ) {
467
+ #[ repr( C ) ]
468
+ struct Foo {
469
+ x : u8 ,
470
+ y : u16 ,
471
+ }
472
+
473
+ trait Projector {
474
+ type Type ;
475
+ }
476
+
477
+ impl Projector for ( ) {
478
+ type Type = Foo ;
479
+ }
480
+
481
+ assert_eq ! ( offset_of!( <( ) as Projector >:: Type , x) , 0 ) ;
482
+ assert_eq ! ( offset_of!( <( ) as Projector >:: Type , y) , 2 ) ;
483
+ }
484
+
485
+ #[ test]
486
+ #[ cfg( not( bootstrap) ) ]
487
+ fn offset_of_alias ( ) {
488
+ #[ repr( C ) ]
489
+ struct Foo {
490
+ x : u8 ,
491
+ y : u16 ,
492
+ }
493
+
494
+ type Bar = Foo ;
495
+
496
+ assert_eq ! ( offset_of!( Bar , x) , 0 ) ;
497
+ assert_eq ! ( offset_of!( Bar , y) , 2 ) ;
498
+ }
499
+
391
500
#[ test]
392
501
#[ cfg( not( bootstrap) ) ]
393
502
fn const_offset_of ( ) {
@@ -425,20 +534,6 @@ fn offset_of_without_const_promotion() {
425
534
inner :: < ( ) > ( ) ;
426
535
}
427
536
428
- #[ test]
429
- #[ cfg( not( bootstrap) ) ]
430
- fn offset_of_dst ( ) {
431
- #[ repr( C ) ]
432
- struct Foo {
433
- x : u8 ,
434
- y : u16 ,
435
- slice : [ u8 ] ,
436
- }
437
-
438
- assert_eq ! ( offset_of!( Foo , x) , 0 ) ;
439
- assert_eq ! ( offset_of!( Foo , y) , 2 ) ;
440
- }
441
-
442
537
#[ test]
443
538
#[ cfg( not( bootstrap) ) ]
444
539
fn offset_of_addr ( ) {
0 commit comments