]> git.lizzy.rs Git - rust.git/commitdiff
Use the lowest of `unsafe_op_in_unsafe_fn` and `safe_borrow_packed` for packed borrow...
authorLeSeulArtichaut <leseulartichaut@gmail.com>
Mon, 18 May 2020 22:19:31 +0000 (00:19 +0200)
committerLeSeulArtichaut <leseulartichaut@gmail.com>
Wed, 27 May 2020 18:37:57 +0000 (20:37 +0200)
src/librustc_middle/mir/query.rs
src/librustc_mir/transform/check_unsafety.rs

index c63d7215867c2a82fef6a998c639ea3e4501fcbd..99bfb74c243b47c7742c4d30bd57764d92c0b126 100644 (file)
@@ -26,6 +26,10 @@ pub enum UnsafetyViolationKind {
     /// Has to be handled as a lint for backwards compatibility.
     /// Should stay gated under `#![feature(unsafe_block_in_unsafe_fn)]`.
     UnsafeFn,
+    /// Borrow of packed field in an `unsafe fn` but outside an `unsafe` block.
+    /// Has to be handled as a lint for backwards compatibility.
+    /// Should stay gated under `#![feature(unsafe_block_in_unsafe_fn)]`.
+    UnsafeFnBorrowPacked,
 }
 
 #[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable, HashStable)]
index 3cff214f441c0b9a69e4901ed9f428ee7bdd5642..177efa2fc0d6c6257c8766fbe46e3ececca72ad7 100644 (file)
@@ -370,7 +370,8 @@ fn register_violations(
                                 violation.kind = UnsafetyViolationKind::General;
                             }
                         }
-                        UnsafetyViolationKind::UnsafeFn => {
+                        UnsafetyViolationKind::UnsafeFn
+                        | UnsafetyViolationKind::UnsafeFnBorrowPacked => {
                             bug!("`UnsafetyViolationKind::UnsafeFn` in an `Safe` context")
                         }
                     }
@@ -385,8 +386,11 @@ fn register_violations(
                 for violation in violations {
                     let mut violation = *violation;
 
-                    // FIXME(LeSeulArtichaut): what to do with `UnsafetyViolationKind::BorrowPacked`?
-                    violation.kind = UnsafetyViolationKind::UnsafeFn;
+                    if violation.kind == UnsafetyViolationKind::BorrowPacked {
+                        violation.kind = UnsafetyViolationKind::UnsafeFnBorrowPacked;
+                    } else {
+                        violation.kind = UnsafetyViolationKind::UnsafeFn;
+                    }
                     if !self.violations.contains(&violation) {
                         self.violations.push(violation)
                     }
@@ -418,7 +422,8 @@ fn register_violations(
                                     self.violations.push(violation)
                                 }
                             }
-                            UnsafetyViolationKind::UnsafeFn => bug!(
+                            UnsafetyViolationKind::UnsafeFn
+                            | UnsafetyViolationKind::UnsafeFnBorrowPacked => bug!(
                                 "`UnsafetyViolationKind::UnsafeFn` in an `ExplicitUnsafe` context"
                             ),
                         }
@@ -719,13 +724,31 @@ pub fn check_unsafety(tcx: TyCtxt<'_>, def_id: DefId) {
                 |lint| {
                     lint.build(&format!(
                         "{} is unsafe and requires unsafe block (error E0133)",
-                        description
+                        description,
                     ))
                     .span_label(source_info.span, &*description.as_str())
                     .note(&details.as_str())
                     .emit();
                 },
             ),
+            UnsafetyViolationKind::UnsafeFnBorrowPacked => {
+                let lint = if tcx.lint_level_at_node(SAFE_PACKED_BORROWS, lint_root).0
+                    <= tcx.lint_level_at_node(UNSAFE_OP_IN_UNSAFE_FN, lint_root).0
+                {
+                    SAFE_PACKED_BORROWS
+                } else {
+                    UNSAFE_OP_IN_UNSAFE_FN
+                };
+                tcx.struct_span_lint_hir(&lint, lint_root, source_info.span, |lint| {
+                    lint.build(&format!(
+                        "{} is unsafe and requires unsafe block (error E0133)",
+                        description,
+                    ))
+                    .span_label(source_info.span, &*description.as_str())
+                    .note(&details.as_str())
+                    .emit();
+                })
+            }
         }
     }