Check for ptr-to-int casts in const functions in THIR unsafeck
This commit is contained in:
parent
592fecbafb
commit
6b327aaa08
6 changed files with 105 additions and 14 deletions
|
@ -25,6 +25,7 @@ struct UnsafetyVisitor<'a, 'tcx> {
|
|||
/// The `#[target_feature]` attributes of the body. Used for checking
|
||||
/// calls to functions with `#[target_feature]` (RFC 2396).
|
||||
body_target_features: &'tcx Vec<Symbol>,
|
||||
is_const: bool,
|
||||
}
|
||||
|
||||
impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
|
||||
|
@ -187,6 +188,16 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
|
|||
(Bound::Unbounded, Bound::Unbounded) => {}
|
||||
_ => self.requires_unsafe(expr.span, InitializingTypeWith),
|
||||
},
|
||||
ExprKind::Cast { source } => {
|
||||
let source = &self.thir[source];
|
||||
if self.tcx.features().const_raw_ptr_to_usize_cast
|
||||
&& self.is_const
|
||||
&& (source.ty.is_unsafe_ptr() || source.ty.is_fn_ptr())
|
||||
&& expr.ty.is_integral()
|
||||
{
|
||||
self.requires_unsafe(expr.span, CastOfPointerToInt);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
|
@ -230,7 +241,6 @@ enum UnsafeOpKind {
|
|||
CallToUnsafeFunction,
|
||||
UseOfInlineAssembly,
|
||||
InitializingTypeWith,
|
||||
#[allow(dead_code)] // FIXME
|
||||
CastOfPointerToInt,
|
||||
#[allow(dead_code)] // FIXME
|
||||
UseOfMutableStatic,
|
||||
|
@ -331,6 +341,11 @@ pub fn check_unsafety<'tcx>(
|
|||
let body_target_features = &tcx.codegen_fn_attrs(def_id).target_features;
|
||||
let safety_context =
|
||||
if body_unsafety.is_unsafe() { SafetyContext::UnsafeFn } else { SafetyContext::Safe };
|
||||
let is_const = match tcx.hir().body_owner_kind(hir_id) {
|
||||
hir::BodyOwnerKind::Closure => false,
|
||||
hir::BodyOwnerKind::Fn => tcx.is_const_fn_raw(def_id.to_def_id()),
|
||||
hir::BodyOwnerKind::Const | hir::BodyOwnerKind::Static(_) => true,
|
||||
};
|
||||
let mut visitor = UnsafetyVisitor {
|
||||
tcx,
|
||||
thir,
|
||||
|
@ -338,6 +353,7 @@ pub fn check_unsafety<'tcx>(
|
|||
hir_context: hir_id,
|
||||
body_unsafety,
|
||||
body_target_features,
|
||||
is_const,
|
||||
};
|
||||
visitor.visit_expr(&thir[expr]);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue