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

Interaction between serde and strum that results in a compiler error #73

Closed
agodnic opened this issue Oct 26, 2019 · 2 comments
Closed

Interaction between serde and strum that results in a compiler error #73

agodnic opened this issue Oct 26, 2019 · 2 comments

Comments

@agodnic
Copy link

@agodnic agodnic commented Oct 26, 2019

Hi!

I've noticed that using strum's derive EnumDiscriminants with some serde variant attributes, results in a compiler error.

I'm not sure of this is an issue in strum, in serde, or both.

Here's a program to reproduce the issue:

Cargo.toml

[dependencies]
serde = { version = "1.0", features = ["derive"] }
strum = "0.16.0"
strum_macros = "0.16.0"

src/main.rs

extern crate strum;
#[macro_use] extern crate strum_macros;
extern crate serde;

use std::str::FromStr;

use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, Debug, EnumDiscriminants)]
#[serde(tag = "type")]
enum Message {
    #[serde(rename = "request" )]
    Request { id: String, method: String, params: u8 },
    #[serde(rename = "response" )]
    Response { id: String, result: u8 },
}

fn main() {
    println!("Hello, world!");
}

Compiler error

error[E0658]: the attribute `serde` is currently unknown to the compiler and may have meaning added to it in the future
  --> src/main.rs:12:5
   |
12 |     #[serde(rename = "request" )]
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: for more information, see https://github.com/rust-lang/rust/issues/29642

error[E0658]: the attribute `serde` is currently unknown to the compiler and may have meaning added to it in the future
  --> src/main.rs:14:5
   |
14 |     #[serde(rename = "response" )]
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: for more information, see https://github.com/rust-lang/rust/issues/29642

warning: unused import: `std::str::FromStr`
 --> src/main.rs:5:5
  |
5 | use std::str::FromStr;
  |     ^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0658`.

Additional info

The program compiles successfully if the lines #[serde(rename = "response" )] are removed.

@Peternator7
Copy link
Owner

@Peternator7 Peternator7 commented Dec 20, 2019

Hi!

Thanks for reporting this issue, and sorry for the delay. The problem is that the newly created Discriminants enum doesn't implement Serialize so the compiler doesn't know what serde means in the context of the new type. EnumDiscriminants copies the attributes on each of the variants over to the new enum type, but it doesn't copy the derives over from the original type.

The current solution is to add a strum_discriminants attribute to the original enum and include
additional traits to derive. Here's and example that implements strum::EnumIter on the discriminant enum. Github Link

In this particular case, it would look something like:

#[derive(Serialize, Deserialize, Debug, EnumDiscriminants)]
#[serde(tag = "type")]
#[strum_discriminants(derive(Serialize, Deserialize))]
enum Message {
    #[serde(rename = "request" )]
    Request { id: String, method: String, params: u8 },
    #[serde(rename = "response" )]
    Response { id: String, result: u8 },
}
@azriel91
Copy link
Contributor

@azriel91 azriel91 commented Jan 20, 2020

Heya, I've opened #82 which is one way to resolve this. Hopefully the whitelist is sensible -- I've been using it for a number of projects and haven't run into any weird problems.

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

Successfully merging a pull request may close this issue.

3 participants