rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
93.93k stars 12.09k forks source link

Unexpected trait bound not satisfied related to `std::io::Write` #125235

Open sorairolake opened 2 weeks ago

sorairolake commented 2 weeks ago

When I tried to build the documentation for zip crate v1.3.0 (cargo doc), it failed with unexpected errors (see zopfli-rs/zopfli#42). There seems to be no problem up to Rust 1.74.0, but the error occurs with Rust 1.75.0 or later.

Code

I tried this code:

https://github.com/zopfli-rs/zopfli/blob/5cea5a62d791e16440e8556feb663cffd3e888cf/src/deflate.rs#L23-L156 https://github.com/zopfli-rs/zopfli/blob/5cea5a62d791e16440e8556feb663cffd3e888cf/src/gzip.rs#L3-L97 https://github.com/zopfli-rs/zopfli/blob/5cea5a62d791e16440e8556feb663cffd3e888cf/src/zlib.rs#L3-L90

I expected to see this happen: Successfully build the documentation.

Instead, this happened:

error[E0277]: the trait bound `deflate::DeflateEncoder<W>: std::io::Write` is not satisfied
  --> zopfli-0.8.0/src/deflate.rs:63:73
   |
63 |     pub fn new_buffered(options: Options, btype: BlockType, sink: W) -> std::io::BufWriter<Self> {
   |                                                                         ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::io::Write` is not implemented for `deflate::DeflateEncoder<W>`
   |
note: required by a bound in `std::io::BufWriter`
  --> library/std/src/io/buffered/bufwriter.rs:69:1

error[E0277]: the trait bound `gzip::GzipEncoder<W>: std::io::Write` is not satisfied
  --> zopfli-0.8.0/src/gzip.rs:48:17
   |
48 |     ) -> Result<std::io::BufWriter<Self>, Error> {
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::io::Write` is not implemented for `gzip::GzipEncoder<W>`
   |
note: required by a bound in `std::io::BufWriter`
  --> library/std/src/io/buffered/bufwriter.rs:69:1

error[E0277]: the trait bound `zlib::ZlibEncoder<W>: std::io::Write` is not satisfied
  --> zopfli-0.8.0/src/zlib.rs:43:17
   |
43 |     ) -> Result<std::io::BufWriter<Self>, Error> {
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::io::Write` is not implemented for `zlib::ZlibEncoder<W>`
   |
note: required by a bound in `std::io::BufWriter`
  --> library/std/src/io/buffered/bufwriter.rs:69:1

For more information about this error, try `rustc --explain E0277`.
error: could not document `zopfli`
warning: build failed, waiting for other jobs to finish...

The trait std::io::Write seems to be implemented for deflate::DeflateEncoder<W>, so I think it satisfies the trait bound.

Version it worked on

It most recently worked on: Rust 1.74.0

Version with regression

rustc --version --verbose:

rustc 1.78.0 (9b00956e5 2024-04-29)
binary: rustc
commit-hash: 9b00956e56009bab2aa15d7bff10916599e3d6d6
commit-date: 2024-04-29
host: x86_64-unknown-linux-gnu
release: 1.78.0
LLVM version: 18.1.2
lukas-code commented 2 weeks ago

The error looks correct to me. Your crate contains this cfg(doc):

#[cfg(all(not(doc), feature = "std"))]
use std::io::{Error, Write};

#[cfg(any(doc, not(feature = "std")))]
pub use io::{Error, ErrorKind, Write};

So when you run cargo doc, DeflateEncoder actually implements your own copy of the Write trait, rather than the one from the standard library, but std::io::BufWriter requires the one from the standard library.

Prior to 1.75.0, this code was incorrectly accepted by rustdoc due to a bug, which got fixed in https://github.com/rust-lang/rust/pull/117450.

@rustbot label T-rustdoc