Remove dummy ProcMacroClient in favor of Option

This commit is contained in:
Jonas Schievink 2020-12-07 17:16:50 +01:00
parent fb21a215be
commit 2b2318e695
5 changed files with 59 additions and 71 deletions

View file

@ -57,15 +57,10 @@ impl tt::TokenExpander for ProcMacroProcessExpander {
}
}
#[derive(Debug)]
enum ProcMacroClientKind {
Process { process: Arc<ProcMacroProcessSrv>, thread: ProcMacroProcessThread },
Dummy,
}
#[derive(Debug)]
pub struct ProcMacroClient {
kind: ProcMacroClientKind,
process: Arc<ProcMacroProcessSrv>,
thread: ProcMacroProcessThread,
}
impl ProcMacroClient {
@ -74,20 +69,11 @@ impl ProcMacroClient {
args: impl IntoIterator<Item = impl AsRef<OsStr>>,
) -> io::Result<ProcMacroClient> {
let (thread, process) = ProcMacroProcessSrv::run(process_path, args)?;
Ok(ProcMacroClient {
kind: ProcMacroClientKind::Process { process: Arc::new(process), thread },
})
}
pub fn dummy() -> ProcMacroClient {
ProcMacroClient { kind: ProcMacroClientKind::Dummy }
Ok(ProcMacroClient { process: Arc::new(process), thread })
}
pub fn by_dylib_path(&self, dylib_path: &Path) -> Vec<ProcMacro> {
match &self.kind {
ProcMacroClientKind::Dummy => vec![],
ProcMacroClientKind::Process { process, .. } => {
let macros = match process.find_proc_macros(dylib_path) {
let macros = match self.process.find_proc_macros(dylib_path) {
Err(err) => {
eprintln!("Failed to find proc macros. Error: {:#?}", err);
return vec![];
@ -104,9 +90,8 @@ impl ProcMacroClient {
ProcMacroKind::FuncLike => base_db::ProcMacroKind::FuncLike,
ProcMacroKind::Attr => base_db::ProcMacroKind::Attr,
};
let expander: Arc<dyn tt::TokenExpander> =
Arc::new(ProcMacroProcessExpander {
process: process.clone(),
let expander: Arc<dyn tt::TokenExpander> = Arc::new(ProcMacroProcessExpander {
process: self.process.clone(),
name: name.clone(),
dylib_path: dylib_path.into(),
});
@ -116,5 +101,3 @@ impl ProcMacroClient {
.collect()
}
}
}
}

View file

@ -2,10 +2,14 @@
//! metadata` or `rust-project.json`) into representation stored in the salsa
//! database -- `CrateGraph`.
use std::{fmt, fs, path::Component, process::Command};
use std::{
fmt, fs,
path::{Component, Path},
process::Command,
};
use anyhow::{Context, Result};
use base_db::{CrateDisplayName, CrateGraph, CrateId, CrateName, Edition, Env, FileId};
use base_db::{CrateDisplayName, CrateGraph, CrateId, CrateName, Edition, Env, FileId, ProcMacro};
use cfg::CfgOptions;
use paths::{AbsPath, AbsPathBuf};
use proc_macro_api::ProcMacroClient;
@ -194,15 +198,20 @@ impl ProjectWorkspace {
pub fn to_crate_graph(
&self,
target: Option<&str>,
proc_macro_client: &ProcMacroClient,
proc_macro_client: Option<&ProcMacroClient>,
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
) -> CrateGraph {
let proc_macro_loader = |path: &Path| match proc_macro_client {
Some(client) => client.by_dylib_path(path),
None => Vec::new(),
};
let mut crate_graph = match self {
ProjectWorkspace::Json { project, sysroot } => {
project_json_to_crate_graph(target, proc_macro_client, load, project, sysroot)
project_json_to_crate_graph(target, &proc_macro_loader, load, project, sysroot)
}
ProjectWorkspace::Cargo { cargo, sysroot, rustc } => {
cargo_to_crate_graph(target, proc_macro_client, load, cargo, sysroot, rustc)
cargo_to_crate_graph(target, &proc_macro_loader, load, cargo, sysroot, rustc)
}
};
if crate_graph.patch_cfg_if() {
@ -216,7 +225,7 @@ impl ProjectWorkspace {
fn project_json_to_crate_graph(
target: Option<&str>,
proc_macro_client: &ProcMacroClient,
proc_macro_loader: &dyn Fn(&Path) -> Vec<ProcMacro>,
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
project: &ProjectJson,
sysroot: &Option<Sysroot>,
@ -236,8 +245,7 @@ fn project_json_to_crate_graph(
})
.map(|(crate_id, krate, file_id)| {
let env = krate.env.clone().into_iter().collect();
let proc_macro =
krate.proc_macro_dylib_path.clone().map(|it| proc_macro_client.by_dylib_path(&it));
let proc_macro = krate.proc_macro_dylib_path.clone().map(|it| proc_macro_loader(&it));
let target = krate.target.as_deref().or(target);
let target_cfgs =
@ -279,7 +287,7 @@ fn project_json_to_crate_graph(
fn cargo_to_crate_graph(
target: Option<&str>,
proc_macro_client: &ProcMacroClient,
proc_macro_loader: &dyn Fn(&Path) -> Vec<ProcMacro>,
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
cargo: &CargoWorkspace,
sysroot: &Sysroot,
@ -309,7 +317,7 @@ fn cargo_to_crate_graph(
&mut crate_graph,
&cargo[pkg],
&cfg_options,
proc_macro_client,
proc_macro_loader,
file_id,
);
if cargo[tgt].kind == TargetKind::Lib {
@ -385,7 +393,7 @@ fn cargo_to_crate_graph(
&mut crate_graph,
&rustc_workspace[pkg],
&cfg_options,
proc_macro_client,
proc_macro_loader,
file_id,
);
pkg_to_lib_crate.insert(pkg, crate_id);
@ -433,7 +441,7 @@ fn add_target_crate_root(
crate_graph: &mut CrateGraph,
pkg: &cargo_workspace::PackageData,
cfg_options: &CfgOptions,
proc_macro_client: &ProcMacroClient,
proc_macro_loader: &dyn Fn(&Path) -> Vec<ProcMacro>,
file_id: FileId,
) -> CrateId {
let edition = pkg.edition;
@ -452,11 +460,8 @@ fn add_target_crate_root(
env.set("OUT_DIR", out_dir);
}
}
let proc_macro = pkg
.proc_macro_dylib_path
.as_ref()
.map(|it| proc_macro_client.by_dylib_path(&it))
.unwrap_or_default();
let proc_macro =
pkg.proc_macro_dylib_path.as_ref().map(|it| proc_macro_loader(&it)).unwrap_or_default();
let display_name = CrateDisplayName::from_canonical_name(pkg.name.clone());
let crate_id = crate_graph.add_crate_root(

View file

@ -33,12 +33,12 @@ pub fn load_cargo(
let proc_macro_client = if with_proc_macro {
let path = std::env::current_exe()?;
ProcMacroClient::extern_process(path, &["proc-macro"]).unwrap()
Some(ProcMacroClient::extern_process(path, &["proc-macro"]).unwrap())
} else {
ProcMacroClient::dummy()
None
};
let crate_graph = ws.to_crate_graph(None, &proc_macro_client, &mut |path: &AbsPath| {
let crate_graph = ws.to_crate_graph(None, proc_macro_client.as_ref(), &mut |path: &AbsPath| {
let contents = loader.load_sync(path);
let path = vfs::VfsPath::from(path.to_path_buf());
vfs.set_file_contents(path.clone(), contents);

View file

@ -75,7 +75,7 @@ pub(crate) struct GlobalState {
pub(crate) shutdown_requested: bool,
pub(crate) status: Status,
pub(crate) source_root_config: SourceRootConfig,
pub(crate) proc_macro_client: ProcMacroClient,
pub(crate) proc_macro_client: Option<ProcMacroClient>,
pub(crate) workspaces: Arc<Vec<ProjectWorkspace>>,
latest_requests: Arc<RwLock<LatestRequests>>,
}
@ -127,7 +127,7 @@ impl GlobalState {
shutdown_requested: false,
status: Status::default(),
source_root_config: SourceRootConfig::default(),
proc_macro_client: ProcMacroClient::dummy(),
proc_macro_client: None,
workspaces: Arc::new(Vec::new()),
latest_requests: Default::default(),
}

View file

@ -171,16 +171,16 @@ impl GlobalState {
let project_folders = ProjectFolders::new(&workspaces);
self.proc_macro_client = match &self.config.proc_macro_srv {
None => ProcMacroClient::dummy(),
None => None,
Some((path, args)) => match ProcMacroClient::extern_process(path.into(), args) {
Ok(it) => it,
Ok(it) => Some(it),
Err(err) => {
log::error!(
"Failed to run proc_macro_srv from path {}, error: {:?}",
path.display(),
err
);
ProcMacroClient::dummy()
None
}
},
};
@ -212,7 +212,7 @@ impl GlobalState {
for ws in workspaces.iter() {
crate_graph.extend(ws.to_crate_graph(
self.config.cargo.target.as_deref(),
&self.proc_macro_client,
self.proc_macro_client.as_ref(),
&mut load,
));
}