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

"variants never constructed" lint regression in Rust 1.64.0 #318

Open
bunnie opened this issue Oct 9, 2022 · 3 comments
Open

"variants never constructed" lint regression in Rust 1.64.0 #318

bunnie opened this issue Oct 9, 2022 · 3 comments
Labels
bug Something isn't working
Milestone

Comments

@bunnie
Copy link

bunnie commented Oct 9, 2022

I'm using a fairly old version of rkyv (0.4.3) and wondering if this lint warning is also appearing for users in later versions of rkyv:


warning: variants `Sha512Result`, `Sha512Trunc256Result`, `SuspendError`, `Uninitialized` and `IdMismatch` are never constructed
 --> services\engine-sha512\src\api.rs:5:5
  |
3 | #[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
  |                 ------------- variants in this enum
4 | pub enum Sha2Result {
5 |     Sha512Result([u8; 64]),
  |     ^^^^^^^^^^^^
6 |     Sha512Trunc256Result([u8; 32]),
  |     ^^^^^^^^^^^^^^^^^^^^
7 |     SuspendError,
  |     ^^^^^^^^^^^^
8 |     Uninitialized,
  |     ^^^^^^^^^^^^^
9 |     IdMismatch,
  |     ^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

This just started appearing in Rust 1.64.0; in Rust 1.63.0 and older, such warnings were not emitted.

We try to keep our builds 100% free of warnings, so we can actually see warnings that matter when the builds happen. Unfortunately, we use a lot of rkyv so with this regression our build logs are overwhelmed with Rust complaining about Archive-derived enums not being used.

I tried to actually construct the derived enums with some code like this:

    pub fn use_rust_1_64_enums(&self, val: u32) -> ArchivedSha2Result {
        match val {
            0 => ArchivedSha2Result::Sha512Result([0; 64]),
            1 => ArchivedSha2Result::Sha512Trunc256Result([0; 32]),
            2 => ArchivedSha2Result::IdMismatch,
            3 => ArchivedSha2Result::SuspendError,
            _ => ArchivedSha2Result::Uninitialized,
        }
    }

But even if I call this code and pass the result on to something else, Rust is insisting that we never construct that enum type (tried creating both the ArchivedSha2Result and the Sha2ResultResolver and casting the results to something the compiler shouldn't disregard). I'm guessing maybe the derive creates some extra enum types that I'm not aware of or perhaps it does something exciting with the discriminants that cause the rust linter to think variants are not being used.

Furthermore, if I add #[allow(dead_code)] to the enum definition, it still throws the warning; adding #[allow(dead_code)] to every element has the same result, more warnings.

The only way I've been able to silence this is to use the "big hammer" of #![allow(dead_code)] to the entire file.

So, the current work-around I have is to move all rkyv-derive types to its own file, and then drop the file-wide silencer on top.

This is an acceptable work-around; but now I am left with lingering curiosity!

If this is also happening in the latest rkyv, maybe this is a compiler regression? If it's not happening, something else subtle going on that I'm not understanding in order to construct the derived variants so I can make the warning go away...?

@bunnie bunnie changed the title variants never constructed lint warning regression in Rust 1.64.0 "variants never constructed" lint regression in Rust 1.64.0 Oct 9, 2022
@bunnie
Copy link
Author

bunnie commented Oct 9, 2022

Ah, one other important detail to note -- the warnings only happen in rkyv enums that take values, that is enums that have variants that take some other type as an argument.

Simple enums that are just lists of keywords with no values work just fine.

bunnie added a commit to betrusted-io/xous-core that referenced this issue Oct 9, 2022
rkyv-derived enums throw warnings that they are unused.
Not sure how to get rid of them; I've tried surrounding them in
`#[allow(dead_code)]` directives, and also explicitly creating
each enum variant that I'm aware of through some dummy code and
some log prints of the dummy code calls to ensure the results are
used, and the warnings still persist.

Significantly, the warnings only happen for enums that take
a value. Simple rkyv enums do not throw a warning.

These warnings overwhelm the build logs, so they need to be isolated
in order for us to ensure that the build is clean of other more
important warnings.

The method chosen to isolate them is to take the enums that are
problematic, carve them into their own special file, and add
a file-wide `#![allow(dead_code)]` directive. This only happens
in 7 service crates, and this change commits all of the modifications
in a single big commit that could be undone later if this turns out
to be a compiler regression or something similar.

I also opened an issue with the rkyv maintainer to see what his
opinion is on the situation.

rkyv/rkyv#318
@djkoloski djkoloski added the bug Something isn't working label Oct 13, 2022
@djkoloski
Copy link
Collaborator

This might be related to some spanning problems that were fixed up later on in rkyv's life. I'll take a look and see if we can get those backported to a 0.4.* version.

@bunnie
Copy link
Author

bunnie commented Oct 14, 2022

thanks! For now the work-around I have of moving rkyv-types to their own file is working so far.

If it turns out to be a distraction to backport to 0.4.*, we plan to forward-port our bindings to your 0.8 release once that's ready!

@djkoloski djkoloski added this to the Backlog milestone Mar 10, 2024
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

No branches or pull requests

2 participants