Skip to content

Commit 357fd66

Browse files
committed
Avoid encoding non-constness or non-asyncness in metadata
1 parent 217cb73 commit 357fd66

File tree

5 files changed

+78
-5
lines changed

5 files changed

+78
-5
lines changed

compiler/rustc_hir/src/hir.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4224,8 +4224,10 @@ impl fmt::Display for Safety {
42244224
}
42254225

42264226
#[derive(Copy, Clone, PartialEq, Eq, Debug, Encodable, Decodable, HashStable_Generic)]
4227+
#[derive(Default)]
42274228
pub enum Constness {
42284229
Const,
4230+
#[default]
42294231
NotConst,
42304232
}
42314233

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,10 +1519,18 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
15191519
record!(self.tables.type_of[def_id] <- self.tcx.type_of(def_id));
15201520
}
15211521
if should_encode_constness(def_kind) {
1522-
self.tables.constness.set_some(def_id.index, self.tcx.constness(def_id));
1522+
let constness = self.tcx.constness(def_id);
1523+
match constness {
1524+
hir::Constness::Const => self.tables.constness.set(def_id.index, constness),
1525+
hir::Constness::NotConst => {}
1526+
}
15231527
}
15241528
if let DefKind::Fn | DefKind::AssocFn = def_kind {
1525-
self.tables.asyncness.set_some(def_id.index, tcx.asyncness(def_id));
1529+
let asyncness = tcx.asyncness(def_id);
1530+
match asyncness {
1531+
ty::Asyncness::Yes => self.tables.asyncness.set(def_id.index, asyncness),
1532+
ty::Asyncness::No => {}
1533+
}
15261534
record_array!(self.tables.fn_arg_idents[def_id] <- tcx.fn_arg_idents(def_id));
15271535
}
15281536
if let Some(name) = tcx.intrinsic(def_id) {

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,8 @@ define_tables! {
400400
// individually instead of `DefId`s.
401401
module_children_reexports: Table<DefIndex, LazyArray<ModChild>>,
402402
cross_crate_inlinable: Table<DefIndex, bool>,
403+
asyncness: Table<DefIndex, ty::Asyncness>,
404+
constness: Table<DefIndex, hir::Constness>,
403405

404406
- optional:
405407
attributes: Table<DefIndex, LazyArray<hir::Attribute>>,
@@ -433,15 +435,13 @@ define_tables! {
433435
promoted_mir: Table<DefIndex, LazyValue<IndexVec<mir::Promoted, mir::Body<'static>>>>,
434436
thir_abstract_const: Table<DefIndex, LazyValue<ty::EarlyBinder<'static, ty::Const<'static>>>>,
435437
impl_parent: Table<DefIndex, RawDefId>,
436-
constness: Table<DefIndex, hir::Constness>,
437438
const_conditions: Table<DefIndex, LazyValue<ty::ConstConditions<'static>>>,
438439
defaultness: Table<DefIndex, hir::Defaultness>,
439440
// FIXME(eddyb) perhaps compute this on the fly if cheap enough?
440441
coerce_unsized_info: Table<DefIndex, LazyValue<ty::adjustment::CoerceUnsizedInfo>>,
441442
mir_const_qualif: Table<DefIndex, LazyValue<mir::ConstQualifs>>,
442443
rendered_const: Table<DefIndex, LazyValue<String>>,
443444
rendered_precise_capturing_args: Table<DefIndex, LazyArray<PreciseCapturingArgKind<Symbol, Symbol>>>,
444-
asyncness: Table<DefIndex, ty::Asyncness>,
445445
fn_arg_idents: Table<DefIndex, LazyArray<Option<Ident>>>,
446446
coroutine_kind: Table<DefIndex, hir::CoroutineKind>,
447447
coroutine_for_closure: Table<DefIndex, RawDefId>,

compiler/rustc_metadata/src/rmeta/table.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@ impl IsDefault for bool {
2525
}
2626
}
2727

28+
impl IsDefault for ty::Asyncness {
29+
fn is_default(&self) -> bool {
30+
match self {
31+
ty::Asyncness::Yes => false,
32+
ty::Asyncness::No => true,
33+
}
34+
}
35+
}
36+
37+
impl IsDefault for hir::Constness {
38+
fn is_default(&self) -> bool {
39+
match self {
40+
rustc_hir::Constness::Const => false,
41+
rustc_hir::Constness::NotConst => true,
42+
}
43+
}
44+
}
45+
2846
impl IsDefault for u32 {
2947
fn is_default(&self) -> bool {
3048
*self == 0
@@ -293,6 +311,50 @@ impl FixedSizeEncoding for bool {
293311
}
294312
}
295313

314+
impl FixedSizeEncoding for ty::Asyncness {
315+
type ByteArray = [u8; 1];
316+
317+
#[inline]
318+
fn from_bytes(b: &[u8; 1]) -> Self {
319+
match b[0] {
320+
0 => ty::Asyncness::No,
321+
1 => ty::Asyncness::Yes,
322+
_ => unreachable!(),
323+
}
324+
}
325+
326+
#[inline]
327+
fn write_to_bytes(self, b: &mut [u8; 1]) {
328+
debug_assert!(!self.is_default());
329+
b[0] = match self {
330+
ty::Asyncness::No => 0,
331+
ty::Asyncness::Yes => 1,
332+
}
333+
}
334+
}
335+
336+
impl FixedSizeEncoding for hir::Constness {
337+
type ByteArray = [u8; 1];
338+
339+
#[inline]
340+
fn from_bytes(b: &[u8; 1]) -> Self {
341+
match b[0] {
342+
0 => hir::Constness::NotConst,
343+
1 => hir::Constness::Const,
344+
_ => unreachable!(),
345+
}
346+
}
347+
348+
#[inline]
349+
fn write_to_bytes(self, b: &mut [u8; 1]) {
350+
debug_assert!(!self.is_default());
351+
b[0] = match self {
352+
hir::Constness::NotConst => 0,
353+
hir::Constness::Const => 1,
354+
}
355+
}
356+
}
357+
296358
// NOTE(eddyb) there could be an impl for `usize`, which would enable a more
297359
// generic `LazyValue<T>` impl, but in the general case we might not need / want
298360
// to fit every `usize` in `u32`.

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,10 @@ pub struct ImplTraitHeader<'tcx> {
250250
}
251251

252252
#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, HashStable, Debug)]
253-
#[derive(TypeFoldable, TypeVisitable)]
253+
#[derive(TypeFoldable, TypeVisitable, Default)]
254254
pub enum Asyncness {
255255
Yes,
256+
#[default]
256257
No,
257258
}
258259

0 commit comments

Comments
 (0)