about summary refs log tree commit diff
path: root/compiler/rustc_mir_transform
diff options
context:
space:
mode:
authorWim Looman <git@nemo157.com>2023-05-27 22:20:14 +0200
committerWim Looman <git@nemo157.com>2023-06-13 15:48:57 +0200
commit8f3e876e522f9ecc8f855485bb4857163c0f86e4 (patch)
tree35cabbf833c365a1aacd372192ed1db970904469 /compiler/rustc_mir_transform
parent62a712a8bbb29a5d2f4e82b74d1ee5aa0490a5a3 (diff)
downloadrust-8f3e876e522f9ecc8f855485bb4857163c0f86e4.tar.gz
rust-8f3e876e522f9ecc8f855485bb4857163c0f86e4.zip
Add note about unsafe functions body not being unsafe
Diffstat (limited to 'compiler/rustc_mir_transform')
-rw-r--r--compiler/rustc_mir_transform/messages.ftl1
-rw-r--r--compiler/rustc_mir_transform/src/check_unsafety.rs7
-rw-r--r--compiler/rustc_mir_transform/src/errors.rs10
3 files changed, 15 insertions, 3 deletions
diff --git a/compiler/rustc_mir_transform/messages.ftl b/compiler/rustc_mir_transform/messages.ftl
index 300e592a6b4..2598eb2ed09 100644
--- a/compiler/rustc_mir_transform/messages.ftl
+++ b/compiler/rustc_mir_transform/messages.ftl
@@ -56,6 +56,7 @@ mir_transform_union_access_label = access to union field
 mir_transform_union_access_note = the field may not be properly initialized: using uninitialized data will cause undefined behavior
 mir_transform_unsafe_op_in_unsafe_fn = {$details} is unsafe and requires unsafe block (error E0133)
     .suggestion = consider wrapping the function body in an unsafe block
+    .note = an unsafe function restricts its caller, but its body is safe by default
 
 mir_transform_unused_unsafe = unnecessary `unsafe` block
     .label = because it's nested under this `unsafe` block
diff --git a/compiler/rustc_mir_transform/src/check_unsafety.rs b/compiler/rustc_mir_transform/src/check_unsafety.rs
index fd8e02ebeab..41e31df77cb 100644
--- a/compiler/rustc_mir_transform/src/check_unsafety.rs
+++ b/compiler/rustc_mir_transform/src/check_unsafety.rs
@@ -571,11 +571,16 @@ pub fn check_unsafety(tcx: TyCtxt<'_>, def_id: LocalDefId) {
                     errors::UnsafeOpInUnsafeFn {
                         details,
                         suggest_unsafe_block: suggest_unsafe_block.then(|| {
+                            let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
+                            let fn_sig = tcx
+                                .hir()
+                                .fn_sig_by_hir_id(hir_id)
+                                .expect("this violation only occurs in fn");
                             let body = tcx.hir().body_owned_by(def_id);
                             let body_span = tcx.hir().body(body).value.span;
                             let start = tcx.sess.source_map().start_point(body_span).shrink_to_hi();
                             let end = tcx.sess.source_map().end_point(body_span).shrink_to_lo();
-                            (start, end)
+                            (start, end, fn_sig.span)
                         }),
                     },
                 );
diff --git a/compiler/rustc_mir_transform/src/errors.rs b/compiler/rustc_mir_transform/src/errors.rs
index 6570da51bcc..05a27f853b8 100644
--- a/compiler/rustc_mir_transform/src/errors.rs
+++ b/compiler/rustc_mir_transform/src/errors.rs
@@ -131,7 +131,12 @@ impl RequiresUnsafeDetail {
 
 pub(crate) struct UnsafeOpInUnsafeFn {
     pub details: RequiresUnsafeDetail,
-    pub suggest_unsafe_block: Option<(Span, Span)>,
+
+    /// These spans point to:
+    ///  1. the start of the function body
+    ///  2. the end of the function body
+    ///  3. the function signature
+    pub suggest_unsafe_block: Option<(Span, Span, Span)>,
 }
 
 impl<'a> DecorateLint<'a, ()> for UnsafeOpInUnsafeFn {
@@ -146,7 +151,8 @@ impl<'a> DecorateLint<'a, ()> for UnsafeOpInUnsafeFn {
         diag.span_label(self.details.span, self.details.label());
         diag.note(self.details.note());
 
-        if let Some((start, end)) = self.suggest_unsafe_block {
+        if let Some((start, end, fn_sig)) = self.suggest_unsafe_block {
+            diag.span_note(fn_sig, crate::fluent_generated::mir_transform_note);
             diag.tool_only_multipart_suggestion(
                 crate::fluent_generated::mir_transform_suggestion,
                 vec![(start, " unsafe {".into()), (end, "}".into())],