Skip to content

Commit 50bbe01

Browse files
committed
resolve: Make bindings for derive helper attributes unique
instead of creating them every time such attribute is used
1 parent 02640f9 commit 50bbe01

File tree

4 files changed

+25
-28
lines changed

4 files changed

+25
-28
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
10321032
.get(&expn_id)
10331033
.into_iter()
10341034
.flatten()
1035-
.map(|ident| TypoSuggestion::typo_from_ident(*ident, res)),
1035+
.map(|(ident, _)| TypoSuggestion::typo_from_ident(*ident, res)),
10361036
);
10371037
}
10381038
}

compiler/rustc_resolve/src/ident.rs

+9-21
Original file line numberDiff line numberDiff line change
@@ -421,26 +421,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
421421
orig_ident.span.ctxt(),
422422
|this, scope, use_prelude, ctxt| {
423423
let ident = Ident::new(orig_ident.name, orig_ident.span.with_ctxt(ctxt));
424-
let ok = |res, span, arenas| {
425-
Ok((
426-
(res, Visibility::Public, span, LocalExpnId::ROOT).to_name_binding(arenas),
427-
Flags::empty(),
428-
))
429-
};
430424
let result = match scope {
431425
Scope::DeriveHelpers(expn_id) => {
432-
if let Some(attr) = this
433-
.helper_attrs
434-
.get(&expn_id)
435-
.and_then(|attrs| attrs.iter().rfind(|i| ident == **i))
436-
{
437-
let binding = (
438-
Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper),
439-
Visibility::Public,
440-
attr.span,
441-
expn_id,
442-
)
443-
.to_name_binding(this.arenas);
426+
if let Some(binding) = this.helper_attrs.get(&expn_id).and_then(|attrs| {
427+
attrs.iter().rfind(|(i, _)| ident == *i).map(|(_, binding)| *binding)
428+
}) {
444429
Ok((binding, Flags::empty()))
445430
} else {
446431
Err(Determinacy::Determined)
@@ -459,11 +444,14 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
459444
) {
460445
Ok((Some(ext), _)) => {
461446
if ext.helper_attrs.contains(&ident.name) {
462-
result = ok(
447+
let binding = (
463448
Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat),
449+
Visibility::Public,
464450
derive.span,
465-
this.arenas,
466-
);
451+
LocalExpnId::ROOT,
452+
)
453+
.to_name_binding(this.arenas);
454+
result = Ok((binding, Flags::empty()));
467455
break;
468456
}
469457
}

compiler/rustc_resolve/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ pub struct Resolver<'a, 'tcx> {
10471047
/// `macro_rules` scopes produced by `macro_rules` item definitions.
10481048
macro_rules_scopes: FxHashMap<LocalDefId, MacroRulesScopeRef<'a>>,
10491049
/// Helper attributes that are in scope for the given expansion.
1050-
helper_attrs: FxHashMap<LocalExpnId, Vec<Ident>>,
1050+
helper_attrs: FxHashMap<LocalExpnId, Vec<(Ident, NameBinding<'a>)>>,
10511051
/// Ready or in-progress results of resolving paths inside the `#[derive(...)]` attribute
10521052
/// with the given `ExpnId`.
10531053
derive_data: FxHashMap<LocalExpnId, DeriveData>,

compiler/rustc_resolve/src/macros.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::errors::{
77
use crate::Namespace::*;
88
use crate::{BuiltinMacroState, Determinacy};
99
use crate::{DeriveData, Finalize, ParentScope, ResolutionError, Resolver, ScopeSet};
10-
use crate::{ModuleKind, ModuleOrUniformRoot, NameBinding, PathResult, Segment};
10+
use crate::{ModuleKind, ModuleOrUniformRoot, NameBinding, PathResult, Segment, ToNameBinding};
1111
use rustc_ast::expand::StrippedCfgItem;
1212
use rustc_ast::{self as ast, attr, Crate, Inline, ItemKind, ModKind, NodeId};
1313
use rustc_ast_pretty::pprust;
@@ -20,10 +20,10 @@ use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind};
2020
use rustc_expand::compile_declarative_macro;
2121
use rustc_expand::expand::{AstFragment, Invocation, InvocationKind, SupportsMacroExpansion};
2222
use rustc_hir::def::{self, DefKind, NonMacroAttrKind};
23-
use rustc_hir::def_id::{CrateNum, LocalDefId};
23+
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
2424
use rustc_middle::middle::stability;
2525
use rustc_middle::ty::RegisteredTools;
26-
use rustc_middle::ty::TyCtxt;
26+
use rustc_middle::ty::{TyCtxt, Visibility};
2727
use rustc_session::lint::builtin::{
2828
LEGACY_DERIVE_HELPERS, SOFT_UNSTABLE, UNKNOWN_DIAGNOSTIC_ATTRIBUTES,
2929
};
@@ -401,8 +401,17 @@ impl<'a, 'tcx> ResolverExpand for Resolver<'a, 'tcx> {
401401
}
402402
// Sort helpers in a stable way independent from the derive resolution order.
403403
entry.helper_attrs.sort_by_key(|(i, _)| *i);
404-
self.helper_attrs
405-
.insert(expn_id, entry.helper_attrs.iter().map(|(_, ident)| *ident).collect());
404+
let helper_attrs = entry
405+
.helper_attrs
406+
.iter()
407+
.map(|(_, ident)| {
408+
let res = Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper);
409+
let binding = (res, Visibility::<DefId>::Public, ident.span, expn_id)
410+
.to_name_binding(self.arenas);
411+
(*ident, binding)
412+
})
413+
.collect();
414+
self.helper_attrs.insert(expn_id, helper_attrs);
406415
// Mark this derive as having `Copy` either if it has `Copy` itself or if its parent derive
407416
// has `Copy`, to support cases like `#[derive(Clone, Copy)] #[derive(Debug)]`.
408417
if entry.has_derive_copy || self.has_derive_copy(parent_scope.expansion) {

0 commit comments

Comments
 (0)