From ed8cf98f2a0eb01c37cb709b6ebff9fd2ade515f Mon Sep 17 00:00:00 2001 From: mrl5 <31549762+mrl5@users.noreply.github.com> Date: Thu, 13 Jul 2023 22:25:01 +0200 Subject: [PATCH] fix(postgres): PgHasArrayType is not implemented for Vec caused by #2086 closes #2611 seems like #2086 fixed issue described by @Wopple by making that block of code unreachable. Not as it was stated in > Problem: PgHasArrayType was checking the application's postgres feature > Solution: only check the library's postgres feature after checking https://doc.rust-lang.org/std/macro.cfg.html > `cfg!`, unlike `#[cfg]`, does not remove any code and only evaluates > to true or false. For example, all blocks in an if/else expression > need to be valid when `cfg!` is used for the condition, regardless of > what `cfg!` is evaluating. so to my understanding it would be `true` anyway in @Woople scenario --- sqlx-macros-core/src/derives/type.rs | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/sqlx-macros-core/src/derives/type.rs b/sqlx-macros-core/src/derives/type.rs index 2dd1d3fdfc..065a5be490 100644 --- a/sqlx-macros-core/src/derives/type.rs +++ b/sqlx-macros-core/src/derives/type.rs @@ -77,7 +77,7 @@ fn expand_derive_has_sql_type_transparent( .push(parse_quote!(#ty: ::sqlx::postgres::PgHasArrayType)); let (array_impl_generics, _, array_where_clause) = array_generics.split_for_impl(); - let mut tokens = quote!( + return Ok(quote!( #[automatically_derived] impl #impl_generics ::sqlx::Type< DB > for #ident #ty_generics #where_clause { fn type_info() -> DB::TypeInfo { @@ -88,21 +88,15 @@ fn expand_derive_has_sql_type_transparent( <#ty as ::sqlx::Type>::compatible(ty) } } - ); - - if cfg!(feature = "postgres") { - tokens.extend(quote!( - #[automatically_derived] - impl #array_impl_generics ::sqlx::postgres::PgHasArrayType for #ident #ty_generics - #array_where_clause { - fn array_type_info() -> ::sqlx::postgres::PgTypeInfo { - <#ty as ::sqlx::postgres::PgHasArrayType>::array_type_info() - } + #[automatically_derived] + #[cfg(feature = "postgres")] + impl #array_impl_generics ::sqlx::postgres::PgHasArrayType for #ident #ty_generics + #array_where_clause { + fn array_type_info() -> ::sqlx::postgres::PgTypeInfo { + <#ty as ::sqlx::postgres::PgHasArrayType>::array_type_info() } - )); - } - - return Ok(tokens); + } + )); } let mut tts = TokenStream::new();