Skip to content

Commit

Permalink
rustc_borrowck: Suggest changing &raw const to &raw mut if applic…
Browse files Browse the repository at this point in the history
…able
  • Loading branch information
Enselic committed Dec 16, 2024
1 parent 47b559d commit 70a0dc1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
19 changes: 15 additions & 4 deletions compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1478,11 +1478,22 @@ fn suggest_ampmut<'tcx>(
&& let Ok(rhs_str) = tcx.sess.source_map().span_to_snippet(rhs_span)
&& let Some(rhs_str_no_amp) = rhs_str.strip_prefix('&')
{
let is_raw_ref = rhs_str_no_amp.trim_start().starts_with("raw ");
// We don't support raw refs yet
if is_raw_ref {
return None;
// Suggest changing `&raw const` to `&raw mut` if applicable.
if rhs_str_no_amp.trim_start().strip_prefix("raw const").is_some() {
let const_idx = rhs_str.find("const").unwrap() as u32;
let const_span = rhs_span
.with_lo(rhs_span.lo() + BytePos(const_idx))
.with_hi(rhs_span.lo() + BytePos(const_idx + "const".len() as u32));

return Some(AmpMutSugg {
has_sugg: true,
span: const_span,
suggestion: "mut".to_owned(),
additional: None,
});
}

// Figure out if rhs already is `&mut`.
let is_mut = if let Some(rest) = rhs_str_no_amp.trim_start().strip_prefix("mut") {
match rest.chars().next() {
// e.g. `&mut x`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ error[E0594]: cannot assign to `*ptr`, which is behind a `*const` pointer
|
LL | unsafe { *ptr = 3; }
| ^^^^^^^^ `ptr` is a `*const` pointer, so the data it refers to cannot be written
|
help: consider changing this to be a mutable pointer
|
LL | let ptr = &raw mut val;
| ~~~

error: aborting due to 1 previous error

Expand Down

0 comments on commit 70a0dc1

Please sign in to comment.