Skip to content

Commit 042528a

Browse files
committed
Move proc-macro protocol into legacy module
1 parent 0affbd4 commit 042528a

File tree

7 files changed

+38
-33
lines changed

7 files changed

+38
-33
lines changed

src/tools/rust-analyzer/crates/load-cargo/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub fn load_workspace(
8282
};
8383
match &proc_macro_server {
8484
Ok(server) => {
85-
tracing::info!(path=%server.path(), "Proc-macro server started")
85+
tracing::info!(path=%server.server_path(), "Proc-macro server started")
8686
}
8787
Err((e, _)) => {
8888
tracing::info!(%e, "Failed to start proc-macro server")

src/tools/rust-analyzer/crates/proc-macro-api/src/msg.rs renamed to src/tools/rust-analyzer/crates/proc-macro-api/src/legacy_protocol/msg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use serde_derive::{Deserialize, Serialize};
99

1010
use crate::ProcMacroKind;
1111

12-
pub use crate::msg::flat::{
12+
pub use self::flat::{
1313
deserialize_span_data_index_map, serialize_span_data_index_map, FlatTree, SpanDataIndexMap,
1414
};
1515
pub use span::TokenId;

src/tools/rust-analyzer/crates/proc-macro-api/src/msg/flat.rs renamed to src/tools/rust-analyzer/crates/proc-macro-api/src/legacy_protocol/msg/flat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use span::{
4444
EditionedFileId, ErasedFileAstId, Span, SpanAnchor, SyntaxContextId, TextRange, TokenId,
4545
};
4646

47-
use crate::msg::{ENCODE_CLOSE_SPAN_VERSION, EXTENDED_LEAF_DATA};
47+
use crate::legacy_protocol::msg::{ENCODE_CLOSE_SPAN_VERSION, EXTENDED_LEAF_DATA};
4848

4949
pub type SpanDataIndexMap =
5050
indexmap::IndexSet<Span, std::hash::BuildHasherDefault<rustc_hash::FxHasher>>;

src/tools/rust-analyzer/crates/proc-macro-api/src/lib.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@
55
//! is used to provide basic infrastructure for communication between two
66
//! processes: Client (RA itself), Server (the external program)
77
8-
pub mod json;
9-
pub mod msg;
8+
pub mod legacy_protocol {
9+
pub mod json;
10+
pub mod msg;
11+
}
1012
mod process;
1113

1214
use paths::{AbsPath, AbsPathBuf};
1315
use span::Span;
1416
use std::{fmt, io, sync::Arc};
1517

1618
use crate::{
17-
msg::{
19+
legacy_protocol::msg::{
1820
deserialize_span_data_index_map, flat::serialize_span_data_index_map, ExpandMacro,
19-
ExpnGlobals, FlatTree, PanicMessage, SpanDataIndexMap, HAS_GLOBAL_SPANS,
20-
RUST_ANALYZER_SPAN_SUPPORT,
21+
ExpandMacroData, ExpnGlobals, FlatTree, PanicMessage, Request, Response, SpanDataIndexMap,
22+
HAS_GLOBAL_SPANS, RUST_ANALYZER_SPAN_SUPPORT,
2123
},
2224
process::ProcMacroServerProcess,
2325
};
@@ -54,10 +56,10 @@ impl MacroDylib {
5456
}
5557
}
5658

57-
/// A handle to a specific macro (a `#[proc_macro]` annotated function).
59+
/// A handle to a specific proc-macro (a `#[proc_macro]` annotated function).
5860
///
59-
/// It exists within a context of a specific [`ProcMacroProcess`] -- currently
60-
/// we share a single expander process for all macros.
61+
/// It exists within the context of a specific proc-macro server -- currently
62+
/// we share a single expander process for all macros within a workspace.
6163
#[derive(Debug, Clone)]
6264
pub struct ProcMacro {
6365
process: Arc<ProcMacroServerProcess>,
@@ -104,10 +106,11 @@ impl ProcMacroClient {
104106
Ok(ProcMacroClient { process: Arc::new(process), path: process_path.to_owned() })
105107
}
106108

107-
pub fn path(&self) -> &AbsPath {
109+
pub fn server_path(&self) -> &AbsPath {
108110
&self.path
109111
}
110112

113+
/// Loads a proc-macro dylib into the server process returning a list of `ProcMacro`s loaded.
111114
pub fn load_dylib(&self, dylib: MacroDylib) -> Result<Vec<ProcMacro>, ServerError> {
112115
let _p = tracing::info_span!("ProcMacroServer::load_dylib").entered();
113116
let macros = self.process.find_proc_macros(&dylib.path)?;
@@ -158,7 +161,7 @@ impl ProcMacro {
158161
let call_site = span_data_table.insert_full(call_site).0;
159162
let mixed_site = span_data_table.insert_full(mixed_site).0;
160163
let task = ExpandMacro {
161-
data: msg::ExpandMacroData {
164+
data: ExpandMacroData {
162165
macro_body: FlatTree::new(subtree, version, &mut span_data_table),
163166
macro_name: self.name.to_string(),
164167
attributes: attr
@@ -180,13 +183,13 @@ impl ProcMacro {
180183
current_dir,
181184
};
182185

183-
let response = self.process.send_task(msg::Request::ExpandMacro(Box::new(task)))?;
186+
let response = self.process.send_task(Request::ExpandMacro(Box::new(task)))?;
184187

185188
match response {
186-
msg::Response::ExpandMacro(it) => {
189+
Response::ExpandMacro(it) => {
187190
Ok(it.map(|tree| FlatTree::to_subtree_resolved(tree, version, &span_data_table)))
188191
}
189-
msg::Response::ExpandMacroExtended(it) => Ok(it.map(|resp| {
192+
Response::ExpandMacroExtended(it) => Ok(it.map(|resp| {
190193
FlatTree::to_subtree_resolved(
191194
resp.tree,
192195
version,

src/tools/rust-analyzer/crates/proc-macro-api/src/process.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ use paths::AbsPath;
1111
use stdx::JodChild;
1212

1313
use crate::{
14-
json::{read_json, write_json},
15-
msg::{Message, Request, Response, SpanMode, CURRENT_API_VERSION, RUST_ANALYZER_SPAN_SUPPORT},
14+
legacy_protocol::{
15+
json::{read_json, write_json},
16+
msg::{
17+
Message, Request, Response, ServerConfig, SpanMode, CURRENT_API_VERSION,
18+
RUST_ANALYZER_SPAN_SUPPORT,
19+
},
20+
},
1621
ProcMacroKind, ServerError,
1722
};
1823

@@ -40,8 +45,8 @@ impl ProcMacroServerProcess {
4045
env: impl IntoIterator<Item = (impl AsRef<std::ffi::OsStr>, impl AsRef<std::ffi::OsStr>)>
4146
+ Clone,
4247
) -> io::Result<ProcMacroServerProcess> {
43-
let create_srv = |null_stderr| {
44-
let mut process = Process::run(process_path, env.clone(), null_stderr)?;
48+
let create_srv = || {
49+
let mut process = Process::run(process_path, env.clone())?;
4550
let (stdin, stdout) = process.stdio().expect("couldn't access child stdio");
4651

4752
io::Result::Ok(ProcMacroServerProcess {
@@ -51,7 +56,7 @@ impl ProcMacroServerProcess {
5156
exited: OnceLock::new(),
5257
})
5358
};
54-
let mut srv = create_srv(true)?;
59+
let mut srv = create_srv()?;
5560
tracing::info!("sending proc-macro server version check");
5661
match srv.version_check() {
5762
Ok(v) if v > CURRENT_API_VERSION => Err(io::Error::new(
@@ -62,7 +67,6 @@ impl ProcMacroServerProcess {
6267
)),
6368
Ok(v) => {
6469
tracing::info!("Proc-macro server version: {v}");
65-
srv = create_srv(false)?;
6670
srv.version = v;
6771
if srv.version >= RUST_ANALYZER_SPAN_SUPPORT {
6872
if let Ok(mode) = srv.enable_rust_analyzer_spans() {
@@ -73,8 +77,10 @@ impl ProcMacroServerProcess {
7377
Ok(srv)
7478
}
7579
Err(e) => {
76-
tracing::info!(%e, "proc-macro version check failed, restarting and assuming version 0");
77-
create_srv(false)
80+
tracing::info!(%e, "proc-macro version check failed");
81+
Err(
82+
io::Error::new(io::ErrorKind::Other, format!("proc-macro server version check failed: {e}")),
83+
)
7884
}
7985
}
8086
}
@@ -98,13 +104,11 @@ impl ProcMacroServerProcess {
98104
}
99105

100106
fn enable_rust_analyzer_spans(&self) -> Result<SpanMode, ServerError> {
101-
let request = Request::SetConfig(crate::msg::ServerConfig {
102-
span_mode: crate::msg::SpanMode::RustAnalyzer,
103-
});
107+
let request = Request::SetConfig(ServerConfig { span_mode: SpanMode::RustAnalyzer });
104108
let response = self.send_task(request)?;
105109

106110
match response {
107-
Response::SetConfig(crate::msg::ServerConfig { span_mode }) => Ok(span_mode),
111+
Response::SetConfig(ServerConfig { span_mode }) => Ok(span_mode),
108112
_ => Err(ServerError { message: "unexpected response".to_owned(), io: None }),
109113
}
110114
}
@@ -182,9 +186,8 @@ impl Process {
182186
fn run(
183187
path: &AbsPath,
184188
env: impl IntoIterator<Item = (impl AsRef<std::ffi::OsStr>, impl AsRef<std::ffi::OsStr>)>,
185-
null_stderr: bool,
186189
) -> io::Result<Process> {
187-
let child = JodChild(mk_child(path, env, null_stderr)?);
190+
let child = JodChild(mk_child(path, env)?);
188191
Ok(Process { child })
189192
}
190193

@@ -200,15 +203,14 @@ impl Process {
200203
fn mk_child(
201204
path: &AbsPath,
202205
env: impl IntoIterator<Item = (impl AsRef<std::ffi::OsStr>, impl AsRef<std::ffi::OsStr>)>,
203-
null_stderr: bool,
204206
) -> io::Result<Child> {
205207
#[allow(clippy::disallowed_methods)]
206208
let mut cmd = Command::new(path);
207209
cmd.envs(env)
208210
.env("RUST_ANALYZER_INTERNALS_DO_NOT_USE", "this is unstable")
209211
.stdin(Stdio::piped())
210212
.stdout(Stdio::piped())
211-
.stderr(if null_stderr { Stdio::null() } else { Stdio::inherit() });
213+
.stderr(Stdio::inherit());
212214
if cfg!(windows) {
213215
let mut path_var = std::ffi::OsString::new();
214216
path_var.push(path.parent().unwrap().parent().unwrap());

src/tools/rust-analyzer/crates/proc-macro-srv-cli/src/main_loop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! The main loop of the proc-macro server.
22
use std::io;
33

4-
use proc_macro_api::{
4+
use proc_macro_api::legacy_protocol::{
55
json::{read_json, write_json},
66
msg::{
77
self, deserialize_span_data_index_map, serialize_span_data_index_map, ExpandMacroData,

0 commit comments

Comments
 (0)