Skip to content

Commit f558990

Browse files
committed
Auto merge of #97004 - nnethercote:proc-macro-tweaks, r=eddyb
Proc macro tweaks Various improvements I spotted while looking through the proc macro code. r? `@eddyb`
2 parents 9a42c65 + 41c10dd commit f558990

File tree

11 files changed

+139
-148
lines changed

11 files changed

+139
-148
lines changed

compiler/rustc_builtin_macros/src/proc_macro_harness.rs

+17-27
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,16 @@ struct ProcMacroDerive {
2222
attrs: Vec<Symbol>,
2323
}
2424

25-
enum ProcMacroDefType {
26-
Attr,
27-
Bang,
28-
}
29-
3025
struct ProcMacroDef {
3126
id: NodeId,
3227
function_name: Ident,
3328
span: Span,
34-
def_type: ProcMacroDefType,
3529
}
3630

3731
enum ProcMacro {
3832
Derive(ProcMacroDerive),
39-
Def(ProcMacroDef),
33+
Attr(ProcMacroDef),
34+
Bang(ProcMacroDef),
4035
}
4136

4237
struct CollectProcMacros<'a> {
@@ -128,11 +123,10 @@ impl<'a> CollectProcMacros<'a> {
128123

129124
fn collect_attr_proc_macro(&mut self, item: &'a ast::Item) {
130125
if self.in_root && item.vis.kind.is_pub() {
131-
self.macros.push(ProcMacro::Def(ProcMacroDef {
126+
self.macros.push(ProcMacro::Attr(ProcMacroDef {
132127
id: item.id,
133128
span: item.span,
134129
function_name: item.ident,
135-
def_type: ProcMacroDefType::Attr,
136130
}));
137131
} else {
138132
let msg = if !self.in_root {
@@ -147,11 +141,10 @@ impl<'a> CollectProcMacros<'a> {
147141

148142
fn collect_bang_proc_macro(&mut self, item: &'a ast::Item) {
149143
if self.in_root && item.vis.kind.is_pub() {
150-
self.macros.push(ProcMacro::Def(ProcMacroDef {
144+
self.macros.push(ProcMacro::Bang(ProcMacroDef {
151145
id: item.id,
152146
span: item.span,
153147
function_name: item.ident,
154-
def_type: ProcMacroDefType::Bang,
155148
}));
156149
} else {
157150
let msg = if !self.in_root {
@@ -308,6 +301,17 @@ fn mk_decls(cx: &mut ExtCtxt<'_>, macros: &[ProcMacro]) -> P<ast::Item> {
308301
let proc_macro_ty_method_path = |cx: &ExtCtxt<'_>, method| {
309302
cx.expr_path(cx.path(span, vec![proc_macro, bridge, client, proc_macro_ty, method]))
310303
};
304+
let attr_or_bang = |cx: &mut ExtCtxt<'_>, ca: &ProcMacroDef, ident| {
305+
cx.resolver.declare_proc_macro(ca.id);
306+
cx.expr_call(
307+
span,
308+
proc_macro_ty_method_path(cx, ident),
309+
vec![
310+
cx.expr_str(ca.span, ca.function_name.name),
311+
local_path(cx, ca.span, ca.function_name),
312+
],
313+
)
314+
};
311315
macros
312316
.iter()
313317
.map(|m| match m {
@@ -329,22 +333,8 @@ fn mk_decls(cx: &mut ExtCtxt<'_>, macros: &[ProcMacro]) -> P<ast::Item> {
329333
],
330334
)
331335
}
332-
ProcMacro::Def(ca) => {
333-
cx.resolver.declare_proc_macro(ca.id);
334-
let ident = match ca.def_type {
335-
ProcMacroDefType::Attr => attr,
336-
ProcMacroDefType::Bang => bang,
337-
};
338-
339-
cx.expr_call(
340-
span,
341-
proc_macro_ty_method_path(cx, ident),
342-
vec![
343-
cx.expr_str(ca.span, ca.function_name.name),
344-
local_path(cx, ca.span, ca.function_name),
345-
],
346-
)
347-
}
336+
ProcMacro::Attr(ca) => attr_or_bang(cx, &ca, attr),
337+
ProcMacro::Bang(ca) => attr_or_bang(cx, &ca, bang),
348338
})
349339
.collect()
350340
};

compiler/rustc_expand/src/base.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ where
266266
}
267267
}
268268

269-
pub trait ProcMacro {
269+
pub trait BangProcMacro {
270270
fn expand<'cx>(
271271
&self,
272272
ecx: &'cx mut ExtCtxt<'_>,
@@ -275,7 +275,7 @@ pub trait ProcMacro {
275275
) -> Result<TokenStream, ErrorGuaranteed>;
276276
}
277277

278-
impl<F> ProcMacro for F
278+
impl<F> BangProcMacro for F
279279
where
280280
F: Fn(TokenStream) -> TokenStream,
281281
{
@@ -640,7 +640,7 @@ pub enum SyntaxExtensionKind {
640640
/// A token-based function-like macro.
641641
Bang(
642642
/// An expander with signature TokenStream -> TokenStream.
643-
Box<dyn ProcMacro + sync::Sync + sync::Send>,
643+
Box<dyn BangProcMacro + sync::Sync + sync::Send>,
644644
),
645645

646646
/// An AST-based function-like macro.

compiler/rustc_expand/src/proc_macro.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct BangProcMacro {
1717
pub client: pm::bridge::client::Client<fn(pm::TokenStream) -> pm::TokenStream>,
1818
}
1919

20-
impl base::ProcMacro for BangProcMacro {
20+
impl base::BangProcMacro for BangProcMacro {
2121
fn expand<'cx>(
2222
&self,
2323
ecx: &'cx mut ExtCtxt<'_>,
@@ -72,11 +72,11 @@ impl base::AttrProcMacro for AttrProcMacro {
7272
}
7373
}
7474

75-
pub struct ProcMacroDerive {
75+
pub struct DeriveProcMacro {
7676
pub client: pm::bridge::client::Client<fn(pm::TokenStream) -> pm::TokenStream>,
7777
}
7878

79-
impl MultiItemModifier for ProcMacroDerive {
79+
impl MultiItemModifier for DeriveProcMacro {
8080
fn expand(
8181
&self,
8282
ecx: &mut ExtCtxt<'_>,

compiler/rustc_metadata/src/rmeta/decoder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_data_structures::svh::Svh;
1111
use rustc_data_structures::sync::{Lock, LockGuard, Lrc, OnceCell};
1212
use rustc_data_structures::unhash::UnhashMap;
1313
use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind};
14-
use rustc_expand::proc_macro::{AttrProcMacro, BangProcMacro, ProcMacroDerive};
14+
use rustc_expand::proc_macro::{AttrProcMacro, BangProcMacro, DeriveProcMacro};
1515
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
1616
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE};
1717
use rustc_hir::definitions::{DefKey, DefPath, DefPathData, DefPathHash};
@@ -837,7 +837,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
837837
attributes.iter().cloned().map(Symbol::intern).collect::<Vec<_>>();
838838
(
839839
trait_name,
840-
SyntaxExtensionKind::Derive(Box::new(ProcMacroDerive { client })),
840+
SyntaxExtensionKind::Derive(Box::new(DeriveProcMacro { client })),
841841
helper_attrs,
842842
)
843843
}

library/proc_macro/src/bridge/buffer.rs

+24-24
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,37 @@ use std::ops::{Deref, DerefMut};
66
use std::slice;
77

88
#[repr(C)]
9-
pub struct Buffer<T: Copy> {
10-
data: *mut T,
9+
pub struct Buffer {
10+
data: *mut u8,
1111
len: usize,
1212
capacity: usize,
13-
reserve: extern "C" fn(Buffer<T>, usize) -> Buffer<T>,
14-
drop: extern "C" fn(Buffer<T>),
13+
reserve: extern "C" fn(Buffer, usize) -> Buffer,
14+
drop: extern "C" fn(Buffer),
1515
}
1616

17-
unsafe impl<T: Copy + Sync> Sync for Buffer<T> {}
18-
unsafe impl<T: Copy + Send> Send for Buffer<T> {}
17+
unsafe impl Sync for Buffer {}
18+
unsafe impl Send for Buffer {}
1919

20-
impl<T: Copy> Default for Buffer<T> {
20+
impl Default for Buffer {
2121
fn default() -> Self {
2222
Self::from(vec![])
2323
}
2424
}
2525

26-
impl<T: Copy> Deref for Buffer<T> {
27-
type Target = [T];
28-
fn deref(&self) -> &[T] {
29-
unsafe { slice::from_raw_parts(self.data as *const T, self.len) }
26+
impl Deref for Buffer {
27+
type Target = [u8];
28+
fn deref(&self) -> &[u8] {
29+
unsafe { slice::from_raw_parts(self.data as *const u8, self.len) }
3030
}
3131
}
3232

33-
impl<T: Copy> DerefMut for Buffer<T> {
34-
fn deref_mut(&mut self) -> &mut [T] {
33+
impl DerefMut for Buffer {
34+
fn deref_mut(&mut self) -> &mut [u8] {
3535
unsafe { slice::from_raw_parts_mut(self.data, self.len) }
3636
}
3737
}
3838

39-
impl<T: Copy> Buffer<T> {
39+
impl Buffer {
4040
pub(super) fn new() -> Self {
4141
Self::default()
4242
}
@@ -53,7 +53,7 @@ impl<T: Copy> Buffer<T> {
5353
// because in the case of small arrays, codegen can be more efficient
5454
// (avoiding a memmove call). With extend_from_slice, LLVM at least
5555
// currently is not able to make that optimization.
56-
pub(super) fn extend_from_array<const N: usize>(&mut self, xs: &[T; N]) {
56+
pub(super) fn extend_from_array<const N: usize>(&mut self, xs: &[u8; N]) {
5757
if xs.len() > (self.capacity - self.len) {
5858
let b = self.take();
5959
*self = (b.reserve)(b, xs.len());
@@ -64,7 +64,7 @@ impl<T: Copy> Buffer<T> {
6464
}
6565
}
6666

67-
pub(super) fn extend_from_slice(&mut self, xs: &[T]) {
67+
pub(super) fn extend_from_slice(&mut self, xs: &[u8]) {
6868
if xs.len() > (self.capacity - self.len) {
6969
let b = self.take();
7070
*self = (b.reserve)(b, xs.len());
@@ -75,7 +75,7 @@ impl<T: Copy> Buffer<T> {
7575
}
7676
}
7777

78-
pub(super) fn push(&mut self, v: T) {
78+
pub(super) fn push(&mut self, v: u8) {
7979
// The code here is taken from Vec::push, and we know that reserve()
8080
// will panic if we're exceeding isize::MAX bytes and so there's no need
8181
// to check for overflow.
@@ -90,7 +90,7 @@ impl<T: Copy> Buffer<T> {
9090
}
9191
}
9292

93-
impl Write for Buffer<u8> {
93+
impl Write for Buffer {
9494
fn write(&mut self, xs: &[u8]) -> io::Result<usize> {
9595
self.extend_from_slice(xs);
9696
Ok(xs.len())
@@ -106,35 +106,35 @@ impl Write for Buffer<u8> {
106106
}
107107
}
108108

109-
impl<T: Copy> Drop for Buffer<T> {
109+
impl Drop for Buffer {
110110
fn drop(&mut self) {
111111
let b = self.take();
112112
(b.drop)(b);
113113
}
114114
}
115115

116-
impl<T: Copy> From<Vec<T>> for Buffer<T> {
117-
fn from(mut v: Vec<T>) -> Self {
116+
impl From<Vec<u8>> for Buffer {
117+
fn from(mut v: Vec<u8>) -> Self {
118118
let (data, len, capacity) = (v.as_mut_ptr(), v.len(), v.capacity());
119119
mem::forget(v);
120120

121121
// This utility function is nested in here because it can *only*
122122
// be safely called on `Buffer`s created by *this* `proc_macro`.
123-
fn to_vec<T: Copy>(b: Buffer<T>) -> Vec<T> {
123+
fn to_vec(b: Buffer) -> Vec<u8> {
124124
unsafe {
125125
let Buffer { data, len, capacity, .. } = b;
126126
mem::forget(b);
127127
Vec::from_raw_parts(data, len, capacity)
128128
}
129129
}
130130

131-
extern "C" fn reserve<T: Copy>(b: Buffer<T>, additional: usize) -> Buffer<T> {
131+
extern "C" fn reserve(b: Buffer, additional: usize) -> Buffer {
132132
let mut v = to_vec(b);
133133
v.reserve(additional);
134134
Buffer::from(v)
135135
}
136136

137-
extern "C" fn drop<T: Copy>(b: Buffer<T>) {
137+
extern "C" fn drop(b: Buffer) {
138138
mem::drop(to_vec(b));
139139
}
140140

0 commit comments

Comments
 (0)