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

Emit an error if a variant of an untagged enum is annoted with #[serde(rename)] or #[serde(alias)] #2797

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

devnetsec
Copy link

Fixes #2787.

Comment on lines 152 to 155

if container_is_untagged && attrs.name().renamed() {
cx.error_spanned_by(&variant.ident, "renaming or adding a alias to a variant of an untagged enum does nothing");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, error should point to the each problematic attribute instead of a variant name. Also, compile tests should be added to test_suite/tests/ui

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed those two things. Are there any other attributes that need to have error messages like this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, much better, but it is better to point to the alias = "..." and rename = "..." parts only, so when you have multiple attributes on a single line, errors pointed to the corresponding parts of that line.

Don't known about other attributes. You may check other if they need improvements.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made it point towards the alias and rename parts to be even more clear. I think #[serde(rename_all)] may be another attribute that has this issue, but I couldn't get that error to work in 6d72564.

@devnetsec devnetsec requested a review from Mingun August 14, 2024 02:43
@oli-obk
Copy link
Member

oli-obk commented Sep 3, 2024

Can someone run crater-at-home on this to gauge the impact? I worry someone accidentally is using it in a crate that will impact many other crates.

Or switch it to emitting a warning instead (can we do that from proc macros?)

@devnetsec
Copy link
Author

Unfortunately, I don't think this can be switched to a warning. Compile errors are handled by serde::internals::ctxt, which holds Syn errors. Syn uses Rust's compile_error! macro to print them. It looks like we're waiting on the nightly-only Diagnostic API to stabilize. See this Stack Overflow thread. Also, the test suite only compares expected and actual output when compilation fails.

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.

#[serde(rename)] and #[serde(alias)] should be forbidden on variants of untagged enums
3 participants