Skip to content

Commit

Permalink
Auto merge of #54086 - petrochenkov:derhelp, r=alexcrichton
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
bors committed Sep 13, 2018
2 parents f2302da + 2b3e98f commit 994cdd9
Show file tree
Hide file tree
Showing 11 changed files with 221 additions and 106 deletions.
13 changes: 10 additions & 3 deletions src/librustc_resolve/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//! Here we build the "reduced graph": the graph of the module tree without
//! any imports resolved.

use macros::{InvocationData, LegacyScope};
use macros::{InvocationData, ParentScope, LegacyScope};
use resolve_imports::ImportDirective;
use resolve_imports::ImportDirectiveSubclass::{self, GlobImport, SingleImport};
use {Module, ModuleData, ModuleKind, NameBinding, NameBindingKind, ToNameBinding};
Expand Down Expand Up @@ -1061,8 +1061,15 @@ impl<'a, 'b, 'cl> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b, 'cl> {

fn visit_attribute(&mut self, attr: &'a ast::Attribute) {
if !attr.is_sugared_doc && is_builtin_attr(attr) {
self.resolver.current_module.builtin_attrs.borrow_mut().push((
attr.path.segments[0].ident, self.expansion, self.current_legacy_scope
let parent_scope = ParentScope {
module: self.resolver.current_module.nearest_item_scope(),
expansion: self.expansion,
legacy: self.current_legacy_scope,
// Let's hope discerning built-in attributes from derive helpers is not necessary
derives: Vec::new(),
};
parent_scope.module.builtin_attrs.borrow_mut().push((
attr.path.segments[0].ident, parent_scope
));
}
visit::walk_attribute(self, attr);
Expand Down
21 changes: 11 additions & 10 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ use std::mem::replace;
use rustc_data_structures::sync::Lrc;

use resolve_imports::{ImportDirective, ImportDirectiveSubclass, NameResolution, ImportResolver};
use macros::{InvocationData, LegacyBinding, LegacyScope};
use macros::{InvocationData, LegacyBinding, ParentScope};

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

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

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

fn resolve_path_with_parent_expansion(
fn resolve_path_with_parent_scope(
&mut self,
base_module: Option<ModuleOrUniformRoot<'a>>,
path: &[Ident],
opt_ns: Option<Namespace>, // `None` indicates a module path
parent_expansion: Mark,
parent_scope: &ParentScope<'a>,
record_used: bool,
path_span: Span,
crate_lint: CrateLint,
) -> PathResult<'a> {
let mut module = base_module;
let mut allow_super = true;
let mut second_binding = None;
self.current_module = parent_scope.module;

debug!(
"resolve_path(path={:?}, opt_ns={:?}, record_used={:?}, \
Expand Down Expand Up @@ -3596,9 +3598,8 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
self.resolve_ident_in_module(module, ident, ns, record_used, path_span)
} else if opt_ns == Some(MacroNS) {
assert!(ns == TypeNS);
self.resolve_lexical_macro_path_segment(ident, ns, None, parent_expansion,
record_used, record_used, path_span)
.map(|(binding, _)| binding)
self.resolve_lexical_macro_path_segment(ident, ns, None, parent_scope, record_used,
record_used, path_span).map(|(b, _)| b)
} else {
let record_used_id =
if record_used { crate_lint.node_id().or(Some(CRATE_NODE_ID)) } else { None };
Expand Down
Loading

0 comments on commit 994cdd9

Please sign in to comment.