6030: Small proc macro cleanup r=jonas-schievink a=jonas-schievink

git got really confused, but all I did in the first commit was unindent a few lines

Co-authored-by: Jonas Schievink <jonas.schievink@ferrous-systems.com>
This commit is contained in:
bors[bot] 2020-09-18 13:40:45 +00:00 committed by GitHub
commit 5e1500ec74
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 167 additions and 168 deletions

View file

@ -908,12 +908,12 @@ impl MacroDef {
/// Indicate it is a proc-macro /// Indicate it is a proc-macro
pub fn is_proc_macro(&self) -> bool { pub fn is_proc_macro(&self) -> bool {
matches!(self.id.kind, MacroDefKind::CustomDerive(_)) matches!(self.id.kind, MacroDefKind::ProcMacro(_))
} }
/// Indicate it is a derive macro /// Indicate it is a derive macro
pub fn is_derive_macro(&self) -> bool { pub fn is_derive_macro(&self) -> bool {
matches!(self.id.kind, MacroDefKind::CustomDerive(_) | MacroDefKind::BuiltInDerive(_)) matches!(self.id.kind, MacroDefKind::ProcMacro(_) | MacroDefKind::BuiltInDerive(_))
} }
} }

View file

@ -273,7 +273,7 @@ impl DefCollector<'_> {
let macro_id = MacroDefId { let macro_id = MacroDefId {
ast_id: None, ast_id: None,
krate: Some(krate), krate: Some(krate),
kind: MacroDefKind::CustomDerive(expander), kind: MacroDefKind::ProcMacro(expander),
local_inner: false, local_inner: false,
}; };
@ -874,184 +874,183 @@ impl ModCollector<'_, '_> {
for &item in items { for &item in items {
let attrs = self.item_tree.attrs(item.into()); let attrs = self.item_tree.attrs(item.into());
if self.is_cfg_enabled(attrs) { if !self.is_cfg_enabled(attrs) {
let module = continue;
ModuleId { krate: self.def_collector.def_map.krate, local_id: self.module_id }; }
let container = ContainerId::ModuleId(module); let module =
ModuleId { krate: self.def_collector.def_map.krate, local_id: self.module_id };
let container = ContainerId::ModuleId(module);
let mut def = None; let mut def = None;
match item { match item {
ModItem::Mod(m) => self.collect_module(&self.item_tree[m], attrs), ModItem::Mod(m) => self.collect_module(&self.item_tree[m], attrs),
ModItem::Import(import_id) => { ModItem::Import(import_id) => {
self.def_collector.unresolved_imports.push(ImportDirective { self.def_collector.unresolved_imports.push(ImportDirective {
module_id: self.module_id, module_id: self.module_id,
import: Import::from_use( import: Import::from_use(
&self.item_tree, &self.item_tree,
InFile::new(self.file_id, import_id), InFile::new(self.file_id, import_id),
), ),
status: PartialResolvedImport::Unresolved, status: PartialResolvedImport::Unresolved,
}) })
} }
ModItem::ExternCrate(import_id) => { ModItem::ExternCrate(import_id) => {
self.def_collector.unresolved_imports.push(ImportDirective { self.def_collector.unresolved_imports.push(ImportDirective {
module_id: self.module_id, module_id: self.module_id,
import: Import::from_extern_crate( import: Import::from_extern_crate(
&self.item_tree, &self.item_tree,
InFile::new(self.file_id, import_id), InFile::new(self.file_id, import_id),
), ),
status: PartialResolvedImport::Unresolved, status: PartialResolvedImport::Unresolved,
}) })
} }
ModItem::MacroCall(mac) => self.collect_macro(&self.item_tree[mac]), ModItem::MacroCall(mac) => self.collect_macro(&self.item_tree[mac]),
ModItem::Impl(imp) => { ModItem::Impl(imp) => {
let module = ModuleId { let module = ModuleId {
krate: self.def_collector.def_map.krate, krate: self.def_collector.def_map.krate,
local_id: self.module_id, local_id: self.module_id,
}; };
let container = ContainerId::ModuleId(module); let container = ContainerId::ModuleId(module);
let impl_id = ImplLoc { container, id: ItemTreeId::new(self.file_id, imp) } let impl_id = ImplLoc { container, id: ItemTreeId::new(self.file_id, imp) }
.intern(self.def_collector.db); .intern(self.def_collector.db);
self.def_collector.def_map.modules[self.module_id] self.def_collector.def_map.modules[self.module_id].scope.define_impl(impl_id)
.scope }
.define_impl(impl_id) ModItem::Function(id) => {
} let func = &self.item_tree[id];
ModItem::Function(id) => { def = Some(DefData {
let func = &self.item_tree[id]; id: FunctionLoc {
def = Some(DefData { container: container.into(),
id: FunctionLoc { id: ItemTreeId::new(self.file_id, id),
container: container.into(),
id: ItemTreeId::new(self.file_id, id),
}
.intern(self.def_collector.db)
.into(),
name: &func.name,
visibility: &self.item_tree[func.visibility],
has_constructor: false,
});
}
ModItem::Struct(id) => {
let it = &self.item_tree[id];
// FIXME: check attrs to see if this is an attribute macro invocation;
// in which case we don't add the invocation, just a single attribute
// macro invocation
self.collect_derives(attrs, it.ast_id.upcast());
def = Some(DefData {
id: StructLoc { container, id: ItemTreeId::new(self.file_id, id) }
.intern(self.def_collector.db)
.into(),
name: &it.name,
visibility: &self.item_tree[it.visibility],
has_constructor: it.kind != StructDefKind::Record,
});
}
ModItem::Union(id) => {
let it = &self.item_tree[id];
// FIXME: check attrs to see if this is an attribute macro invocation;
// in which case we don't add the invocation, just a single attribute
// macro invocation
self.collect_derives(attrs, it.ast_id.upcast());
def = Some(DefData {
id: UnionLoc { container, id: ItemTreeId::new(self.file_id, id) }
.intern(self.def_collector.db)
.into(),
name: &it.name,
visibility: &self.item_tree[it.visibility],
has_constructor: false,
});
}
ModItem::Enum(id) => {
let it = &self.item_tree[id];
// FIXME: check attrs to see if this is an attribute macro invocation;
// in which case we don't add the invocation, just a single attribute
// macro invocation
self.collect_derives(attrs, it.ast_id.upcast());
def = Some(DefData {
id: EnumLoc { container, id: ItemTreeId::new(self.file_id, id) }
.intern(self.def_collector.db)
.into(),
name: &it.name,
visibility: &self.item_tree[it.visibility],
has_constructor: false,
});
}
ModItem::Const(id) => {
let it = &self.item_tree[id];
if let Some(name) = &it.name {
def = Some(DefData {
id: ConstLoc {
container: container.into(),
id: ItemTreeId::new(self.file_id, id),
}
.intern(self.def_collector.db)
.into(),
name,
visibility: &self.item_tree[it.visibility],
has_constructor: false,
});
} }
} .intern(self.def_collector.db)
ModItem::Static(id) => { .into(),
let it = &self.item_tree[id]; name: &func.name,
visibility: &self.item_tree[func.visibility],
has_constructor: false,
});
}
ModItem::Struct(id) => {
let it = &self.item_tree[id];
def = Some(DefData { // FIXME: check attrs to see if this is an attribute macro invocation;
id: StaticLoc { container, id: ItemTreeId::new(self.file_id, id) } // in which case we don't add the invocation, just a single attribute
.intern(self.def_collector.db) // macro invocation
.into(), self.collect_derives(attrs, it.ast_id.upcast());
name: &it.name,
visibility: &self.item_tree[it.visibility],
has_constructor: false,
});
}
ModItem::Trait(id) => {
let it = &self.item_tree[id];
def = Some(DefData { def = Some(DefData {
id: TraitLoc { container, id: ItemTreeId::new(self.file_id, id) } id: StructLoc { container, id: ItemTreeId::new(self.file_id, id) }
.intern(self.def_collector.db) .intern(self.def_collector.db)
.into(), .into(),
name: &it.name, name: &it.name,
visibility: &self.item_tree[it.visibility], visibility: &self.item_tree[it.visibility],
has_constructor: false, has_constructor: it.kind != StructDefKind::Record,
}); });
} }
ModItem::TypeAlias(id) => { ModItem::Union(id) => {
let it = &self.item_tree[id]; let it = &self.item_tree[id];
// FIXME: check attrs to see if this is an attribute macro invocation;
// in which case we don't add the invocation, just a single attribute
// macro invocation
self.collect_derives(attrs, it.ast_id.upcast());
def = Some(DefData {
id: UnionLoc { container, id: ItemTreeId::new(self.file_id, id) }
.intern(self.def_collector.db)
.into(),
name: &it.name,
visibility: &self.item_tree[it.visibility],
has_constructor: false,
});
}
ModItem::Enum(id) => {
let it = &self.item_tree[id];
// FIXME: check attrs to see if this is an attribute macro invocation;
// in which case we don't add the invocation, just a single attribute
// macro invocation
self.collect_derives(attrs, it.ast_id.upcast());
def = Some(DefData {
id: EnumLoc { container, id: ItemTreeId::new(self.file_id, id) }
.intern(self.def_collector.db)
.into(),
name: &it.name,
visibility: &self.item_tree[it.visibility],
has_constructor: false,
});
}
ModItem::Const(id) => {
let it = &self.item_tree[id];
if let Some(name) = &it.name {
def = Some(DefData { def = Some(DefData {
id: TypeAliasLoc { id: ConstLoc {
container: container.into(), container: container.into(),
id: ItemTreeId::new(self.file_id, id), id: ItemTreeId::new(self.file_id, id),
} }
.intern(self.def_collector.db) .intern(self.def_collector.db)
.into(), .into(),
name: &it.name, name,
visibility: &self.item_tree[it.visibility], visibility: &self.item_tree[it.visibility],
has_constructor: false, has_constructor: false,
}); });
} }
} }
ModItem::Static(id) => {
let it = &self.item_tree[id];
if let Some(DefData { id, name, visibility, has_constructor }) = def { def = Some(DefData {
self.def_collector.def_map.modules[self.module_id].scope.define_def(id); id: StaticLoc { container, id: ItemTreeId::new(self.file_id, id) }
let vis = self .intern(self.def_collector.db)
.def_collector .into(),
.def_map name: &it.name,
.resolve_visibility(self.def_collector.db, self.module_id, visibility) visibility: &self.item_tree[it.visibility],
.unwrap_or(Visibility::Public); has_constructor: false,
self.def_collector.update( });
self.module_id,
&[(Some(name.clone()), PerNs::from_def(id, vis, has_constructor))],
vis,
ImportType::Named,
)
} }
ModItem::Trait(id) => {
let it = &self.item_tree[id];
def = Some(DefData {
id: TraitLoc { container, id: ItemTreeId::new(self.file_id, id) }
.intern(self.def_collector.db)
.into(),
name: &it.name,
visibility: &self.item_tree[it.visibility],
has_constructor: false,
});
}
ModItem::TypeAlias(id) => {
let it = &self.item_tree[id];
def = Some(DefData {
id: TypeAliasLoc {
container: container.into(),
id: ItemTreeId::new(self.file_id, id),
}
.intern(self.def_collector.db)
.into(),
name: &it.name,
visibility: &self.item_tree[it.visibility],
has_constructor: false,
});
}
}
if let Some(DefData { id, name, visibility, has_constructor }) = def {
self.def_collector.def_map.modules[self.module_id].scope.define_def(id);
let vis = self
.def_collector
.def_map
.resolve_visibility(self.def_collector.db, self.module_id, visibility)
.unwrap_or(Visibility::Public);
self.def_collector.update(
self.module_id,
&[(Some(name.clone()), PerNs::from_def(id, vis, has_constructor))],
vis,
ImportType::Named,
)
} }
} }
} }

View file

@ -143,7 +143,7 @@ pub(crate) fn macro_def(
Some(Arc::new((TokenExpander::BuiltinDerive(expander), mbe::TokenMap::default()))) Some(Arc::new((TokenExpander::BuiltinDerive(expander), mbe::TokenMap::default())))
} }
MacroDefKind::BuiltInEager(_) => None, MacroDefKind::BuiltInEager(_) => None,
MacroDefKind::CustomDerive(expander) => { MacroDefKind::ProcMacro(expander) => {
Some(Arc::new((TokenExpander::ProcMacro(expander), mbe::TokenMap::default()))) Some(Arc::new((TokenExpander::ProcMacro(expander), mbe::TokenMap::default())))
} }
} }
@ -249,7 +249,7 @@ pub(crate) fn expand_proc_macro(
}; };
let expander = match loc.def.kind { let expander = match loc.def.kind {
MacroDefKind::CustomDerive(expander) => expander, MacroDefKind::ProcMacro(expander) => expander,
_ => unreachable!(), _ => unreachable!(),
}; };

View file

@ -129,7 +129,7 @@ fn eager_macro_recur(
MacroDefKind::Declarative MacroDefKind::Declarative
| MacroDefKind::BuiltIn(_) | MacroDefKind::BuiltIn(_)
| MacroDefKind::BuiltInDerive(_) | MacroDefKind::BuiltInDerive(_)
| MacroDefKind::CustomDerive(_) => { | MacroDefKind::ProcMacro(_) => {
let expanded = lazy_expand(db, &def, curr.with_value(child.clone()), krate)?; let expanded = lazy_expand(db, &def, curr.with_value(child.clone()), krate)?;
// replace macro inside // replace macro inside
eager_macro_recur(db, expanded, krate, macro_resolver)? eager_macro_recur(db, expanded, krate, macro_resolver)?

View file

@ -33,7 +33,7 @@ impl Hygiene {
MacroDefKind::BuiltIn(_) => (None, false), MacroDefKind::BuiltIn(_) => (None, false),
MacroDefKind::BuiltInDerive(_) => (None, false), MacroDefKind::BuiltInDerive(_) => (None, false),
MacroDefKind::BuiltInEager(_) => (None, false), MacroDefKind::BuiltInEager(_) => (None, false),
MacroDefKind::CustomDerive(_) => (None, false), MacroDefKind::ProcMacro(_) => (None, false),
} }
} }
MacroCallId::EagerMacro(_id) => (None, false), MacroCallId::EagerMacro(_id) => (None, false),

View file

@ -246,7 +246,7 @@ pub enum MacroDefKind {
// FIXME: maybe just Builtin and rename BuiltinFnLikeExpander to BuiltinExpander // FIXME: maybe just Builtin and rename BuiltinFnLikeExpander to BuiltinExpander
BuiltInDerive(BuiltinDeriveExpander), BuiltInDerive(BuiltinDeriveExpander),
BuiltInEager(EagerExpander), BuiltInEager(EagerExpander),
CustomDerive(ProcMacroExpander), ProcMacro(ProcMacroExpander),
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]