-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: Support the 1.64 nightly proc macro ABI #12795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
//! Macro ABI for version 1.63 of rustc | ||
|
||
#[allow(dead_code)] | ||
#[doc(hidden)] | ||
mod proc_macro; | ||
|
||
#[allow(dead_code)] | ||
#[doc(hidden)] | ||
mod rustc_server; | ||
|
||
use libloading::Library; | ||
use proc_macro_api::ProcMacroKind; | ||
|
||
use super::PanicMessage; | ||
|
||
pub(crate) struct Abi { | ||
exported_macros: Vec<proc_macro::bridge::client::ProcMacro>, | ||
} | ||
|
||
impl From<proc_macro::bridge::PanicMessage> for PanicMessage { | ||
fn from(p: proc_macro::bridge::PanicMessage) -> Self { | ||
Self { message: p.as_str().map(|s| s.to_string()) } | ||
} | ||
} | ||
|
||
impl Abi { | ||
pub unsafe fn from_lib(lib: &Library, symbol_name: String) -> Result<Abi, libloading::Error> { | ||
let macros: libloading::Symbol<&&[proc_macro::bridge::client::ProcMacro]> = | ||
lib.get(symbol_name.as_bytes())?; | ||
Ok(Self { exported_macros: macros.to_vec() }) | ||
} | ||
|
||
pub fn expand( | ||
&self, | ||
macro_name: &str, | ||
macro_body: &tt::Subtree, | ||
attributes: Option<&tt::Subtree>, | ||
) -> Result<tt::Subtree, PanicMessage> { | ||
let parsed_body = rustc_server::TokenStream::with_subtree(macro_body.clone()); | ||
|
||
let parsed_attributes = attributes.map_or(rustc_server::TokenStream::new(), |attr| { | ||
rustc_server::TokenStream::with_subtree(attr.clone()) | ||
}); | ||
|
||
for proc_macro in &self.exported_macros { | ||
match proc_macro { | ||
proc_macro::bridge::client::ProcMacro::CustomDerive { | ||
trait_name, client, .. | ||
} if *trait_name == macro_name => { | ||
let res = client.run( | ||
&proc_macro::bridge::server::SameThread, | ||
rustc_server::Rustc::default(), | ||
parsed_body, | ||
true, | ||
); | ||
return res.map(|it| it.into_subtree()).map_err(PanicMessage::from); | ||
} | ||
proc_macro::bridge::client::ProcMacro::Bang { name, client } | ||
if *name == macro_name => | ||
{ | ||
let res = client.run( | ||
&proc_macro::bridge::server::SameThread, | ||
rustc_server::Rustc::default(), | ||
parsed_body, | ||
true, | ||
); | ||
return res.map(|it| it.into_subtree()).map_err(PanicMessage::from); | ||
} | ||
proc_macro::bridge::client::ProcMacro::Attr { name, client } | ||
if *name == macro_name => | ||
{ | ||
let res = client.run( | ||
&proc_macro::bridge::server::SameThread, | ||
rustc_server::Rustc::default(), | ||
parsed_attributes, | ||
parsed_body, | ||
true, | ||
); | ||
return res.map(|it| it.into_subtree()).map_err(PanicMessage::from); | ||
} | ||
_ => continue, | ||
} | ||
} | ||
|
||
Err(proc_macro::bridge::PanicMessage::String("Nothing to expand".to_string()).into()) | ||
} | ||
|
||
pub fn list_macros(&self) -> Vec<(String, ProcMacroKind)> { | ||
self.exported_macros | ||
.iter() | ||
.map(|proc_macro| match proc_macro { | ||
proc_macro::bridge::client::ProcMacro::CustomDerive { trait_name, .. } => { | ||
(trait_name.to_string(), ProcMacroKind::CustomDerive) | ||
} | ||
proc_macro::bridge::client::ProcMacro::Bang { name, .. } => { | ||
(name.to_string(), ProcMacroKind::FuncLike) | ||
} | ||
proc_macro::bridge::client::ProcMacro::Attr { name, .. } => { | ||
(name.to_string(), ProcMacroKind::Attr) | ||
} | ||
}) | ||
.collect() | ||
} | ||
} |
156 changes: 156 additions & 0 deletions
156
crates/proc-macro-srv/src/abis/abi_1_64/proc_macro/bridge/buffer.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
//! Buffer management for same-process client<->server communication. | ||
|
||
use std::io::{self, Write}; | ||
use std::mem; | ||
use std::ops::{Deref, DerefMut}; | ||
use std::slice; | ||
|
||
#[repr(C)] | ||
pub struct Buffer { | ||
data: *mut u8, | ||
len: usize, | ||
capacity: usize, | ||
reserve: extern "C" fn(Buffer, usize) -> Buffer, | ||
drop: extern "C" fn(Buffer), | ||
} | ||
|
||
unsafe impl Sync for Buffer {} | ||
unsafe impl Send for Buffer {} | ||
|
||
impl Default for Buffer { | ||
#[inline] | ||
fn default() -> Self { | ||
Self::from(vec![]) | ||
} | ||
} | ||
|
||
impl Deref for Buffer { | ||
type Target = [u8]; | ||
#[inline] | ||
fn deref(&self) -> &[u8] { | ||
unsafe { slice::from_raw_parts(self.data as *const u8, self.len) } | ||
} | ||
} | ||
|
||
impl DerefMut for Buffer { | ||
#[inline] | ||
fn deref_mut(&mut self) -> &mut [u8] { | ||
unsafe { slice::from_raw_parts_mut(self.data, self.len) } | ||
} | ||
} | ||
|
||
impl Buffer { | ||
#[inline] | ||
pub(super) fn new() -> Self { | ||
Self::default() | ||
} | ||
|
||
#[inline] | ||
pub(super) fn clear(&mut self) { | ||
self.len = 0; | ||
} | ||
|
||
#[inline] | ||
pub(super) fn take(&mut self) -> Self { | ||
mem::take(self) | ||
} | ||
|
||
// We have the array method separate from extending from a slice. This is | ||
// because in the case of small arrays, codegen can be more efficient | ||
// (avoiding a memmove call). With extend_from_slice, LLVM at least | ||
// currently is not able to make that optimization. | ||
#[inline] | ||
pub(super) fn extend_from_array<const N: usize>(&mut self, xs: &[u8; N]) { | ||
if xs.len() > (self.capacity - self.len) { | ||
let b = self.take(); | ||
*self = (b.reserve)(b, xs.len()); | ||
} | ||
unsafe { | ||
xs.as_ptr().copy_to_nonoverlapping(self.data.add(self.len), xs.len()); | ||
self.len += xs.len(); | ||
} | ||
} | ||
|
||
#[inline] | ||
pub(super) fn extend_from_slice(&mut self, xs: &[u8]) { | ||
if xs.len() > (self.capacity - self.len) { | ||
let b = self.take(); | ||
*self = (b.reserve)(b, xs.len()); | ||
} | ||
unsafe { | ||
xs.as_ptr().copy_to_nonoverlapping(self.data.add(self.len), xs.len()); | ||
self.len += xs.len(); | ||
} | ||
} | ||
|
||
#[inline] | ||
pub(super) fn push(&mut self, v: u8) { | ||
// The code here is taken from Vec::push, and we know that reserve() | ||
// will panic if we're exceeding isize::MAX bytes and so there's no need | ||
// to check for overflow. | ||
if self.len == self.capacity { | ||
let b = self.take(); | ||
*self = (b.reserve)(b, 1); | ||
} | ||
unsafe { | ||
*self.data.add(self.len) = v; | ||
self.len += 1; | ||
} | ||
} | ||
} | ||
|
||
impl Write for Buffer { | ||
#[inline] | ||
fn write(&mut self, xs: &[u8]) -> io::Result<usize> { | ||
self.extend_from_slice(xs); | ||
Ok(xs.len()) | ||
} | ||
|
||
#[inline] | ||
fn write_all(&mut self, xs: &[u8]) -> io::Result<()> { | ||
self.extend_from_slice(xs); | ||
Ok(()) | ||
} | ||
|
||
#[inline] | ||
fn flush(&mut self) -> io::Result<()> { | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl Drop for Buffer { | ||
#[inline] | ||
fn drop(&mut self) { | ||
let b = self.take(); | ||
(b.drop)(b); | ||
} | ||
} | ||
|
||
impl From<Vec<u8>> for Buffer { | ||
fn from(mut v: Vec<u8>) -> Self { | ||
let (data, len, capacity) = (v.as_mut_ptr(), v.len(), v.capacity()); | ||
mem::forget(v); | ||
|
||
// This utility function is nested in here because it can *only* | ||
// be safely called on `Buffer`s created by *this* `proc_macro`. | ||
fn to_vec(b: Buffer) -> Vec<u8> { | ||
unsafe { | ||
let Buffer { data, len, capacity, .. } = b; | ||
mem::forget(b); | ||
Vec::from_raw_parts(data, len, capacity) | ||
} | ||
} | ||
|
||
extern "C" fn reserve(b: Buffer, additional: usize) -> Buffer { | ||
let mut v = to_vec(b); | ||
v.reserve(additional); | ||
Buffer::from(v) | ||
} | ||
|
||
extern "C" fn drop(b: Buffer) { | ||
mem::drop(to_vec(b)); | ||
} | ||
|
||
Buffer { data, len, capacity, reserve, drop } | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't that read 1.64?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, removed it in #12802