Skip to content
/ rust Public
forked from rust-lang/rust

Commit 40ce4e0

Browse files
authored
Rollup merge of rust-lang#134397 - Enselic:raw-mut, r=compiler-errors
rustc_borrowck: Suggest changing `&raw const` to `&raw mut` if applicable Closes rust-lang#127562 For reference, here is the diff compared to the original error reported in that issue before rust-lang#134244 stopped suggesting the invalid syntax: ``` diff --git a/tests/ui/borrowck/no-invalid-mut-suggestion-for-raw-pointer-issue-127562.stderr b/tests/ui/borrowck/no-invalid-mut-suggestion-for-raw-pointer-issue-127562.stderr index 0da5d15cf7f..dbe834b6b78 100644 --- a/tests/ui/borrowck/no-invalid-mut-suggestion-for-raw-pointer-issue-127562.stderr +++ b/tests/ui/borrowck/no-invalid-mut-suggestion-for-raw-pointer-issue-127562.stderr ``@@`` -6,8 +6,8 ``@@`` LL | unsafe { *ptr = 3; } | help: consider changing this to be a mutable pointer | -LL | let ptr = &mut raw const val; - | +++ +LL | let ptr = &raw mut val; + | ~~~ error: aborting due to 1 previous error ```
2 parents 68c56f6 + 70a0dc1 commit 40ce4e0

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

Diff for: compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+20-10
Original file line numberDiff line numberDiff line change
@@ -1474,16 +1474,27 @@ fn suggest_ampmut<'tcx>(
14741474
// let x: &i32 = &'a 5;
14751475
// ^^ lifetime annotation not allowed
14761476
//
1477-
if let Some(assignment_rhs_span) = opt_assignment_rhs_span
1478-
&& let Ok(src) = tcx.sess.source_map().span_to_snippet(assignment_rhs_span)
1479-
&& let Some(stripped) = src.strip_prefix('&')
1477+
if let Some(rhs_span) = opt_assignment_rhs_span
1478+
&& let Ok(rhs_str) = tcx.sess.source_map().span_to_snippet(rhs_span)
1479+
&& let Some(rhs_str_no_amp) = rhs_str.strip_prefix('&')
14801480
{
1481-
let is_raw_ref = stripped.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
}
1486-
let is_mut = if let Some(rest) = stripped.trim_start().strip_prefix("mut") {
1495+
1496+
// Figure out if rhs already is `&mut`.
1497+
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`
14891500
Some(c) if c.is_whitespace() => true,
@@ -1500,9 +1511,8 @@ fn suggest_ampmut<'tcx>(
15001511
// if the reference is already mutable then there is nothing we can do
15011512
// here.
15021513
if !is_mut {
1503-
let span = assignment_rhs_span;
15041514
// shrink the span to just after the `&` in `&variable`
1505-
let span = span.with_lo(span.lo() + BytePos(1)).shrink_to_lo();
1515+
let span = rhs_span.with_lo(rhs_span.lo() + BytePos(1)).shrink_to_lo();
15061516

15071517
// FIXME(Ezrashaw): returning is bad because we still might want to
15081518
// update the annotated type, see #106857.

Diff for: 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)