Skip to content

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

Closed
@kpreid

Description

@kpreid

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)

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions