Skip to content

Commit 9c8bf51

Browse files
committed
Remove invalid case for mutable borrow suggestion
If we have a call such as `foo(&mut buf)` and after reference collapsing the type is inferred as `&T` where-as the required type is `&mut T`, don't suggest `foo(&mut mut buf)`. This is wrong syntactically and the issue lies elsewhere, not in the borrow. Fixes #105645
1 parent 71ec145 commit 9c8bf51

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,17 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
13681368
.source_map()
13691369
.span_take_while(span, |c| c.is_whitespace() || *c == '&');
13701370
if points_at_arg && mutability.is_not() && refs_number > 0 {
1371+
// If we have a call like foo(&mut buf), then don't suggest foo(&mut mut buf)
1372+
if "mut"
1373+
== snippet
1374+
.chars()
1375+
.filter(|c| !c.is_whitespace())
1376+
.skip(refs_number)
1377+
.take(3)
1378+
.collect::<String>()
1379+
{
1380+
return;
1381+
}
13711382
err.span_suggestion_verbose(
13721383
sp,
13731384
"consider changing this borrow's mutability",
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
let mut buf = [0u8; 50];
3+
let mut bref = buf.as_slice();
4+
foo(&mut bref);
5+
//~^ ERROR 4:9: 4:18: the trait bound `&[u8]: std::io::Write` is not satisfied [E0277]
6+
}
7+
8+
fn foo(_: &mut impl std::io::Write) {}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0277]: the trait bound `&[u8]: std::io::Write` is not satisfied
2+
--> $DIR/issue-105645.rs:4:9
3+
|
4+
LL | foo(&mut bref);
5+
| --- ^^^^^^^^^ the trait `std::io::Write` is not implemented for `&[u8]`
6+
| |
7+
| required by a bound introduced by this call
8+
|
9+
= help: the trait `std::io::Write` is implemented for `&mut [u8]`
10+
note: required by a bound in `foo`
11+
--> $DIR/issue-105645.rs:8:21
12+
|
13+
LL | fn foo(_: &mut impl std::io::Write) {}
14+
| ^^^^^^^^^^^^^^ required by this bound in `foo`
15+
16+
error: aborting due to previous error
17+
18+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)