From 7458a683ce90f28b779bcd43fc421f89c39fb049 Mon Sep 17 00:00:00 2001 From: Luke Friedrichs Date: Sun, 6 Sep 2026 20:20:45 +0200 Subject: [PATCH] fix(macros): only take an integer type from `repr` in the `Type` derive --- sqlx-macros-core/src/derives/attributes.rs | 24 ++++++++++++++++++++-- tests/sqlite/derives.rs | 23 +++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/sqlx-macros-core/src/derives/attributes.rs b/sqlx-macros-core/src/derives/attributes.rs index 6109863833..133b592597 100644 --- a/sqlx-macros-core/src/derives/attributes.rs +++ b/sqlx-macros-core/src/derives/attributes.rs @@ -127,8 +127,28 @@ pub fn parse_container_attributes(input: &[Attribute]) -> syn::Result = attr.parse_args_with(>::parse_terminated)?; - if let Some(path) = list.iter().find_map(|f| f.require_path_only().ok()) { - try_set!(repr, path.get_ident().unwrap().clone(), list); + // only an integer type is a usable repr; `C`, `transparent` etc. are not + if let Some(ident) = list + .iter() + .filter_map(|f| f.require_path_only().ok()?.get_ident()) + .find(|ident| { + matches!( + ident.to_string().as_str(), + "i8" | "i16" + | "i32" + | "i64" + | "i128" + | "isize" + | "u8" + | "u16" + | "u32" + | "u64" + | "u128" + | "usize" + ) + }) + { + try_set!(repr, ident.clone(), list); } } } diff --git a/tests/sqlite/derives.rs b/tests/sqlite/derives.rs index 3491ab8539..d628abf031 100644 --- a/tests/sqlite/derives.rs +++ b/tests/sqlite/derives.rs @@ -13,6 +13,19 @@ test_type!(origin_enum(Sqlite, "2" == Origin::Bar, )); +// A `repr` without an integer type does not make a weak enum +#[derive(Debug, PartialEq, sqlx::Type)] +#[repr(C)] +enum ReprC { + Foo, + Bar, +} + +test_type!(repr_c_enum(Sqlite, + "'Foo'" == ReprC::Foo, + "'Bar'" == ReprC::Bar, +)); + #[derive(PartialEq, Eq, Debug, sqlx::Type)] #[sqlx(transparent)] struct TransparentTuple(i64); @@ -32,3 +45,13 @@ test_type!(transparent_named(Sqlite, "0" == TransparentNamed { field: 0 }, "23523" == TransparentNamed { field: 23523 }, )); + +#[derive(PartialEq, Eq, Debug, sqlx::Type)] +#[sqlx(transparent)] +#[repr(transparent)] +struct TransparentRepr(i64); + +test_type!(transparent_repr(Sqlite, + "0" == TransparentRepr(0), + "23523" == TransparentRepr(23523) +));