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

#[serde(default=Enum::A) panics #1031

Closed
spease opened this issue Sep 2, 2017 · 1 comment
Closed

#[serde(default=Enum::A) panics #1031

spease opened this issue Sep 2, 2017 · 1 comment

Comments

@spease
Copy link

spease commented Sep 2, 2017

This is troublesome for long structs, as there is no indicator of where or why the panic is occurring. This is a misuse of the default parameter, but it would be nice if there was an error to guide you to the correct usage.

Ideally, it'd be nice if you could use an unquoted parameter for symbols and a quoted parameter for strings. Alas, it's probably too late for this.

Example:

#[macro_use]
extern crate serde_derive;
extern crate serde;

enum Enum {
    A,
    B,
}

#[derive(Deserialize)]
struct A {
  #[serde(default=E::A)]
  a: E,
}

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

Output:

   Compiling playground v0.0.1 (file:///playground)
error: proc-macro derive panicked
  --> src/main.rs:10:10
   |
10 | #[derive(Deserialize)]
   |          ^^^^^^^^^^^
   |
   = help: message: called `Result::unwrap()` on an `Err` value: "failed to parse derive input: \"struct A {\\n    #[serde\\n    ( default = E :: A )]\\n    a: E,\\n}\""

error: Could not compile `playground`.

To learn more, run the command again with --verbose.
@dtolnay
Copy link
Member

dtolnay commented Sep 3, 2017

There are two issues here: default=E::A fails to parse and default=E::A is not supported as a way of specifying the default value for an enum field. The first one will be fixed when we pick up the next release of syn, which supports arbitrary content in an attribute rust-lang/rust#34981. The second one I would prefer to track along with #368 -- I left a comment there. Thanks!

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

No branches or pull requests

2 participants