@@ -25,6 +25,7 @@ struct UnsafetyVisitor<'a, 'tcx> {
25
25
/// The `#[target_feature]` attributes of the body. Used for checking
26
26
/// calls to functions with `#[target_feature]` (RFC 2396).
27
27
body_target_features : & ' tcx Vec < Symbol > ,
28
+ is_const : bool ,
28
29
}
29
30
30
31
impl < ' tcx > UnsafetyVisitor < ' _ , ' tcx > {
@@ -187,6 +188,16 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
187
188
( Bound :: Unbounded , Bound :: Unbounded ) => { }
188
189
_ => self . requires_unsafe ( expr. span , InitializingTypeWith ) ,
189
190
} ,
191
+ ExprKind :: Cast { source } => {
192
+ let source = & self . thir [ source] ;
193
+ if self . tcx . features ( ) . const_raw_ptr_to_usize_cast
194
+ && self . is_const
195
+ && ( source. ty . is_unsafe_ptr ( ) || source. ty . is_fn_ptr ( ) )
196
+ && expr. ty . is_integral ( )
197
+ {
198
+ self . requires_unsafe ( expr. span , CastOfPointerToInt ) ;
199
+ }
200
+ }
190
201
_ => { }
191
202
}
192
203
@@ -230,7 +241,6 @@ enum UnsafeOpKind {
230
241
CallToUnsafeFunction ,
231
242
UseOfInlineAssembly ,
232
243
InitializingTypeWith ,
233
- #[ allow( dead_code) ] // FIXME
234
244
CastOfPointerToInt ,
235
245
#[ allow( dead_code) ] // FIXME
236
246
UseOfMutableStatic ,
@@ -331,13 +341,19 @@ pub fn check_unsafety<'tcx>(
331
341
let body_target_features = & tcx. codegen_fn_attrs ( def_id) . target_features ;
332
342
let safety_context =
333
343
if body_unsafety. is_unsafe ( ) { SafetyContext :: UnsafeFn } else { SafetyContext :: Safe } ;
344
+ let is_const = match tcx. hir ( ) . body_owner_kind ( hir_id) {
345
+ hir:: BodyOwnerKind :: Closure => false ,
346
+ hir:: BodyOwnerKind :: Fn => tcx. is_const_fn_raw ( def_id. to_def_id ( ) ) ,
347
+ hir:: BodyOwnerKind :: Const | hir:: BodyOwnerKind :: Static ( _) => true ,
348
+ } ;
334
349
let mut visitor = UnsafetyVisitor {
335
350
tcx,
336
351
thir,
337
352
safety_context,
338
353
hir_context : hir_id,
339
354
body_unsafety,
340
355
body_target_features,
356
+ is_const,
341
357
} ;
342
358
visitor. visit_expr ( & thir[ expr] ) ;
343
359
}
0 commit comments