Use default_field_values more in Resolver

Provide `const` functions to get around lack of `const Default` for `FxHash*` types.

Use default field values in `Resolver` more.
This commit is contained in:
Esteban Küber 2026-01-16 02:14:01 +00:00
parent 7704328ba5
commit bfd1a9a86f
2 changed files with 41 additions and 51 deletions

View file

@ -28,3 +28,18 @@ macro_rules! define_stable_id_collections {
pub type $entry_name<'a, T> = $crate::fx::IndexEntry<'a, $key, T>;
};
}
pub mod default {
use super::{FxBuildHasher, FxHashMap, FxHashSet};
// FIXME: These two functions will become unnecessary after
// <https://github.com/rust-lang/rustc-hash/pull/63> lands and we start using the corresponding
// `rustc-hash` version. After that we can use `Default::default()` instead.
pub const fn fx_hash_map<K, V>() -> FxHashMap<K, V> {
FxHashMap::with_hasher(FxBuildHasher)
}
pub const fn fx_hash_set<V>() -> FxHashSet<V> {
FxHashSet::with_hasher(FxBuildHasher)
}
}

View file

@ -45,7 +45,7 @@ use rustc_ast::{
self as ast, AngleBracketedArg, CRATE_NODE_ID, Crate, Expr, ExprKind, GenericArg, GenericArgs,
NodeId, Path, attr,
};
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet, default};
use rustc_data_structures::intern::Interned;
use rustc_data_structures::steal::Steal;
use rustc_data_structures::sync::{FreezeReadGuard, FreezeWriteGuard};
@ -1129,7 +1129,7 @@ pub struct Resolver<'ra, 'tcx> {
/// Span of the privacy modifier in fields of an item `DefId` accessible with dot syntax.
/// Used for hints during error reporting.
field_visibility_spans: FxHashMap<DefId, Vec<Span>>,
field_visibility_spans: FxHashMap<DefId, Vec<Span>> = default::fx_hash_map(),
/// All imports known to succeed or fail.
determined_imports: Vec<Import<'ra>> = Vec::new(),
@ -1146,7 +1146,7 @@ pub struct Resolver<'ra, 'tcx> {
/// Resolutions for import nodes, which have multiple resolutions in different namespaces.
import_res_map: NodeMap<PerNS<Option<Res>>>,
/// An import will be inserted into this map if it has been used.
import_use_map: FxHashMap<Import<'ra>, Used>,
import_use_map: FxHashMap<Import<'ra>, Used> = default::fx_hash_map(),
/// Resolutions for labels (node IDs of their corresponding blocks or loops).
label_res_map: NodeMap<NodeId>,
/// Resolutions for lifetimes.
@ -1190,7 +1190,7 @@ pub struct Resolver<'ra, 'tcx> {
glob_map: FxIndexMap<LocalDefId, FxIndexSet<Symbol>>,
glob_error: Option<ErrorGuaranteed> = None,
visibilities_for_hashing: Vec<(LocalDefId, Visibility)> = Vec::new(),
used_imports: FxHashSet<NodeId>,
used_imports: FxHashSet<NodeId> = default::fx_hash_set(),
maybe_unused_trait_imports: FxIndexSet<LocalDefId>,
/// Privacy errors are delayed until the end in order to deduplicate them.
@ -1206,30 +1206,30 @@ pub struct Resolver<'ra, 'tcx> {
/// When a type is re-exported that has an inaccessible constructor because it has fields that
/// are inaccessible from the import's scope, we mark that as the type won't be able to be built
/// through the re-export. We use this information to extend the existing diagnostic.
inaccessible_ctor_reexport: FxHashMap<Span, Span>,
inaccessible_ctor_reexport: FxHashMap<Span, Span> = default::fx_hash_map(),
arenas: &'ra ResolverArenas<'ra>,
dummy_decl: Decl<'ra>,
builtin_type_decls: FxHashMap<Symbol, Decl<'ra>>,
builtin_attr_decls: FxHashMap<Symbol, Decl<'ra>>,
registered_tool_decls: FxHashMap<Ident, Decl<'ra>>,
macro_names: FxHashSet<Ident>,
builtin_macros: FxHashMap<Symbol, SyntaxExtensionKind>,
macro_names: FxHashSet<Ident> = default::fx_hash_set(),
builtin_macros: FxHashMap<Symbol, SyntaxExtensionKind> = default::fx_hash_map(),
registered_tools: &'tcx RegisteredTools,
macro_use_prelude: FxIndexMap<Symbol, Decl<'ra>>,
/// Eagerly populated map of all local macro definitions.
local_macro_map: FxHashMap<LocalDefId, &'ra MacroData>,
local_macro_map: FxHashMap<LocalDefId, &'ra MacroData> = default::fx_hash_map(),
/// Lazily populated cache of macro definitions loaded from external crates.
extern_macro_map: CacheRefCell<FxHashMap<DefId, &'ra MacroData>>,
dummy_ext_bang: Arc<SyntaxExtension>,
dummy_ext_derive: Arc<SyntaxExtension>,
non_macro_attr: &'ra MacroData,
local_macro_def_scopes: FxHashMap<LocalDefId, Module<'ra>>,
ast_transform_scopes: FxHashMap<LocalExpnId, Module<'ra>>,
local_macro_def_scopes: FxHashMap<LocalDefId, Module<'ra>> = default::fx_hash_map(),
ast_transform_scopes: FxHashMap<LocalExpnId, Module<'ra>> = default::fx_hash_map(),
unused_macros: FxIndexMap<LocalDefId, (NodeId, Ident)>,
/// A map from the macro to all its potentially unused arms.
unused_macro_rules: FxIndexMap<NodeId, DenseBitSet<usize>>,
proc_macro_stubs: FxHashSet<LocalDefId>,
proc_macro_stubs: FxHashSet<LocalDefId> = default::fx_hash_set(),
/// Traces collected during macro resolution and validated when it's complete.
single_segment_macro_resolutions:
CmRefCell<Vec<(Ident, MacroKind, ParentScope<'ra>, Option<Decl<'ra>>, Option<Span>)>>,
@ -1239,23 +1239,23 @@ pub struct Resolver<'ra, 'tcx> {
/// `derive(Copy)` marks items they are applied to so they are treated specially later.
/// Derive macros cannot modify the item themselves and have to store the markers in the global
/// context, so they attach the markers to derive container IDs using this resolver table.
containers_deriving_copy: FxHashSet<LocalExpnId>,
containers_deriving_copy: FxHashSet<LocalExpnId> = default::fx_hash_set(),
/// Parent scopes in which the macros were invoked.
/// FIXME: `derives` are missing in these parent scopes and need to be taken from elsewhere.
invocation_parent_scopes: FxHashMap<LocalExpnId, ParentScope<'ra>>,
invocation_parent_scopes: FxHashMap<LocalExpnId, ParentScope<'ra>> = default::fx_hash_map(),
/// `macro_rules` scopes *produced* by expanding the macro invocations,
/// include all the `macro_rules` items and other invocations generated by them.
output_macro_rules_scopes: FxHashMap<LocalExpnId, MacroRulesScopeRef<'ra>>,
output_macro_rules_scopes: FxHashMap<LocalExpnId, MacroRulesScopeRef<'ra>> = default::fx_hash_map(),
/// `macro_rules` scopes produced by `macro_rules` item definitions.
macro_rules_scopes: FxHashMap<LocalDefId, MacroRulesScopeRef<'ra>>,
macro_rules_scopes: FxHashMap<LocalDefId, MacroRulesScopeRef<'ra>> = default::fx_hash_map(),
/// Helper attributes that are in scope for the given expansion.
helper_attrs: FxHashMap<LocalExpnId, Vec<(Macros20NormalizedIdent, Decl<'ra>)>>,
helper_attrs: FxHashMap<LocalExpnId, Vec<(Macros20NormalizedIdent, Decl<'ra>)>> = default::fx_hash_map(),
/// Ready or in-progress results of resolving paths inside the `#[derive(...)]` attribute
/// with the given `ExpnId`.
derive_data: FxHashMap<LocalExpnId, DeriveData>,
derive_data: FxHashMap<LocalExpnId, DeriveData> = default::fx_hash_map(),
/// Avoid duplicated errors for "name already defined".
name_already_seen: FxHashMap<Symbol, Span>,
name_already_seen: FxHashMap<Symbol, Span> = default::fx_hash_map(),
potentially_unused_imports: Vec<Import<'ra>> = Vec::new(),
@ -1275,14 +1275,14 @@ pub struct Resolver<'ra, 'tcx> {
disambiguator: DisambiguatorState,
/// Indices of unnamed struct or variant fields with unresolved attributes.
placeholder_field_indices: FxHashMap<NodeId, usize>,
placeholder_field_indices: FxHashMap<NodeId, usize> = default::fx_hash_map(),
/// When collecting definitions from an AST fragment produced by a macro invocation `ExpnId`
/// we know what parent node that fragment should be attached to thanks to this table,
/// and how the `impl Trait` fragments were introduced.
invocation_parents: FxHashMap<LocalExpnId, InvocationParent>,
/// Amount of lifetime parameters for each item in the crate.
item_generics_num_lifetimes: FxHashMap<LocalDefId, usize>,
item_generics_num_lifetimes: FxHashMap<LocalDefId, usize> = default::fx_hash_map(),
delegation_fn_sigs: LocalDefIdMap<DelegationFnSig>,
delegation_infos: LocalDefIdMap<DelegationInfo>,
@ -1293,7 +1293,7 @@ pub struct Resolver<'ra, 'tcx> {
proc_macros: Vec<LocalDefId> = Vec::new(),
confused_type_with_std_module: FxIndexMap<Span, Span>,
/// Whether lifetime elision was successful.
lifetime_elision_allowed: FxHashSet<NodeId>,
lifetime_elision_allowed: FxHashSet<NodeId> = default::fx_hash_set(),
/// Names of items that were stripped out via cfg with their corresponding cfg meta item.
stripped_cfg_items: Vec<StrippedCfgItem<NodeId>> = Vec::new(),
@ -1304,19 +1304,19 @@ pub struct Resolver<'ra, 'tcx> {
all_macro_rules: UnordSet<Symbol>,
/// Invocation ids of all glob delegations.
glob_delegation_invoc_ids: FxHashSet<LocalExpnId>,
glob_delegation_invoc_ids: FxHashSet<LocalExpnId> = default::fx_hash_set(),
/// Analogue of module `unexpanded_invocations` but in trait impls, excluding glob delegations.
/// Needed because glob delegations wait for all other neighboring macros to expand.
impl_unexpanded_invocations: FxHashMap<LocalDefId, FxHashSet<LocalExpnId>>,
impl_unexpanded_invocations: FxHashMap<LocalDefId, FxHashSet<LocalExpnId>> = default::fx_hash_map(),
/// Simplified analogue of module `resolutions` but in trait impls, excluding glob delegations.
/// Needed because glob delegations exclude explicitly defined names.
impl_binding_keys: FxHashMap<LocalDefId, FxHashSet<BindingKey>>,
impl_binding_keys: FxHashMap<LocalDefId, FxHashSet<BindingKey>> = default::fx_hash_map(),
/// This is the `Span` where an `extern crate foo;` suggestion would be inserted, if `foo`
/// could be a crate that wasn't imported. For diagnostics use only.
current_crate_outer_attr_insert_span: Span,
mods_with_parse_errors: FxHashSet<DefId>,
mods_with_parse_errors: FxHashSet<DefId> = default::fx_hash_set(),
/// Whether `Resolver::register_macros_for_all_crates` has been called once already, as we
/// don't need to run it more than once.
@ -1325,7 +1325,7 @@ pub struct Resolver<'ra, 'tcx> {
// Stores pre-expansion and pre-placeholder-fragment-insertion names for `impl Trait` types
// that were encountered during resolution. These names are used to generate item names
// for APITs, so we don't want to leak details of resolution into these names.
impl_trait_names: FxHashMap<NodeId, Symbol>,
impl_trait_names: FxHashMap<NodeId, Symbol> = default::fx_hash_map(),
}
/// This provides memory for the rest of the crate. The `'ra` lifetime that is
@ -1598,12 +1598,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
field_names: Default::default(),
field_defaults: Default::default(),
field_visibility_spans: FxHashMap::default(),
pat_span_map: Default::default(),
partial_res_map: Default::default(),
import_res_map: Default::default(),
import_use_map: Default::default(),
label_res_map: Default::default(),
lifetimes_res_map: Default::default(),
extra_lifetime_params_map: Default::default(),
@ -1616,12 +1614,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
local_module_map,
extern_module_map: Default::default(),
block_map: Default::default(),
ast_transform_scopes: FxHashMap::default(),
glob_map: Default::default(),
used_imports: FxHashSet::default(),
maybe_unused_trait_imports: Default::default(),
inaccessible_ctor_reexport: Default::default(),
arenas,
dummy_decl: arenas.new_pub_def_decl(Res::Err, DUMMY_SP, LocalExpnId::ROOT),
@ -1649,52 +1644,32 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
(*ident, decl)
})
.collect(),
macro_names: FxHashSet::default(),
builtin_macros: Default::default(),
registered_tools,
macro_use_prelude: Default::default(),
local_macro_map: Default::default(),
extern_macro_map: Default::default(),
dummy_ext_bang: Arc::new(SyntaxExtension::dummy_bang(edition)),
dummy_ext_derive: Arc::new(SyntaxExtension::dummy_derive(edition)),
non_macro_attr: arenas
.alloc_macro(MacroData::new(Arc::new(SyntaxExtension::non_macro_attr(edition)))),
invocation_parent_scopes: Default::default(),
output_macro_rules_scopes: Default::default(),
macro_rules_scopes: Default::default(),
helper_attrs: Default::default(),
derive_data: Default::default(),
local_macro_def_scopes: FxHashMap::default(),
name_already_seen: FxHashMap::default(),
struct_constructors: Default::default(),
unused_macros: Default::default(),
unused_macro_rules: Default::default(),
proc_macro_stubs: Default::default(),
single_segment_macro_resolutions: Default::default(),
multi_segment_macro_resolutions: Default::default(),
builtin_attrs: Default::default(),
containers_deriving_copy: Default::default(),
lint_buffer: LintBuffer::default(),
node_id_to_def_id,
disambiguator: DisambiguatorState::new(),
placeholder_field_indices: Default::default(),
invocation_parents,
item_generics_num_lifetimes: Default::default(),
trait_impls: Default::default(),
confused_type_with_std_module: Default::default(),
lifetime_elision_allowed: Default::default(),
stripped_cfg_items: Default::default(),
effective_visibilities: Default::default(),
doc_link_resolutions: Default::default(),
doc_link_traits_in_scope: Default::default(),
all_macro_rules: Default::default(),
delegation_fn_sigs: Default::default(),
glob_delegation_invoc_ids: Default::default(),
impl_unexpanded_invocations: Default::default(),
impl_binding_keys: Default::default(),
current_crate_outer_attr_insert_span,
mods_with_parse_errors: Default::default(),
impl_trait_names: Default::default(),
delegation_infos: Default::default(),
..
};