Skip to content

Commit 1b4fbfc

Browse files
committed
resolve: Support resolving macro paths without macro kind restrictions
1 parent a7726ce commit 1b4fbfc

File tree

4 files changed

+18
-17
lines changed

4 files changed

+18
-17
lines changed

src/librustc_resolve/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ impl<'a> Resolver<'a> {
574574
for derive in &parent_scope.derives {
575575
let parent_scope = ParentScope { derives: Vec::new(), ..*parent_scope };
576576
if let Ok((Some(ext), _)) = this.resolve_macro_path(
577-
derive, MacroKind::Derive, &parent_scope, false, false
577+
derive, Some(MacroKind::Derive), &parent_scope, false, false
578578
) {
579579
suggestions.extend(ext.helper_attrs.iter().map(|name| {
580580
TypoSuggestion::from_res(*name, res)

src/librustc_resolve/lib.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -3684,11 +3684,9 @@ impl<'a> Resolver<'a> {
36843684
let path = Path { segments: path.iter().map(path_seg).collect(), span };
36853685
let parent_scope =
36863686
ParentScope { module: self.current_module, ..self.dummy_parent_scope() };
3687-
for macro_kind in &[MacroKind::Bang, MacroKind::Attr, MacroKind::Derive] {
3688-
if let Ok((_, res)) = self.resolve_macro_path(&path, *macro_kind,
3689-
&parent_scope, false, false) {
3690-
return Some(PartialRes::new(res));
3691-
}
3687+
if let Ok((_, res)) =
3688+
self.resolve_macro_path(&path, None, &parent_scope, false, false) {
3689+
return Some(PartialRes::new(res));
36923690
}
36933691
}
36943692

src/librustc_resolve/macros.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ impl<'a> base::Resolver for Resolver<'a> {
220220
};
221221

222222
let parent_scope = self.invoc_parent_scope(invoc_id, derives_in_scope);
223-
let (ext, res) = self.smart_resolve_macro_path(path, kind, &parent_scope, true, force)?;
223+
let (ext, res) = self.smart_resolve_macro_path(path, kind, &parent_scope, force)?;
224224

225225
let span = invoc.span();
226226
invoc.expansion_data.mark.set_expn_info(ext.expn_info(span, fast_print_path(path)));
@@ -269,10 +269,10 @@ impl<'a> Resolver<'a> {
269269
path: &ast::Path,
270270
kind: MacroKind,
271271
parent_scope: &ParentScope<'a>,
272-
trace: bool,
273272
force: bool,
274273
) -> Result<(Lrc<SyntaxExtension>, Res), Indeterminate> {
275-
let (ext, res) = match self.resolve_macro_path(path, kind, parent_scope, trace, force) {
274+
let (ext, res) = match self.resolve_macro_path(path, Some(kind), parent_scope,
275+
true, force) {
276276
Ok((Some(ext), res)) => (ext, res),
277277
// Use dummy syntax extensions for unresolved macros for better recovery.
278278
Ok((None, res)) => (self.dummy_ext(kind), res),
@@ -334,7 +334,7 @@ impl<'a> Resolver<'a> {
334334
pub fn resolve_macro_path(
335335
&mut self,
336336
path: &ast::Path,
337-
kind: MacroKind,
337+
kind: Option<MacroKind>,
338338
parent_scope: &ParentScope<'a>,
339339
trace: bool,
340340
force: bool,
@@ -343,7 +343,7 @@ impl<'a> Resolver<'a> {
343343
let mut path = Segment::from_path(path);
344344

345345
// Possibly apply the macro helper hack
346-
if kind == MacroKind::Bang && path.len() == 1 &&
346+
if kind == Some(MacroKind::Bang) && path.len() == 1 &&
347347
path[0].ident.span.ctxt().outer_expn_info()
348348
.map_or(false, |info| info.local_inner_macros) {
349349
let root = Ident::new(kw::DollarCrate, path[0].ident.span);
@@ -364,21 +364,25 @@ impl<'a> Resolver<'a> {
364364
};
365365

366366
if trace {
367+
let kind = kind.expect("macro kind must be specified if tracing is enabled");
367368
parent_scope.module.multi_segment_macro_resolutions.borrow_mut()
368369
.push((path, path_span, kind, parent_scope.clone(), res.ok()));
369370
}
370371

371372
self.prohibit_imported_non_macro_attrs(None, res.ok(), path_span);
372373
res
373374
} else {
375+
// Macro without a specific kind restriction is equvalent to a macro import.
376+
let scope_set = kind.map_or(ScopeSet::Import(MacroNS), ScopeSet::Macro);
374377
let binding = self.early_resolve_ident_in_lexical_scope(
375-
path[0].ident, ScopeSet::Macro(kind), parent_scope, false, force, path_span
378+
path[0].ident, scope_set, parent_scope, false, force, path_span
376379
);
377380
if let Err(Determinacy::Undetermined) = binding {
378381
return Err(Determinacy::Undetermined);
379382
}
380383

381384
if trace {
385+
let kind = kind.expect("macro kind must be specified if tracing is enabled");
382386
parent_scope.module.single_segment_macro_resolutions.borrow_mut()
383387
.push((path[0].ident, kind, parent_scope.clone(), binding.ok()));
384388
}
@@ -452,7 +456,7 @@ impl<'a> Resolver<'a> {
452456
let mut result = Err(Determinacy::Determined);
453457
for derive in &parent_scope.derives {
454458
let parent_scope = ParentScope { derives: Vec::new(), ..*parent_scope };
455-
match this.resolve_macro_path(derive, MacroKind::Derive,
459+
match this.resolve_macro_path(derive, Some(MacroKind::Derive),
456460
&parent_scope, true, force) {
457461
Ok((Some(ext), _)) => if ext.helper_attrs.contains(&ident.name) {
458462
let binding = (Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper),

src/librustdoc/passes/collect_intra_doc_links.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc::lint as lint;
66
use rustc::ty;
77
use syntax;
88
use syntax::ast::{self, Ident};
9+
use syntax::ext::base::SyntaxExtensionKind;
910
use syntax::feature_gate::UnstableFeatures;
1011
use syntax::symbol::Symbol;
1112
use syntax_pos::DUMMY_SP;
@@ -425,12 +426,10 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
425426

426427
/// Resolves a string as a macro.
427428
fn macro_resolve(cx: &DocContext<'_>, path_str: &str) -> Option<Res> {
428-
use syntax::ext::base::{MacroKind, SyntaxExtensionKind};
429-
let segment = ast::PathSegment::from_ident(Ident::from_str(path_str));
430-
let path = ast::Path { segments: vec![segment], span: DUMMY_SP };
429+
let path = ast::Path::from_ident(Ident::from_str(path_str));
431430
cx.enter_resolver(|resolver| {
432431
if let Ok((Some(ext), res)) = resolver.resolve_macro_path(
433-
&path, MacroKind::Bang, &resolver.dummy_parent_scope(), false, false
432+
&path, None, &resolver.dummy_parent_scope(), false, false
434433
) {
435434
if let SyntaxExtensionKind::LegacyBang { .. } = ext.kind {
436435
return Some(res.map_id(|_| panic!("unexpected id")));

0 commit comments

Comments
 (0)