about summary refs log tree commit diff
diff options
context:
space:
mode:
authoryukang <moorekang@gmail.com>2024-07-13 12:43:35 +0800
committeryukang <moorekang@gmail.com>2024-07-16 01:56:27 +0800
commit7ff71e5fb4c025b261f6c7e7eb996b36382c178e (patch)
tree731e8a2cc77a5f298c0466875e97672c96a98068
parent03c2100dede5d8779a032cf1fc1c28201c37048a (diff)
downloadrust-7ff71e5fb4c025b261f6c7e7eb996b36382c178e.tar.gz
rust-7ff71e5fb4c025b261f6c7e7eb996b36382c178e.zip
Remove invalid help diagnostics for const pointer
-rw-r--r--compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs33
-rw-r--r--tests/ui/suggestions/dont_suggest_raw_pointer_syntax-issue-127562.rs7
-rw-r--r--tests/ui/suggestions/dont_suggest_raw_pointer_syntax-issue-127562.stderr9
3 files changed, 37 insertions, 12 deletions
diff --git a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
index 26b0d23b166..0b6c527b8c0 100644
--- a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
+++ b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
@@ -1146,6 +1146,12 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
                     }
                     // don't create labels for compiler-generated spans
                     Some(_) => None,
+                    // don't create labels for the span not from user's code
+                    None if opt_assignment_rhs_span
+                        .is_some_and(|span| self.infcx.tcx.sess.source_map().is_imported(span)) =>
+                    {
+                        None
+                    }
                     None => {
                         let (has_sugg, decl_span, sugg) = if name != kw::SelfLower {
                             suggest_ampmut(
@@ -1198,18 +1204,21 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
                     sugg.push(s);
                 }
 
-                err.multipart_suggestion_verbose(
-                    format!(
-                        "consider changing this to be a mutable {pointer_desc}{}",
-                        if is_trait_sig {
-                            " in the `impl` method and the `trait` definition"
-                        } else {
-                            ""
-                        }
-                    ),
-                    sugg,
-                    Applicability::MachineApplicable,
-                );
+                if sugg.iter().all(|(span, _)| !self.infcx.tcx.sess.source_map().is_imported(*span))
+                {
+                    err.multipart_suggestion_verbose(
+                        format!(
+                            "consider changing this to be a mutable {pointer_desc}{}",
+                            if is_trait_sig {
+                                " in the `impl` method and the `trait` definition"
+                            } else {
+                                ""
+                            }
+                        ),
+                        sugg,
+                        Applicability::MachineApplicable,
+                    );
+                }
             }
             Some((false, err_label_span, message, _)) => {
                 let def_id = self.body.source.def_id();
diff --git a/tests/ui/suggestions/dont_suggest_raw_pointer_syntax-issue-127562.rs b/tests/ui/suggestions/dont_suggest_raw_pointer_syntax-issue-127562.rs
new file mode 100644
index 00000000000..03b736e60b6
--- /dev/null
+++ b/tests/ui/suggestions/dont_suggest_raw_pointer_syntax-issue-127562.rs
@@ -0,0 +1,7 @@
+fn main() {
+    let val = 2;
+    let ptr = std::ptr::addr_of!(val);
+    unsafe {
+        *ptr = 3; //~ ERROR cannot assign to `*ptr`, which is behind a `*const` pointer
+    }
+}
diff --git a/tests/ui/suggestions/dont_suggest_raw_pointer_syntax-issue-127562.stderr b/tests/ui/suggestions/dont_suggest_raw_pointer_syntax-issue-127562.stderr
new file mode 100644
index 00000000000..5396db7940f
--- /dev/null
+++ b/tests/ui/suggestions/dont_suggest_raw_pointer_syntax-issue-127562.stderr
@@ -0,0 +1,9 @@
+error[E0594]: cannot assign to `*ptr`, which is behind a `*const` pointer
+  --> $DIR/dont_suggest_raw_pointer_syntax-issue-127562.rs:5:9
+   |
+LL |         *ptr = 3;
+   |         ^^^^^^^^ `ptr` is a `*const` pointer, so the data it refers to cannot be written
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0594`.