Skip to content

Commit

Permalink
Update to bitflags 2 in the compiler
Browse files Browse the repository at this point in the history
This involves lots of breaking changes. There are two big changes that
force changes. The first is that the bitflag types now don't
automatically implement normal derive traits, so we need to derive them
manually.

Additionally, bitflags now have a hidden inner type by default, which
breaks our custom derives. The bitflags docs recommend using the impl
form in these cases, which I did.
  • Loading branch information
Noratrieb committed Dec 30, 2023
1 parent d59f06f commit 8047b6c
Show file tree
Hide file tree
Showing 30 changed files with 123 additions and 130 deletions.
32 changes: 16 additions & 16 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"

[[package]]
name = "bitflags"
version = "2.4.0"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635"
checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07"

[[package]]
name = "block-buffer"
Expand Down Expand Up @@ -3370,7 +3370,7 @@ dependencies = [
name = "rustc_abi"
version = "0.0.0"
dependencies = [
"bitflags 1.3.2",
"bitflags 2.4.1",
"rand",
"rand_xoshiro",
"rustc_data_structures",
Expand Down Expand Up @@ -3401,7 +3401,7 @@ dependencies = [
name = "rustc_ast"
version = "0.0.0"
dependencies = [
"bitflags 1.3.2",
"bitflags 2.4.1",
"memchr",
"rustc_data_structures",
"rustc_index",
Expand Down Expand Up @@ -3552,7 +3552,7 @@ dependencies = [
name = "rustc_codegen_llvm"
version = "0.0.0"
dependencies = [
"bitflags 1.3.2",
"bitflags 2.4.1",
"itertools",
"libc",
"measureme",
Expand Down Expand Up @@ -3587,7 +3587,7 @@ name = "rustc_codegen_ssa"
version = "0.0.0"
dependencies = [
"ar_archive_writer",
"bitflags 1.3.2",
"bitflags 2.4.1",
"cc",
"itertools",
"jobserver",
Expand Down Expand Up @@ -3654,7 +3654,7 @@ name = "rustc_data_structures"
version = "0.0.0"
dependencies = [
"arrayvec",
"bitflags 1.3.2",
"bitflags 2.4.1",
"elsa",
"ena",
"indexmap",
Expand Down Expand Up @@ -4121,7 +4121,7 @@ dependencies = [
name = "rustc_metadata"
version = "0.0.0"
dependencies = [
"bitflags 1.3.2",
"bitflags 2.4.1",
"libloading 0.7.4",
"odht",
"rustc_ast",
Expand Down Expand Up @@ -4151,7 +4151,7 @@ dependencies = [
name = "rustc_middle"
version = "0.0.0"
dependencies = [
"bitflags 1.3.2",
"bitflags 2.4.1",
"derive_more",
"either",
"field-offset",
Expand Down Expand Up @@ -4286,7 +4286,7 @@ dependencies = [
name = "rustc_parse"
version = "0.0.0"
dependencies = [
"bitflags 1.3.2",
"bitflags 2.4.1",
"rustc_ast",
"rustc_ast_pretty",
"rustc_data_structures",
Expand Down Expand Up @@ -4424,7 +4424,7 @@ dependencies = [
name = "rustc_resolve"
version = "0.0.0"
dependencies = [
"bitflags 1.3.2",
"bitflags 2.4.1",
"pulldown-cmark",
"rustc_arena",
"rustc_ast",
Expand Down Expand Up @@ -4463,7 +4463,7 @@ dependencies = [
name = "rustc_session"
version = "0.0.0"
dependencies = [
"bitflags 1.3.2",
"bitflags 2.4.1",
"getopts",
"libc",
"rustc_ast",
Expand Down Expand Up @@ -4521,7 +4521,7 @@ dependencies = [
name = "rustc_symbol_mangling"
version = "0.0.0"
dependencies = [
"bitflags 1.3.2",
"bitflags 2.4.1",
"punycode",
"rustc-demangle",
"rustc_data_structures",
Expand All @@ -4539,7 +4539,7 @@ dependencies = [
name = "rustc_target"
version = "0.0.0"
dependencies = [
"bitflags 1.3.2",
"bitflags 2.4.1",
"object",
"rustc_abi",
"rustc_data_structures",
Expand Down Expand Up @@ -4638,7 +4638,7 @@ dependencies = [
name = "rustc_type_ir"
version = "0.0.0"
dependencies = [
"bitflags 1.3.2",
"bitflags 2.4.1",
"derivative",
"rustc_data_structures",
"rustc_index",
Expand Down Expand Up @@ -4768,7 +4768,7 @@ version = "0.38.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "745ecfa778e66b2b63c88a61cb36e0eea109e803b0b86bf9879fbc77c70e86ed"
dependencies = [
"bitflags 2.4.0",
"bitflags 2.4.1",
"errno",
"libc",
"linux-raw-sys",
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_abi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"

[dependencies]
# tidy-alphabetical-start
bitflags = "1.2.1"
bitflags = "2.4.1"
rand = { version = "0.8.4", default-features = false, optional = true }
rand_xoshiro = { version = "0.6.0", optional = true }
rustc_data_structures = { path = "../rustc_data_structures", optional = true }
Expand Down
15 changes: 9 additions & 6 deletions compiler/rustc_abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ pub use layout::LayoutCalculator;
/// instead of implementing everything in `rustc_middle`.
pub trait HashStableContext {}

#[derive(Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_Generic))]
pub struct ReprFlags(u8);

bitflags! {
#[derive(Default)]
#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_Generic))]
pub struct ReprFlags: u8 {
impl ReprFlags: u8 {
const IS_C = 1 << 0;
const IS_SIMD = 1 << 1;
const IS_TRANSPARENT = 1 << 2;
Expand All @@ -42,11 +44,12 @@ bitflags! {
// the seed stored in `ReprOptions.layout_seed`
const RANDOMIZE_LAYOUT = 1 << 4;
// Any of these flags being set prevent field reordering optimisation.
const IS_UNOPTIMISABLE = ReprFlags::IS_C.bits
| ReprFlags::IS_SIMD.bits
| ReprFlags::IS_LINEAR.bits;
const IS_UNOPTIMISABLE = ReprFlags::IS_C.bits()
| ReprFlags::IS_SIMD.bits()
| ReprFlags::IS_LINEAR.bits();
}
}
rustc_data_structures::external_bitflags_debug! { ReprFlags }

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_Generic))]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"

[dependencies]
# tidy-alphabetical-start
bitflags = "1.2.1"
bitflags = "2.4.1"
memchr = "2.5.0"
rustc_data_structures = { path = "../rustc_data_structures" }
rustc_index = { path = "../rustc_index" }
Expand Down
11 changes: 9 additions & 2 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2171,9 +2171,10 @@ pub enum InlineAsmRegOrRegClass {
RegClass(Symbol),
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, Encodable, Decodable, HashStable_Generic)]
pub struct InlineAsmOptions(u16);
bitflags::bitflags! {
#[derive(Encodable, Decodable, HashStable_Generic)]
pub struct InlineAsmOptions: u16 {
impl InlineAsmOptions: u16 {
const PURE = 1 << 0;
const NOMEM = 1 << 1;
const READONLY = 1 << 2;
Expand All @@ -2186,6 +2187,12 @@ bitflags::bitflags! {
}
}

impl std::fmt::Debug for InlineAsmOptions {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
bitflags::parser::to_writer(self, f)
}
}

#[derive(Clone, PartialEq, Encodable, Decodable, Debug, Hash, HashStable_Generic)]
pub enum InlineAsmTemplatePiece {
String(String),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test = false

[dependencies]
# tidy-alphabetical-start
bitflags = "1.0"
bitflags = "2.4.1"
itertools = "0.11"
libc = "0.2"
measureme = "10.0.0"
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_llvm/src/llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ pub mod debuginfo {
// These values **must** match with LLVMRustDIFlags!!
bitflags! {
#[repr(transparent)]
#[derive(Default)]
#[derive(Clone, Copy, Default)]
pub struct DIFlags: u32 {
const FlagZero = 0;
const FlagPrivate = 1;
Expand Down Expand Up @@ -751,7 +751,7 @@ pub mod debuginfo {
// These values **must** match with LLVMRustDISPFlags!!
bitflags! {
#[repr(transparent)]
#[derive(Default)]
#[derive(Clone, Copy, Default)]
pub struct DISPFlags: u32 {
const SPFlagZero = 0;
const SPFlagVirtual = 1;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
[dependencies]
# tidy-alphabetical-start
ar_archive_writer = "0.1.5"
bitflags = "1.2.1"
bitflags = "2.4.1"
cc = "1.0.69"
itertools = "0.11"
jobserver = "0.1.27"
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_codegen_ssa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ pub enum ModuleKind {
}

bitflags::bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MemFlags: u8 {
const VOLATILE = 1 << 0;
const NONTEMPORAL = 1 << 1;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_data_structures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
[dependencies]
# tidy-alphabetical-start
arrayvec = { version = "0.7", default-features = false }
bitflags = "1.2.1"
bitflags = "2.4.1"
elsa = "=1.7.1"
ena = "0.14.2"
indexmap = { version = "2.0.0" }
Expand Down
11 changes: 11 additions & 0 deletions compiler/rustc_data_structures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,14 @@ pub fn make_display(f: impl Fn(&mut fmt::Formatter<'_>) -> fmt::Result) -> impl
// See comments in src/librustc_middle/lib.rs
#[doc(hidden)]
pub fn __noop_fix_for_27438() {}

#[macro_export]
macro_rules! external_bitflags_debug {
($Name:ident) => {
impl ::std::fmt::Debug for $Name {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
::bitflags::parser::to_writer(self, f)
}
}
};
}
15 changes: 8 additions & 7 deletions compiler/rustc_data_structures/src/profiling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ use parking_lot::RwLock;
use smallvec::SmallVec;

bitflags::bitflags! {
#[derive(Clone, Copy)]
struct EventFilter: u16 {
const GENERIC_ACTIVITIES = 1 << 0;
const QUERY_PROVIDERS = 1 << 1;
Expand All @@ -114,14 +115,14 @@ bitflags::bitflags! {
const INCR_RESULT_HASHING = 1 << 8;
const ARTIFACT_SIZES = 1 << 9;

const DEFAULT = Self::GENERIC_ACTIVITIES.bits |
Self::QUERY_PROVIDERS.bits |
Self::QUERY_BLOCKED.bits |
Self::INCR_CACHE_LOADS.bits |
Self::INCR_RESULT_HASHING.bits |
Self::ARTIFACT_SIZES.bits;
const DEFAULT = Self::GENERIC_ACTIVITIES.bits() |
Self::QUERY_PROVIDERS.bits() |
Self::QUERY_BLOCKED.bits() |
Self::INCR_CACHE_LOADS.bits() |
Self::INCR_RESULT_HASHING.bits() |
Self::ARTIFACT_SIZES.bits();

const ARGS = Self::QUERY_KEYS.bits | Self::FUNCTION_ARGS.bits;
const ARGS = Self::QUERY_KEYS.bits() | Self::FUNCTION_ARGS.bits();
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"

[dependencies]
# tidy-alphabetical-start
bitflags = "1.2.1"
bitflags = "2.4.1"
libloading = "0.7.1"
odht = { version = "0.3.1", features = ["nightly"] }
rustc_ast = { path = "../rustc_ast" }
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"

[dependencies]
# tidy-alphabetical-start
bitflags = "1.2.1"
bitflags = "2.4.1"
derive_more = "0.99.17"
either = "1.5.0"
field-offset = "0.3.5"
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_middle/src/middle/codegen_fn_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ pub struct CodegenFnAttrs {
pub alignment: Option<u32>,
}

#[derive(Clone, Copy, PartialEq, Eq, TyEncodable, TyDecodable, HashStable)]
pub struct CodegenFnAttrFlags(u32);
bitflags! {
#[derive(TyEncodable, TyDecodable, HashStable)]
pub struct CodegenFnAttrFlags: u32 {
impl CodegenFnAttrFlags: u32 {
/// `#[cold]`: a hint to LLVM that this function, when called, is never on
/// the hot path.
const COLD = 1 << 0;
Expand Down Expand Up @@ -104,6 +105,7 @@ bitflags! {
const NO_BUILTINS = 1 << 20;
}
}
rustc_data_structures::external_bitflags_debug! { CodegenFnAttrFlags }

impl CodegenFnAttrs {
pub const EMPTY: &'static Self = &Self::new();
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_middle/src/ty/adt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ use std::str;

use super::{Destructor, FieldDef, GenericPredicates, Ty, TyCtxt, VariantDef, VariantDiscr};

#[derive(Clone, Copy, PartialEq, Eq, Hash, HashStable, TyEncodable, TyDecodable)]
pub struct AdtFlags(u16);
bitflags! {
#[derive(HashStable, TyEncodable, TyDecodable)]
pub struct AdtFlags: u16 {
impl AdtFlags: u16 {
const NO_ADT_FLAGS = 0;
/// Indicates whether the ADT is an enum.
const IS_ENUM = 1 << 0;
Expand All @@ -51,6 +52,7 @@ bitflags! {
const IS_UNSAFE_CELL = 1 << 9;
}
}
rustc_data_structures::external_bitflags_debug! { AdtFlags }

/// The definition of a user-defined type, e.g., a `struct`, `enum`, or `union`.
///
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1771,9 +1771,10 @@ pub struct Destructor {
pub constness: hir::Constness,
}

#[derive(Clone, Copy, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
pub struct VariantFlags(u8);
bitflags! {
#[derive(HashStable, TyEncodable, TyDecodable)]
pub struct VariantFlags: u8 {
impl VariantFlags: u8 {
const NO_VARIANT_FLAGS = 0;
/// Indicates whether the field list of this variant is `#[non_exhaustive]`.
const IS_FIELD_LIST_NON_EXHAUSTIVE = 1 << 0;
Expand All @@ -1782,6 +1783,7 @@ bitflags! {
const IS_RECOVERED = 1 << 1;
}
}
rustc_data_structures::external_bitflags_debug! { VariantFlags }

/// Definition of a variant -- a struct's fields or an enum variant.
#[derive(Debug, HashStable, TyEncodable, TyDecodable)]
Expand Down
Loading

0 comments on commit 8047b6c

Please sign in to comment.