Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler suggests writing &mut mut foo in E0277 with .as_slice() #105645

Closed
kpreid opened this issue Dec 13, 2022 · 1 comment · Fixed by #105655
Closed

Compiler suggests writing &mut mut foo in E0277 with .as_slice() #105645

kpreid opened this issue Dec 13, 2022 · 1 comment · Fixed by #105655
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@kpreid
Copy link
Contributor

kpreid commented Dec 13, 2022

Given the following code:

fn main() {
    let mut buf = [0u8; 50];
    let mut bref = buf.as_slice();
    foo(&mut bref);
}

fn foo(_: &mut impl std::io::Write) {}

The current output is:

error[E0277]: the trait bound `&[u8]: std::io::Write` is not satisfied
 --> src/main.rs:4:9
  |
4 |     foo(&mut bref);
  |     --- ^^^^^^^^^ the trait `std::io::Write` is not implemented for `&[u8]`
  |     |
  |     required by a bound introduced by this call
  |
  = help: the trait `std::io::Write` is implemented for `&mut [u8]`
note: required by a bound in `foo`
 --> src/main.rs:7:21
  |
7 | fn foo(_: &mut impl std::io::Write) {}
  |                     ^^^^^^^^^^^^^^ required by this bound in `foo`
help: consider changing this borrow's mutability
  |
4 |     foo(&mut mut bref);
  |         ~~~~

Notice that the suggestion is to write foo(&mut mut bref), which is both invalid syntax and not where the problem is located — as_slice() returns an & reference, so that call must be changed and no local syntax change can work.

Occurs on stable 1.65.0 and 1.68.0-nightly (2022-12-11 bdb07a8)

@kpreid kpreid added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 13, 2022
@RedDocMD
Copy link
Contributor

@rustbot claim

@estebank estebank added the D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. label Dec 13, 2022
JohnTitor pushed a commit to JohnTitor/rust that referenced this issue Jan 9, 2023
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 rust-lang#105645
JohnTitor pushed a commit to JohnTitor/rust that referenced this issue Jan 9, 2023
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 rust-lang#105645
fee1-dead added a commit to fee1-dead-contrib/rust that referenced this issue Jan 9, 2023
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 rust-lang#105645
@bors bors closed this as completed in 9c8bf51 Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants