Skip to content

Commit

Permalink
Refactor syntax::ext::base::Resolver::resolve_invoc.
Browse files Browse the repository at this point in the history
  • Loading branch information
jseyfried committed Oct 15, 2016
1 parent d34318d commit d902963
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
26 changes: 9 additions & 17 deletions src/librustc_resolve/macros.rs
Expand Up @@ -19,7 +19,7 @@ use syntax::ast;
use syntax::errors::DiagnosticBuilder;
use syntax::ext::base::{self, Determinacy, MultiModifier, MultiDecorator, MultiItemModifier};
use syntax::ext::base::{NormalTT, SyntaxExtension};
use syntax::ext::expand::{Expansion, Invocation, InvocationKind};
use syntax::ext::expand::Expansion;
use syntax::ext::hygiene::Mark;
use syntax::ext::tt::macro_rules;
use syntax::parse::token::intern;
Expand Down Expand Up @@ -162,30 +162,22 @@ impl<'a> base::Resolver for Resolver<'a> {
None
}

fn resolve_invoc(&mut self, scope: Mark, invoc: &Invocation, force: bool)
fn resolve_macro(&mut self, scope: Mark, path: &ast::Path, force: bool)
-> Result<Rc<SyntaxExtension>, Determinacy> {
let (name, span) = match invoc.kind {
InvocationKind::Bang { ref mac, .. } => {
let path = &mac.node.path;
if path.segments.len() > 1 || path.global ||
!path.segments[0].parameters.is_empty() {
self.session.span_err(path.span,
"expected macro name without module separators");
return Err(Determinacy::Determined);
}
(path.segments[0].identifier.name, path.span)
}
InvocationKind::Attr { ref attr, .. } => (intern(&*attr.name()), attr.span),
};
if path.segments.len() > 1 || path.global || !path.segments[0].parameters.is_empty() {
self.session.span_err(path.span, "expected macro name without module separators");
return Err(Determinacy::Determined);
}
let name = path.segments[0].identifier.name;

let invocation = self.invocations[&scope];
if let LegacyScope::Expansion(parent) = invocation.legacy_scope.get() {
invocation.legacy_scope.set(LegacyScope::simplify_expansion(parent));
}
self.resolve_macro_name(invocation.legacy_scope.get(), name, true).ok_or_else(|| {
if force {
let mut err =
self.session.struct_span_err(span, &format!("macro undefined: '{}!'", name));
let msg = format!("macro undefined: '{}!'", name);
let mut err = self.session.struct_span_err(path.span, &msg);
self.suggest_macro_name(&name.as_str(), &mut err);
err.emit();
Determinacy::Determined
Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax/ext/base.rs
Expand Up @@ -15,7 +15,7 @@ use attr::HasAttrs;
use codemap::{self, CodeMap, ExpnInfo, Spanned, respan};
use syntax_pos::{Span, ExpnId, NO_EXPANSION};
use errors::DiagnosticBuilder;
use ext::expand::{self, Invocation, Expansion};
use ext::expand::{self, Expansion};
use ext::hygiene::Mark;
use fold::{self, Folder};
use parse::{self, parser};
Expand Down Expand Up @@ -522,7 +522,7 @@ pub trait Resolver {
fn add_expansions_at_stmt(&mut self, id: ast::NodeId, macros: Vec<Mark>);

fn find_attr_invoc(&mut self, attrs: &mut Vec<Attribute>) -> Option<Attribute>;
fn resolve_invoc(&mut self, scope: Mark, invoc: &Invocation, force: bool)
fn resolve_macro(&mut self, scope: Mark, path: &ast::Path, force: bool)
-> Result<Rc<SyntaxExtension>, Determinacy>;
fn resolve_derive_mode(&mut self, ident: ast::Ident) -> Option<Rc<MultiItemModifier>>;
}
Expand All @@ -546,7 +546,7 @@ impl Resolver for DummyResolver {

fn find_attr_invoc(&mut self, _attrs: &mut Vec<Attribute>) -> Option<Attribute> { None }
fn resolve_derive_mode(&mut self, _ident: ast::Ident) -> Option<Rc<MultiItemModifier>> { None }
fn resolve_invoc(&mut self, _scope: Mark, _invoc: &Invocation, _force: bool)
fn resolve_macro(&mut self, _scope: Mark, _path: &ast::Path, _force: bool)
-> Result<Rc<SyntaxExtension>, Determinacy> {
Err(Determinacy::Determined)
}
Expand Down
12 changes: 11 additions & 1 deletion src/libsyntax/ext/expand.rs
Expand Up @@ -240,7 +240,17 @@ impl<'a, 'b> MacroExpander<'a, 'b> {

let scope =
if self.monotonic { invoc.expansion_data.mark } else { orig_expansion_data.mark };
let ext = match self.cx.resolver.resolve_invoc(scope, &invoc, force) {
let resolution = match invoc.kind {
InvocationKind::Bang { ref mac, .. } => {
self.cx.resolver.resolve_macro(scope, &mac.node.path, force)
}
InvocationKind::Attr { ref attr, .. } => {
let ident = ast::Ident::with_empty_ctxt(intern(&*attr.name()));
let path = ast::Path::from_ident(attr.span, ident);
self.cx.resolver.resolve_macro(scope, &path, force)
}
};
let ext = match resolution {
Ok(ext) => Some(ext),
Err(Determinacy::Determined) => None,
Err(Determinacy::Undetermined) => {
Expand Down

0 comments on commit d902963

Please sign in to comment.