Skip to content

Commit

Permalink
Remove invalid help diagnostics for const pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Jul 13, 2024
1 parent 03c2100 commit b44a484
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
27 changes: 15 additions & 12 deletions compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1198,18 +1198,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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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
|
help: consider specifying this binding's type
|
LL | let ptr: *mut i32 = std::ptr::addr_of!(val);
| ++++++++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0594`.

0 comments on commit b44a484

Please sign in to comment.