Skip to content

Commit c1f8a3f

Browse files
authored
Rollup merge of #105655 - RedDocMD:bug-105645, r=oli-obk
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
2 parents b7587f1 + afefbb6 commit c1f8a3f

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -1519,6 +1519,13 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
15191519
.source_map()
15201520
.span_take_while(span, |c| c.is_whitespace() || *c == '&');
15211521
if points_at_arg && mutability.is_not() && refs_number > 0 {
1522+
// If we have a call like foo(&mut buf), then don't suggest foo(&mut mut buf)
1523+
if snippet
1524+
.trim_start_matches(|c: char| c.is_whitespace() || c == '&')
1525+
.starts_with("mut")
1526+
{
1527+
return;
1528+
}
15221529
err.span_suggestion_verbose(
15231530
sp,
15241531
"consider changing this borrow's mutability",
+8
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) {}
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)