Skip to content

Commit baa0672

Browse files
committed
Use a common subdiagnostic format for generic borrows
This is setup for unifing the logic for suggestions to borrow arguments in generic positions. As of this commit, it's still special cases for `AsRef`/`Borrow`-like traits and `Fn`-like traits.
1 parent cc01e7e commit baa0672

File tree

5 files changed

+26
-21
lines changed

5 files changed

+26
-21
lines changed

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

+10-5
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
478478
return;
479479
}
480480

481-
let mut is_mut = false;
481+
let mut mutbl = ty::Mutability::Not;
482482
if let Some(param) = arg_param
483483
&& self
484484
.infcx
@@ -504,7 +504,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
504504
]
505505
.contains(&Some(predicate.def_id()))
506506
{
507-
is_mut = true;
507+
mutbl = ty::Mutability::Mut;
508508
}
509509
true
510510
} else {
@@ -514,13 +514,18 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
514514
{
515515
// The type of the argument corresponding to the expression that got moved
516516
// is a type parameter `T`, which is has a `T: AsRef` obligation.
517+
let place_desc = if let Some(desc) = self.describe_place(moved_place) {
518+
format!("`{desc}`")
519+
} else {
520+
"here".to_owned()
521+
};
517522
err.span_suggestion_verbose(
518523
expr.span.shrink_to_lo(),
519-
"borrow the value to avoid moving it",
520-
format!("&{}", if is_mut { "mut " } else { "" }),
524+
format!("consider {}borrowing {place_desc}", mutbl.mutably_str()),
525+
mutbl.ref_prefix_str(),
521526
Applicability::MaybeIncorrect,
522527
);
523-
can_suggest_clone = is_mut;
528+
can_suggest_clone = mutbl.is_mut();
524529
} else if let Some(local_def_id) = def_id.as_local()
525530
&& let node = self.infcx.tcx.hir_node_by_def_id(local_def_id)
526531
&& let Some(fn_decl) = node.fn_decl()

Diff for: tests/ui/moves/moved-value-on-as-ref-arg.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | foo(bar);
88
LL | let _baa = bar;
99
| ^^^ value used here after move
1010
|
11-
help: borrow the value to avoid moving it
11+
help: consider borrowing `bar`
1212
|
1313
LL | foo(&bar);
1414
| +
@@ -31,7 +31,7 @@ LL | struct Bar;
3131
...
3232
LL | qux(bar);
3333
| --- you could clone this value
34-
help: borrow the value to avoid moving it
34+
help: consider mutably borrowing `bar`
3535
|
3636
LL | qux(&mut bar);
3737
| ++++
@@ -46,7 +46,7 @@ LL | bat(bar);
4646
LL | let _baa = bar;
4747
| ^^^ value used here after move
4848
|
49-
help: borrow the value to avoid moving it
49+
help: consider borrowing `bar`
5050
|
5151
LL | bat(&bar);
5252
| +
@@ -69,7 +69,7 @@ LL | struct Bar;
6969
...
7070
LL | baz(bar);
7171
| --- you could clone this value
72-
help: borrow the value to avoid moving it
72+
help: consider mutably borrowing `bar`
7373
|
7474
LL | baz(&mut bar);
7575
| ++++

Diff for: tests/ui/moves/suggest-borrow-for-generic-arg.fixed

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ extern crate suggest_borrow_for_generic_arg_aux as aux;
88

99
pub fn main() {
1010
let bar = aux::Bar;
11-
aux::foo(&bar); //~ HELP borrow the value
11+
aux::foo(&bar); //~ HELP consider borrowing `bar`
1212
let _baa = bar; //~ ERROR use of moved value
1313
let mut bar = aux::Bar;
14-
aux::qux(&mut bar); //~ HELP borrow the value
14+
aux::qux(&mut bar); //~ HELP consider mutably borrowing `bar`
1515
let _baa = bar; //~ ERROR use of moved value
1616
let bar = aux::Bar;
17-
aux::bat(&bar); //~ HELP borrow the value
17+
aux::bat(&bar); //~ HELP consider borrowing `bar`
1818
let _baa = bar; //~ ERROR use of moved value
1919
let mut bar = aux::Bar;
20-
aux::baz(&mut bar); //~ HELP borrow the value
20+
aux::baz(&mut bar); //~ HELP consider mutably borrowing `bar`
2121
let _baa = bar; //~ ERROR use of moved value
2222
}

Diff for: tests/ui/moves/suggest-borrow-for-generic-arg.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ extern crate suggest_borrow_for_generic_arg_aux as aux;
88

99
pub fn main() {
1010
let bar = aux::Bar;
11-
aux::foo(bar); //~ HELP borrow the value
11+
aux::foo(bar); //~ HELP consider borrowing `bar`
1212
let _baa = bar; //~ ERROR use of moved value
1313
let mut bar = aux::Bar;
14-
aux::qux(bar); //~ HELP borrow the value
14+
aux::qux(bar); //~ HELP consider mutably borrowing `bar`
1515
let _baa = bar; //~ ERROR use of moved value
1616
let bar = aux::Bar;
17-
aux::bat(bar); //~ HELP borrow the value
17+
aux::bat(bar); //~ HELP consider borrowing `bar`
1818
let _baa = bar; //~ ERROR use of moved value
1919
let mut bar = aux::Bar;
20-
aux::baz(bar); //~ HELP borrow the value
20+
aux::baz(bar); //~ HELP consider mutably borrowing `bar`
2121
let _baa = bar; //~ ERROR use of moved value
2222
}

Diff for: tests/ui/moves/suggest-borrow-for-generic-arg.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | aux::foo(bar);
88
LL | let _baa = bar;
99
| ^^^ value used here after move
1010
|
11-
help: borrow the value to avoid moving it
11+
help: consider borrowing `bar`
1212
|
1313
LL | aux::foo(&bar);
1414
| +
@@ -23,7 +23,7 @@ LL | aux::qux(bar);
2323
LL | let _baa = bar;
2424
| ^^^ value used here after move
2525
|
26-
help: borrow the value to avoid moving it
26+
help: consider mutably borrowing `bar`
2727
|
2828
LL | aux::qux(&mut bar);
2929
| ++++
@@ -38,7 +38,7 @@ LL | aux::bat(bar);
3838
LL | let _baa = bar;
3939
| ^^^ value used here after move
4040
|
41-
help: borrow the value to avoid moving it
41+
help: consider borrowing `bar`
4242
|
4343
LL | aux::bat(&bar);
4444
| +
@@ -53,7 +53,7 @@ LL | aux::baz(bar);
5353
LL | let _baa = bar;
5454
| ^^^ value used here after move
5555
|
56-
help: borrow the value to avoid moving it
56+
help: consider mutably borrowing `bar`
5757
|
5858
LL | aux::baz(&mut bar);
5959
| ++++

0 commit comments

Comments
 (0)