Skip to content

Commit 2b2318e

Browse files
Remove dummy ProcMacroClient in favor of Option
1 parent fb21a21 commit 2b2318e

File tree

5 files changed

+59
-71
lines changed

5 files changed

+59
-71
lines changed

crates/proc_macro_api/src/lib.rs

Lines changed: 28 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,10 @@ impl tt::TokenExpander for ProcMacroProcessExpander {
5757
}
5858
}
5959

60-
#[derive(Debug)]
61-
enum ProcMacroClientKind {
62-
Process { process: Arc<ProcMacroProcessSrv>, thread: ProcMacroProcessThread },
63-
Dummy,
64-
}
65-
6660
#[derive(Debug)]
6761
pub struct ProcMacroClient {
68-
kind: ProcMacroClientKind,
62+
process: Arc<ProcMacroProcessSrv>,
63+
thread: ProcMacroProcessThread,
6964
}
7065

7166
impl ProcMacroClient {
@@ -74,47 +69,35 @@ impl ProcMacroClient {
7469
args: impl IntoIterator<Item = impl AsRef<OsStr>>,
7570
) -> io::Result<ProcMacroClient> {
7671
let (thread, process) = ProcMacroProcessSrv::run(process_path, args)?;
77-
Ok(ProcMacroClient {
78-
kind: ProcMacroClientKind::Process { process: Arc::new(process), thread },
79-
})
80-
}
81-
82-
pub fn dummy() -> ProcMacroClient {
83-
ProcMacroClient { kind: ProcMacroClientKind::Dummy }
72+
Ok(ProcMacroClient { process: Arc::new(process), thread })
8473
}
8574

8675
pub fn by_dylib_path(&self, dylib_path: &Path) -> Vec<ProcMacro> {
87-
match &self.kind {
88-
ProcMacroClientKind::Dummy => vec![],
89-
ProcMacroClientKind::Process { process, .. } => {
90-
let macros = match process.find_proc_macros(dylib_path) {
91-
Err(err) => {
92-
eprintln!("Failed to find proc macros. Error: {:#?}", err);
93-
return vec![];
94-
}
95-
Ok(macros) => macros,
96-
};
97-
98-
macros
99-
.into_iter()
100-
.map(|(name, kind)| {
101-
let name = SmolStr::new(&name);
102-
let kind = match kind {
103-
ProcMacroKind::CustomDerive => base_db::ProcMacroKind::CustomDerive,
104-
ProcMacroKind::FuncLike => base_db::ProcMacroKind::FuncLike,
105-
ProcMacroKind::Attr => base_db::ProcMacroKind::Attr,
106-
};
107-
let expander: Arc<dyn tt::TokenExpander> =
108-
Arc::new(ProcMacroProcessExpander {
109-
process: process.clone(),
110-
name: name.clone(),
111-
dylib_path: dylib_path.into(),
112-
});
113-
114-
ProcMacro { name, kind, expander }
115-
})
116-
.collect()
76+
let macros = match self.process.find_proc_macros(dylib_path) {
77+
Err(err) => {
78+
eprintln!("Failed to find proc macros. Error: {:#?}", err);
79+
return vec![];
11780
}
118-
}
81+
Ok(macros) => macros,
82+
};
83+
84+
macros
85+
.into_iter()
86+
.map(|(name, kind)| {
87+
let name = SmolStr::new(&name);
88+
let kind = match kind {
89+
ProcMacroKind::CustomDerive => base_db::ProcMacroKind::CustomDerive,
90+
ProcMacroKind::FuncLike => base_db::ProcMacroKind::FuncLike,
91+
ProcMacroKind::Attr => base_db::ProcMacroKind::Attr,
92+
};
93+
let expander: Arc<dyn tt::TokenExpander> = Arc::new(ProcMacroProcessExpander {
94+
process: self.process.clone(),
95+
name: name.clone(),
96+
dylib_path: dylib_path.into(),
97+
});
98+
99+
ProcMacro { name, kind, expander }
100+
})
101+
.collect()
119102
}
120103
}

crates/project_model/src/workspace.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
//! metadata` or `rust-project.json`) into representation stored in the salsa
33
//! database -- `CrateGraph`.
44
5-
use std::{fmt, fs, path::Component, process::Command};
5+
use std::{
6+
fmt, fs,
7+
path::{Component, Path},
8+
process::Command,
9+
};
610

711
use anyhow::{Context, Result};
8-
use base_db::{CrateDisplayName, CrateGraph, CrateId, CrateName, Edition, Env, FileId};
12+
use base_db::{CrateDisplayName, CrateGraph, CrateId, CrateName, Edition, Env, FileId, ProcMacro};
913
use cfg::CfgOptions;
1014
use paths::{AbsPath, AbsPathBuf};
1115
use proc_macro_api::ProcMacroClient;
@@ -194,15 +198,20 @@ impl ProjectWorkspace {
194198
pub fn to_crate_graph(
195199
&self,
196200
target: Option<&str>,
197-
proc_macro_client: &ProcMacroClient,
201+
proc_macro_client: Option<&ProcMacroClient>,
198202
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
199203
) -> CrateGraph {
204+
let proc_macro_loader = |path: &Path| match proc_macro_client {
205+
Some(client) => client.by_dylib_path(path),
206+
None => Vec::new(),
207+
};
208+
200209
let mut crate_graph = match self {
201210
ProjectWorkspace::Json { project, sysroot } => {
202-
project_json_to_crate_graph(target, proc_macro_client, load, project, sysroot)
211+
project_json_to_crate_graph(target, &proc_macro_loader, load, project, sysroot)
203212
}
204213
ProjectWorkspace::Cargo { cargo, sysroot, rustc } => {
205-
cargo_to_crate_graph(target, proc_macro_client, load, cargo, sysroot, rustc)
214+
cargo_to_crate_graph(target, &proc_macro_loader, load, cargo, sysroot, rustc)
206215
}
207216
};
208217
if crate_graph.patch_cfg_if() {
@@ -216,7 +225,7 @@ impl ProjectWorkspace {
216225

217226
fn project_json_to_crate_graph(
218227
target: Option<&str>,
219-
proc_macro_client: &ProcMacroClient,
228+
proc_macro_loader: &dyn Fn(&Path) -> Vec<ProcMacro>,
220229
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
221230
project: &ProjectJson,
222231
sysroot: &Option<Sysroot>,
@@ -236,8 +245,7 @@ fn project_json_to_crate_graph(
236245
})
237246
.map(|(crate_id, krate, file_id)| {
238247
let env = krate.env.clone().into_iter().collect();
239-
let proc_macro =
240-
krate.proc_macro_dylib_path.clone().map(|it| proc_macro_client.by_dylib_path(&it));
248+
let proc_macro = krate.proc_macro_dylib_path.clone().map(|it| proc_macro_loader(&it));
241249

242250
let target = krate.target.as_deref().or(target);
243251
let target_cfgs =
@@ -279,7 +287,7 @@ fn project_json_to_crate_graph(
279287

280288
fn cargo_to_crate_graph(
281289
target: Option<&str>,
282-
proc_macro_client: &ProcMacroClient,
290+
proc_macro_loader: &dyn Fn(&Path) -> Vec<ProcMacro>,
283291
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
284292
cargo: &CargoWorkspace,
285293
sysroot: &Sysroot,
@@ -309,7 +317,7 @@ fn cargo_to_crate_graph(
309317
&mut crate_graph,
310318
&cargo[pkg],
311319
&cfg_options,
312-
proc_macro_client,
320+
proc_macro_loader,
313321
file_id,
314322
);
315323
if cargo[tgt].kind == TargetKind::Lib {
@@ -385,7 +393,7 @@ fn cargo_to_crate_graph(
385393
&mut crate_graph,
386394
&rustc_workspace[pkg],
387395
&cfg_options,
388-
proc_macro_client,
396+
proc_macro_loader,
389397
file_id,
390398
);
391399
pkg_to_lib_crate.insert(pkg, crate_id);
@@ -433,7 +441,7 @@ fn add_target_crate_root(
433441
crate_graph: &mut CrateGraph,
434442
pkg: &cargo_workspace::PackageData,
435443
cfg_options: &CfgOptions,
436-
proc_macro_client: &ProcMacroClient,
444+
proc_macro_loader: &dyn Fn(&Path) -> Vec<ProcMacro>,
437445
file_id: FileId,
438446
) -> CrateId {
439447
let edition = pkg.edition;
@@ -452,11 +460,8 @@ fn add_target_crate_root(
452460
env.set("OUT_DIR", out_dir);
453461
}
454462
}
455-
let proc_macro = pkg
456-
.proc_macro_dylib_path
457-
.as_ref()
458-
.map(|it| proc_macro_client.by_dylib_path(&it))
459-
.unwrap_or_default();
463+
let proc_macro =
464+
pkg.proc_macro_dylib_path.as_ref().map(|it| proc_macro_loader(&it)).unwrap_or_default();
460465

461466
let display_name = CrateDisplayName::from_canonical_name(pkg.name.clone());
462467
let crate_id = crate_graph.add_crate_root(

crates/rust-analyzer/src/cli/load_cargo.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ pub fn load_cargo(
3333

3434
let proc_macro_client = if with_proc_macro {
3535
let path = std::env::current_exe()?;
36-
ProcMacroClient::extern_process(path, &["proc-macro"]).unwrap()
36+
Some(ProcMacroClient::extern_process(path, &["proc-macro"]).unwrap())
3737
} else {
38-
ProcMacroClient::dummy()
38+
None
3939
};
4040

41-
let crate_graph = ws.to_crate_graph(None, &proc_macro_client, &mut |path: &AbsPath| {
41+
let crate_graph = ws.to_crate_graph(None, proc_macro_client.as_ref(), &mut |path: &AbsPath| {
4242
let contents = loader.load_sync(path);
4343
let path = vfs::VfsPath::from(path.to_path_buf());
4444
vfs.set_file_contents(path.clone(), contents);

crates/rust-analyzer/src/global_state.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub(crate) struct GlobalState {
7575
pub(crate) shutdown_requested: bool,
7676
pub(crate) status: Status,
7777
pub(crate) source_root_config: SourceRootConfig,
78-
pub(crate) proc_macro_client: ProcMacroClient,
78+
pub(crate) proc_macro_client: Option<ProcMacroClient>,
7979
pub(crate) workspaces: Arc<Vec<ProjectWorkspace>>,
8080
latest_requests: Arc<RwLock<LatestRequests>>,
8181
}
@@ -127,7 +127,7 @@ impl GlobalState {
127127
shutdown_requested: false,
128128
status: Status::default(),
129129
source_root_config: SourceRootConfig::default(),
130-
proc_macro_client: ProcMacroClient::dummy(),
130+
proc_macro_client: None,
131131
workspaces: Arc::new(Vec::new()),
132132
latest_requests: Default::default(),
133133
}

crates/rust-analyzer/src/reload.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,16 @@ impl GlobalState {
171171
let project_folders = ProjectFolders::new(&workspaces);
172172

173173
self.proc_macro_client = match &self.config.proc_macro_srv {
174-
None => ProcMacroClient::dummy(),
174+
None => None,
175175
Some((path, args)) => match ProcMacroClient::extern_process(path.into(), args) {
176-
Ok(it) => it,
176+
Ok(it) => Some(it),
177177
Err(err) => {
178178
log::error!(
179179
"Failed to run proc_macro_srv from path {}, error: {:?}",
180180
path.display(),
181181
err
182182
);
183-
ProcMacroClient::dummy()
183+
None
184184
}
185185
},
186186
};
@@ -212,7 +212,7 @@ impl GlobalState {
212212
for ws in workspaces.iter() {
213213
crate_graph.extend(ws.to_crate_graph(
214214
self.config.cargo.target.as_deref(),
215-
&self.proc_macro_client,
215+
self.proc_macro_client.as_ref(),
216216
&mut load,
217217
));
218218
}

0 commit comments

Comments
 (0)