Skip to content

Commit 994cdd9

Browse files
committed
Auto merge of #54086 - petrochenkov:derhelp, r=alexcrichton
resolve: Future proof derive helper attributes Derive helpers no longer require going through recovery mode (fixes #53481). They also report an error if they are ambiguous with any other macro in scope, so we can resolve the question about their exact priority sometime later (cc #52226).
2 parents f2302da + 2b3e98f commit 994cdd9

File tree

11 files changed

+221
-106
lines changed

11 files changed

+221
-106
lines changed

src/librustc_resolve/build_reduced_graph.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//! Here we build the "reduced graph": the graph of the module tree without
1414
//! any imports resolved.
1515
16-
use macros::{InvocationData, LegacyScope};
16+
use macros::{InvocationData, ParentScope, LegacyScope};
1717
use resolve_imports::ImportDirective;
1818
use resolve_imports::ImportDirectiveSubclass::{self, GlobImport, SingleImport};
1919
use {Module, ModuleData, ModuleKind, NameBinding, NameBindingKind, ToNameBinding};
@@ -1061,8 +1061,15 @@ impl<'a, 'b, 'cl> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b, 'cl> {
10611061

10621062
fn visit_attribute(&mut self, attr: &'a ast::Attribute) {
10631063
if !attr.is_sugared_doc && is_builtin_attr(attr) {
1064-
self.resolver.current_module.builtin_attrs.borrow_mut().push((
1065-
attr.path.segments[0].ident, self.expansion, self.current_legacy_scope
1064+
let parent_scope = ParentScope {
1065+
module: self.resolver.current_module.nearest_item_scope(),
1066+
expansion: self.expansion,
1067+
legacy: self.current_legacy_scope,
1068+
// Let's hope discerning built-in attributes from derive helpers is not necessary
1069+
derives: Vec::new(),
1070+
};
1071+
parent_scope.module.builtin_attrs.borrow_mut().push((
1072+
attr.path.segments[0].ident, parent_scope
10661073
));
10671074
}
10681075
visit::walk_attribute(self, attr);

src/librustc_resolve/lib.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ use std::mem::replace;
8080
use rustc_data_structures::sync::Lrc;
8181

8282
use resolve_imports::{ImportDirective, ImportDirectiveSubclass, NameResolution, ImportResolver};
83-
use macros::{InvocationData, LegacyBinding, LegacyScope};
83+
use macros::{InvocationData, LegacyBinding, ParentScope};
8484

8585
// NB: This module needs to be declared first so diagnostics are
8686
// registered before they are used.
@@ -1009,9 +1009,9 @@ pub struct ModuleData<'a> {
10091009
normal_ancestor_id: DefId,
10101010

10111011
resolutions: RefCell<FxHashMap<(Ident, Namespace), &'a RefCell<NameResolution<'a>>>>,
1012-
legacy_macro_resolutions: RefCell<Vec<(Ident, MacroKind, Mark, LegacyScope<'a>, Option<Def>)>>,
1012+
legacy_macro_resolutions: RefCell<Vec<(Ident, MacroKind, ParentScope<'a>, Option<Def>)>>,
10131013
macro_resolutions: RefCell<Vec<(Box<[Ident]>, Span)>>,
1014-
builtin_attrs: RefCell<Vec<(Ident, Mark, LegacyScope<'a>)>>,
1014+
builtin_attrs: RefCell<Vec<(Ident, ParentScope<'a>)>>,
10151015

10161016
// Macro invocations that can expand into items in this module.
10171017
unresolved_invocations: RefCell<FxHashSet<Mark>>,
@@ -3494,23 +3494,25 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
34943494
path_span: Span,
34953495
crate_lint: CrateLint,
34963496
) -> PathResult<'a> {
3497-
self.resolve_path_with_parent_expansion(base_module, path, opt_ns, Mark::root(),
3498-
record_used, path_span, crate_lint)
3497+
let parent_scope = ParentScope { module: self.current_module, ..self.dummy_parent_scope() };
3498+
self.resolve_path_with_parent_scope(base_module, path, opt_ns, &parent_scope,
3499+
record_used, path_span, crate_lint)
34993500
}
35003501

3501-
fn resolve_path_with_parent_expansion(
3502+
fn resolve_path_with_parent_scope(
35023503
&mut self,
35033504
base_module: Option<ModuleOrUniformRoot<'a>>,
35043505
path: &[Ident],
35053506
opt_ns: Option<Namespace>, // `None` indicates a module path
3506-
parent_expansion: Mark,
3507+
parent_scope: &ParentScope<'a>,
35073508
record_used: bool,
35083509
path_span: Span,
35093510
crate_lint: CrateLint,
35103511
) -> PathResult<'a> {
35113512
let mut module = base_module;
35123513
let mut allow_super = true;
35133514
let mut second_binding = None;
3515+
self.current_module = parent_scope.module;
35143516

35153517
debug!(
35163518
"resolve_path(path={:?}, opt_ns={:?}, record_used={:?}, \
@@ -3596,9 +3598,8 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
35963598
self.resolve_ident_in_module(module, ident, ns, record_used, path_span)
35973599
} else if opt_ns == Some(MacroNS) {
35983600
assert!(ns == TypeNS);
3599-
self.resolve_lexical_macro_path_segment(ident, ns, None, parent_expansion,
3600-
record_used, record_used, path_span)
3601-
.map(|(binding, _)| binding)
3601+
self.resolve_lexical_macro_path_segment(ident, ns, None, parent_scope, record_used,
3602+
record_used, path_span).map(|(b, _)| b)
36023603
} else {
36033604
let record_used_id =
36043605
if record_used { crate_lint.node_id().or(Some(CRATE_NODE_ID)) } else { None };

0 commit comments

Comments
 (0)