about summary refs log tree commit diff
path: root/compiler/rustc_lint/src/reference_casting.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-09-07 00:24:45 +0000
committerbors <bors@rust-lang.org>2023-09-07 00:24:45 +0000
commit4e5b31c2b0023dba53a1b2827f4b7ac42aaaa18f (patch)
tree628bbb11a517f98e33e45c1b19da882e077870ec /compiler/rustc_lint/src/reference_casting.rs
parentb0d45536acf8be99036c6a1261359e3cf89f9d63 (diff)
parent89800a27fcbcd15641e06fb870b51c320a78668f (diff)
downloadrust-4e5b31c2b0023dba53a1b2827f4b7ac42aaaa18f.tar.gz
rust-4e5b31c2b0023dba53a1b2827f4b7ac42aaaa18f.zip
Auto merge of #115166 - Urgau:invalid_ref_casting-invalid-unsafecell-usage, r=est31
Lint on invalid usage of `UnsafeCell::raw_get` in reference casting

This PR proposes to take into account `UnsafeCell::raw_get` method call for non-Freeze types for the `invalid_reference_casting` lint.

The goal of this is to catch those kind of invalid reference casting:
```rust
fn as_mut<T>(x: &T) -> &mut T {
    unsafe { &mut *std::cell::UnsafeCell::raw_get(x as *const _ as *const _) }
    //~^ ERROR casting `&T` to `&mut T` is undefined behavior
}
```

r? `@est31`
Diffstat (limited to 'compiler/rustc_lint/src/reference_casting.rs')
-rw-r--r--compiler/rustc_lint/src/reference_casting.rs44
1 files changed, 39 insertions, 5 deletions
diff --git a/compiler/rustc_lint/src/reference_casting.rs b/compiler/rustc_lint/src/reference_casting.rs
index 883f6242b56..d540f473ae8 100644
--- a/compiler/rustc_lint/src/reference_casting.rs
+++ b/compiler/rustc_lint/src/reference_casting.rs
@@ -128,7 +128,11 @@ fn is_operation_we_care_about<'tcx>(
 fn is_cast_from_const_to_mut<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>) -> bool {
     let e = e.peel_blocks();
 
-    fn from_casts<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>) -> Option<&'tcx Expr<'tcx>> {
+    fn from_casts<'tcx>(
+        cx: &LateContext<'tcx>,
+        e: &'tcx Expr<'tcx>,
+        need_check_freeze: &mut bool,
+    ) -> Option<&'tcx Expr<'tcx>> {
         // <expr> as *mut ...
         let mut e = if let ExprKind::Cast(e, t) = e.kind
             && let ty::RawPtr(TypeAndMut { mutbl: Mutability::Mut, .. }) = cx.typeck_results().node_type(t.hir_id).kind() {
@@ -138,6 +142,14 @@ fn is_cast_from_const_to_mut<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>)
             && let Some(def_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
             && cx.tcx.is_diagnostic_item(sym::ptr_cast_mut, def_id) {
             expr
+        // UnsafeCell::raw_get(<expr>)
+        } else if let ExprKind::Call(path, [arg]) = e.kind
+            && let ExprKind::Path(ref qpath) = path.kind
+            && let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
+            && cx.tcx.is_diagnostic_item(sym::unsafe_cell_raw_get, def_id)
+        {
+            *need_check_freeze = true;
+            arg
         } else {
             return None;
         };
@@ -160,11 +172,18 @@ fn is_cast_from_const_to_mut<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>)
             {
                 had_at_least_one_cast = true;
                 expr
-            // ptr::from_ref(<expr>)
+            // ptr::from_ref(<expr>) or UnsafeCell::raw_get(<expr>)
             } else if let ExprKind::Call(path, [arg]) = e.kind
                 && let ExprKind::Path(ref qpath) = path.kind
                 && let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
-                && cx.tcx.is_diagnostic_item(sym::ptr_from_ref, def_id) {
+                && matches!(
+                    cx.tcx.get_diagnostic_name(def_id),
+                    Some(sym::ptr_from_ref | sym::unsafe_cell_raw_get)
+                )
+            {
+                if cx.tcx.is_diagnostic_item(sym::unsafe_cell_raw_get, def_id) {
+                    *need_check_freeze = true;
+                }
                 return Some(arg);
             } else if had_at_least_one_cast {
                 return Some(e);
@@ -190,10 +209,25 @@ fn is_cast_from_const_to_mut<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>)
         }
     }
 
-    let Some(e) = from_casts(cx, e).or_else(|| from_transmute(cx, e)) else {
+    let mut need_check_freeze = false;
+    let Some(e) = from_casts(cx, e, &mut need_check_freeze).or_else(|| from_transmute(cx, e))
+    else {
         return false;
     };
 
     let e = e.peel_blocks();
-    matches!(cx.typeck_results().node_type(e.hir_id).kind(), ty::Ref(_, _, Mutability::Not))
+    let node_type = cx.typeck_results().node_type(e.hir_id);
+    if let ty::Ref(_, inner_ty, Mutability::Not) = node_type.kind() {
+        // If an UnsafeCell method is involved we need to additionaly check the
+        // inner type for the presence of the Freeze trait (ie does NOT contain
+        // an UnsafeCell), since in that case we would incorrectly lint on valid casts.
+        //
+        // We also consider non concrete skeleton types (ie generics)
+        // to be an issue since there is no way to make it safe for abitrary types.
+        !need_check_freeze
+            || inner_ty.is_freeze(cx.tcx, cx.param_env)
+            || !inner_ty.has_concrete_skeleton()
+    } else {
+        false
+    }
 }