Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions sqlx-macros-core/src/derives/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,28 @@ pub fn parse_container_attributes(input: &[Attribute]) -> syn::Result<SqlxContai
let list: Punctuated<Meta, Token![,]> =
attr.parse_args_with(<Punctuated<Meta, Token![,]>>::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);
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions tests/sqlite/derives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ test_type!(origin_enum<Origin>(Sqlite,
"2" == Origin::Bar,
));

// A `repr` without an integer type does not make a weak enum
#[derive(Debug, PartialEq, sqlx::Type)]
#[repr(C)]

@abonander abonander Sep 9, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would prefer to forbid #[repr(C)] because it's ambiguous what the enum should map to.

This test assumes these enums should map to a string. However, some users may actually expect us to apply the C-like enum rules and map this to an integer. I don't think that's particularly unreasonable, actually. The real problem is that the actual integer type is defined per-target, so we'd have to encode all those rules and validate them. But then you'd potentially get a different type on various platforms when you're presumably trying to map that to the same schema type.

That's a lot of technical debt for a rather niche feature.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it didn't even compile before, forbidding #[repr(C)] is a backwards compatible change.

enum ReprC {
Foo,
Bar,
}

test_type!(repr_c_enum<ReprC>(Sqlite,
"'Foo'" == ReprC::Foo,
"'Bar'" == ReprC::Bar,
));

#[derive(PartialEq, Eq, Debug, sqlx::Type)]
#[sqlx(transparent)]
struct TransparentTuple(i64);
Expand All @@ -32,3 +45,13 @@ test_type!(transparent_named<TransparentNamed>(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<TransparentRepr>(Sqlite,
"0" == TransparentRepr(0),
"23523" == TransparentRepr(23523)
));
Loading