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

cfg_attr does not work in combination with diesel derive attribute #2983

Closed
Ten0 opened this issue Dec 16, 2021 · 5 comments
Closed

cfg_attr does not work in combination with diesel derive attribute #2983

Ten0 opened this issue Dec 16, 2021 · 5 comments

Comments

@Ten0
Copy link
Member

Ten0 commented Dec 16, 2021

Setup

Versions

  • Rust: Latest stable
  • Diesel: Latest master (8a53cb7)
  • Database: Postgres
  • Operating System Linux

Feature Flags

  • diesel: postgres

Problem Description

cfg_attr does not work in combination with diesel derive attribute

What are you trying to accomplish?

#[cfg_attr(
	feature = "diesel-by-alpha3-as-char3",
	derive(diesel::AsExpression, diesel::FromSqlRow)
)]
#[cfg_attr(
	feature = "diesel-by-alpha3-as-char3",
	diesel(sql_type = diesel::sql_types::Text)
)]
pub enum Alpha3 {
	/// Aruba
	ABW,
	/// Afghanistan
	AFG,
}

What is the expected output?

compiles

What is the actual output?

At least one `sql_type` is needed for deriving `AsExpression` on a structure.

If however I remove the cfg_attr on the diesel attribute, this compiles (but then obviously fails to compile when the feature is disabled).

Steps to reproduce

Add to the code above a FromSql/ToSql implementation

#[cfg(feature = "diesel-by-alpha3-as-char3")]
impl<DB> diesel::deserialize::FromSql<diesel::sql_types::Text, DB> for Alpha3
where
	DB: diesel::backend::Backend,
	String: diesel::deserialize::FromSql<diesel::sql_types::Text, DB>,
{
	fn from_sql(bytes: diesel::backend::RawValue<DB>) -> diesel::deserialize::Result<Self> {
		todo!()
	}
}

#[cfg(feature = "diesel-by-alpha3-as-char3")]
impl<DB> diesel::serialize::ToSql<diesel::sql_types::Text, DB> for Alpha3
where
	DB: diesel::backend::Backend,
	str: diesel::serialize::ToSql<diesel::sql_types::Text, DB>,
{
	fn to_sql<'b>(&'b self, out: &mut diesel::serialize::Output<'b, '_, DB>) -> diesel::serialize::Result {
		todo!()
	}
}

Notes

Might be related to #2626 @pksunkara ?

@Ten0 Ten0 changed the title cfg_attr does not work in combination with diesel derive attribute cfg_attr does not work in combination with diesel derive attribute Dec 16, 2021
@weiznich
Copy link
Member

A first step to debug this would be to dump the attributes passed to diesel_derives here.

@pksunkara
Copy link
Member

Looks to be more of a Rust macro expansion related issue. There was a ton of work recently done on this but I am not sure if that work included cfg_attr related expansion.

@pksunkara
Copy link
Member

Now that we bumped the MSRV to 1.56, this might be fixed. @Ten0 can you check?

@pksunkara
Copy link
Member

Confirmed that this has been fixed.

@Ten0
Copy link
Member Author

Ten0 commented Feb 23, 2023

Indeed! Sorry for missing your previous message.

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

No branches or pull requests

3 participants