Skip to content

Commit

Permalink
tweak spans for ref mut suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
Ezrashaw committed Apr 20, 2023
1 parent 7a90241 commit 57d603e
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 19 deletions.
16 changes: 9 additions & 7 deletions compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,9 +559,9 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
binding_mode: ty::BindingMode::BindByReference(_),
..
})) => {
let pattern_span = local_decl.source_info.span;
let pattern_span: Span = local_decl.source_info.span;
suggest_ref_mut(self.infcx.tcx, pattern_span)
.map(|replacement| (true, pattern_span, replacement))
.map(|span| (true, span, "mut ".to_owned()))
}

_ => unreachable!(),
Expand Down Expand Up @@ -1316,11 +1316,13 @@ fn get_mut_span_in_struct_field<'tcx>(
}

/// If possible, suggest replacing `ref` with `ref mut`.
fn suggest_ref_mut(tcx: TyCtxt<'_>, binding_span: Span) -> Option<String> {
let hi_src = tcx.sess.source_map().span_to_snippet(binding_span).ok()?;
if hi_src.starts_with("ref") && hi_src["ref".len()..].starts_with(rustc_lexer::is_whitespace) {
let replacement = format!("ref mut{}", &hi_src["ref".len()..]);
Some(replacement)
fn suggest_ref_mut(tcx: TyCtxt<'_>, span: Span) -> Option<Span> {
let pattern_str = tcx.sess.source_map().span_to_snippet(span).ok()?;
if pattern_str.starts_with("ref")
&& pattern_str["ref".len()..].starts_with(rustc_lexer::is_whitespace)
{
let span = span.with_lo(span.lo() + BytePos(4)).shrink_to_lo();
Some(span)
} else {
None
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/nll/issue-51244.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | *my_ref = 0;
help: consider changing this to be a mutable reference
|
LL | let ref mut my_ref @ _ = 0;
| ~~~~~~~~~~~~~~
| +++

error: aborting due to previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ LL | *_x0 = U;
help: consider changing this to be a mutable reference
|
LL | let (ref mut _x0, _x1, ref _x2, ..) = tup;
| ~~~~~~~~~~~
| +++

error[E0594]: cannot assign to `*_x2`, which is behind a `&` reference
--> $DIR/borrowck-move-ref-pattern.rs:27:5
Expand All @@ -123,7 +123,7 @@ LL | *_x2 = U;
help: consider changing this to be a mutable reference
|
LL | let (ref _x0, _x1, ref mut _x2, ..) = tup;
| ~~~~~~~~~~~
| +++

error[E0382]: use of moved value: `tup.1`
--> $DIR/borrowck-move-ref-pattern.rs:28:10
Expand Down
3 changes: 0 additions & 3 deletions tests/ui/suggestions/suggest-ref-mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,16 @@ impl X {
fn main() {
let ref foo = 16;
//~^ HELP
//~| SUGGESTION ref mut foo
*foo = 32;
//~^ ERROR
if let Some(ref bar) = Some(16) {
//~^ HELP
//~| SUGGESTION ref mut bar
*bar = 32;
//~^ ERROR
}
match 16 {
ref quo => { *quo = 32; },
//~^ ERROR
//~| HELP
//~| SUGGESTION ref mut quo
}
}
12 changes: 6 additions & 6 deletions tests/ui/suggestions/suggest-ref-mut.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,37 @@ LL | fn zap(&mut self) {
| ~~~~~~~~~

error[E0594]: cannot assign to `*foo`, which is behind a `&` reference
--> $DIR/suggest-ref-mut.rs:16:5
--> $DIR/suggest-ref-mut.rs:15:5
|
LL | *foo = 32;
| ^^^^^^^^^ `foo` is a `&` reference, so the data it refers to cannot be written
|
help: consider changing this to be a mutable reference
|
LL | let ref mut foo = 16;
| ~~~~~~~~~~~
| +++

error[E0594]: cannot assign to `*bar`, which is behind a `&` reference
--> $DIR/suggest-ref-mut.rs:21:9
--> $DIR/suggest-ref-mut.rs:19:9
|
LL | *bar = 32;
| ^^^^^^^^^ `bar` is a `&` reference, so the data it refers to cannot be written
|
help: consider changing this to be a mutable reference
|
LL | if let Some(ref mut bar) = Some(16) {
| ~~~~~~~~~~~
| +++

error[E0594]: cannot assign to `*quo`, which is behind a `&` reference
--> $DIR/suggest-ref-mut.rs:25:22
--> $DIR/suggest-ref-mut.rs:23:22
|
LL | ref quo => { *quo = 32; },
| ^^^^^^^^^ `quo` is a `&` reference, so the data it refers to cannot be written
|
help: consider changing this to be a mutable reference
|
LL | ref mut quo => { *quo = 32; },
| ~~~~~~~~~~~
| +++

error: aborting due to 4 previous errors

Expand Down

0 comments on commit 57d603e

Please sign in to comment.