From 66a69561b0fc0f5b518fa785cefb919a74fa721e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 7 Dec 2024 03:04:51 +0000 Subject: [PATCH] Support x-crate default fields --- compiler/rustc_metadata/src/rmeta/decoder.rs | 6 +++++- compiler/rustc_metadata/src/rmeta/encoder.rs | 7 +++++++ compiler/rustc_metadata/src/rmeta/mod.rs | 1 + tests/ui/structs/auxiliary/struct_field_default.rs | 5 +++++ tests/ui/structs/default-field-values-support.rs | 6 ++++++ 5 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 tests/ui/structs/auxiliary/struct_field_default.rs diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 4a4930fff9d53..b9586338655e7 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -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, @@ -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 { + self.root.tables.default_fields.get(self, id).map(|d| d.decode(self)) + } + fn get_trait_item_def_id(self, id: DefIndex) -> Option { self.root.tables.trait_item_def_id.get(self, id).map(|d| d.decode_from_cdata(self)) } diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index a34ea18f71697..452e132b1ab49 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -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); diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index a486ad42482a6..ea46f2d38d307 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -451,6 +451,7 @@ define_tables! { trait_def: Table>, trait_item_def_id: Table, expn_that_defined: Table>, + default_fields: Table>, params_in_repr: Table>>, repr_options: Table>, // `def_keys` and `def_path_hashes` represent a lazy version of a diff --git a/tests/ui/structs/auxiliary/struct_field_default.rs b/tests/ui/structs/auxiliary/struct_field_default.rs new file mode 100644 index 0000000000000..b315df5dba287 --- /dev/null +++ b/tests/ui/structs/auxiliary/struct_field_default.rs @@ -0,0 +1,5 @@ +#![feature(default_field_values)] + +pub struct A { + pub a: isize = 42, +} diff --git a/tests/ui/structs/default-field-values-support.rs b/tests/ui/structs/default-field-values-support.rs index bdf21e1272d87..da0379af94b8f 100644 --- a/tests/ui/structs/default-field-values-support.rs +++ b/tests/ui/structs/default-field-values-support.rs @@ -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:: { .. }; assert!(matches!(Qux:: { 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)); }