Skip to content

resolve: Stop creating NameBindings on every use, create them once per definition instead #113408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if binding is already Some?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a case where binding from command line (--extern foo) is replaced with a binding from extern crate foo item in source code.

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