2020-04-01 00:11:26 -05:00
|
|
|
//! RA Proc Macro Server
|
|
|
|
//!
|
|
|
|
//! This library is able to call compiled Rust custom derive dynamic libraries on arbitrary code.
|
2021-06-13 23:57:10 -05:00
|
|
|
//! The general idea here is based on <https://github.com/fedochet/rust-proc-macro-expander>.
|
2020-04-01 00:11:26 -05:00
|
|
|
//!
|
2020-04-20 13:26:10 -05:00
|
|
|
//! But we adapt it to better fit RA needs:
|
2020-04-01 00:11:26 -05:00
|
|
|
//!
|
2020-08-12 09:46:20 -05:00
|
|
|
//! * We use `tt` for proc-macro `TokenStream` server, it is easier to manipulate and interact with
|
2020-04-20 13:26:10 -05:00
|
|
|
//! RA than `proc-macro2` token stream.
|
2020-04-01 00:11:26 -05:00
|
|
|
//! * By **copying** the whole rustc `lib_proc_macro` code, we are able to build this with `stable`
|
2020-08-13 05:07:28 -05:00
|
|
|
//! rustc rather than `unstable`. (Although in general ABI compatibility is still an issue)…
|
2022-07-20 07:59:42 -05:00
|
|
|
|
2023-12-11 03:16:01 -06:00
|
|
|
#![cfg(any(feature = "sysroot-abi", rust_analyzer))]
|
2024-01-09 00:56:53 -06:00
|
|
|
#![cfg_attr(feature = "in-rust-tree", feature(rustc_private))]
|
|
|
|
#![feature(proc_macro_internals, proc_macro_diagnostic, proc_macro_span)]
|
2023-12-11 03:16:01 -06:00
|
|
|
#![warn(rust_2018_idioms, unused_lifetimes)]
|
2023-11-22 00:30:09 -06:00
|
|
|
#![allow(unreachable_pub, internal_features)]
|
2020-04-01 00:11:26 -05:00
|
|
|
|
2023-06-05 04:04:23 -05:00
|
|
|
extern crate proc_macro;
|
2024-01-09 00:56:53 -06:00
|
|
|
#[cfg(feature = "in-rust-tree")]
|
2024-01-04 04:02:20 -06:00
|
|
|
extern crate rustc_driver as _;
|
2021-07-12 09:47:47 -05:00
|
|
|
|
2023-06-05 04:04:23 -05:00
|
|
|
mod dylib;
|
|
|
|
mod server;
|
|
|
|
mod proc_macros;
|
2022-10-11 02:37:35 -05:00
|
|
|
|
2020-04-23 21:23:01 -05:00
|
|
|
use std::{
|
|
|
|
collections::{hash_map::Entry, HashMap},
|
2022-01-27 12:34:18 -06:00
|
|
|
env,
|
|
|
|
ffi::OsString,
|
|
|
|
fs,
|
2020-04-23 21:23:01 -05:00
|
|
|
path::{Path, PathBuf},
|
2022-08-30 06:51:24 -05:00
|
|
|
thread,
|
2020-04-24 23:30:28 -05:00
|
|
|
time::SystemTime,
|
2020-04-23 21:23:01 -05:00
|
|
|
};
|
|
|
|
|
internal: cleanup proc macro server error handlig
When dealing with proc macros, there are two very different kinds of
errors:
* first, usual errors of "proc macro panicked on this particular input"
* second, the proc macro server might day if the user, eg, kills it
First kind of errors are expected and are a normal output, while the
second kind are genuine IO-errors.
For this reason, we use a curious nested result here: `Result<Result<T,
E1>, E2>` pattern, which is 100% inspired by http://sled.rs/errors.html
2021-08-31 11:01:39 -05:00
|
|
|
use proc_macro_api::{
|
2024-01-03 03:35:07 -06:00
|
|
|
msg::{
|
|
|
|
self, deserialize_span_data_index_map, serialize_span_data_index_map, ExpnGlobals,
|
|
|
|
SpanMode, TokenId, CURRENT_API_VERSION,
|
|
|
|
},
|
internal: cleanup proc macro server error handlig
When dealing with proc macros, there are two very different kinds of
errors:
* first, usual errors of "proc macro panicked on this particular input"
* second, the proc macro server might day if the user, eg, kills it
First kind of errors are expected and are a normal output, while the
second kind are genuine IO-errors.
For this reason, we use a curious nested result here: `Result<Result<T,
E1>, E2>` pattern, which is 100% inspired by http://sled.rs/errors.html
2021-08-31 11:01:39 -05:00
|
|
|
ProcMacroKind,
|
|
|
|
};
|
2024-01-03 03:35:07 -06:00
|
|
|
use span::Span;
|
internal: cleanup proc macro server error handlig
When dealing with proc macros, there are two very different kinds of
errors:
* first, usual errors of "proc macro panicked on this particular input"
* second, the proc macro server might day if the user, eg, kills it
First kind of errors are expected and are a normal output, while the
second kind are genuine IO-errors.
For this reason, we use a curious nested result here: `Result<Result<T,
E1>, E2>` pattern, which is 100% inspired by http://sled.rs/errors.html
2021-08-31 11:01:39 -05:00
|
|
|
|
2024-01-03 03:35:07 -06:00
|
|
|
use crate::server::TokenStream;
|
2023-12-11 03:16:01 -06:00
|
|
|
|
2024-01-03 03:35:07 -06:00
|
|
|
// see `build.rs`
|
|
|
|
include!(concat!(env!("OUT_DIR"), "/rustc_version.rs"));
|
2023-12-11 03:16:01 -06:00
|
|
|
|
2024-01-03 03:35:07 -06:00
|
|
|
trait ProcMacroSrvSpan: tt::Span {
|
|
|
|
type Server: proc_macro::bridge::server::Server<TokenStream = TokenStream<Self>>;
|
|
|
|
fn make_server(call_site: Self, def_site: Self, mixed_site: Self) -> Self::Server;
|
2023-12-11 03:16:01 -06:00
|
|
|
}
|
2023-02-13 05:55:14 -06:00
|
|
|
|
2024-01-03 03:35:07 -06:00
|
|
|
impl ProcMacroSrvSpan for TokenId {
|
|
|
|
type Server = server::token_id::TokenIdServer;
|
|
|
|
|
|
|
|
fn make_server(call_site: Self, def_site: Self, mixed_site: Self) -> Self::Server {
|
|
|
|
Self::Server { interner: &server::SYMBOL_INTERNER, call_site, def_site, mixed_site }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl ProcMacroSrvSpan for Span {
|
|
|
|
type Server = server::rust_analyzer_span::RaSpanServer;
|
|
|
|
fn make_server(call_site: Self, def_site: Self, mixed_site: Self) -> Self::Server {
|
|
|
|
Self::Server {
|
|
|
|
interner: &server::SYMBOL_INTERNER,
|
|
|
|
call_site,
|
|
|
|
def_site,
|
|
|
|
mixed_site,
|
|
|
|
tracked_env_vars: Default::default(),
|
|
|
|
tracked_paths: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-05 04:04:23 -05:00
|
|
|
|
2020-04-23 21:23:01 -05:00
|
|
|
#[derive(Default)]
|
2023-06-05 04:04:23 -05:00
|
|
|
pub struct ProcMacroSrv {
|
2020-04-24 23:30:28 -05:00
|
|
|
expanders: HashMap<(PathBuf, SystemTime), dylib::Expander>,
|
2024-01-03 03:35:07 -06:00
|
|
|
span_mode: SpanMode,
|
2020-04-23 21:23:01 -05:00
|
|
|
}
|
2020-04-04 03:10:45 -05:00
|
|
|
|
2022-08-02 01:05:16 -05:00
|
|
|
const EXPANDER_STACK_SIZE: usize = 8 * 1024 * 1024;
|
|
|
|
|
2020-04-23 21:23:01 -05:00
|
|
|
impl ProcMacroSrv {
|
2024-01-03 03:35:07 -06:00
|
|
|
pub fn set_span_mode(&mut self, span_mode: SpanMode) {
|
|
|
|
self.span_mode = span_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn span_mode(&self) -> SpanMode {
|
|
|
|
self.span_mode
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn expand(
|
|
|
|
&mut self,
|
|
|
|
task: msg::ExpandMacro,
|
|
|
|
) -> Result<(msg::FlatTree, Vec<u32>), msg::PanicMessage> {
|
|
|
|
let span_mode = self.span_mode;
|
internal: cleanup proc macro server error handlig
When dealing with proc macros, there are two very different kinds of
errors:
* first, usual errors of "proc macro panicked on this particular input"
* second, the proc macro server might day if the user, eg, kills it
First kind of errors are expected and are a normal output, while the
second kind are genuine IO-errors.
For this reason, we use a curious nested result here: `Result<Result<T,
E1>, E2>` pattern, which is 100% inspired by http://sled.rs/errors.html
2021-08-31 11:01:39 -05:00
|
|
|
let expander = self.expander(task.lib.as_ref()).map_err(|err| {
|
|
|
|
debug_assert!(false, "should list macros before asking to expand");
|
2023-06-05 04:04:23 -05:00
|
|
|
msg::PanicMessage(format!("failed to load macro: {err}"))
|
internal: cleanup proc macro server error handlig
When dealing with proc macros, there are two very different kinds of
errors:
* first, usual errors of "proc macro panicked on this particular input"
* second, the proc macro server might day if the user, eg, kills it
First kind of errors are expected and are a normal output, while the
second kind are genuine IO-errors.
For this reason, we use a curious nested result here: `Result<Result<T,
E1>, E2>` pattern, which is 100% inspired by http://sled.rs/errors.html
2021-08-31 11:01:39 -05:00
|
|
|
})?;
|
2020-12-11 07:57:50 -06:00
|
|
|
|
2022-01-27 12:34:18 -06:00
|
|
|
let prev_env = EnvSnapshot::new();
|
2020-12-11 07:57:50 -06:00
|
|
|
for (k, v) in &task.env {
|
|
|
|
env::set_var(k, v);
|
|
|
|
}
|
2024-01-03 03:35:07 -06:00
|
|
|
let prev_working_dir = match &task.current_dir {
|
2022-01-27 06:54:06 -06:00
|
|
|
Some(dir) => {
|
|
|
|
let prev_working_dir = std::env::current_dir().ok();
|
2024-01-03 03:35:07 -06:00
|
|
|
if let Err(err) = std::env::set_current_dir(dir) {
|
2023-01-09 12:36:22 -06:00
|
|
|
eprintln!("Failed to set the current working dir to {dir}. Error: {err:?}")
|
2022-01-27 06:54:06 -06:00
|
|
|
}
|
|
|
|
prev_working_dir
|
|
|
|
}
|
|
|
|
None => None,
|
|
|
|
};
|
2020-12-11 07:57:50 -06:00
|
|
|
|
2023-12-11 03:16:01 -06:00
|
|
|
let ExpnGlobals { def_site, call_site, mixed_site, .. } = task.has_global_spans;
|
2024-01-03 03:35:07 -06:00
|
|
|
|
|
|
|
let result = match span_mode {
|
|
|
|
SpanMode::Id => {
|
|
|
|
expand_id(task, expander, def_site, call_site, mixed_site).map(|it| (it, vec![]))
|
2022-07-21 14:35:15 -05:00
|
|
|
}
|
2024-01-03 03:35:07 -06:00
|
|
|
SpanMode::RustAnalyzer => {
|
|
|
|
expand_ra_span(task, expander, def_site, call_site, mixed_site)
|
|
|
|
}
|
|
|
|
};
|
2020-12-11 07:57:50 -06:00
|
|
|
|
2022-01-27 12:34:18 -06:00
|
|
|
prev_env.rollback();
|
|
|
|
|
2022-01-27 06:54:06 -06:00
|
|
|
if let Some(dir) = prev_working_dir {
|
|
|
|
if let Err(err) = std::env::set_current_dir(&dir) {
|
|
|
|
eprintln!(
|
|
|
|
"Failed to set the current working dir to {}. Error: {:?}",
|
|
|
|
dir.display(),
|
|
|
|
err
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2020-12-11 07:57:50 -06:00
|
|
|
|
2023-06-05 04:04:23 -05:00
|
|
|
result.map_err(msg::PanicMessage)
|
2020-04-04 03:10:45 -05:00
|
|
|
}
|
|
|
|
|
2023-06-05 04:04:23 -05:00
|
|
|
pub fn list_macros(
|
internal: cleanup proc macro server error handlig
When dealing with proc macros, there are two very different kinds of
errors:
* first, usual errors of "proc macro panicked on this particular input"
* second, the proc macro server might day if the user, eg, kills it
First kind of errors are expected and are a normal output, while the
second kind are genuine IO-errors.
For this reason, we use a curious nested result here: `Result<Result<T,
E1>, E2>` pattern, which is 100% inspired by http://sled.rs/errors.html
2021-08-31 11:01:39 -05:00
|
|
|
&mut self,
|
|
|
|
dylib_path: &Path,
|
|
|
|
) -> Result<Vec<(String, ProcMacroKind)>, String> {
|
|
|
|
let expander = self.expander(dylib_path)?;
|
|
|
|
Ok(expander.list_macros())
|
2020-04-23 21:23:01 -05:00
|
|
|
}
|
2020-04-20 13:26:10 -05:00
|
|
|
|
2020-04-23 21:23:01 -05:00
|
|
|
fn expander(&mut self, path: &Path) -> Result<&dylib::Expander, String> {
|
2023-01-09 12:36:22 -06:00
|
|
|
let time = fs::metadata(path)
|
|
|
|
.and_then(|it| it.modified())
|
|
|
|
.map_err(|err| format!("Failed to get file metadata for {}: {err}", path.display()))?;
|
2020-04-24 23:30:28 -05:00
|
|
|
|
|
|
|
Ok(match self.expanders.entry((path.to_path_buf(), time)) {
|
2023-01-09 12:36:22 -06:00
|
|
|
Entry::Vacant(v) => {
|
|
|
|
v.insert(dylib::Expander::new(path).map_err(|err| {
|
|
|
|
format!("Cannot create expander for {}: {err}", path.display())
|
|
|
|
})?)
|
|
|
|
}
|
2020-04-23 21:23:01 -05:00
|
|
|
Entry::Occupied(e) => e.into_mut(),
|
|
|
|
})
|
|
|
|
}
|
2020-04-01 00:11:26 -05:00
|
|
|
}
|
2020-04-09 12:43:47 -05:00
|
|
|
|
2024-01-03 03:35:07 -06:00
|
|
|
fn expand_id(
|
|
|
|
task: msg::ExpandMacro,
|
|
|
|
expander: &dylib::Expander,
|
|
|
|
def_site: usize,
|
|
|
|
call_site: usize,
|
|
|
|
mixed_site: usize,
|
|
|
|
) -> Result<msg::FlatTree, String> {
|
|
|
|
let def_site = TokenId(def_site as u32);
|
|
|
|
let call_site = TokenId(call_site as u32);
|
|
|
|
let mixed_site = TokenId(mixed_site as u32);
|
|
|
|
|
|
|
|
let macro_body = task.macro_body.to_subtree_unresolved(CURRENT_API_VERSION);
|
|
|
|
let attributes = task.attributes.map(|it| it.to_subtree_unresolved(CURRENT_API_VERSION));
|
|
|
|
let result = thread::scope(|s| {
|
|
|
|
let thread = thread::Builder::new()
|
|
|
|
.stack_size(EXPANDER_STACK_SIZE)
|
|
|
|
.name(task.macro_name.clone())
|
|
|
|
.spawn_scoped(s, || {
|
|
|
|
expander
|
|
|
|
.expand(
|
|
|
|
&task.macro_name,
|
|
|
|
macro_body,
|
|
|
|
attributes,
|
|
|
|
def_site,
|
|
|
|
call_site,
|
|
|
|
mixed_site,
|
|
|
|
)
|
|
|
|
.map(|it| msg::FlatTree::new_raw(&it, CURRENT_API_VERSION))
|
|
|
|
});
|
|
|
|
let res = match thread {
|
|
|
|
Ok(handle) => handle.join(),
|
|
|
|
Err(e) => std::panic::resume_unwind(Box::new(e)),
|
|
|
|
};
|
|
|
|
|
|
|
|
match res {
|
|
|
|
Ok(res) => res,
|
|
|
|
Err(e) => std::panic::resume_unwind(e),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expand_ra_span(
|
|
|
|
task: msg::ExpandMacro,
|
|
|
|
expander: &dylib::Expander,
|
|
|
|
def_site: usize,
|
|
|
|
call_site: usize,
|
|
|
|
mixed_site: usize,
|
|
|
|
) -> Result<(msg::FlatTree, Vec<u32>), String> {
|
|
|
|
let mut span_data_table = deserialize_span_data_index_map(&task.span_data_table);
|
|
|
|
|
|
|
|
let def_site = span_data_table[def_site];
|
|
|
|
let call_site = span_data_table[call_site];
|
|
|
|
let mixed_site = span_data_table[mixed_site];
|
|
|
|
|
|
|
|
let macro_body = task.macro_body.to_subtree_resolved(CURRENT_API_VERSION, &span_data_table);
|
|
|
|
let attributes =
|
|
|
|
task.attributes.map(|it| it.to_subtree_resolved(CURRENT_API_VERSION, &span_data_table));
|
|
|
|
let result = thread::scope(|s| {
|
|
|
|
let thread = thread::Builder::new()
|
|
|
|
.stack_size(EXPANDER_STACK_SIZE)
|
|
|
|
.name(task.macro_name.clone())
|
|
|
|
.spawn_scoped(s, || {
|
|
|
|
expander
|
|
|
|
.expand(
|
|
|
|
&task.macro_name,
|
|
|
|
macro_body,
|
|
|
|
attributes,
|
|
|
|
def_site,
|
|
|
|
call_site,
|
|
|
|
mixed_site,
|
|
|
|
)
|
|
|
|
.map(|it| {
|
|
|
|
(
|
|
|
|
msg::FlatTree::new(&it, CURRENT_API_VERSION, &mut span_data_table),
|
|
|
|
serialize_span_data_index_map(&span_data_table),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
});
|
|
|
|
let res = match thread {
|
|
|
|
Ok(handle) => handle.join(),
|
|
|
|
Err(e) => std::panic::resume_unwind(Box::new(e)),
|
|
|
|
};
|
|
|
|
|
|
|
|
match res {
|
|
|
|
Ok(res) => res,
|
|
|
|
Err(e) => std::panic::resume_unwind(e),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2023-06-05 04:04:23 -05:00
|
|
|
pub struct PanicMessage {
|
|
|
|
message: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PanicMessage {
|
2023-12-11 03:16:01 -06:00
|
|
|
pub fn into_string(self) -> Option<String> {
|
|
|
|
self.message
|
2023-06-05 04:04:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 12:34:18 -06:00
|
|
|
struct EnvSnapshot {
|
|
|
|
vars: HashMap<OsString, OsString>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EnvSnapshot {
|
|
|
|
fn new() -> EnvSnapshot {
|
|
|
|
EnvSnapshot { vars: env::vars_os().collect() }
|
|
|
|
}
|
|
|
|
|
2023-06-05 04:04:23 -05:00
|
|
|
fn rollback(self) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for EnvSnapshot {
|
|
|
|
fn drop(&mut self) {
|
2022-01-27 12:34:18 -06:00
|
|
|
for (name, value) in env::vars_os() {
|
2023-06-05 04:04:23 -05:00
|
|
|
let old_value = self.vars.remove(&name);
|
2022-01-27 12:34:18 -06:00
|
|
|
if old_value != Some(value) {
|
|
|
|
match old_value {
|
|
|
|
None => env::remove_var(name),
|
|
|
|
Some(old_value) => env::set_var(name, old_value),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-05 04:04:23 -05:00
|
|
|
for (name, old_value) in self.vars.drain() {
|
2022-01-27 12:34:18 -06:00
|
|
|
env::set_var(name, old_value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-05 04:04:23 -05:00
|
|
|
#[cfg(test)]
|
2022-10-11 02:37:35 -05:00
|
|
|
mod tests;
|
2020-04-16 08:13:57 -05:00
|
|
|
|
2020-04-09 12:43:47 -05:00
|
|
|
#[cfg(test)]
|
2022-10-11 02:37:35 -05:00
|
|
|
pub fn proc_macro_test_dylib_path() -> std::path::PathBuf {
|
|
|
|
proc_macro_test::PROC_MACRO_TEST_LOCATION.into()
|
|
|
|
}
|