rust-lang / rust-lang/rust

Nonsensical error when using Read::take through BufRead

Open
#145,212 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-trait-system C-bug T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

The following code is an adaption of the example in std::io::Read::take's documentation.

use std::io::Read;

fn foo(mut a: impl Read) {
    a.by_ref().take(5);
}

Take a BufRead as an argument instead of a Read.

use std::io::BufRead;
use std::io::Read;

fn foo(mut a: impl BufRead) {
    a.by_ref().take(5);
}

These two snippets compile fine. But when we remove the Read import like this:

use std::io::BufRead;
// use std::io::Read;

fn foo(mut a: impl BufRead) {
    a.by_ref().take(5);
}

Then compilations fails:

error[E0507]: cannot move out of a mutable reference
    --> src/lib.rs:5:5
     |
5    |     a.by_ref().take(5);
     |     ^^^^^^^^^^ ------- value moved due to this method call
     |     |
     |     move occurs because value has type `impl BufRead`, which does not implement the `Copy` trait
     |
note: `std::io::Read::take` takes ownership of the receiver `self`, which moves value
    --> /home/e/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/io/mod.rs:1239:13
     |
1239 |     fn take(self, limit: u64) -> Take<Self>
     |             ^^^^  ^^^^

This does not make sense. We always call the same std::io::Read::by_ref and std::io::Read::take function. If you needed to import the supertrait (BufRead: Read) in order to use it (which is not the case), then the error should be about a missing function and not about the move.

It might be interesting to test whether this still happens when you copy and minimize Read and BufRead into two new traits. I did not do this.

Meta

rustc --version --verbose:

rustc 1.89.0 (29483883e 2025-08-04)
binary: rustc
commit-hash: 29483883eed69d5fb4db01964cdf2af4d86e9cb2
commit-date: 2025-08-04
host: x86_64-unknown-linux-gnu
release: 1.89.0
LLVM version: 20.1.7

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling the two minimal examples from the issue and compare the method-resolution diagnostics. Read the std::io::Read and BufRead definitions around library/std/src/io/mod.rs:1239, then trace the compiler path producing E0507. Done means the cause is documented and the reported diagnostic or method-resolution behavior is corrected with a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.