Skip to content

Commit 29db33c

Browse files
Address review comments
1 parent abe0ead commit 29db33c

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

crates/proc_macro_api/src/lib.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::{
1515
ffi::OsStr,
1616
io,
1717
path::{Path, PathBuf},
18-
sync::Arc,
18+
sync::{Arc, Mutex},
1919
};
2020

2121
use tt::{SmolStr, Subtree};
@@ -27,7 +27,7 @@ pub use version::{read_dylib_info, RustCInfo};
2727

2828
#[derive(Debug, Clone)]
2929
struct ProcMacroProcessExpander {
30-
process: Arc<ProcMacroProcessSrv>,
30+
process: Arc<Mutex<ProcMacroProcessSrv>>,
3131
dylib_path: PathBuf,
3232
name: SmolStr,
3333
}
@@ -56,14 +56,24 @@ impl base_db::ProcMacroExpander for ProcMacroProcessExpander {
5656
env: env.iter().map(|(k, v)| (k.to_string(), v.to_string())).collect(),
5757
};
5858

59-
let result: ExpansionResult = self.process.send_task(msg::Request::ExpansionMacro(task))?;
59+
let result: ExpansionResult = self
60+
.process
61+
.lock()
62+
.unwrap_or_else(|e| e.into_inner())
63+
.send_task(msg::Request::ExpansionMacro(task))?;
6064
Ok(result.expansion)
6165
}
6266
}
6367

6468
#[derive(Debug)]
6569
pub struct ProcMacroClient {
66-
process: Arc<ProcMacroProcessSrv>,
70+
/// Currently, the proc macro process expands all procedural macros sequentially.
71+
///
72+
/// That means that concurrent salsa requests may block each other when expanding proc macros,
73+
/// which is unfortunate, but simple and good enough for the time being.
74+
///
75+
/// Therefore, we just wrap the `ProcMacroProcessSrv` in a mutex here.
76+
process: Arc<Mutex<ProcMacroProcessSrv>>,
6777
}
6878

6979
impl ProcMacroClient {
@@ -73,7 +83,7 @@ impl ProcMacroClient {
7383
args: impl IntoIterator<Item = impl AsRef<OsStr>>,
7484
) -> io::Result<ProcMacroClient> {
7585
let process = ProcMacroProcessSrv::run(process_path, args)?;
76-
Ok(ProcMacroClient { process: Arc::new(process) })
86+
Ok(ProcMacroClient { process: Arc::new(Mutex::new(process)) })
7787
}
7888

7989
pub fn by_dylib_path(&self, dylib_path: &Path) -> Vec<ProcMacro> {
@@ -93,7 +103,12 @@ impl ProcMacroClient {
93103
}
94104
}
95105

96-
let macros = match self.process.find_proc_macros(dylib_path) {
106+
let macros = match self
107+
.process
108+
.lock()
109+
.unwrap_or_else(|e| e.into_inner())
110+
.find_proc_macros(dylib_path)
111+
{
97112
Err(err) => {
98113
eprintln!("Failed to find proc macros. Error: {:#?}", err);
99114
return vec![];

crates/proc_macro_api/src/process.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::{
66
io::{self, BufRead, BufReader, Write},
77
path::{Path, PathBuf},
88
process::{Child, ChildStdin, ChildStdout, Command, Stdio},
9-
sync::Mutex,
109
};
1110

1211
use stdx::JodChild;
@@ -18,8 +17,9 @@ use crate::{
1817

1918
#[derive(Debug)]
2019
pub(crate) struct ProcMacroProcessSrv {
21-
process: Mutex<Process>,
22-
stdio: Mutex<(ChildStdin, BufReader<ChildStdout>)>,
20+
process: Process,
21+
stdin: ChildStdin,
22+
stdout: BufReader<ChildStdout>,
2323
}
2424

2525
impl ProcMacroProcessSrv {
@@ -30,16 +30,13 @@ impl ProcMacroProcessSrv {
3030
let mut process = Process::run(process_path, args)?;
3131
let (stdin, stdout) = process.stdio().expect("couldn't access child stdio");
3232

33-
let srv = ProcMacroProcessSrv {
34-
process: Mutex::new(process),
35-
stdio: Mutex::new((stdin, stdout)),
36-
};
33+
let srv = ProcMacroProcessSrv { process, stdin, stdout };
3734

3835
Ok(srv)
3936
}
4037

4138
pub(crate) fn find_proc_macros(
42-
&self,
39+
&mut self,
4340
dylib_path: &Path,
4441
) -> Result<Vec<(String, ProcMacroKind)>, tt::ExpansionError> {
4542
let task = ListMacrosTask { lib: dylib_path.to_path_buf() };
@@ -48,22 +45,18 @@ impl ProcMacroProcessSrv {
4845
Ok(result.macros)
4946
}
5047

51-
pub(crate) fn send_task<R>(&self, req: Request) -> Result<R, tt::ExpansionError>
48+
pub(crate) fn send_task<R>(&mut self, req: Request) -> Result<R, tt::ExpansionError>
5249
where
5350
R: TryFrom<Response, Error = &'static str>,
5451
{
55-
let mut guard = self.stdio.lock().unwrap_or_else(|e| e.into_inner());
56-
let stdio = &mut *guard;
57-
let (stdin, stdout) = (&mut stdio.0, &mut stdio.1);
58-
5952
let mut buf = String::new();
60-
let res = match send_request(stdin, stdout, req, &mut buf) {
53+
let res = match send_request(&mut self.stdin, &mut self.stdout, req, &mut buf) {
6154
Ok(res) => res,
6255
Err(err) => {
63-
let mut process = self.process.lock().unwrap_or_else(|e| e.into_inner());
56+
let result = self.process.child.try_wait();
6457
log::error!(
6558
"proc macro server crashed, server process state: {:?}, server request error: {:?}",
66-
process.child.try_wait(),
59+
result,
6760
err
6861
);
6962
let res = Response::Error(ResponseError {

0 commit comments

Comments
 (0)