@@ -2142,3 +2142,57 @@ impl LintPass for UnstableFeatures {
2142
2142
}
2143
2143
}
2144
2144
}
2145
+
2146
+ /// Lints for attempts to impl Drop on types that have `#[repr(C)]`
2147
+ /// attribute (see issue #24585).
2148
+ #[ derive( Copy , Clone ) ]
2149
+ pub struct DropWithReprExtern ;
2150
+
2151
+ declare_lint ! {
2152
+ DROP_WITH_REPR_EXTERN ,
2153
+ Warn ,
2154
+ "use of #[repr(C)] on a type that implements Drop"
2155
+ }
2156
+
2157
+ impl LintPass for DropWithReprExtern {
2158
+ fn get_lints ( & self ) -> LintArray {
2159
+ lint_array ! ( DROP_WITH_REPR_EXTERN )
2160
+ }
2161
+ fn check_crate ( & mut self , ctx : & Context , _: & ast:: Crate ) {
2162
+ for dtor_did in ctx. tcx . destructors . borrow ( ) . iter ( ) {
2163
+ let ( drop_impl_did, dtor_self_type) =
2164
+ if dtor_did. krate == ast:: LOCAL_CRATE {
2165
+ let impl_did = ctx. tcx . map . get_parent_did ( dtor_did. node ) ;
2166
+ let ty = ty:: lookup_item_type ( ctx. tcx , impl_did) . ty ;
2167
+ ( impl_did, ty)
2168
+ } else {
2169
+ continue ;
2170
+ } ;
2171
+
2172
+ match dtor_self_type. sty {
2173
+ ty:: ty_enum( self_type_did, _) |
2174
+ ty:: ty_struct( self_type_did, _) |
2175
+ ty:: ty_closure( self_type_did, _) => {
2176
+ let hints = ty:: lookup_repr_hints ( ctx. tcx , self_type_did) ;
2177
+ if hints. iter ( ) . any ( |attr| * attr == attr:: ReprExtern ) &&
2178
+ ty:: ty_dtor ( ctx. tcx , self_type_did) . has_drop_flag ( ) {
2179
+ let drop_impl_span = ctx. tcx . map . def_id_span ( drop_impl_did,
2180
+ codemap:: DUMMY_SP ) ;
2181
+ let self_defn_span = ctx. tcx . map . def_id_span ( self_type_did,
2182
+ codemap:: DUMMY_SP ) ;
2183
+ ctx. span_lint ( DROP_WITH_REPR_EXTERN ,
2184
+ drop_impl_span,
2185
+ "implementing Drop adds hidden state to types, \
2186
+ possibly conflicting with `#[repr(C)]`") ;
2187
+ // FIXME #19668: could be span_lint_note instead of manual guard.
2188
+ if ctx. current_level ( DROP_WITH_REPR_EXTERN ) != Level :: Allow {
2189
+ ctx. sess ( ) . span_note ( self_defn_span,
2190
+ "the `#[repr(C)]` attribute is attached here" ) ;
2191
+ }
2192
+ }
2193
+ }
2194
+ _ => { }
2195
+ }
2196
+ }
2197
+ }
2198
+ }
0 commit comments