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

enum support in 0.12 breaks --derive Default #87

Closed
doy-materialize opened this issue Oct 7, 2022 · 4 comments · Fixed by #89
Closed

enum support in 0.12 breaks --derive Default #87

doy-materialize opened this issue Oct 7, 2022 · 4 comments · Fixed by #89
Assignees
Labels
bug Something isn't working

Comments

@doy-materialize
Copy link

the enums generated by 0.12 (#65) don't label which variant is the default, so if you try using --derive Default, you will end up with a lot of errors like

error: no default declared
    --> foo/bar/baz.rs:1116:48
     |
1116 | #[derive(Serialize, Deserialize, Clone, Debug, Default)]
     |                                                ^^^^^^^
     |
     = help: make a unit variant default by placing `#[default]` above it
     = note: this error originates in the derive macro `Default` (in Nightly builds, run with -Z macro-backtrace for more info)
@clux
Copy link
Member

clux commented Oct 7, 2022

oh, there's not a great way to solve this; what are we supposed to do?

  • if the enum is nested within a struct that derives default, then the enum needs to impl Default (so we can't just skip it for enums)
  • we can't just pick a default (if it's not specified in the schema)

So either we have to tell the user to impl Default for EnumVariant or we have to bail! if there's no specified default in the schema. Not sure what is good to do here immediately.

@clux clux added enhancement New feature or request help wanted Extra attention is needed bug Something isn't working and removed enhancement New feature or request labels Oct 7, 2022
@clux
Copy link
Member

clux commented Oct 8, 2022

Actually, I think the sane thing for us to do is to:

  • never add #[derive(Default)] to an enum - done in Stop auto-deriving default for enum #89
  • if we see a default value in the schema; then add a special impl Default for ThatEnum - via handle default field #67
  • if we don't see that; leave it missing, requiring the user to impl Default for ThatEnum - (nothing we can do in this case)

@clux clux removed the help wanted Extra attention is needed label Oct 8, 2022
@clux clux self-assigned this Oct 8, 2022
@clux clux closed this as completed in #89 Oct 8, 2022
@clux
Copy link
Member

clux commented Oct 8, 2022

Master has a partial fix for this now. We will no longer add #[derive(Default)] to enums.

This will still end up not compiling in the larger context, but you can at least now sideload a impl Default for the enum to fix it. We can never truly fix this for all cases (if it's not in the schema), and for the cases we can cover, I'll defer to issue #67.

@mt-inside
Copy link

Thanks very much for this @clux , I was just about raise this and suggest that exact solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants