pgcentralfoundation / pgrx

Build Postgres Extensions with Rust!
Other
3.7k stars 249 forks source link

Not all constants generated from headers have correct types #1875

Open YohDeadfall opened 1 month ago

YohDeadfall commented 1 month ago

Today I found an issue while working with transactions and non-atomic connections:

Spi::check_status(unsafe { pg_sys::SPI_connect_ext(pg_sys::SPI_OPT_NONATOMIC) })?;
                           ----------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `i32`, found `u32`
                           |
                           arguments to this function are incorrect

Not sure that it's possible to be done automatically from the code only, but having a list of hints can fix it.

eeeebbbbrrrr commented 1 month ago

This is fairly common. All the #define constants are considered to be u32s.

Generally an “as” cast is good enough.

I think providing some kind of hint list would be a lot of upkeep

workingjubilee commented 1 month ago

Yeah, there's no way to fix this generally because the C code doesn't really care what the type is: it is going to impose numeric coercions on inputs until it gets what it wants. So it will be coerced in different ways in different places. So you have to do the same by hand in Rust, sometimes.

workingjubilee commented 1 month ago

for note, we could tell bindgen to shrink the constants to their smallest applicable type, but that would probably be really fucking annoying rather than helpful.