Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/proc-macro-srv/src/lib.rs')
-rw-r--r--crates/proc-macro-srv/src/lib.rs56
1 files changed, 34 insertions, 22 deletions
diff --git a/crates/proc-macro-srv/src/lib.rs b/crates/proc-macro-srv/src/lib.rs
index ee70fe7d4f..2aac379031 100644
--- a/crates/proc-macro-srv/src/lib.rs
+++ b/crates/proc-macro-srv/src/lib.rs
@@ -10,17 +10,16 @@
//! * By **copying** the whole rustc `lib_proc_macro` code, we are able to build this with `stable`
//! rustc rather than `unstable`. (Although in general ABI compatibility is still an issue)…
+#![cfg(feature = "sysroot-abi")]
+#![feature(proc_macro_internals, proc_macro_diagnostic, proc_macro_span)]
#![warn(rust_2018_idioms, unused_lifetimes, semicolon_in_expressions_from_macros)]
-#![cfg_attr(
- feature = "sysroot-abi",
- feature(proc_macro_internals, proc_macro_diagnostic, proc_macro_span)
-)]
#![allow(unreachable_pub)]
-mod dylib;
-mod abis;
+extern crate proc_macro;
-pub mod cli;
+mod dylib;
+mod server;
+mod proc_macros;
use std::{
collections::{hash_map::Entry, HashMap},
@@ -32,25 +31,25 @@ use std::{
time::SystemTime,
};
-use proc_macro_api::{
- msg::{ExpandMacro, FlatTree, PanicMessage},
- ProcMacroKind,
-};
+use proc_macro_api::{msg, ProcMacroKind};
use ::tt::token_id as tt;
+// see `build.rs`
+include!(concat!(env!("OUT_DIR"), "/rustc_version.rs"));
+
#[derive(Default)]
-pub(crate) struct ProcMacroSrv {
+pub struct ProcMacroSrv {
expanders: HashMap<(PathBuf, SystemTime), dylib::Expander>,
}
const EXPANDER_STACK_SIZE: usize = 8 * 1024 * 1024;
impl ProcMacroSrv {
- pub fn expand(&mut self, task: ExpandMacro) -> Result<FlatTree, PanicMessage> {
+ pub fn expand(&mut self, task: msg::ExpandMacro) -> Result<msg::FlatTree, msg::PanicMessage> {
let expander = self.expander(task.lib.as_ref()).map_err(|err| {
debug_assert!(false, "should list macros before asking to expand");
- PanicMessage(format!("failed to load macro: {err}"))
+ msg::PanicMessage(format!("failed to load macro: {err}"))
})?;
let prev_env = EnvSnapshot::new();
@@ -77,7 +76,7 @@ impl ProcMacroSrv {
.spawn_scoped(s, || {
expander
.expand(&task.macro_name, &macro_body, attributes.as_ref())
- .map(|it| FlatTree::new(&it))
+ .map(|it| msg::FlatTree::new(&it))
});
let res = match thread {
Ok(handle) => handle.join(),
@@ -102,10 +101,10 @@ impl ProcMacroSrv {
}
}
- result.map_err(PanicMessage)
+ result.map_err(msg::PanicMessage)
}
- pub(crate) fn list_macros(
+ pub fn list_macros(
&mut self,
dylib_path: &Path,
) -> Result<Vec<(String, ProcMacroKind)>, String> {
@@ -129,6 +128,16 @@ impl ProcMacroSrv {
}
}
+pub struct PanicMessage {
+ message: Option<String>,
+}
+
+impl PanicMessage {
+ pub fn as_str(&self) -> Option<String> {
+ self.message.clone()
+ }
+}
+
struct EnvSnapshot {
vars: HashMap<OsString, OsString>,
}
@@ -138,10 +147,13 @@ impl EnvSnapshot {
EnvSnapshot { vars: env::vars_os().collect() }
}
- fn rollback(self) {
- let mut old_vars = self.vars;
+ fn rollback(self) {}
+}
+
+impl Drop for EnvSnapshot {
+ fn drop(&mut self) {
for (name, value) in env::vars_os() {
- let old_value = old_vars.remove(&name);
+ let old_value = self.vars.remove(&name);
if old_value != Some(value) {
match old_value {
None => env::remove_var(name),
@@ -149,13 +161,13 @@ impl EnvSnapshot {
}
}
}
- for (name, old_value) in old_vars {
+ for (name, old_value) in self.vars.drain() {
env::set_var(name, old_value)
}
}
}
-#[cfg(all(feature = "sysroot-abi", test))]
+#[cfg(test)]
mod tests;
#[cfg(test)]