Remove MacroKind::ProcMacroStub

It's internal to resolve and always results in `Res::Err` outside of resolve.
Instead put `DefKind::Fn`s themselves into the macro namespace, it's ok.

Proc macro stubs are items placed into macro namespase for functions that define proc macros.
https://github.com/rust-lang/rust/pull/52383

The rustdoc test is changed because the old test didn't actually reproduce the ICE it was supposed to reproduce.
This commit is contained in:
Vadim Petrochenkov 2019-06-18 22:23:13 +03:00
parent c6a9e766f9
commit 48635226d8
10 changed files with 35 additions and 53 deletions

View File

@ -89,7 +89,6 @@ impl_stable_hash_for!(enum ::syntax::ext::base::MacroKind {
Bang, Bang,
Attr, Attr,
Derive, Derive,
ProcMacroStub,
}); });

View File

@ -29,7 +29,7 @@ use syntax::attr;
use syntax::ast::{self, Block, ForeignItem, ForeignItemKind, Item, ItemKind, NodeId}; use syntax::ast::{self, Block, ForeignItem, ForeignItemKind, Item, ItemKind, NodeId};
use syntax::ast::{MetaItemKind, StmtKind, TraitItem, TraitItemKind, Variant}; use syntax::ast::{MetaItemKind, StmtKind, TraitItem, TraitItemKind, Variant};
use syntax::ext::base::{MacroKind, SyntaxExtension}; use syntax::ext::base::SyntaxExtension;
use syntax::ext::base::Determinacy::Undetermined; use syntax::ext::base::Determinacy::Undetermined;
use syntax::ext::hygiene::Mark; use syntax::ext::hygiene::Mark;
use syntax::ext::tt::macro_rules; use syntax::ext::tt::macro_rules;
@ -46,6 +46,20 @@ use log::debug;
type Res = def::Res<NodeId>; type Res = def::Res<NodeId>;
fn proc_macro_stub(item: &Item) -> Option<(Ident, Span)> {
if attr::contains_name(&item.attrs, sym::proc_macro) ||
attr::contains_name(&item.attrs, sym::proc_macro_attribute) {
return Some((item.ident, item.span));
} else if let Some(attr) = attr::find_by_name(&item.attrs, sym::proc_macro_derive) {
if let Some(nested_meta) = attr.meta_item_list().and_then(|list| list.get(0).cloned()) {
if let Some(ident) = nested_meta.ident() {
return Some((ident, ident.span));
}
}
}
None
}
impl<'a> ToNameBinding<'a> for (Module<'a>, ty::Visibility, Span, Mark) { impl<'a> ToNameBinding<'a> for (Module<'a>, ty::Visibility, Span, Mark) {
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> { fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
arenas.alloc_name_binding(NameBinding { arenas.alloc_name_binding(NameBinding {
@ -456,22 +470,8 @@ impl<'a> Resolver<'a> {
// Functions introducing procedural macros reserve a slot // Functions introducing procedural macros reserve a slot
// in the macro namespace as well (see #52225). // in the macro namespace as well (see #52225).
if attr::contains_name(&item.attrs, sym::proc_macro) || if let Some((ident, span)) = proc_macro_stub(item) {
attr::contains_name(&item.attrs, sym::proc_macro_attribute) { self.define(parent, ident, MacroNS, (res, vis, span, expansion));
let res = Res::Def(DefKind::Macro(MacroKind::ProcMacroStub), res.def_id());
self.define(parent, ident, MacroNS, (res, vis, sp, expansion));
}
if let Some(attr) = attr::find_by_name(&item.attrs, sym::proc_macro_derive) {
if let Some(trait_attr) =
attr.meta_item_list().and_then(|list| list.get(0).cloned()) {
if let Some(ident) = trait_attr.ident() {
let res = Res::Def(
DefKind::Macro(MacroKind::ProcMacroStub),
res.def_id(),
);
self.define(parent, ident, MacroNS, (res, vis, ident.span, expansion));
}
}
} }
} }
@ -778,8 +778,6 @@ impl<'a> Resolver<'a> {
crate fn opt_get_macro(&mut self, res: Res) -> Option<Lrc<SyntaxExtension>> { crate fn opt_get_macro(&mut self, res: Res) -> Option<Lrc<SyntaxExtension>> {
let def_id = match res { let def_id = match res {
Res::Def(DefKind::Macro(MacroKind::ProcMacroStub), _) =>
return Some(self.non_macro_attr(true)), // some dummy extension
Res::Def(DefKind::Macro(..), def_id) => def_id, Res::Def(DefKind::Macro(..), def_id) => def_id,
Res::NonMacroAttr(attr_kind) => Res::NonMacroAttr(attr_kind) =>
return Some(self.non_macro_attr(attr_kind == NonMacroAttrKind::Tool)), return Some(self.non_macro_attr(attr_kind == NonMacroAttrKind::Tool)),

View File

@ -102,12 +102,11 @@ fn sub_namespace_match(candidate: Option<MacroKind>, requirement: Option<MacroKi
#[derive(PartialEq)] #[derive(PartialEq)]
enum SubNS { Bang, AttrLike } enum SubNS { Bang, AttrLike }
let sub_ns = |kind| match kind { let sub_ns = |kind| match kind {
MacroKind::Bang => Some(SubNS::Bang), MacroKind::Bang => SubNS::Bang,
MacroKind::Attr | MacroKind::Derive => Some(SubNS::AttrLike), MacroKind::Attr | MacroKind::Derive => SubNS::AttrLike,
MacroKind::ProcMacroStub => None,
}; };
let requirement = requirement.and_then(|kind| sub_ns(kind)); let candidate = candidate.map(sub_ns);
let candidate = candidate.and_then(|kind| sub_ns(kind)); let requirement = requirement.map(sub_ns);
// "No specific sub-namespace" means "matches anything" for both requirements and candidates. // "No specific sub-namespace" means "matches anything" for both requirements and candidates.
candidate.is_none() || requirement.is_none() || candidate == requirement candidate.is_none() || requirement.is_none() || candidate == requirement
} }
@ -310,15 +309,15 @@ impl<'a> Resolver<'a> {
let res = res?; let res = res?;
match res { match res {
Res::Def(DefKind::Macro(macro_kind), def_id) => { Res::Def(DefKind::Macro(_), def_id) => {
if let Some(node_id) = self.definitions.as_local_node_id(def_id) { if let Some(node_id) = self.definitions.as_local_node_id(def_id) {
self.unused_macros.remove(&node_id); self.unused_macros.remove(&node_id);
} }
if macro_kind == MacroKind::ProcMacroStub { }
let msg = "can't use a procedural macro from the same crate that defines it"; Res::Def(DefKind::Fn, _) => {
self.session.span_err(path.span, msg); let msg = "can't use a procedural macro from the same crate that defines it";
return Err(Determinacy::Determined); self.session.span_err(path.span, msg);
} return Err(Determinacy::Determined);
} }
Res::NonMacroAttr(attr_kind) => { Res::NonMacroAttr(attr_kind) => {
if kind == MacroKind::Attr { if kind == MacroKind::Attr {

View File

@ -4199,7 +4199,6 @@ pub fn register_res(cx: &DocContext<'_>, res: Res) -> DefId {
MacroKind::Bang => (i, TypeKind::Macro), MacroKind::Bang => (i, TypeKind::Macro),
MacroKind::Attr => (i, TypeKind::Attr), MacroKind::Attr => (i, TypeKind::Attr),
MacroKind::Derive => (i, TypeKind::Derive), MacroKind::Derive => (i, TypeKind::Derive),
MacroKind::ProcMacroStub => unreachable!(),
}, },
Res::Def(DefKind::TraitAlias, i) => (i, TypeKind::TraitAlias), Res::Def(DefKind::TraitAlias, i) => (i, TypeKind::TraitAlias),
Res::SelfTy(Some(def_id), _) => (def_id, TypeKind::Trait), Res::SelfTy(Some(def_id), _) => (def_id, TypeKind::Trait),

View File

@ -92,7 +92,6 @@ impl<'a> From<&'a clean::Item> for ItemType {
MacroKind::Bang => ItemType::Macro, MacroKind::Bang => ItemType::Macro,
MacroKind::Attr => ItemType::ProcAttribute, MacroKind::Attr => ItemType::ProcAttribute,
MacroKind::Derive => ItemType::ProcDerive, MacroKind::Derive => ItemType::ProcDerive,
MacroKind::ProcMacroStub => unreachable!(),
} }
clean::StrippedItem(..) => unreachable!(), clean::StrippedItem(..) => unreachable!(),
} }

View File

@ -2471,7 +2471,6 @@ impl<'a> fmt::Display for Item<'a> {
MacroKind::Bang => write!(fmt, "Macro ")?, MacroKind::Bang => write!(fmt, "Macro ")?,
MacroKind::Attr => write!(fmt, "Attribute Macro ")?, MacroKind::Attr => write!(fmt, "Attribute Macro ")?,
MacroKind::Derive => write!(fmt, "Derive Macro ")?, MacroKind::Derive => write!(fmt, "Derive Macro ")?,
MacroKind::ProcMacroStub => unreachable!(),
} }
clean::PrimitiveItem(..) => write!(fmt, "Primitive Type ")?, clean::PrimitiveItem(..) => write!(fmt, "Primitive Type ")?,
clean::StaticItem(..) | clean::ForeignStaticItem(..) => write!(fmt, "Static ")?, clean::StaticItem(..) | clean::ForeignStaticItem(..) => write!(fmt, "Static ")?,
@ -5092,7 +5091,6 @@ fn item_proc_macro(w: &mut fmt::Formatter<'_>, cx: &Context, it: &clean::Item, m
} }
write!(w, "</pre>")?; write!(w, "</pre>")?;
} }
_ => {}
} }
document(w, cx, it) document(w, cx, it)
} }

View File

@ -429,15 +429,11 @@ fn macro_resolve(cx: &DocContext<'_>, path_str: &str) -> Option<Res> {
let segment = ast::PathSegment::from_ident(Ident::from_str(path_str)); let segment = ast::PathSegment::from_ident(Ident::from_str(path_str));
let path = ast::Path { segments: vec![segment], span: DUMMY_SP }; let path = ast::Path { segments: vec![segment], span: DUMMY_SP };
cx.enter_resolver(|resolver| { cx.enter_resolver(|resolver| {
let parent_scope = resolver.dummy_parent_scope(); if let Ok(res @ Res::Def(DefKind::Macro(_), _)) = resolver.resolve_macro_to_res_inner(
if let Ok(res) = resolver.resolve_macro_to_res_inner(&path, MacroKind::Bang, &path, MacroKind::Bang, &resolver.dummy_parent_scope(), false, false
&parent_scope, false, false) { ) {
if let Res::Def(DefKind::Macro(MacroKind::ProcMacroStub), _) = res { if let SyntaxExtensionKind::LegacyBang { .. } = resolver.get_macro(res).kind {
// skip proc-macro stubs, they'll cause `get_macro` to crash return Some(res.map_id(|_| panic!("unexpected id")));
} else {
if let SyntaxExtensionKind::LegacyBang(..) = resolver.get_macro(res).kind {
return Some(res.map_id(|_| panic!("unexpected id")));
}
} }
} }
if let Some(res) = resolver.all_macros.get(&Symbol::intern(path_str)) { if let Some(res) = resolver.all_macros.get(&Symbol::intern(path_str)) {

View File

@ -406,11 +406,8 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
// Struct and variant constructors and proc macro stubs always show up alongside // Struct and variant constructors and proc macro stubs always show up alongside
// their definitions, we've already processed them so just discard these. // their definitions, we've already processed them so just discard these.
match path.res { if let Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) = path.res {
Res::Def(DefKind::Ctor(..), _) return;
| Res::SelfCtor(..)
| Res::Def(DefKind::Macro(MacroKind::ProcMacroStub), _) => return,
_ => {}
} }
// If there was a private module in the current path then don't bother inlining // If there was a private module in the current path then don't bother inlining

View File

@ -527,8 +527,6 @@ pub enum MacroKind {
Attr, Attr,
/// A derive attribute macro - #[derive(Foo)] /// A derive attribute macro - #[derive(Foo)]
Derive, Derive,
/// A view of a procedural macro from the same crate that defines it.
ProcMacroStub,
} }
impl MacroKind { impl MacroKind {
@ -537,7 +535,6 @@ impl MacroKind {
MacroKind::Bang => "macro", MacroKind::Bang => "macro",
MacroKind::Attr => "attribute macro", MacroKind::Attr => "attribute macro",
MacroKind::Derive => "derive macro", MacroKind::Derive => "derive macro",
MacroKind::ProcMacroStub => "crate-local procedural macro",
} }
} }

View File

@ -7,7 +7,7 @@
// @has some_macros/index.html // @has some_macros/index.html
// @has - '//a/[@href="attr.some_proc_attr.html"]' 'some_proc_attr' // @has - '//a/[@href="attr.some_proc_attr.html"]' 'some_proc_attr'
//! include a link to [some_proc_attr] to make sure it works. //! include a link to [some_proc_macro] to make sure it works.
extern crate proc_macro; extern crate proc_macro;