Skip to content

Commit aba743d

Browse files
committed
Auto merge of #157271 - cyrgani:pm-cleaning, r=<try>
simplify some `proc_macro` things
2 parents c0bb140 + 7f9f32a commit aba743d

10 files changed

Lines changed: 45 additions & 119 deletions

File tree

compiler/rustc_expand/src/proc_macro_server.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -556,10 +556,6 @@ impl server::Server for Rustc<'_, '_> {
556556
diag.emit();
557557
}
558558

559-
fn ts_drop(&mut self, stream: Self::TokenStream) {
560-
drop(stream);
561-
}
562-
563559
fn ts_clone(&mut self, stream: &Self::TokenStream) -> Self::TokenStream {
564560
stream.clone()
565561
}

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,11 +1047,11 @@ impl CrateMetadata {
10471047

10481048
fn load_proc_macro<'tcx>(&self, tcx: TyCtxt<'tcx>, id: DefIndex) -> SyntaxExtension {
10491049
let (name, kind, helper_attrs) = match *self.raw_proc_macro(tcx, id) {
1050-
ProcMacro::CustomDerive { trait_name, attributes, client } => {
1050+
ProcMacro::CustomDerive { name, attributes, client } => {
10511051
let helper_attrs =
10521052
attributes.iter().cloned().map(Symbol::intern).collect::<Vec<_>>();
10531053
(
1054-
trait_name,
1054+
name,
10551055
SyntaxExtensionKind::Derive(Arc::new(DeriveProcMacro { client })),
10561056
helper_attrs,
10571057
)
Lines changed: 24 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
//! Buffer management for same-process client<->server communication.
22
3-
use std::io::{self, Write};
4-
use std::mem::{self, ManuallyDrop};
5-
use std::ops::{Deref, DerefMut};
6-
use std::slice;
3+
use std::alloc::{self, Layout};
4+
use std::ops::Deref;
5+
use std::ptr::null_mut;
6+
use std::{mem, slice};
77

88
#[repr(C)]
99
pub struct Buffer {
1010
data: *mut u8,
1111
len: usize,
1212
capacity: usize,
13-
reserve: extern "C" fn(Buffer, usize) -> Buffer,
14-
drop: extern "C" fn(Buffer),
1513
}
1614

1715
unsafe impl Sync for Buffer {}
@@ -20,7 +18,7 @@ unsafe impl Send for Buffer {}
2018
impl Default for Buffer {
2119
#[inline]
2220
fn default() -> Self {
23-
Self::from(vec![])
21+
Self { data: null_mut(), len: 0, capacity: 0 }
2422
}
2523
}
2624

@@ -32,13 +30,6 @@ impl Deref for Buffer {
3230
}
3331
}
3432

35-
impl DerefMut for Buffer {
36-
#[inline]
37-
fn deref_mut(&mut self) -> &mut [u8] {
38-
unsafe { slice::from_raw_parts_mut(self.data, self.len) }
39-
}
40-
}
41-
4233
impl Buffer {
4334
#[inline]
4435
pub(super) fn new() -> Self {
@@ -55,27 +46,10 @@ impl Buffer {
5546
mem::take(self)
5647
}
5748

58-
// We have the array method separate from extending from a slice. This is
59-
// because in the case of small arrays, codegen can be more efficient
60-
// (avoiding a memmove call). With extend_from_slice, LLVM at least
61-
// currently is not able to make that optimization.
62-
#[inline]
63-
pub(super) fn extend_from_array<const N: usize>(&mut self, xs: &[u8; N]) {
64-
if xs.len() > (self.capacity - self.len) {
65-
let b = self.take();
66-
*self = (b.reserve)(b, xs.len());
67-
}
68-
unsafe {
69-
xs.as_ptr().copy_to_nonoverlapping(self.data.add(self.len), xs.len());
70-
self.len += xs.len();
71-
}
72-
}
73-
7449
#[inline]
7550
pub(super) fn extend_from_slice(&mut self, xs: &[u8]) {
7651
if xs.len() > (self.capacity - self.len) {
77-
let b = self.take();
78-
*self = (b.reserve)(b, xs.len());
52+
self.reserve(xs.len());
7953
}
8054
unsafe {
8155
xs.as_ptr().copy_to_nonoverlapping(self.data.add(self.len), xs.len());
@@ -89,67 +63,42 @@ impl Buffer {
8963
// will panic if we're exceeding isize::MAX bytes and so there's no need
9064
// to check for overflow.
9165
if self.len == self.capacity {
92-
let b = self.take();
93-
*self = (b.reserve)(b, 1);
66+
self.reserve(1);
9467
}
9568
unsafe {
9669
*self.data.add(self.len) = v;
9770
self.len += 1;
9871
}
9972
}
100-
}
101-
102-
impl Write for Buffer {
103-
#[inline]
104-
fn write(&mut self, xs: &[u8]) -> io::Result<usize> {
105-
self.extend_from_slice(xs);
106-
Ok(xs.len())
107-
}
10873

10974
#[inline]
110-
fn write_all(&mut self, xs: &[u8]) -> io::Result<()> {
111-
self.extend_from_slice(xs);
112-
Ok(())
75+
fn layout(&self) -> Layout {
76+
Layout::array::<u8>(self.capacity).unwrap()
11377
}
11478

115-
#[inline]
116-
fn flush(&mut self) -> io::Result<()> {
117-
Ok(())
79+
fn reserve(&mut self, amount: usize) {
80+
debug_assert!(amount > 0);
81+
self.capacity += amount;
82+
assert!(self.capacity < isize::MAX as usize);
83+
let layout = self.layout();
84+
self.data = if self.data.is_null() {
85+
unsafe { alloc::alloc(layout) }
86+
} else {
87+
unsafe { alloc::realloc(self.data, layout, self.capacity) }
88+
};
89+
if self.data.is_null() {
90+
alloc::handle_alloc_error(layout);
91+
}
11892
}
11993
}
12094

12195
impl Drop for Buffer {
12296
#[inline]
12397
fn drop(&mut self) {
124-
let b = self.take();
125-
(b.drop)(b);
126-
}
127-
}
128-
129-
impl From<Vec<u8>> for Buffer {
130-
fn from(v: Vec<u8>) -> Self {
131-
let mut v = ManuallyDrop::new(v);
132-
let (data, len, capacity) = (v.as_mut_ptr(), v.len(), v.capacity());
133-
134-
// This utility function is nested in here because it can *only*
135-
// be safely called on `Buffer`s created by *this* `proc_macro`.
136-
fn to_vec(b: Buffer) -> Vec<u8> {
98+
if !self.data.is_null() {
13799
unsafe {
138-
let b = ManuallyDrop::new(b);
139-
Vec::from_raw_parts(b.data, b.len, b.capacity)
100+
alloc::dealloc(self.data, self.layout());
140101
}
141102
}
142-
143-
extern "C" fn reserve(b: Buffer, additional: usize) -> Buffer {
144-
let mut v = to_vec(b);
145-
v.reserve(additional);
146-
Buffer::from(v)
147-
}
148-
149-
extern "C" fn drop(b: Buffer) {
150-
mem::drop(to_vec(b));
151-
}
152-
153-
Buffer { data, len, capacity, reserve, drop }
154103
}
155104
}

library/proc_macro/src/bridge/client.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@ pub(crate) struct TokenStream {
1212
impl !Send for TokenStream {}
1313
impl !Sync for TokenStream {}
1414

15-
// Forward `Drop::drop` to the inherent `drop` method.
16-
impl Drop for TokenStream {
17-
fn drop(&mut self) {
18-
Methods::ts_drop(TokenStream { handle: self.handle });
19-
}
20-
}
21-
2215
impl<S> Encode<S> for TokenStream {
2316
fn encode(self, w: &mut Buffer, s: &mut S) {
2417
mem::ManuallyDrop::new(self).handle.encode(w, s);
@@ -283,7 +276,7 @@ impl Client<crate::TokenStream, crate::TokenStream> {
283276
pub const fn expand1(f: impl Fn(crate::TokenStream) -> crate::TokenStream + Copy) -> Self {
284277
Client {
285278
run: super::selfless_reify::reify_to_extern_c_fn_hrt_bridge(move |bridge| {
286-
run_client(bridge, |input| f(input))
279+
run_client(bridge, f)
287280
}),
288281
_marker: PhantomData,
289282
}
@@ -307,7 +300,7 @@ impl Client<(crate::TokenStream, crate::TokenStream), crate::TokenStream> {
307300
#[derive(Copy, Clone)]
308301
pub enum ProcMacro {
309302
CustomDerive {
310-
trait_name: &'static str,
303+
name: &'static str,
311304
attributes: &'static [&'static str],
312305
client: Client<crate::TokenStream, crate::TokenStream>,
313306
},
@@ -326,18 +319,18 @@ pub enum ProcMacro {
326319
impl ProcMacro {
327320
pub fn name(&self) -> &'static str {
328321
match self {
329-
ProcMacro::CustomDerive { trait_name, .. } => trait_name,
330-
ProcMacro::Attr { name, .. } => name,
331-
ProcMacro::Bang { name, .. } => name,
322+
ProcMacro::CustomDerive { name, .. }
323+
| ProcMacro::Attr { name, .. }
324+
| ProcMacro::Bang { name, .. } => name,
332325
}
333326
}
334327

335328
pub const fn custom_derive(
336-
trait_name: &'static str,
329+
name: &'static str,
337330
attributes: &'static [&'static str],
338331
expand: impl Fn(crate::TokenStream) -> crate::TokenStream + Copy,
339332
) -> Self {
340-
ProcMacro::CustomDerive { trait_name, attributes, client: Client::expand1(expand) }
333+
ProcMacro::CustomDerive { name, attributes, client: Client::expand1(expand) }
341334
}
342335

343336
pub const fn attr(

library/proc_macro/src/bridge/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ macro_rules! with_api {
4040
fn literal_from_str(s: &str) -> Result<Literal<$Span, $Symbol>, String>;
4141
fn emit_diagnostic(diagnostic: Diagnostic<$Span>);
4242

43-
fn ts_drop(stream: $TokenStream);
4443
fn ts_clone(stream: &$TokenStream) -> $TokenStream;
4544
fn ts_is_empty(stream: &$TokenStream) -> bool;
4645
fn ts_expand_expr(stream: &$TokenStream) -> Result<$TokenStream, ()>;

library/proc_macro/src/bridge/rpc.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Serialization for client-server communication.
22
33
use std::any::Any;
4-
use std::io::Write;
54
use std::num::NonZero;
65

76
use super::buffer::Buffer;
@@ -25,7 +24,7 @@ macro_rules! rpc_encode_decode {
2524
let mut bytes = [0; $size];
2625
bytes[..N].copy_from_slice(&self.to_le_bytes());
2726

28-
w.extend_from_array(&bytes);
27+
w.extend_from_slice(&bytes);
2928
}
3029
}
3130

@@ -168,7 +167,7 @@ impl<S> Encode<S> for &str {
168167
fn encode(self, w: &mut Buffer, s: &mut S) {
169168
let bytes = self.as_bytes();
170169
bytes.len().encode(w, s);
171-
w.write_all(bytes).unwrap();
170+
w.extend_from_slice(bytes);
172171
}
173172
}
174173

library/proc_macro/src/lib.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#![warn(unreachable_pub)]
3737
#![deny(unsafe_op_in_unsafe_fn)]
3838

39-
#[unstable(feature = "proc_macro_internals", issue = "27812")]
39+
#[unstable(feature = "proc_macro_internals", issue = "none")]
4040
#[doc(hidden)]
4141
pub mod bridge;
4242

@@ -165,7 +165,7 @@ impl TokenStream {
165165
/// Checks if this `TokenStream` is empty.
166166
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
167167
pub fn is_empty(&self) -> bool {
168-
self.0.as_ref().map(|h| BridgeMethods::ts_is_empty(h)).unwrap_or(true)
168+
self.0.as_ref().map(BridgeMethods::ts_is_empty).unwrap_or(true)
169169
}
170170

171171
/// Parses this `TokenStream` as an expression and attempts to expand any
@@ -444,9 +444,7 @@ pub mod token_stream {
444444
type IntoIter = IntoIter;
445445

446446
fn into_iter(self) -> IntoIter {
447-
IntoIter(
448-
self.0.map(|v| BridgeMethods::ts_into_trees(v)).unwrap_or_default().into_iter(),
449-
)
447+
IntoIter(self.0.map(BridgeMethods::ts_into_trees).unwrap_or_default().into_iter())
450448
}
451449
}
452450
}
@@ -464,7 +462,7 @@ pub macro quote($($t:tt)*) {
464462
/* compiler built-in */
465463
}
466464

467-
#[unstable(feature = "proc_macro_internals", issue = "27812")]
465+
#[unstable(feature = "proc_macro_internals", issue = "none")]
468466
#[doc(hidden)]
469467
mod quote;
470468

@@ -624,14 +622,14 @@ impl Span {
624622

625623
// Used by the implementation of `Span::quote`
626624
#[doc(hidden)]
627-
#[unstable(feature = "proc_macro_internals", issue = "27812")]
625+
#[unstable(feature = "proc_macro_internals", issue = "none")]
628626
pub fn save_span(&self) -> usize {
629627
BridgeMethods::span_save_span(self.0)
630628
}
631629

632630
// Used by the implementation of `Span::quote`
633631
#[doc(hidden)]
634-
#[unstable(feature = "proc_macro_internals", issue = "27812")]
632+
#[unstable(feature = "proc_macro_internals", issue = "none")]
635633
pub fn recover_proc_macro_span(id: usize) -> Span {
636634
Span(BridgeMethods::span_recover_proc_macro_span(id))
637635
}

src/tools/rust-analyzer/crates/proc-macro-srv/src/dylib/proc_macros.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ impl ProcMacros {
2626

2727
for proc_macro in &self.0 {
2828
match proc_macro {
29-
bridge::client::ProcMacro::CustomDerive { trait_name, client, .. }
30-
if *trait_name == macro_name =>
29+
bridge::client::ProcMacro::CustomDerive { name, client, .. }
30+
if *name == macro_name =>
3131
{
3232
let res = client.run(
3333
&bridge::server::SAME_THREAD,
@@ -65,8 +65,8 @@ impl ProcMacros {
6565

6666
pub(crate) fn list_macros(&self) -> impl Iterator<Item = (&str, ProcMacroKind)> {
6767
self.0.iter().map(|proc_macro| match *proc_macro {
68-
bridge::client::ProcMacro::CustomDerive { trait_name, .. } => {
69-
(trait_name, ProcMacroKind::CustomDerive)
68+
bridge::client::ProcMacro::CustomDerive { name, .. } => {
69+
(name, ProcMacroKind::CustomDerive)
7070
}
7171
bridge::client::ProcMacro::Bang { name, .. } => (name, ProcMacroKind::Bang),
7272
bridge::client::ProcMacro::Attr { name, .. } => (name, ProcMacroKind::Attr),

src/tools/rust-analyzer/crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@ impl server::Server for RaSpanServer<'_> {
7171
// FIXME handle diagnostic
7272
}
7373

74-
fn ts_drop(&mut self, stream: Self::TokenStream) {
75-
drop(stream);
76-
}
77-
7874
fn ts_clone(&mut self, stream: &Self::TokenStream) -> Self::TokenStream {
7975
stream.clone()
8076
}

src/tools/rust-analyzer/crates/proc-macro-srv/src/server_impl/token_id.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,6 @@ impl server::Server for SpanIdServer<'_> {
7474

7575
fn emit_diagnostic(&mut self, _: Diagnostic<Self::Span>) {}
7676

77-
fn ts_drop(&mut self, stream: Self::TokenStream) {
78-
drop(stream);
79-
}
80-
8177
fn ts_clone(&mut self, stream: &Self::TokenStream) -> Self::TokenStream {
8278
stream.clone()
8379
}

0 commit comments

Comments
 (0)