1
1
use std:: iter;
2
2
use std:: ops:: ControlFlow ;
3
3
4
- use rustc_abi:: { BackendRepr , ExternAbi , TagEncoding , Variants , WrappingRange } ;
4
+ use rustc_abi:: { BackendRepr , ExternAbi , TagEncoding , VariantIdx , Variants , WrappingRange } ;
5
5
use rustc_data_structures:: fx:: FxHashSet ;
6
6
use rustc_errors:: DiagMessage ;
7
7
use rustc_hir:: { Expr , ExprKind , LangItem } ;
8
8
use rustc_middle:: bug;
9
9
use rustc_middle:: ty:: layout:: { LayoutOf , SizeSkeleton } ;
10
10
use rustc_middle:: ty:: {
11
- self , AdtKind , GenericArgsRef , Ty , TyCtxt , TypeSuperVisitable , TypeVisitable , TypeVisitableExt ,
11
+ self , Adt , AdtKind , GenericArgsRef , Ty , TyCtxt , TypeSuperVisitable , TypeVisitable ,
12
+ TypeVisitableExt ,
12
13
} ;
13
14
use rustc_session:: { declare_lint, declare_lint_pass, impl_lint_pass} ;
14
15
use rustc_span:: def_id:: LocalDefId ;
@@ -23,7 +24,7 @@ use crate::lints::{
23
24
AmbiguousWidePointerComparisonsAddrSuggestion , AtomicOrderingFence , AtomicOrderingLoad ,
24
25
AtomicOrderingStore , ImproperCTypes , InvalidAtomicOrderingDiag , InvalidNanComparisons ,
25
26
InvalidNanComparisonsSuggestion , UnpredictableFunctionPointerComparisons ,
26
- UnpredictableFunctionPointerComparisonsSuggestion , UnusedComparisons ,
27
+ UnpredictableFunctionPointerComparisonsSuggestion , UnusedComparisons , UsesPowerAlignment ,
27
28
VariantSizeDifferencesDiag ,
28
29
} ;
29
30
use crate :: { LateContext , LateLintPass , LintContext , fluent_generated as fluent} ;
@@ -727,7 +728,50 @@ declare_lint! {
727
728
"proper use of libc types in foreign item definitions"
728
729
}
729
730
730
- declare_lint_pass ! ( ImproperCTypesDefinitions => [ IMPROPER_CTYPES_DEFINITIONS ] ) ;
731
+ declare_lint ! {
732
+ /// The `uses_power_alignment` lint detects specific `repr(C)`
733
+ /// aggregates on AIX.
734
+ /// In its platform C ABI, AIX uses the "power" (as in PowerPC) alignment
735
+ /// rule (detailed in https://www.ibm.com/docs/en/xl-c-and-cpp-aix/16.1?topic=data-using-alignment-modes#alignment),
736
+ /// which can also be set for XLC by `#pragma align(power)` or
737
+ /// `-qalign=power`. Aggregates with a floating-point type as the
738
+ /// recursively first field (as in "at offset 0") modify the layout of
739
+ /// *subsequent* fields of the associated structs to use an alignment value
740
+ /// where the floating-point type is aligned on a 4-byte boundary.
741
+ ///
742
+ /// The power alignment rule for structs needed for C compatibility is
743
+ /// unimplementable within `repr(C)` in the compiler without building in
744
+ /// handling of references to packed fields and infectious nested layouts,
745
+ /// so a warning is produced in these situations.
746
+ ///
747
+ /// ### Example
748
+ ///
749
+ /// ```rust
750
+ /// #[repr(C)]
751
+ /// pub struct Floats {
752
+ /// a: f64,
753
+ /// b: u8,
754
+ /// c: f64,
755
+ /// }
756
+ /// ```
757
+ ///
758
+ /// {{produces}}
759
+ ///
760
+ /// ### Explanation
761
+ ///
762
+ /// The power alignment rule specifies that the above struct has the
763
+ /// following alignment:
764
+ /// - offset_of!(Floats, a) == 0
765
+ /// - offset_of!(Floats, b) == 8
766
+ /// - offset_of!(Floats, c) == 12
767
+ /// However, rust currently aligns `c` at offset_of!(Floats, c) == 16.
768
+ /// Thus, a warning should be produced for the above struct in this case.
769
+ USES_POWER_ALIGNMENT ,
770
+ Warn ,
771
+ "Structs do not follow the power alignment rule under repr(C)"
772
+ }
773
+
774
+ declare_lint_pass ! ( ImproperCTypesDefinitions => [ IMPROPER_CTYPES_DEFINITIONS , USES_POWER_ALIGNMENT ] ) ;
731
775
732
776
#[ derive( Clone , Copy ) ]
733
777
pub ( crate ) enum CItemKind {
@@ -1539,6 +1583,71 @@ impl ImproperCTypesDefinitions {
1539
1583
vis. check_type_for_ffi_and_report_errors ( span, fn_ptr_ty, true , false ) ;
1540
1584
}
1541
1585
}
1586
+
1587
+ fn check_arg_for_power_alignment < ' tcx > (
1588
+ & mut self ,
1589
+ cx : & LateContext < ' tcx > ,
1590
+ ty : Ty < ' tcx > ,
1591
+ ) -> bool {
1592
+ // Structs (under repr(C)) follow the power alignment rule if:
1593
+ // - the first field of the struct is a floating-point type that
1594
+ // is greater than 4-bytes, or
1595
+ // - the first field of the struct is an aggregate whose
1596
+ // recursively first field is a floating-point type greater than
1597
+ // 4 bytes.
1598
+ if cx. tcx . sess . target . os != "aix" {
1599
+ return false ;
1600
+ }
1601
+ if ty. is_floating_point ( ) && ty. primitive_size ( cx. tcx ) . bytes ( ) > 4 {
1602
+ return true ;
1603
+ } else if let Adt ( adt_def, _) = ty. kind ( )
1604
+ && adt_def. is_struct ( )
1605
+ {
1606
+ let struct_variant = adt_def. variant ( VariantIdx :: ZERO ) ;
1607
+ // Within a nested struct, all fields are examined to correctly
1608
+ // report if any fields after the nested struct within the
1609
+ // original struct are misaligned.
1610
+ for struct_field in & struct_variant. fields {
1611
+ let field_ty = cx. tcx . type_of ( struct_field. did ) . instantiate_identity ( ) ;
1612
+ if self . check_arg_for_power_alignment ( cx, field_ty) {
1613
+ return true ;
1614
+ }
1615
+ }
1616
+ }
1617
+ return false ;
1618
+ }
1619
+
1620
+ fn check_struct_for_power_alignment < ' tcx > (
1621
+ & mut self ,
1622
+ cx : & LateContext < ' tcx > ,
1623
+ item : & ' tcx hir:: Item < ' tcx > ,
1624
+ ) {
1625
+ let adt_def = cx. tcx . adt_def ( item. owner_id . to_def_id ( ) ) ;
1626
+ if adt_def. repr ( ) . c ( )
1627
+ && !adt_def. repr ( ) . packed ( )
1628
+ && cx. tcx . sess . target . os == "aix"
1629
+ && !adt_def. all_fields ( ) . next ( ) . is_none ( )
1630
+ {
1631
+ let struct_variant_data = item. expect_struct ( ) . 0 ;
1632
+ for ( index, ..) in struct_variant_data. fields ( ) . iter ( ) . enumerate ( ) {
1633
+ // Struct fields (after the first field) are checked for the
1634
+ // power alignment rule, as fields after the first are likely
1635
+ // to be the fields that are misaligned.
1636
+ if index != 0 {
1637
+ let first_field_def = struct_variant_data. fields ( ) [ index] ;
1638
+ let def_id = first_field_def. def_id ;
1639
+ let ty = cx. tcx . type_of ( def_id) . instantiate_identity ( ) ;
1640
+ if self . check_arg_for_power_alignment ( cx, ty) {
1641
+ cx. emit_span_lint (
1642
+ USES_POWER_ALIGNMENT ,
1643
+ first_field_def. span ,
1644
+ UsesPowerAlignment ,
1645
+ ) ;
1646
+ }
1647
+ }
1648
+ }
1649
+ }
1650
+ }
1542
1651
}
1543
1652
1544
1653
/// `ImproperCTypesDefinitions` checks items outside of foreign items (e.g. stuff that isn't in
@@ -1562,8 +1671,13 @@ impl<'tcx> LateLintPass<'tcx> for ImproperCTypesDefinitions {
1562
1671
}
1563
1672
// See `check_fn`..
1564
1673
hir:: ItemKind :: Fn { .. } => { }
1674
+ // Structs are checked based on if they follow the power alignment
1675
+ // rule (under repr(C)).
1676
+ hir:: ItemKind :: Struct ( ..) => {
1677
+ self . check_struct_for_power_alignment ( cx, item) ;
1678
+ }
1565
1679
// See `check_field_def`..
1566
- hir:: ItemKind :: Union ( ..) | hir:: ItemKind :: Struct ( .. ) | hir :: ItemKind :: Enum ( ..) => { }
1680
+ hir:: ItemKind :: Union ( ..) | hir:: ItemKind :: Enum ( ..) => { }
1567
1681
// Doesn't define something that can contain a external type to be checked.
1568
1682
hir:: ItemKind :: Impl ( ..)
1569
1683
| hir:: ItemKind :: TraitAlias ( ..)
0 commit comments