Skip to content

Commit d902963

Browse files
committed
Refactor syntax::ext::base::Resolver::resolve_invoc.
1 parent d34318d commit d902963

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

src/librustc_resolve/macros.rs

+9-17
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use syntax::ast;
1919
use syntax::errors::DiagnosticBuilder;
2020
use syntax::ext::base::{self, Determinacy, MultiModifier, MultiDecorator, MultiItemModifier};
2121
use syntax::ext::base::{NormalTT, SyntaxExtension};
22-
use syntax::ext::expand::{Expansion, Invocation, InvocationKind};
22+
use syntax::ext::expand::Expansion;
2323
use syntax::ext::hygiene::Mark;
2424
use syntax::ext::tt::macro_rules;
2525
use syntax::parse::token::intern;
@@ -162,30 +162,22 @@ impl<'a> base::Resolver for Resolver<'a> {
162162
None
163163
}
164164

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

181173
let invocation = self.invocations[&scope];
182174
if let LegacyScope::Expansion(parent) = invocation.legacy_scope.get() {
183175
invocation.legacy_scope.set(LegacyScope::simplify_expansion(parent));
184176
}
185177
self.resolve_macro_name(invocation.legacy_scope.get(), name, true).ok_or_else(|| {
186178
if force {
187-
let mut err =
188-
self.session.struct_span_err(span, &format!("macro undefined: '{}!'", name));
179+
let msg = format!("macro undefined: '{}!'", name);
180+
let mut err = self.session.struct_span_err(path.span, &msg);
189181
self.suggest_macro_name(&name.as_str(), &mut err);
190182
err.emit();
191183
Determinacy::Determined

src/libsyntax/ext/base.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use attr::HasAttrs;
1515
use codemap::{self, CodeMap, ExpnInfo, Spanned, respan};
1616
use syntax_pos::{Span, ExpnId, NO_EXPANSION};
1717
use errors::DiagnosticBuilder;
18-
use ext::expand::{self, Invocation, Expansion};
18+
use ext::expand::{self, Expansion};
1919
use ext::hygiene::Mark;
2020
use fold::{self, Folder};
2121
use parse::{self, parser};
@@ -522,7 +522,7 @@ pub trait Resolver {
522522
fn add_expansions_at_stmt(&mut self, id: ast::NodeId, macros: Vec<Mark>);
523523

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

547547
fn find_attr_invoc(&mut self, _attrs: &mut Vec<Attribute>) -> Option<Attribute> { None }
548548
fn resolve_derive_mode(&mut self, _ident: ast::Ident) -> Option<Rc<MultiItemModifier>> { None }
549-
fn resolve_invoc(&mut self, _scope: Mark, _invoc: &Invocation, _force: bool)
549+
fn resolve_macro(&mut self, _scope: Mark, _path: &ast::Path, _force: bool)
550550
-> Result<Rc<SyntaxExtension>, Determinacy> {
551551
Err(Determinacy::Determined)
552552
}

src/libsyntax/ext/expand.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,17 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
240240

241241
let scope =
242242
if self.monotonic { invoc.expansion_data.mark } else { orig_expansion_data.mark };
243-
let ext = match self.cx.resolver.resolve_invoc(scope, &invoc, force) {
243+
let resolution = match invoc.kind {
244+
InvocationKind::Bang { ref mac, .. } => {
245+
self.cx.resolver.resolve_macro(scope, &mac.node.path, force)
246+
}
247+
InvocationKind::Attr { ref attr, .. } => {
248+
let ident = ast::Ident::with_empty_ctxt(intern(&*attr.name()));
249+
let path = ast::Path::from_ident(attr.span, ident);
250+
self.cx.resolver.resolve_macro(scope, &path, force)
251+
}
252+
};
253+
let ext = match resolution {
244254
Ok(ext) => Some(ext),
245255
Err(Determinacy::Determined) => None,
246256
Err(Determinacy::Undetermined) => {

0 commit comments

Comments
 (0)