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,
Attr,
Derive,
ProcMacroStub,
});

View file

@ -29,7 +29,7 @@ use syntax::attr;
use syntax::ast::{self, Block, ForeignItem, ForeignItemKind, Item, ItemKind, NodeId};
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::hygiene::Mark;
use syntax::ext::tt::macro_rules;
@ -46,6 +46,20 @@ use log::debug;
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) {
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
arenas.alloc_name_binding(NameBinding {
@ -456,22 +470,8 @@ impl<'a> Resolver<'a> {
// Functions introducing procedural macros reserve a slot
// in the macro namespace as well (see #52225).
if attr::contains_name(&item.attrs, sym::proc_macro) ||
attr::contains_name(&item.attrs, sym::proc_macro_attribute) {
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));
}
}
if let Some((ident, span)) = proc_macro_stub(item) {
self.define(parent, ident, MacroNS, (res, vis, span, expansion));
}
}
@ -778,8 +778,6 @@ impl<'a> Resolver<'a> {
crate fn opt_get_macro(&mut self, res: Res) -> Option<Lrc<SyntaxExtension>> {
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::NonMacroAttr(attr_kind) =>
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)]
enum SubNS { Bang, AttrLike }
let sub_ns = |kind| match kind {
MacroKind::Bang => Some(SubNS::Bang),
MacroKind::Attr | MacroKind::Derive => Some(SubNS::AttrLike),
MacroKind::ProcMacroStub => None,
MacroKind::Bang => SubNS::Bang,
MacroKind::Attr | MacroKind::Derive => SubNS::AttrLike,
};
let requirement = requirement.and_then(|kind| sub_ns(kind));
let candidate = candidate.and_then(|kind| sub_ns(kind));
let candidate = candidate.map(sub_ns);
let requirement = requirement.map(sub_ns);
// "No specific sub-namespace" means "matches anything" for both requirements and candidates.
candidate.is_none() || requirement.is_none() || candidate == requirement
}
@ -310,15 +309,15 @@ impl<'a> Resolver<'a> {
let res = 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) {
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";
self.session.span_err(path.span, msg);
return Err(Determinacy::Determined);
}
}
Res::Def(DefKind::Fn, _) => {
let msg = "can't use a procedural macro from the same crate that defines it";
self.session.span_err(path.span, msg);
return Err(Determinacy::Determined);
}
Res::NonMacroAttr(attr_kind) => {
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::Attr => (i, TypeKind::Attr),
MacroKind::Derive => (i, TypeKind::Derive),
MacroKind::ProcMacroStub => unreachable!(),
},
Res::Def(DefKind::TraitAlias, i) => (i, TypeKind::TraitAlias),
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::Attr => ItemType::ProcAttribute,
MacroKind::Derive => ItemType::ProcDerive,
MacroKind::ProcMacroStub => unreachable!(),
}
clean::StrippedItem(..) => unreachable!(),
}

View file

@ -2471,7 +2471,6 @@ impl<'a> fmt::Display for Item<'a> {
MacroKind::Bang => write!(fmt, "Macro ")?,
MacroKind::Attr => write!(fmt, "Attribute Macro ")?,
MacroKind::Derive => write!(fmt, "Derive Macro ")?,
MacroKind::ProcMacroStub => unreachable!(),
}
clean::PrimitiveItem(..) => write!(fmt, "Primitive Type ")?,
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>")?;
}
_ => {}
}
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 path = ast::Path { segments: vec![segment], span: DUMMY_SP };
cx.enter_resolver(|resolver| {
let parent_scope = resolver.dummy_parent_scope();
if let Ok(res) = resolver.resolve_macro_to_res_inner(&path, MacroKind::Bang,
&parent_scope, false, false) {
if let Res::Def(DefKind::Macro(MacroKind::ProcMacroStub), _) = res {
// skip proc-macro stubs, they'll cause `get_macro` to crash
} else {
if let SyntaxExtensionKind::LegacyBang(..) = resolver.get_macro(res).kind {
return Some(res.map_id(|_| panic!("unexpected id")));
}
if let Ok(res @ Res::Def(DefKind::Macro(_), _)) = resolver.resolve_macro_to_res_inner(
&path, MacroKind::Bang, &resolver.dummy_parent_scope(), false, false
) {
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)) {

View file

@ -406,11 +406,8 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
// Struct and variant constructors and proc macro stubs always show up alongside
// their definitions, we've already processed them so just discard these.
match path.res {
Res::Def(DefKind::Ctor(..), _)
| Res::SelfCtor(..)
| Res::Def(DefKind::Macro(MacroKind::ProcMacroStub), _) => return,
_ => {}
if let Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) = path.res {
return;
}
// 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,
/// A derive attribute macro - #[derive(Foo)]
Derive,
/// A view of a procedural macro from the same crate that defines it.
ProcMacroStub,
}
impl MacroKind {
@ -537,7 +535,6 @@ impl MacroKind {
MacroKind::Bang => "macro",
MacroKind::Attr => "attribute macro",
MacroKind::Derive => "derive macro",
MacroKind::ProcMacroStub => "crate-local procedural macro",
}
}

View file

@ -7,7 +7,7 @@
// @has some_macros/index.html
// @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;