Skip to content

Commit

Permalink
Support x-crate default fields
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Dec 7, 2024
1 parent 233995b commit 66a6956
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 1 deletion.
6 changes: 5 additions & 1 deletion compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,7 @@ impl<'a> CrateMetadataRef<'a> {
name: self.item_name(did.index),
vis: self.get_visibility(did.index),
safety: self.get_safety(did.index),
value: None,
value: self.get_default_field(did.index),
})
.collect(),
adt_kind,
Expand Down Expand Up @@ -1170,6 +1170,10 @@ impl<'a> CrateMetadataRef<'a> {
self.root.tables.safety.get(self, id).unwrap_or_else(|| self.missing("safety", id))
}

fn get_default_field(self, id: DefIndex) -> Option<DefId> {
self.root.tables.default_fields.get(self, id).map(|d| d.decode(self))
}

fn get_trait_item_def_id(self, id: DefIndex) -> Option<DefId> {
self.root.tables.trait_item_def_id.get(self, id).map(|d| d.decode_from_cdata(self))
}
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
continue;
}

if def_kind == DefKind::Field
&& let hir::Node::Field(field) = tcx.hir_node_by_def_id(local_id)
&& let Some(anon) = field.default
{
record!(self.tables.default_fields[def_id] <- anon.def_id.to_def_id());
}

if should_encode_span(def_kind) {
let def_span = tcx.def_span(local_id);
record!(self.tables.def_span[def_id] <- def_span);
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_metadata/src/rmeta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ define_tables! {
trait_def: Table<DefIndex, LazyValue<ty::TraitDef>>,
trait_item_def_id: Table<DefIndex, RawDefId>,
expn_that_defined: Table<DefIndex, LazyValue<ExpnId>>,
default_fields: Table<DefIndex, LazyValue<DefId>>,
params_in_repr: Table<DefIndex, LazyValue<BitSet<u32>>>,
repr_options: Table<DefIndex, LazyValue<ReprOptions>>,
// `def_keys` and `def_path_hashes` represent a lazy version of a
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/structs/auxiliary/struct_field_default.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#![feature(default_field_values)]

pub struct A {
pub a: isize = 42,
}
6 changes: 6 additions & 0 deletions tests/ui/structs/default-field-values-support.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
//@ run-pass
//@ aux-build:struct_field_default.rs
#![feature(default_field_values, generic_const_exprs)]
#![allow(unused_variables, dead_code, incomplete_features)]

extern crate struct_field_default as xc;

pub struct S;

#[derive(Default)]
Expand Down Expand Up @@ -65,4 +68,7 @@ fn main () {
let x = Qux::<i32, 4> { .. };
assert!(matches!(Qux::<i32, 4> { bar: S, baz: 42, bat: 2, baq: 2, bay: 4, .. }, x));
assert!(x.bak.is_empty());

let x = xc::A { .. };
assert!(matches!(xc::A { a: 42 }, x));
}

0 comments on commit 66a6956

Please sign in to comment.