Remove collect proc_macro definitions

This commit is contained in:
Edwin Cheng 2020-03-25 20:14:22 +08:00
parent 5bd3aa05d6
commit 2adc9a8d5f
2 changed files with 25 additions and 29 deletions

View File

@ -65,6 +65,9 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: CrateDefMap) -> Cr
unexpanded_attribute_macros: Vec::new(),
mod_dirs: FxHashMap::default(),
cfg_options,
// FIXME: pass proc-macro from crate-graph
proc_macros: Default::default(),
};
collector.collect();
collector.finish()
@ -123,6 +126,7 @@ struct DefCollector<'a> {
unexpanded_attribute_macros: Vec<DeriveDirective>,
mod_dirs: FxHashMap<LocalModuleId, ModDir>,
cfg_options: &'a CfgOptions,
proc_macros: Vec<(Name, ProcMacroExpander)>,
}
impl DefCollector<'_> {
@ -178,6 +182,24 @@ impl DefCollector<'_> {
for directive in unresolved_imports {
self.record_resolved_import(&directive)
}
// Record proc-macros
self.collect_proc_macro();
}
fn collect_proc_macro(&mut self) {
let proc_macros = std::mem::take(&mut self.proc_macros);
for (name, expander) in proc_macros {
let krate = self.def_map.krate;
let macro_id = MacroDefId {
ast_id: None,
krate: Some(krate),
kind: MacroDefKind::CustomDerive(expander),
};
self.define_proc_macro(name.clone(), macro_id);
}
}
/// Define a macro with `macro_rules`.
@ -801,7 +823,6 @@ impl ModCollector<'_, '_> {
// in which case we don't add the invocation, just a single attribute
// macro invocation
self.collect_derives(attrs, def);
self.collect_proc_macro(attrs);
let name = def.name.clone();
let container = ContainerId::ModuleId(module);
@ -878,28 +899,6 @@ impl ModCollector<'_, '_> {
}
}
fn collect_proc_macro(&mut self, attrs: &Attrs) {
if let Some(derive_subtree) = attrs.by_key("proc_macro_derive").tt_values().next() {
if let Some(tt) = derive_subtree.token_trees.get(0) {
let ident = match &tt {
tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => ident,
_ => return, // anything else would be an error (which we currently ignore)
};
let name = ident.as_name();
let krate = self.def_collector.def_map.krate;
let expander = ProcMacroExpander::new(krate);
let macro_id = MacroDefId {
ast_id: None,
krate: Some(krate),
kind: MacroDefKind::CustomDerive(expander),
};
self.def_collector.define_proc_macro(name.clone(), macro_id);
}
}
}
fn collect_macro(&mut self, mac: &raw::MacroData) {
let mut ast_id = AstIdWithPath::new(self.file_id, mac.ast_id, mac.path.clone());
@ -1001,6 +1000,7 @@ mod tests {
unexpanded_attribute_macros: Vec::new(),
mod_dirs: FxHashMap::default(),
cfg_options: &CfgOptions::default(),
proc_macros: Default::default(),
};
collector.collect();
collector.def_map

View File

@ -642,9 +642,10 @@ mod clone {
#[test]
fn infer_custom_derive_simple() {
// FIXME: this test current now do nothing
let (db, pos) = TestDB::with_position(
r#"
//- /main.rs crate:main deps:foo
//- /main.rs crate:main
use foo::Foo;
#[derive(Foo)]
@ -653,11 +654,6 @@ struct S{}
fn test() {
S{}<|>;
}
//- /lib.rs crate:foo
#[proc_macro_derive(Foo)]
pub fn derive_foo(_item: TokenStream) -> TokenStream {
}
"#,
);
assert_eq!("S", type_at_pos(&db, pos));