1
Fork 0

Lowering field access for anonymous adts

This commit is contained in:
Frank King 2024-01-04 21:45:06 +08:00
parent 36d7e7fd3f
commit 0c0df4efe0
18 changed files with 391 additions and 71 deletions

View file

@ -238,6 +238,7 @@ trivial! {
Option<rustc_span::def_id::DefId>,
Option<rustc_span::def_id::LocalDefId>,
Option<rustc_span::Span>,
Option<rustc_target::abi::FieldIdx>,
Option<rustc_target::spec::PanicStrategy>,
Option<usize>,
Result<(), rustc_errors::ErrorGuaranteed>,

View file

@ -2217,6 +2217,10 @@ rustc_queries! {
desc { "whether the item should be made inlinable across crates" }
separate_provide_extern
}
query find_field((def_id, ident): (DefId, rustc_span::symbol::Ident)) -> Option<rustc_target::abi::FieldIdx> {
desc { |tcx| "find the index of maybe nested field `{ident}` in `{}`", tcx.def_path_str(def_id) }
}
}
rustc_query_append! { define_callbacks! }

View file

@ -44,6 +44,12 @@ pub struct TypeckResults<'tcx> {
/// belongs, but it may not exist if it's a tuple field (`tuple.0`).
field_indices: ItemLocalMap<FieldIdx>,
/// Resolved types and indices for the nested fields' accesses of `obj.field` (expanded
/// to `obj._(1)._(2).field` in THIR). This map only stores the intermediate type
/// of `obj._(1)` and index of `_(1)._(2)`, and the type of `_(1)._(2)`, and the index of
/// `_(2).field`.
nested_fields: ItemLocalMap<Vec<(Ty<'tcx>, FieldIdx)>>,
/// Stores the types for various nodes in the AST. Note that this table
/// is not guaranteed to be populated outside inference. See
/// typeck::check::fn_ctxt for details.
@ -214,6 +220,7 @@ impl<'tcx> TypeckResults<'tcx> {
hir_owner,
type_dependent_defs: Default::default(),
field_indices: Default::default(),
nested_fields: Default::default(),
user_provided_types: Default::default(),
user_provided_sigs: Default::default(),
node_types: Default::default(),
@ -285,6 +292,18 @@ impl<'tcx> TypeckResults<'tcx> {
self.field_indices().get(id).cloned()
}
pub fn nested_fields(&self) -> LocalTableInContext<'_, Vec<(Ty<'tcx>, FieldIdx)>> {
LocalTableInContext { hir_owner: self.hir_owner, data: &self.nested_fields }
}
pub fn nested_fields_mut(&mut self) -> LocalTableInContextMut<'_, Vec<(Ty<'tcx>, FieldIdx)>> {
LocalTableInContextMut { hir_owner: self.hir_owner, data: &mut self.nested_fields }
}
pub fn nested_field_tys_and_indices(&self, id: hir::HirId) -> &[(Ty<'tcx>, FieldIdx)] {
self.nested_fields().get(id).map_or(&[], Vec::as_slice)
}
pub fn user_provided_types(&self) -> LocalTableInContext<'_, CanonicalUserType<'tcx>> {
LocalTableInContext { hir_owner: self.hir_owner, data: &self.user_provided_types }
}