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

Show variant aliases in error message #2566

Merged
merged 2 commits into from
Oct 21, 2024
Merged

Conversation

Mingun
Copy link
Contributor

@Mingun Mingun commented Aug 10, 2023

Before that PR an error message of such enum did not include aliases of possible variants:

#[derive(Deserialize)]
enum Enum {
  #[serde(alias = "C")]
  A,
  B
}

Now the error message will mention C

@Mingun
Copy link
Contributor Author

Mingun commented Mar 3, 2024

@dtolnay, @oli-obk, can you give some feedback?

@oli-obk
Copy link
Member

oli-obk commented Mar 4, 2024

Not in the near future. The only PR I'm giving attention to right now is the serde_core one

@oli-obk oli-obk merged commit 3415619 into serde-rs:master Oct 21, 2024
15 checks passed
@Mingun Mingun deleted the variant-aliases branch October 21, 2024 20:18
@juntyr
Copy link

juntyr commented Oct 25, 2024

This change breaks serde-reflection, which inspects the slice of known variants to get their numeric indices, assuming that the mapping from names to indices is bijective.

https://github.com/zefchain/serde-reflection/blob/d1a1ff0494fa3396af0423950cd43b7141890045/serde-reflection/src/de.rs#L394-L443

@Mingun @oli-obk do you have ideas for how the previous functionality could be regained?

@dtolnay
Copy link
Member

dtolnay commented Oct 28, 2024

Serde-reflection was assuming that if an enum has variant names {"A", "B", "B1"} then it should expect to deserialize them all using the same number of distinct variant indices {0, 1, 2}. This isn't correct because two different variant names can share the same index in the case of aliases, and there might not exist any variant with index 2 or 1. I implemented a better approach in zefchain/serde-reflection#53 by using variant names to deserialize instead of indices, and then comparing the discriminant of each of the deserialized values to unify aliases.

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

Successfully merging this pull request may close these issues.

4 participants