Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decode with postgres: hr bound for fields #3195

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

lcnr
Copy link

@lcnr lcnr commented Apr 11, 2024

fixes #3185

// sqlx = { git = "https://github.com/launchbadge/sqlx", features = ["postgres"] }
use sqlx::Type;
#[derive(Type)]
struct Foo {
    a: u32,
}

expands to the following impl for Decode

impl<'r> Decode<'r, Postgres> for Foo
where
    i32: Decode<'r, Postgres>,
    i32: Type<Postgres>,
{
    fn decode(
        value: Postgres::PgValueRef<'r>,
    ) -> Result<Self, Box<dyn Error + 'static + Send + Sync>> {
        let mut decoder = PgRecordDecoder::new(value)?;
        let a = decoder.try_decode::<i32>()?;
        Ok(Foo { a })
    }
}

decoder.try_decode has the following bound:

T: for<'a> Decode<'a, Postgres> + Type<Postgres>,

The where-bound only provides Decode for a concrete lifetime 'r however. This previously compiled because the compiler eagerly rejected the where-bound and used the more general impl. This behavior has changed in rust-lang/rust#119820.


It would have already been broken before rust-lang/rust#119820 if the type of a were generic, e.g.

struct Foo<T> {
    a: T,
}

However, this currently results in an unrelated error as the expansion does not add the generic arguments to Foo, resulting in

error[E0107]: missing generics for struct `Foo`
 --> src/main.rs:3:8
  |
3 | struct Foo<T> {
  |        ^^^ expected 1 generic argument
  |
note: struct defined here, with 1 generic parameter: `T`
 --> src/main.rs:3:8
  |
3 | struct Foo<T> {
  |        ^^^ -
help: add missing generic argument
  |
3 | struct Foo<T><T> {
  |           +++

I first tried to change try_decode to not require a higher-ranked bound, requiring T: Decode<'r, Postgres> instead. However, we need the higher ranked bound as T::decode is called with a local buffer:

let buf = if element.is_empty() && !quoted {
// completely empty input means NULL
None
} else {
Some(element.as_bytes())
};
// NOTE: we do not call [`accepts`] or give a chance to from a user as
// TEXT sequences are not strongly typed
T::decode(PgValueRef {
// NOTE: We pass `0` as the type ID because we don't have a reasonable value
// we could use.
type_info: PgTypeInfo::with_oid(Oid(0)),
format: self.fmt,
value: buf,
row: None,
})

I therefore ended up changing the derive to require a higher ranked bound instead. I tested this locally but did not add a test for this as I didn't immediately understand your test suite 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Compile error with #[derive(Type)] starting from nightly-2024-04-05
1 participant