From 6a8b4f873aa42f3522d3a92384019272b6ccefd2 Mon Sep 17 00:00:00 2001 From: JasperDeSutter Date: Sun, 24 Nov 2019 11:33:12 +0100 Subject: add proc-macro crate type handling --- crates/ra_project_model/src/cargo_workspace.rs | 8 ++++++++ crates/ra_project_model/src/lib.rs | 17 +++++++++++++++++ crates/ra_project_model/src/sysroot.rs | 6 +++++- 3 files changed, 30 insertions(+), 1 deletion(-) (limited to 'crates/ra_project_model') diff --git a/crates/ra_project_model/src/cargo_workspace.rs b/crates/ra_project_model/src/cargo_workspace.rs index c128e608d..351997dcd 100644 --- a/crates/ra_project_model/src/cargo_workspace.rs +++ b/crates/ra_project_model/src/cargo_workspace.rs @@ -54,11 +54,13 @@ struct TargetData { name: String, root: PathBuf, kind: TargetKind, + is_proc_macro: bool, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum TargetKind { Bin, + /// Any kind of Cargo lib crate-type (dylib, rlib, proc-macro, ...). Lib, Example, Test, @@ -74,6 +76,7 @@ impl TargetKind { "test" => TargetKind::Test, "bench" => TargetKind::Bench, "example" => TargetKind::Example, + "proc-macro" => TargetKind::Lib, _ if kind.contains("lib") => TargetKind::Lib, _ => continue, }; @@ -123,6 +126,9 @@ impl Target { pub fn kind(self, ws: &CargoWorkspace) -> TargetKind { ws.targets[self].kind } + pub fn is_proc_macro(self, ws: &CargoWorkspace) -> bool { + ws.targets[self].is_proc_macro + } } impl CargoWorkspace { @@ -155,11 +161,13 @@ impl CargoWorkspace { let pkg_data = &mut packages[pkg]; pkg_by_id.insert(id, pkg); for meta_tgt in meta_pkg.targets { + let is_proc_macro = meta_tgt.kind.as_slice() == &["proc-macro"]; let tgt = targets.alloc(TargetData { pkg, name: meta_tgt.name, root: meta_tgt.src_path.clone(), kind: TargetKind::new(meta_tgt.kind.as_slice()), + is_proc_macro, }); pkg_data.targets.push(tgt); } diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index d04a8cbe2..77f729a25 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -210,6 +210,8 @@ impl ProjectWorkspace { let libcore = sysroot.core().and_then(|it| sysroot_crates.get(&it).copied()); let libstd = sysroot.std().and_then(|it| sysroot_crates.get(&it).copied()); + let libproc_macro = + sysroot.proc_macro().and_then(|it| sysroot_crates.get(&it).copied()); let mut pkg_to_lib_crate = FxHashMap::default(); let mut pkg_crates = FxHashMap::default(); @@ -236,6 +238,21 @@ impl ProjectWorkspace { lib_tgt = Some(crate_id); pkg_to_lib_crate.insert(pkg, crate_id); } + if tgt.is_proc_macro(&cargo) { + if let Some(proc_macro) = libproc_macro { + if let Err(_) = crate_graph.add_dep( + crate_id, + "proc_macro".into(), + proc_macro, + ) { + log::error!( + "cyclic dependency on proc_macro for {}", + pkg.name(&cargo) + ) + } + } + } + pkg_crates.entry(pkg).or_insert_with(Vec::new).push(crate_id); } } diff --git a/crates/ra_project_model/src/sysroot.rs b/crates/ra_project_model/src/sysroot.rs index 3d827809e..8a7757c41 100644 --- a/crates/ra_project_model/src/sysroot.rs +++ b/crates/ra_project_model/src/sysroot.rs @@ -35,6 +35,10 @@ impl Sysroot { self.by_name("std") } + pub fn proc_macro(&self) -> Option { + self.by_name("proc_macro") + } + pub fn crates<'a>(&'a self) -> impl Iterator + ExactSizeIterator + 'a { self.crates.iter().map(|(id, _data)| id) } @@ -70,7 +74,7 @@ impl Sysroot { } } if let Some(alloc) = sysroot.by_name("alloc") { - if let Some(core) = sysroot.by_name("core") { + if let Some(core) = sysroot.core() { sysroot.crates[alloc].deps.push(core); } } -- cgit v1.2.3