Skip to content

Nonsensical error when using Read::take through BufRead #145212

@e00E

Description

@e00E

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemC-bugCategory: This is a bug.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