Skip to content

Commit

Permalink
Auto merge of #113408 - petrochenkov:bindintern2, r=cjgillot
Browse files Browse the repository at this point in the history
resolve: Stop creating `NameBinding`s on every use, create them once per definition instead

`NameBinding` values are supposed to be unique, use referential equality, and be created once for every name declaration.

Before this PR many `NameBinding`s were created on name use, rather than on name declaration, because it's sufficiently cheap, and comparisons are not actually used in practice for some binding kinds.
This PR makes `NameBinding`s consistently unique and created on name declaration.

There are two special cases
- for extern prelude names creating `NameBinding` requires loading the corresponding crate, which is expensive, so such bindings are created lazily on first use, but they still keep the uniqueness by being reused on further uses.
- for legacy derive helpers (helper attributes written before derives that introduce them) the declaration and the use is basically the same thing (that's one of the reasons why they are deprecated), so they are still created on use, but we can still maybe do a bit better in a way that I described in FIXME in the last commit.
  • Loading branch information
bors committed Aug 24, 2023
2 parents b60e31b + 453edeb commit 58eefc3
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 79 deletions.
17 changes: 9 additions & 8 deletions compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -870,10 +870,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
let imported_binding = self.r.import(binding, import);
if parent == self.r.graph_root {
if let Some(entry) = self.r.extern_prelude.get(&ident.normalize_to_macros_2_0()) {
if expansion != LocalExpnId::ROOT
&& orig_name.is_some()
&& entry.extern_crate_item.is_none()
{
if expansion != LocalExpnId::ROOT && orig_name.is_some() && !entry.is_import() {
let msg = "macro-expanded `extern crate` items cannot \
shadow names passed with `--extern`";
self.r.tcx.sess.span_err(item.span, msg);
Expand All @@ -884,10 +881,14 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
return;
}
}
let entry = self.r.extern_prelude.entry(ident.normalize_to_macros_2_0()).or_insert(
ExternPreludeEntry { extern_crate_item: None, introduced_by_item: true },
);
entry.extern_crate_item = Some(imported_binding);
let entry = self
.r
.extern_prelude
.entry(ident.normalize_to_macros_2_0())
.or_insert(ExternPreludeEntry { binding: None, introduced_by_item: true });
// Binding from `extern crate` item in source code can replace
// a binding from `--extern` on command line here.
entry.binding = Some(imported_binding);
if orig_name.is_some() {
entry.introduced_by_item = true;
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
.get(&expn_id)
.into_iter()
.flatten()
.map(|ident| TypoSuggestion::typo_from_ident(*ident, res)),
.map(|(ident, _)| TypoSuggestion::typo_from_ident(*ident, res)),
);
}
}
Expand Down
66 changes: 24 additions & 42 deletions compiler/rustc_resolve/src/ident.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use rustc_ast::{self as ast, NodeId};
use rustc_feature::is_builtin_attr_name;
use rustc_hir::def::{DefKind, Namespace, NonMacroAttrKind, PartialRes, PerNS};
use rustc_hir::PrimTy;
use rustc_middle::bug;
use rustc_middle::ty;
use rustc_session::lint::builtin::PROC_MACRO_DERIVE_RESOLUTION_FALLBACK;
use rustc_session::lint::BuiltinLintDiagnostics;
use rustc_span::def_id::LocalDefId;
use rustc_span::hygiene::{ExpnId, ExpnKind, LocalExpnId, MacroKind, SyntaxContext};
use rustc_span::symbol::{kw, Ident};
use rustc_span::{Span, DUMMY_SP};
use rustc_span::Span;

use crate::errors::{ParamKindInEnumDiscriminant, ParamKindInNonTrivialAnonConst};
use crate::late::{
Expand Down Expand Up @@ -423,32 +421,22 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
orig_ident.span.ctxt(),
|this, scope, use_prelude, ctxt| {
let ident = Ident::new(orig_ident.name, orig_ident.span.with_ctxt(ctxt));
let ok = |res, span, arenas| {
Ok((
(res, Visibility::Public, span, LocalExpnId::ROOT).to_name_binding(arenas),
Flags::empty(),
))
};
let result = match scope {
Scope::DeriveHelpers(expn_id) => {
if let Some(attr) = this
.helper_attrs
.get(&expn_id)
.and_then(|attrs| attrs.iter().rfind(|i| ident == **i))
{
let binding = (
Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper),
Visibility::Public,
attr.span,
expn_id,
)
.to_name_binding(this.arenas);
if let Some(binding) = this.helper_attrs.get(&expn_id).and_then(|attrs| {
attrs.iter().rfind(|(i, _)| ident == *i).map(|(_, binding)| *binding)
}) {
Ok((binding, Flags::empty()))
} else {
Err(Determinacy::Determined)
}
}
Scope::DeriveHelpersCompat => {
// FIXME: Try running this logic eariler, to allocate name bindings for
// legacy derive helpers when creating an attribute invocation with
// following derives. Legacy derive helpers are not common, so it shouldn't
// affect performance. It should also allow to remove the `derives`
// component from `ParentScope`.
let mut result = Err(Determinacy::Determined);
for derive in parent_scope.derives {
let parent_scope = &ParentScope { derives: &[], ..*parent_scope };
Expand All @@ -461,11 +449,14 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
) {
Ok((Some(ext), _)) => {
if ext.helper_attrs.contains(&ident.name) {
result = ok(
let binding = (
Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat),
Visibility::Public,
derive.span,
this.arenas,
);
LocalExpnId::ROOT,
)
.to_name_binding(this.arenas);
result = Ok((binding, Flags::empty()));
break;
}
}
Expand Down Expand Up @@ -562,17 +553,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
)),
}
}
Scope::BuiltinAttrs => {
if is_builtin_attr_name(ident.name) {
ok(
Res::NonMacroAttr(NonMacroAttrKind::Builtin(ident.name)),
DUMMY_SP,
this.arenas,
)
} else {
Err(Determinacy::Determined)
}
}
Scope::BuiltinAttrs => match this.builtin_attrs_bindings.get(&ident.name) {
Some(binding) => Ok((*binding, Flags::empty())),
None => Err(Determinacy::Determined),
},
Scope::ExternPrelude => {
match this.extern_prelude_get(ident, finalize.is_some()) {
Some(binding) => Ok((binding, Flags::empty())),
Expand All @@ -581,8 +565,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
)),
}
}
Scope::ToolPrelude => match this.registered_tools.get(&ident).cloned() {
Some(ident) => ok(Res::ToolMod, ident.span, this.arenas),
Scope::ToolPrelude => match this.registered_tool_bindings.get(&ident) {
Some(binding) => Ok((*binding, Flags::empty())),
None => Err(Determinacy::Determined),
},
Scope::StdLibPrelude => {
Expand All @@ -603,8 +587,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
}
result
}
Scope::BuiltinTypes => match PrimTy::from_name(ident.name) {
Some(prim_ty) => ok(Res::PrimTy(prim_ty), DUMMY_SP, this.arenas),
Scope::BuiltinTypes => match this.builtin_types_bindings.get(&ident.name) {
Some(binding) => Ok((*binding, Flags::empty())),
None => Err(Determinacy::Determined),
},
};
Expand Down Expand Up @@ -842,9 +826,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
if ns == TypeNS {
if ident.name == kw::Crate || ident.name == kw::DollarCrate {
let module = self.resolve_crate_root(ident);
let binding = (module, Visibility::Public, module.span, LocalExpnId::ROOT)
.to_name_binding(self.arenas);
return Ok(binding);
return Ok(self.module_self_bindings[&module]);
} else if ident.name == kw::Super || ident.name == kw::SelfLower {
// FIXME: Implement these with renaming requirements so that e.g.
// `use super;` doesn't work, but `use super as name;` does.
Expand Down
Loading

0 comments on commit 58eefc3

Please sign in to comment.