Skip to content

Commit 70a0dc1

Browse files
committed
rustc_borrowck: Suggest changing &raw const to &raw mut if applicable
1 parent 47b559d commit 70a0dc1

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -1478,11 +1478,22 @@ fn suggest_ampmut<'tcx>(
14781478
&& let Ok(rhs_str) = tcx.sess.source_map().span_to_snippet(rhs_span)
14791479
&& let Some(rhs_str_no_amp) = rhs_str.strip_prefix('&')
14801480
{
1481-
let is_raw_ref = rhs_str_no_amp.trim_start().starts_with("raw ");
1482-
// We don't support raw refs yet
1483-
if is_raw_ref {
1484-
return None;
1481+
// Suggest changing `&raw const` to `&raw mut` if applicable.
1482+
if rhs_str_no_amp.trim_start().strip_prefix("raw const").is_some() {
1483+
let const_idx = rhs_str.find("const").unwrap() as u32;
1484+
let const_span = rhs_span
1485+
.with_lo(rhs_span.lo() + BytePos(const_idx))
1486+
.with_hi(rhs_span.lo() + BytePos(const_idx + "const".len() as u32));
1487+
1488+
return Some(AmpMutSugg {
1489+
has_sugg: true,
1490+
span: const_span,
1491+
suggestion: "mut".to_owned(),
1492+
additional: None,
1493+
});
14851494
}
1495+
1496+
// Figure out if rhs already is `&mut`.
14861497
let is_mut = if let Some(rest) = rhs_str_no_amp.trim_start().strip_prefix("mut") {
14871498
match rest.chars().next() {
14881499
// e.g. `&mut x`

tests/ui/borrowck/no-invalid-mut-suggestion-for-raw-pointer-issue-127562.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ error[E0594]: cannot assign to `*ptr`, which is behind a `*const` pointer
33
|
44
LL | unsafe { *ptr = 3; }
55
| ^^^^^^^^ `ptr` is a `*const` pointer, so the data it refers to cannot be written
6+
|
7+
help: consider changing this to be a mutable pointer
8+
|
9+
LL | let ptr = &raw mut val;
10+
| ~~~
611

712
error: aborting due to 1 previous error
813

0 commit comments

Comments
 (0)