diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 786280c24c119..cb5523533c0f5 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -4224,8 +4224,10 @@ impl fmt::Display for Safety { } #[derive(Copy, Clone, PartialEq, Eq, Debug, Encodable, Decodable, HashStable_Generic)] +#[derive(Default)] pub enum Constness { Const, + #[default] NotConst, } diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 2c256ee9b7029..fc9b790211498 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1519,10 +1519,18 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { record!(self.tables.type_of[def_id] <- self.tcx.type_of(def_id)); } if should_encode_constness(def_kind) { - self.tables.constness.set_some(def_id.index, self.tcx.constness(def_id)); + let constness = self.tcx.constness(def_id); + match constness { + hir::Constness::Const => self.tables.constness.set(def_id.index, constness), + hir::Constness::NotConst => {} + } } if let DefKind::Fn | DefKind::AssocFn = def_kind { - self.tables.asyncness.set_some(def_id.index, tcx.asyncness(def_id)); + let asyncness = tcx.asyncness(def_id); + match asyncness { + ty::Asyncness::Yes => self.tables.asyncness.set(def_id.index, asyncness), + ty::Asyncness::No => {} + } record_array!(self.tables.fn_arg_idents[def_id] <- tcx.fn_arg_idents(def_id)); } if let Some(name) = tcx.intrinsic(def_id) { diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index c20c45ae5812b..672ebe3de483c 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -400,6 +400,8 @@ define_tables! { // individually instead of `DefId`s. module_children_reexports: Table>, cross_crate_inlinable: Table, + asyncness: Table, + constness: Table, - optional: attributes: Table>, @@ -433,7 +435,6 @@ define_tables! { promoted_mir: Table>>>, thir_abstract_const: Table>>>, impl_parent: Table, - constness: Table, const_conditions: Table>>, defaultness: Table, // FIXME(eddyb) perhaps compute this on the fly if cheap enough? @@ -441,7 +442,6 @@ define_tables! { mir_const_qualif: Table>, rendered_const: Table>, rendered_precise_capturing_args: Table>>, - asyncness: Table, fn_arg_idents: Table>>, coroutine_kind: Table, coroutine_for_closure: Table, diff --git a/compiler/rustc_metadata/src/rmeta/table.rs b/compiler/rustc_metadata/src/rmeta/table.rs index a882ee4f2b92e..7118d89434204 100644 --- a/compiler/rustc_metadata/src/rmeta/table.rs +++ b/compiler/rustc_metadata/src/rmeta/table.rs @@ -25,6 +25,24 @@ impl IsDefault for bool { } } +impl IsDefault for ty::Asyncness { + fn is_default(&self) -> bool { + match self { + ty::Asyncness::Yes => false, + ty::Asyncness::No => true, + } + } +} + +impl IsDefault for hir::Constness { + fn is_default(&self) -> bool { + match self { + rustc_hir::Constness::Const => false, + rustc_hir::Constness::NotConst => true, + } + } +} + impl IsDefault for u32 { fn is_default(&self) -> bool { *self == 0 @@ -293,6 +311,50 @@ impl FixedSizeEncoding for bool { } } +impl FixedSizeEncoding for ty::Asyncness { + type ByteArray = [u8; 1]; + + #[inline] + fn from_bytes(b: &[u8; 1]) -> Self { + match b[0] { + 0 => ty::Asyncness::No, + 1 => ty::Asyncness::Yes, + _ => unreachable!(), + } + } + + #[inline] + fn write_to_bytes(self, b: &mut [u8; 1]) { + debug_assert!(!self.is_default()); + b[0] = match self { + ty::Asyncness::No => 0, + ty::Asyncness::Yes => 1, + } + } +} + +impl FixedSizeEncoding for hir::Constness { + type ByteArray = [u8; 1]; + + #[inline] + fn from_bytes(b: &[u8; 1]) -> Self { + match b[0] { + 0 => hir::Constness::NotConst, + 1 => hir::Constness::Const, + _ => unreachable!(), + } + } + + #[inline] + fn write_to_bytes(self, b: &mut [u8; 1]) { + debug_assert!(!self.is_default()); + b[0] = match self { + hir::Constness::NotConst => 0, + hir::Constness::Const => 1, + } + } +} + // NOTE(eddyb) there could be an impl for `usize`, which would enable a more // generic `LazyValue` impl, but in the general case we might not need / want // to fit every `usize` in `u32`. diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 5eb8f1713a138..a8f7b22f32736 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -250,9 +250,10 @@ pub struct ImplTraitHeader<'tcx> { } #[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, HashStable, Debug)] -#[derive(TypeFoldable, TypeVisitable)] +#[derive(TypeFoldable, TypeVisitable, Default)] pub enum Asyncness { Yes, + #[default] No, }