Skip to content

Commit 4c62359

Browse files
committed
Faster env snapshotting in proc-macro-srv
1 parent ef4b974 commit 4c62359

File tree

6 files changed

+147
-117
lines changed

6 files changed

+147
-117
lines changed

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -156,23 +156,26 @@ impl ProcMacro {
156156
let call_site = span_data_table.insert_full(call_site).0;
157157
let mixed_site = span_data_table.insert_full(mixed_site).0;
158158
let task = ExpandMacro {
159-
macro_body: FlatTree::new(subtree, version, &mut span_data_table),
160-
macro_name: self.name.to_string(),
161-
attributes: attr.map(|subtree| FlatTree::new(subtree, version, &mut span_data_table)),
159+
data: msg::ExpandMacroData {
160+
macro_body: FlatTree::new(subtree, version, &mut span_data_table),
161+
macro_name: self.name.to_string(),
162+
attributes: attr
163+
.map(|subtree| FlatTree::new(subtree, version, &mut span_data_table)),
164+
has_global_spans: ExpnGlobals {
165+
serialize: version >= HAS_GLOBAL_SPANS,
166+
def_site,
167+
call_site,
168+
mixed_site,
169+
},
170+
span_data_table: if version >= RUST_ANALYZER_SPAN_SUPPORT {
171+
serialize_span_data_index_map(&span_data_table)
172+
} else {
173+
Vec::new()
174+
},
175+
},
162176
lib: self.dylib_path.to_path_buf().into(),
163177
env: env.into(),
164178
current_dir,
165-
has_global_spans: ExpnGlobals {
166-
serialize: version >= HAS_GLOBAL_SPANS,
167-
def_site,
168-
call_site,
169-
mixed_site,
170-
},
171-
span_data_table: if version >= RUST_ANALYZER_SPAN_SUPPORT {
172-
serialize_span_data_index_map(&span_data_table)
173-
} else {
174-
Vec::new()
175-
},
176179
};
177180

178181
let response = self.process.send_task(msg::Request::ExpandMacro(Box::new(task)))?;

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

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ pub struct PanicMessage(pub String);
7272

7373
#[derive(Debug, Serialize, Deserialize)]
7474
pub struct ExpandMacro {
75+
pub lib: Utf8PathBuf,
76+
/// Environment variables to set during macro expansion.
77+
pub env: Vec<(String, String)>,
78+
pub current_dir: Option<String>,
79+
#[serde(flatten)]
80+
pub data: ExpandMacroData,
81+
}
82+
83+
#[derive(Debug, Serialize, Deserialize)]
84+
pub struct ExpandMacroData {
7585
/// Argument of macro call.
7686
///
7787
/// In custom derive this will be a struct or enum; in attribute-like macro - underlying
@@ -86,13 +96,6 @@ pub struct ExpandMacro {
8696

8797
/// Possible attributes for the attribute-like macros.
8898
pub attributes: Option<FlatTree>,
89-
90-
pub lib: Utf8PathBuf,
91-
92-
/// Environment variables to set during macro expansion.
93-
pub env: Vec<(String, String)>,
94-
95-
pub current_dir: Option<String>,
9699
/// marker for serde skip stuff
97100
#[serde(skip_serializing_if = "ExpnGlobals::skip_serializing_if")]
98101
#[serde(default)]
@@ -268,25 +271,30 @@ mod tests {
268271
let tt = fixture_token_tree();
269272
let mut span_data_table = Default::default();
270273
let task = ExpandMacro {
271-
macro_body: FlatTree::new(&tt, CURRENT_API_VERSION, &mut span_data_table),
272-
macro_name: Default::default(),
273-
attributes: None,
274+
data: ExpandMacroData {
275+
macro_body: FlatTree::new(&tt, CURRENT_API_VERSION, &mut span_data_table),
276+
macro_name: Default::default(),
277+
attributes: None,
278+
has_global_spans: ExpnGlobals {
279+
serialize: true,
280+
def_site: 0,
281+
call_site: 0,
282+
mixed_site: 0,
283+
},
284+
span_data_table: Vec::new(),
285+
},
274286
lib: Utf8PathBuf::from_path_buf(std::env::current_dir().unwrap()).unwrap(),
275287
env: Default::default(),
276288
current_dir: Default::default(),
277-
has_global_spans: ExpnGlobals {
278-
serialize: true,
279-
def_site: 0,
280-
call_site: 0,
281-
mixed_site: 0,
282-
},
283-
span_data_table: Vec::new(),
284289
};
285290

286291
let json = serde_json::to_string(&task).unwrap();
287292
// println!("{}", json);
288293
let back: ExpandMacro = serde_json::from_str(&json).unwrap();
289294

290-
assert_eq!(tt, back.macro_body.to_subtree_resolved(CURRENT_API_VERSION, &span_data_table));
295+
assert_eq!(
296+
tt,
297+
back.data.macro_body.to_subtree_resolved(CURRENT_API_VERSION, &span_data_table)
298+
);
291299
}
292300
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@ fn run() -> io::Result<()> {
3333
#[cfg(any(feature = "sysroot-abi", rust_analyzer))]
3434
fn run() -> io::Result<()> {
3535
use proc_macro_api::msg::{self, Message};
36+
use proc_macro_srv::EnvSnapshot;
3637

3738
let read_request = |buf: &mut String| msg::Request::read(&mut io::stdin().lock(), buf);
3839

3940
let write_response = |msg: msg::Response| msg.write(&mut io::stdout().lock());
4041

41-
let mut srv = proc_macro_srv::ProcMacroSrv::default();
42+
let env = EnvSnapshot::new();
43+
let mut srv = proc_macro_srv::ProcMacroSrv::new(&env);
4244
let mut buf = String::new();
4345

4446
while let Some(req) = read_request(&mut buf)? {

0 commit comments

Comments
 (0)