launchbadge / sqlx

🧰 The Rust SQL Toolkit. An async, pure Rust SQL crate featuring compile-time checked queries without a DSL. Supports PostgreSQL, MySQL, and SQLite.
Apache License 2.0
13.46k stars 1.28k forks source link

Lifetime does not live long enough for deriving enum types. #2673

Open Wopple opened 1 year ago

Wopple commented 1 year ago

Bug Description

I'm trying to derive postgres / sqlx types for:

  1. An enum which is represented as an integer in postgres
  2. A struct which uses that enum as one of its fields

This is creating a compiler error due to lifetime constraints which is weird to me since I'm not using references. Even when I implement copy semantics I get the same error.

Minimal Reproduction

#[derive(sqlx::Type)]
#[repr(i32)]
pub enum Inner {
    V1 = 1,
    V2 = 2,
}

#[derive(sqlx::Type)]
pub struct Outer {
    inner: Inner,
    other: i32,
}

Error 1:

error: lifetime may not live long enough
 --> rust/year_quarter/src/lib.rs:8:10
  |
8 | #[derive(sqlx::Type)]
  |          ^^^^^^^^^^
  |          |
  |          lifetime `'r` defined here
  |          requires that `'r` must outlive `'static`
  |
  = note: this error originates in the derive macro `sqlx::Type` (in Nightly builds, run with -Z macro-backtrace for more info)

Error 2:

error: implementation of `sqlx::Decode` is not general enough
 --> rust/year_quarter/src/lib.rs:8:10
  |
8 | #[derive(sqlx::Type)]
  |          ^^^^^^^^^^ implementation of `sqlx::Decode` is not general enough
  |
  = note: `Inner` must implement `sqlx::Decode<'0, Postgres>`, for any lifetime `'0`...
  = note: ...but it actually implements `sqlx::Decode<'1, Postgres>`, for some specific lifetime `'1`
  = note: this error originates in the derive macro `sqlx::Type` (in Nightly builds, run with -Z macro-backtrace for more info)

Info

Wopple commented 1 year ago

With both of those, it does not compile.

tinternet commented 9 months ago

Same happens with Option enum.

ahmed-said-jax commented 4 months ago

I suppose there is no movement on this? It's a blocking issue for me so just wondering

turulix commented 4 months ago

I upgraded rust from nightly-2023-11-09 to nightly-2024-07-02 and started facing the same issue with f32

#[derive(Debug, sqlx::Type, Serialize, Deserialize)]
#[sqlx(type_name = "margin_setting")]
pub struct MarginSetting {
    pub min_price: f32,
    pub max_price: f32,
    pub margin: f32,
}
error: implementation of `sqlx::Decode` is not general enough
 --> shared\src\database\custom_types\margin_settings.rs:4:17
  |
4 | #[derive(Debug, sqlx::Type, Serialize, Deserialize)]
  |                 ^^^^^^^^^^ implementation of `sqlx::Decode` is not general enough
  |
  = note: `f32` must implement `sqlx::Decode<'0, Postgres>`, for any lifetime `'0`...
  = note: ...but it actually implements `sqlx::Decode<'1, Postgres>`, for some specific lifetime `'1`
  = note: this error originates in the derive macro `sqlx::Type` (in Nightly builds, run with -Z macro-backtrace for more info)
error: lifetime may not live long enough
  --> shared\src\database\custom_types\margin_settings.rs:4:17
   |
4  | #[derive(Debug, sqlx::Type, Serialize, Deserialize)]
   |                 ^^^^^^^^^^
   |                 |
   |                 lifetime `'r` defined here
   |                 requires that `'r` must outlive `'static`
   |
note: due to current limitations in the borrow checker, this implies a `'static` lifetime
  --> C:\Users\Tim\.cargo\registry\src\index.crates.io-6f17d22bba15001f\sqlx-postgres-0.7.4\src\types\record.rs:97:12
   |
97 |         T: for<'a> Decode<'a, Postgres> + Type<Postgres>,
   |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: this error originates in the derive macro `sqlx::Type` (in Nightly builds, run with -Z macro-backtrace for more info)