Skip to content

Commit

Permalink
[rust][third-party] Update bitflags to 2.4
Browse files Browse the repository at this point in the history
Summary:
## Motivation

Since the latest compiler update, we are getting `clippy::bad_bit_mask` errors at the callsites of `bitflags!` macros where one of the variant is zero.
[Upstream won't address it in the `1.x` branch](bitflags/bitflags#373) and recommends upgrading to the `2.x` branch.

We are very close to reaching **zero clippy lints** in [Mononoke and other servers](https://fburl.com/code/pd76yn5e), which would feel nice.

## Specific categories of changes (in case it helps with the code review)

The change from `1.x` to `2.x` introduces a number  of backward compatibility breakages which I had to workaround in our codebase.
See [the release notes for 2.0](https://github.com/bitflags/bitflags/releases/tag/2.0.0) for the explanation for the manual fixes I had to perform at each call site.
 ---
**Adding traits to derive:**
```
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
```
> Generated flags types now derive fewer traits. If you need to maintain backwards compatibility, you can derive the following yourself:
 ---
**Replacing read uses of `.bits` with `.bits()`**
> You can now use the .bits() method instead of the old .bits.
> The representation of generated flags types has changed from a struct with the single field bits to a newtype.
 ---
**Replacing raw setting of `.bits` with `.from_bits_retain()`**
Due to the point above, the representation of the type is not exposed anymore. From [the documentation](https://docs.rs/bitflags/latest/bitflags/example_generated/struct.Flags.html#method.from_bits_retain), `from_bits_retain` "convert from a bits value exactly", which matches the old behaviour
 ---
**Replacing the unsafe `from_bits_unchecked` method with `from_bits_retain`**
> The unsafe from_bits_unchecked method is now a safe from_bits_retain method.
 ---
**Extracting some structs outside of the `bitflags!` macro**
Apart from the derives that `bitflags` knows about, such as `serde`, `bitflags` now can't deal with custom derives in macros with the previous syntax. I followed the recommendation from [their documentation](https://docs.rs/bitflags/latest/bitflags/index.html#custom-derives) to declare the `struct` ahead of of the macro call and only declare the `impl` block inside the macro.
 ---
**Changes to test output**
This does not stand out in the release notes, but as of [this upstream PR](bitflags/bitflags#297), the `Debug` output of generated bitflags has changed. This means any tests that rely on this (and of course, there are a few) needed updating.
In particular, the `vespa` tests rely on that output in a non-obvious way. You might have to trust me (and CI) on these ones...

Test Plan: dall_e_sandcastle_mondrian_style

Reviewed By: dtolnay

Differential Revision: D49742979

fbshipit-source-id: c818c37af45f0964e8fdb7ec6173ad66bb982c00
  • Loading branch information
Pierre Chevalier authored and facebook-github-bot committed Jan 16, 2024
1 parent ced29b7 commit c99e566
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
3 changes: 2 additions & 1 deletion antlir/antlir2/antlir2_btrfs/src/lib.rs
Expand Up @@ -57,13 +57,14 @@ pub struct Subvolume {
}

bitflags! {
#[derive(Default)]
#[derive(Default, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
pub struct SnapshotFlags: u64 {
const READONLY = 1 << 1;
}
}

bitflags! {
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
struct SubvolFlags: u64 {
const READ_ONLY = 1 << 1;
}
Expand Down
Expand Up @@ -85,7 +85,7 @@ impl std::fmt::Debug for Extent {
}

bitflags::bitflags! {
#[derive(Default)]
#[derive(Default, Copy, Clone, Debug)]
struct ExtentFlags: u32 {
/// This is generally the last extent in the file. A mapping attempt
/// past this extent may return nothing. Some implementations set this
Expand Down

0 comments on commit c99e566

Please sign in to comment.