rustc_borrowck: Suggest changing &raw const to &raw mut if applic… · rust-lang/rust@70a0dc1 (original) (raw)

File tree

2 files changed

lines changed

2 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -1478,11 +1478,22 @@ fn suggest_ampmut<'tcx>(
1478 1478 && let Ok(rhs_str) = tcx.sess.source_map().span_to_snippet(rhs_span)
1479 1479 && let Some(rhs_str_no_amp) = rhs_str.strip_prefix('&')
1480 1480 {
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 +});
1485 1494 }
1495 +
1496 +// Figure out if rhs already is `&mut`.
1486 1497 let is_mut = if let Some(rest) = rhs_str_no_amp.trim_start().strip_prefix("mut") {
1487 1498 match rest.chars().next() {
1488 1499 // e.g. `&mut x`
Original file line number Diff line number Diff line change
@@ -3,6 +3,11 @@ error[E0594]: cannot assign to `*ptr`, which is behind a `*const` pointer
3 3 |
4 4 LL | unsafe { *ptr = 3; }
5 5 | ^^^^^^^^ `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 + | ~~~
6 11
7 12 error: aborting due to 1 previous error
8 13