rust-lang / rust-bindgen

Automatically generates Rust FFI bindings to C (and some C++) libraries.
https://rust-lang.github.io/rust-bindgen/
BSD 3-Clause "New" or "Revised" License
4.45k stars 694 forks source link

Generated code breaks doc tests #1478

Closed izderadicka closed 5 years ago

izderadicka commented 5 years ago

I'm generating binding for libavformat (ffmpeg libraries).
Code works however generated binding breaks doc tests. Using ffmpeg-4.1 code

Input C/C++ Header

#include "libavformat/avformat.h"

Bindgen Invocation

bindgen \
--whitelist-type AVFormatContext \
--whitelist-function av_log_set_level \
--whitelist-function av_register_all \
--whitelist-function avformat_version \
--whitelist-function avformat_alloc_context \
--whitelist-function avformat_open_input \
--whitelist-function avformat_find_stream_info \
--whitelist-function avformat_close_input \
--whitelist-function av_dump_format \
--whitelist-var AV_LOG_QUIET \
wrapper.h -- -I ffmpeg-4.1 \
> src/ffi.rs

Actual Results

this is result of cargo test (doc tests section)

   Doc-tests media_info

running 3 tests
test src/ffi.rs - ffi::AVCodecContext::hw_frames_ctx (line 4100) ... FAILED
test src/ffi.rs - ffi::AVCodecContext::hw_frames_ctx (line 4111) ... FAILED
test src/ffi.rs - ffi::AVChromaLocation (line 947) ... FAILED

failures:

---- src/ffi.rs - ffi::AVCodecContext::hw_frames_ctx (line 4100) stdout ----
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `the`
 --> src/ffi.rs:4101:4
  |
3 | If the default get_buffer2() is used with a hwaccel pixel
  |    ^^^ expected one of 8 possible tokens here

error[E0425]: cannot find value `If` in this scope
 --> src/ffi.rs:4101:1
  |
3 | If the default get_buffer2() is used with a hwaccel pixel
  | ^^ not found in this scope

thread 'src/ffi.rs - ffi::AVCodecContext::hw_frames_ctx (line 4100)' panicked at 'couldn't compile the test', librustdoc/test.rs:333:13
note: Run with `RUST_BACKTRACE=1` for a backtrace.

---- src/ffi.rs - ffi::AVCodecContext::hw_frames_ctx (line 4111) stdout ----
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `field`
 --> src/ffi.rs:4112:6
  |
3 | This field should be set before avcodec_open2() is called.
  |      ^^^^^ expected one of 8 possible tokens here

error[E0425]: cannot find value `This` in this scope
 --> src/ffi.rs:4112:1
  |
3 | This field should be set before avcodec_open2() is called.
  | ^^^^ not found in this scope

thread 'src/ffi.rs - ffi::AVCodecContext::hw_frames_ctx (line 4111)' panicked at 'couldn't compile the test', librustdoc/test.rs:333:13

---- src/ffi.rs - ffi::AVChromaLocation (line 947) stdout ----
error: invalid suffix `st` for numeric literal
 --> src/ffi.rs:948:1
  |
3 | 1st 2nd       1st 2nd horizontal luma sample positions
  | ^^^
  |
  = help: the suffix must be one of the integral types (`u32`, `isize`, etc)

error: expected one of `.`, `;`, `?`, `}`, or an operator, found `2nd`
 --> src/ffi.rs:948:5
  |
3 | 1st 2nd       1st 2nd horizontal luma sample positions
  |     ^^^ expected one of `.`, `;`, `?`, `}`, or an operator here

error[E0308]: mismatched types
 --> src/ffi.rs:948:1
  |
2 | fn main() {
  |           - expected `()` because of default return type
3 | 1st 2nd       1st 2nd horizontal luma sample positions
  | ^^^ expected (), found integral variable
  |
  = note: expected type `()`
             found type `{integer}`

thread 'src/ffi.rs - ffi::AVChromaLocation (line 947)' panicked at 'couldn't compile the test', librustdoc/test.rs:333:13

failures:
    src/ffi.rs - ffi::AVChromaLocation (line 947)
    src/ffi.rs - ffi::AVCodecContext::hw_frames_ctx (line 4100)
    src/ffi.rs - ffi::AVCodecContext::hw_frames_ctx (line 4111)

Expected Results

Of course expected that doc tests will pass. After added option --no-doc-comments problem is resolved, but would expect to include comments in a way that will not infer with doc tests.

bindgen version

bindgen 0.45.0

emilio commented 5 years ago

This is a dupe of https://github.com/rust-lang/rust-bindgen/issues/1313.