Skip to content

Commit e5a2b25

Browse files
authored
Rollup merge of #150521 - petrochenkov:decl, r=nnethercote
resolve: Rename "name bindings" to "name declarations" This was discussed previously in some name resolution specification threads on zulip. Link: [#t-compiler/help > understanding early name resolution of imports @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/182449-t-compiler.2Fhelp/topic/understanding.20early.20name.20resolution.20of.20imports/near/545306658) The main change is that `NameBinding` is renamed to `Decl`. The `Name` part is skipped because almost everything in `rustc_resolve` is about names. So in general the naming looks more compact now (`binding: NameBinding` -> `decl: Decl`). I didn't rename *everything* including all the local variables, but did rename most of significant interfaces. Late resolution in particular uses "bindings" in slightly different meanings, I didn't touch that. Also, some interface comments are added/improved. Renaming list: - fn `define_binding_local` -> `plant_decl_into_local_module` - fn `add_macro_use_binding` -> `add_macro_use_decl` - fn `binding_description` -> `decl_description` - enum `PendingBinding` -> `PendingDecl` - field `ImportKind::Single::bindings` -> `ImportKind::Single::decls` - field `NameResolution::non_glob_binding` -> `NameResolution::non_glob_decl` - field `NameResolution::glob_binding` -> `NameResolution::glob_decl` - fn `best_binding` -> `best_decl` - fn `import` -> `new_import_decl` - fn `try_define_local` -> `try_plant_decl_into_local_module` - fn `new_ambiguity_binding` -> `new_decl_with_ambiguity` - fn `new_warn_ambiguity_binding` -> `new_decl_with_warn_ambiguity` - field `UnnecessaryQualification::binding` -> `UnnecessaryQualification::decl` - struct `MacroRulesBinding` -> `MacroRulesDef` - field `MacroRulesBinding::binding` -> `MacroRulesDef::decl` - variant `MacroRulesScope::Binding` -> `MacroRulesScope::Def` - enum `LexicalScopeBinding` -> `LateDecl` - variant `LexicalScopeBinding::Item` -> `LateDecl::Decl` - variant `LexicalScopeBinding::Res` -> `LateDecl::RibDef` - field `ModuleData::self_binding` -> `ModuleData::self_decl` - struct `NameBindingData` -> `DeclData` - type `NameBinding` -> `Decl` - enum `NameBindingKind` -> `DeclKind` - variant `NameBindingKind::Res` -> `DeclKind::Def` - field `NameBindingKind::Import::binding` -> `DeclKind::Import::source_decl` - field `PrivacyError::binding` -> `PrivacyError::decl` - field `ExternPreludeEntry::item_binding` -> `ExternPreludeEntry::item_decl` - field `ExternPreludeEntry::flag_binding` -> `ExternPreludeEntry::flag_decl` - field `Resolver::binding_parent_modules` -> `Resolver::decl_parent_modules` - field `Resolver::dummy_binding` -> `Resolver::dummy_decl` - field `Resolver::builtin_types_bindings` -> `Resolver::builtin_type_decls` - field `Resolver::builtin_attrs_bindings` -> `Resolver::builtin_attr_decls` - field `Resolver::registered_tool_bindings` -> `Resolver::registered_tool_decls` - fn `ResolverArenas::new_res_binding` -> `ResolverArenas::new_def_decl` - fn `ResolverArenas::new_pub_res_binding` -> `ResolverArenas::new_pub_def_decl` - fn `ResolverArenas::alloc_name_binding` -> `ResolverArenas::alloc_decl` - fn `ResolverArenas::alloc_macro_rules_binding` -> `ResolverArenas::alloc_macro_rules_def` - fn `set_binding_parent_module` -> `set_decl_parent_module`
2 parents 8add20b + db26d01 commit e5a2b25

11 files changed

Lines changed: 596 additions & 639 deletions

File tree

compiler/rustc_middle/src/metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl Reexport {
2626
}
2727
}
2828

29-
/// This structure is supposed to keep enough data to re-create `NameBinding`s for other crates
29+
/// This structure is supposed to keep enough data to re-create `Decl`s for other crates
3030
/// during name resolution. Right now the bindings are not recreated entirely precisely so we may
3131
/// need to add more data in the future to correctly support macros 2.0, for example.
3232
/// Module child can be either a proper item or a reexport (including private imports).

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 47 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,33 @@ use tracing::debug;
3333
use crate::Namespace::{MacroNS, TypeNS, ValueNS};
3434
use crate::def_collector::collect_definitions;
3535
use crate::imports::{ImportData, ImportKind};
36-
use crate::macros::{MacroRulesBinding, MacroRulesScope, MacroRulesScopeRef};
36+
use crate::macros::{MacroRulesDecl, MacroRulesScope, MacroRulesScopeRef};
3737
use crate::ref_mut::CmCell;
3838
use crate::{
39-
BindingKey, ExternPreludeEntry, Finalize, MacroData, Module, ModuleKind, ModuleOrUniformRoot,
40-
NameBinding, NameBindingData, NameBindingKind, ParentScope, PathResult, ResolutionError,
41-
Resolver, Segment, Used, VisResolutionError, errors,
39+
BindingKey, Decl, DeclData, DeclKind, ExternPreludeEntry, Finalize, MacroData, Module,
40+
ModuleKind, ModuleOrUniformRoot, ParentScope, PathResult, ResolutionError, Resolver, Segment,
41+
Used, VisResolutionError, errors,
4242
};
4343

4444
type Res = def::Res<NodeId>;
4545

4646
impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
47-
/// Defines `name` in namespace `ns` of module `parent` to be `def` if it is not yet defined;
48-
/// otherwise, reports an error.
49-
pub(crate) fn define_binding_local(
47+
/// Attempt to put the declaration with the given name and namespace into the module,
48+
/// and report an error in case of a collision.
49+
pub(crate) fn plant_decl_into_local_module(
5050
&mut self,
5151
parent: Module<'ra>,
5252
ident: Ident,
5353
ns: Namespace,
54-
binding: NameBinding<'ra>,
54+
decl: Decl<'ra>,
5555
) {
56-
if let Err(old_binding) = self.try_define_local(parent, ident, ns, binding, false) {
57-
self.report_conflict(parent, ident, ns, old_binding, binding);
56+
if let Err(old_decl) = self.try_plant_decl_into_local_module(parent, ident, ns, decl, false)
57+
{
58+
self.report_conflict(parent, ident, ns, old_decl, decl);
5859
}
5960
}
6061

62+
/// Create a name definitinon from the given components, and put it into the local module.
6163
fn define_local(
6264
&mut self,
6365
parent: Module<'ra>,
@@ -68,10 +70,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
6870
span: Span,
6971
expn_id: LocalExpnId,
7072
) {
71-
let binding = self.arenas.new_res_binding(res, vis.to_def_id(), span, expn_id);
72-
self.define_binding_local(parent, ident, ns, binding);
73+
let decl = self.arenas.new_def_decl(res, vis.to_def_id(), span, expn_id);
74+
self.plant_decl_into_local_module(parent, ident, ns, decl);
7375
}
7476

77+
/// Create a name definitinon from the given components, and put it into the extern module.
7578
fn define_extern(
7679
&self,
7780
parent: Module<'ra>,
@@ -82,10 +85,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
8285
vis: Visibility<DefId>,
8386
span: Span,
8487
expansion: LocalExpnId,
85-
ambiguity: Option<NameBinding<'ra>>,
88+
ambiguity: Option<Decl<'ra>>,
8689
) {
87-
let binding = self.arenas.alloc_name_binding(NameBindingData {
88-
kind: NameBindingKind::Res(res),
90+
let decl = self.arenas.alloc_decl(DeclData {
91+
kind: DeclKind::Def(res),
8992
ambiguity,
9093
// External ambiguities always report the `AMBIGUOUS_GLOB_IMPORTS` lint at the moment.
9194
warn_ambiguity: true,
@@ -101,8 +104,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
101104
if self
102105
.resolution_or_default(parent, key)
103106
.borrow_mut_unchecked()
104-
.non_glob_binding
105-
.replace(binding)
107+
.non_glob_decl
108+
.replace(decl)
106109
.is_some()
107110
{
108111
span_bug!(span, "an external binding was already defined");
@@ -284,7 +287,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
284287
let ModChild { ident: _, res, vis, ref reexport_chain } = *ambig_child;
285288
let span = child_span(self, reexport_chain, res);
286289
let res = res.expect_non_local();
287-
self.arenas.new_res_binding(res, vis, span, expansion)
290+
self.arenas.new_def_decl(res, vis, span, expansion)
288291
});
289292

290293
// Record primary definitions.
@@ -691,7 +694,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
691694
let kind = ImportKind::Single {
692695
source: source.ident,
693696
target: ident,
694-
bindings: Default::default(),
697+
decls: Default::default(),
695698
type_ns_only,
696699
nested,
697700
id,
@@ -977,7 +980,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
977980
let parent_scope = self.parent_scope;
978981
let expansion = parent_scope.expansion;
979982

980-
let (used, module, binding) = if orig_name.is_none() && ident.name == kw::SelfLower {
983+
let (used, module, decl) = if orig_name.is_none() && ident.name == kw::SelfLower {
981984
self.r.dcx().emit_err(errors::ExternCrateSelfRequiresRenaming { span: sp });
982985
return;
983986
} else if orig_name == Some(kw::SelfLower) {
@@ -997,10 +1000,10 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
9971000
}
9981001
.map(|module| {
9991002
let used = self.process_macro_use_imports(item, module);
1000-
let binding = self.r.arenas.new_pub_res_binding(module.res().unwrap(), sp, expansion);
1001-
(used, Some(ModuleOrUniformRoot::Module(module)), binding)
1003+
let decl = self.r.arenas.new_pub_def_decl(module.res().unwrap(), sp, expansion);
1004+
(used, Some(ModuleOrUniformRoot::Module(module)), decl)
10021005
})
1003-
.unwrap_or((true, None, self.r.dummy_binding));
1006+
.unwrap_or((true, None, self.r.dummy_decl));
10041007
let import = self.r.arenas.alloc_import(ImportData {
10051008
kind: ImportKind::ExternCrate { source: orig_name, target: ident, id: item.id },
10061009
root_id: item.id,
@@ -1019,15 +1022,15 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
10191022
self.r.import_use_map.insert(import, Used::Other);
10201023
}
10211024
self.r.potentially_unused_imports.push(import);
1022-
let imported_binding = self.r.import(binding, import);
1025+
let import_decl = self.r.new_import_decl(decl, import);
10231026
if ident.name != kw::Underscore && parent == self.r.graph_root {
10241027
let norm_ident = Macros20NormalizedIdent::new(ident);
10251028
// FIXME: this error is technically unnecessary now when extern prelude is split into
10261029
// two scopes, remove it with lang team approval.
10271030
if let Some(entry) = self.r.extern_prelude.get(&norm_ident)
10281031
&& expansion != LocalExpnId::ROOT
10291032
&& orig_name.is_some()
1030-
&& entry.item_binding.is_none()
1033+
&& entry.item_decl.is_none()
10311034
{
10321035
self.r.dcx().emit_err(
10331036
errors::MacroExpandedExternCrateCannotShadowExternArguments { span: item.span },
@@ -1038,21 +1041,21 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
10381041
match self.r.extern_prelude.entry(norm_ident) {
10391042
Entry::Occupied(mut occupied) => {
10401043
let entry = occupied.get_mut();
1041-
if entry.item_binding.is_some() {
1044+
if entry.item_decl.is_some() {
10421045
let msg = format!("extern crate `{ident}` already in extern prelude");
10431046
self.r.tcx.dcx().span_delayed_bug(item.span, msg);
10441047
} else {
1045-
entry.item_binding = Some((imported_binding, orig_name.is_some()));
1048+
entry.item_decl = Some((import_decl, orig_name.is_some()));
10461049
}
10471050
entry
10481051
}
10491052
Entry::Vacant(vacant) => vacant.insert(ExternPreludeEntry {
1050-
item_binding: Some((imported_binding, true)),
1051-
flag_binding: None,
1053+
item_decl: Some((import_decl, true)),
1054+
flag_decl: None,
10521055
}),
10531056
};
10541057
}
1055-
self.r.define_binding_local(parent, ident, TypeNS, imported_binding);
1058+
self.r.plant_decl_into_local_module(parent, ident, TypeNS, import_decl);
10561059
}
10571060

10581061
/// Constructs the reduced graph for one foreign item.
@@ -1089,14 +1092,14 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
10891092
}
10901093
}
10911094

1092-
fn add_macro_use_binding(
1095+
fn add_macro_use_decl(
10931096
&mut self,
10941097
name: Symbol,
1095-
binding: NameBinding<'ra>,
1098+
decl: Decl<'ra>,
10961099
span: Span,
10971100
allow_shadowing: bool,
10981101
) {
1099-
if self.r.macro_use_prelude.insert(name, binding).is_some() && !allow_shadowing {
1102+
if self.r.macro_use_prelude.insert(name, decl).is_some() && !allow_shadowing {
11001103
self.r.dcx().emit_err(errors::MacroUseNameAlreadyInUse { span, name });
11011104
}
11021105
}
@@ -1167,8 +1170,8 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
11671170
}
11681171
macro_use_import(this, span, true)
11691172
};
1170-
let import_binding = this.r.import(binding, import);
1171-
this.add_macro_use_binding(ident.name, import_binding, span, allow_shadowing);
1173+
let import_decl = this.r.new_import_decl(binding, import);
1174+
this.add_macro_use_decl(ident.name, import_decl, span, allow_shadowing);
11721175
}
11731176
});
11741177
} else {
@@ -1183,13 +1186,8 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
11831186
if let Ok(binding) = result {
11841187
let import = macro_use_import(self, ident.span, false);
11851188
self.r.potentially_unused_imports.push(import);
1186-
let imported_binding = self.r.import(binding, import);
1187-
self.add_macro_use_binding(
1188-
ident.name,
1189-
imported_binding,
1190-
ident.span,
1191-
allow_shadowing,
1192-
);
1189+
let import_decl = self.r.new_import_decl(binding, import);
1190+
self.add_macro_use_decl(ident.name, import_decl, ident.span, allow_shadowing);
11931191
} else {
11941192
self.r.dcx().emit_err(errors::ImportedMacroNotFound { span: ident.span });
11951193
}
@@ -1300,8 +1298,8 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
13001298
} else {
13011299
Visibility::Restricted(CRATE_DEF_ID)
13021300
};
1303-
let binding = self.r.arenas.new_res_binding(res, vis.to_def_id(), span, expansion);
1304-
self.r.set_binding_parent_module(binding, parent_scope.module);
1301+
let decl = self.r.arenas.new_def_decl(res, vis.to_def_id(), span, expansion);
1302+
self.r.set_decl_parent_module(decl, parent_scope.module);
13051303
self.r.all_macro_rules.insert(ident.name);
13061304
if is_macro_export {
13071305
let import = self.r.arenas.alloc_import(ImportData {
@@ -1319,17 +1317,17 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
13191317
vis_span: item.vis.span,
13201318
});
13211319
self.r.import_use_map.insert(import, Used::Other);
1322-
let import_binding = self.r.import(binding, import);
1323-
self.r.define_binding_local(self.r.graph_root, ident, MacroNS, import_binding);
1320+
let import_decl = self.r.new_import_decl(decl, import);
1321+
self.r.plant_decl_into_local_module(self.r.graph_root, ident, MacroNS, import_decl);
13241322
} else {
13251323
self.r.check_reserved_macro_name(ident, res);
13261324
self.insert_unused_macro(ident, def_id, item.id);
13271325
}
13281326
self.r.feed_visibility(feed, vis);
1329-
let scope = self.r.arenas.alloc_macro_rules_scope(MacroRulesScope::Binding(
1330-
self.r.arenas.alloc_macro_rules_binding(MacroRulesBinding {
1327+
let scope = self.r.arenas.alloc_macro_rules_scope(MacroRulesScope::Def(
1328+
self.r.arenas.alloc_macro_rules_decl(MacroRulesDecl {
13311329
parent_macro_rules_scope: parent_scope.macro_rules,
1332-
binding,
1330+
decl,
13331331
ident,
13341332
}),
13351333
));

compiler/rustc_resolve/src/check_unused.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use rustc_session::lint::builtin::{
3636
use rustc_span::{DUMMY_SP, Ident, Macros20NormalizedIdent, Span, kw};
3737

3838
use crate::imports::{Import, ImportKind};
39-
use crate::{LexicalScopeBinding, NameBindingKind, Resolver, module_to_string};
39+
use crate::{DeclKind, LateDecl, Resolver, module_to_string};
4040

4141
struct UnusedImport {
4242
use_tree: ast::UseTree,
@@ -514,8 +514,8 @@ impl Resolver<'_, '_> {
514514
let mut check_redundant_imports = FxIndexSet::default();
515515
for module in &self.local_modules {
516516
for (_key, resolution) in self.resolutions(*module).borrow().iter() {
517-
if let Some(binding) = resolution.borrow().best_binding()
518-
&& let NameBindingKind::Import { import, .. } = binding.kind
517+
if let Some(decl) = resolution.borrow().best_decl()
518+
&& let DeclKind::Import { import, .. } = decl.kind
519519
&& let ImportKind::Single { id, .. } = import.kind
520520
{
521521
if let Some(unused_import) = unused_imports.get(&import.root_id)
@@ -542,8 +542,8 @@ impl Resolver<'_, '_> {
542542
// Deleting both unused imports and unnecessary segments of an item may result
543543
// in the item not being found.
544544
for unn_qua in &self.potentially_unnecessary_qualifications {
545-
if let LexicalScopeBinding::Item(name_binding) = unn_qua.binding
546-
&& let NameBindingKind::Import { import, .. } = name_binding.kind
545+
if let LateDecl::Decl(decl) = unn_qua.decl
546+
&& let DeclKind::Import { import, .. } = decl.kind
547547
&& (is_unused_import(import, &unused_imports)
548548
|| is_redundant_import(import, &redundant_imports))
549549
{

0 commit comments

Comments
 (0)