Support x-crate default fields

This commit is contained in:
Esteban Küber
2024-12-07 03:04:51 +00:00
parent 0757641f4d
commit fa331f4d0d
5 changed files with 24 additions and 1 deletions
+5 -1
View File
@@ -1104,7 +1104,7 @@ fn get_variant(
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,
@@ -1170,6 +1170,10 @@ fn get_safety(self, id: DefIndex) -> Safety {
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))
}
@@ -1401,6 +1401,13 @@ fn encode_def_ids(&mut self) {
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);
+1
View File
@@ -450,6 +450,7 @@ fn encode(&self, buf: &mut FileEncoder) -> LazyTables {
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
@@ -0,0 +1,5 @@
#![feature(default_field_values)]
pub struct A {
pub a: isize = 42,
}
@@ -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)]
@@ -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));
}