Skip to content

Commit bd6b858

Browse files
authored
Rollup merge of #157271 - cyrgani:pm-cleaning, r=petrochenkov
simplify some `proc_macro` things Each commit should be reviewable on its own. Locally, this also resulted in some slightly better perf results.
2 parents f7ba114 + 30004a8 commit bd6b858

7 files changed

Lines changed: 117 additions & 95 deletions

File tree

library/proc_macro/src/bridge/buffer.rs

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
//! Buffer management for same-process client<->server communication.
22
3-
use std::io::{self, Write};
43
use std::mem::{self, ManuallyDrop};
5-
use std::ops::{Deref, DerefMut};
4+
use std::ops::Deref;
65
use std::slice;
76

87
#[repr(C)]
@@ -32,13 +31,6 @@ impl Deref for Buffer {
3231
}
3332
}
3433

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-
4234
impl Buffer {
4335
#[inline]
4436
pub(super) fn new() -> Self {
@@ -99,25 +91,6 @@ impl Buffer {
9991
}
10092
}
10193

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-
}
108-
109-
#[inline]
110-
fn write_all(&mut self, xs: &[u8]) -> io::Result<()> {
111-
self.extend_from_slice(xs);
112-
Ok(())
113-
}
114-
115-
#[inline]
116-
fn flush(&mut self) -> io::Result<()> {
117-
Ok(())
118-
}
119-
}
120-
12194
impl Drop for Buffer {
12295
#[inline]
12396
fn drop(&mut self) {
@@ -128,8 +101,7 @@ impl Drop for Buffer {
128101

129102
impl From<Vec<u8>> for Buffer {
130103
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());
104+
let (data, len, capacity) = v.into_raw_parts();
133105

134106
// This utility function is nested in here because it can *only*
135107
// be safely called on `Buffer`s created by *this* `proc_macro`.

library/proc_macro/src/bridge/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ impl Client {
282282
pub const fn expand1(f: impl Fn(crate::TokenStream) -> crate::TokenStream + Copy) -> Self {
283283
Client {
284284
run: super::selfless_reify::reify_to_extern_c_fn_hrt_bridge(move |bridge| {
285-
run_client(bridge, |input| f(input))
285+
run_client(bridge, f)
286286
}),
287287
}
288288
}

library/proc_macro/src/bridge/rpc.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Serialization for client-server communication.
22
3-
use std::io::Write;
43
use std::num::NonZero;
54

65
use super::buffer::Buffer;
@@ -209,7 +208,7 @@ impl<S> Encode<S> for &str {
209208
fn encode(self, w: &mut Buffer, s: &mut S) {
210209
let bytes = self.as_bytes();
211210
bytes.len().encode(w, s);
212-
w.write_all(bytes).unwrap();
211+
w.extend_from_slice(bytes);
213212
}
214213
}
215214

library/proc_macro/src/lib.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#![warn(unreachable_pub)]
3838
#![deny(unsafe_op_in_unsafe_fn)]
3939

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

@@ -296,7 +296,7 @@ impl TokenStream {
296296
/// Checks if this `TokenStream` is empty.
297297
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
298298
pub fn is_empty(&self) -> bool {
299-
self.0.as_ref().map(|h| BridgeMethods::ts_is_empty(h)).unwrap_or(true)
299+
self.0.as_ref().map(BridgeMethods::ts_is_empty).unwrap_or(true)
300300
}
301301

302302
/// Parses this `TokenStream` as an expression and attempts to expand any
@@ -575,9 +575,7 @@ pub mod token_stream {
575575
type IntoIter = IntoIter;
576576

577577
fn into_iter(self) -> IntoIter {
578-
IntoIter(
579-
self.0.map(|v| BridgeMethods::ts_into_trees(v)).unwrap_or_default().into_iter(),
580-
)
578+
IntoIter(self.0.map(BridgeMethods::ts_into_trees).unwrap_or_default().into_iter())
581579
}
582580
}
583581
}
@@ -595,7 +593,7 @@ pub macro quote($($t:tt)*) {
595593
/* compiler built-in */
596594
}
597595

598-
#[unstable(feature = "proc_macro_internals", issue = "27812")]
596+
#[unstable(feature = "proc_macro_internals", issue = "none")]
599597
#[doc(hidden)]
600598
mod quote;
601599

@@ -755,14 +753,14 @@ impl Span {
755753

756754
// Used by the implementation of `Span::quote`
757755
#[doc(hidden)]
758-
#[unstable(feature = "proc_macro_internals", issue = "27812")]
756+
#[unstable(feature = "proc_macro_internals", issue = "none")]
759757
pub fn save_span(&self) -> usize {
760758
BridgeMethods::span_save_span(self.0)
761759
}
762760

763761
// Used by the implementation of `Span::quote`
764762
#[doc(hidden)]
765-
#[unstable(feature = "proc_macro_internals", issue = "27812")]
763+
#[unstable(feature = "proc_macro_internals", issue = "none")]
766764
pub fn recover_proc_macro_span(id: usize) -> Span {
767765
Span(BridgeMethods::span_recover_proc_macro_span(id))
768766
}
@@ -1296,7 +1294,7 @@ macro_rules! unsuffixed_int_literals {
12961294
/// specified on this token, meaning that invocations like
12971295
/// `Literal::i8_unsuffixed(1)` are equivalent to
12981296
/// `Literal::u32_unsuffixed(1)`.
1299-
/// Literals created from negative numbers might not survive rountrips through
1297+
/// Literals created from negative numbers might not survive roundtrips through
13001298
/// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).
13011299
///
13021300
/// Literals created through this method have the `Span::call_site()`
@@ -1416,7 +1414,7 @@ impl Literal {
14161414
/// This constructor is similar to those like `Literal::i8_unsuffixed` where
14171415
/// the float's value is emitted directly into the token but no suffix is
14181416
/// used, so it may be inferred to be a `f64` later in the compiler.
1419-
/// Literals created from negative numbers might not survive rountrips through
1417+
/// Literals created from negative numbers might not survive roundtrips through
14201418
/// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).
14211419
///
14221420
/// # Panics
@@ -1441,7 +1439,7 @@ impl Literal {
14411439
/// specified is the preceding part of the token and `f32` is the suffix of
14421440
/// the token. This token will always be inferred to be an `f32` in the
14431441
/// compiler.
1444-
/// Literals created from negative numbers might not survive rountrips through
1442+
/// Literals created from negative numbers might not survive roundtrips through
14451443
/// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).
14461444
///
14471445
/// # Panics
@@ -1461,7 +1459,7 @@ impl Literal {
14611459
/// This constructor is similar to those like `Literal::i8_unsuffixed` where
14621460
/// the float's value is emitted directly into the token but no suffix is
14631461
/// used, so it may be inferred to be a `f64` later in the compiler.
1464-
/// Literals created from negative numbers might not survive rountrips through
1462+
/// Literals created from negative numbers might not survive roundtrips through
14651463
/// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).
14661464
///
14671465
/// # Panics
@@ -1486,7 +1484,7 @@ impl Literal {
14861484
/// specified is the preceding part of the token and `f64` is the suffix of
14871485
/// the token. This token will always be inferred to be an `f64` in the
14881486
/// compiler.
1489-
/// Literals created from negative numbers might not survive rountrips through
1487+
/// Literals created from negative numbers might not survive roundtrips through
14901488
/// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).
14911489
///
14921490
/// # Panics

tests/ui/proc-macro/auxiliary/nonfatal-parsing-body.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,38 @@ enum Mode {
1515
OtherWithPanic,
1616
}
1717

18+
fn print_unspanned<T>(s: &str) -> Result<T, LexError>
19+
where
20+
T: FromStr<Err = LexError> + Debug,
21+
{
22+
let t = T::from_str(s);
23+
let mut s = format!("{t:?}");
24+
while let Some((l, r)) = s.split_once("span: #") {
25+
let (_, r) = r.split_once(")").unwrap();
26+
s = format!("{l}span: Span{r}");
27+
}
28+
println!("{s}");
29+
t
30+
}
31+
1832
fn parse<T>(s: &str, mode: Mode)
1933
where
2034
T: FromStr<Err = LexError> + Debug,
2135
{
2236
match mode {
2337
NormalOk => {
24-
let t = T::from_str(s);
25-
println!("{:?}", t);
38+
let t = print_unspanned::<T>(s);
2639
assert!(t.is_ok());
2740
}
2841
NormalErr => {
29-
let t = T::from_str(s);
30-
println!("{:?}", t);
42+
let t = print_unspanned::<T>(s);
3143
assert!(t.is_err());
3244
}
3345
OtherError => {
34-
println!("{:?}", T::from_str(s));
46+
print_unspanned::<T>(s);
3547
}
3648
OtherWithPanic => {
37-
if catch_unwind(|| println!("{:?}", T::from_str(s))).is_ok() {
49+
if catch_unwind(|| print_unspanned::<T>(s)).is_ok() {
3850
eprintln!("{s} did not panic");
3951
}
4052
}
@@ -64,14 +76,18 @@ fn lit(s: &str, mode: Mode) {
6476
}
6577

6678
pub fn run() {
79+
assert_eq!("\'", "'");
6780
// returns Ok(valid instance)
81+
lit("r\"g\"", NormalOk);
82+
lit("r#\"g\"#", NormalOk);
6883
lit("123", NormalOk);
6984
lit("\"ab\"", NormalOk);
7085
lit("\'b\'", NormalOk);
7186
lit("'b'", NormalOk);
7287
lit("b\"b\"", NormalOk);
7388
lit("c\"b\"", NormalOk);
7489
lit("cr\"b\"", NormalOk);
90+
lit("'\\''", NormalOk);
7591
lit("b'b'", NormalOk);
7692
lit("256u8", NormalOk);
7793
lit("-256u8", NormalOk);
@@ -99,10 +115,13 @@ pub fn run() {
99115
NormalOk,
100116
);
101117
stream("/*a*/ //", NormalOk);
118+
lit("\"\"", NormalOk);
119+
stream("", NormalOk);
102120

103121
println!("### ERRORS");
104122

105123
// returns Err(LexError)
124+
lit("", NormalErr);
106125
lit("\'c\'/**/", NormalErr);
107126
lit(" 0", NormalErr);
108127
lit("0 ", NormalErr);
@@ -119,10 +138,12 @@ pub fn run() {
119138
// emits diagnostics and returns LexError
120139
lit("r'r'", OtherError);
121140
lit("c'r'", OtherError);
141+
lit("\u{2000}", OtherError);
122142

123143
// emits diagnostic and returns a seemingly valid tokenstream
124144
stream("r'r'", OtherError);
125145
stream("c'r'", OtherError);
146+
stream("\u{2000}", OtherError);
126147

127148
for parse in [stream as fn(&str, Mode), lit] {
128149
// emits diagnostic(s), then panics

tests/ui/proc-macro/nonfatal-parsing.stderr

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ help: consider inserting whitespace here
2222
LL | c 'r'
2323
| +
2424

25+
error: unknown start of token: \u{2000}
26+
--> <proc-macro source code>:1:1
27+
|
28+
LL |  
29+
| ^
30+
|
31+
help: Unicode character ' ' (En Quad) looks like ' ' (Space), but it is not
32+
|
33+
LL |
34+
|
35+
2536
error: prefix `r` is unknown
2637
--> $DIR/nonfatal-parsing.rs:15:5
2738
|
@@ -40,6 +51,19 @@ LL | nonfatal_parsing::run!();
4051
= note: prefixed identifiers and literals are reserved since Rust 2021
4152
= note: this error originates in the macro `nonfatal_parsing::run` (in Nightly builds, run with -Z macro-backtrace for more info)
4253

54+
error: unknown start of token: \u{2000}
55+
--> $DIR/nonfatal-parsing.rs:15:5
56+
|
57+
LL | nonfatal_parsing::run!();
58+
| ^^^^^^^^^^^^^^^^^^^^^^^^
59+
|
60+
= note: this error originates in the macro `nonfatal_parsing::run` (in Nightly builds, run with -Z macro-backtrace for more info)
61+
help: Unicode character ' ' (En Quad) looks like ' ' (Space), but it is not
62+
--> <proc-macro source code>:1:1
63+
|
64+
LL |
65+
|
66+
4367
error: found invalid character; only `#` is allowed in raw string delimitation: \u{0}
4468
--> $DIR/nonfatal-parsing.rs:15:5
4569
|
@@ -175,6 +199,6 @@ error: invalid digit for a base 2 literal
175199
LL | /*a*/ 0b2 //
176200
| ^
177201

178-
error: aborting due to 20 previous errors
202+
error: aborting due to 22 previous errors
179203

180204
For more information about this error, try `rustc --explain E0768`.

0 commit comments

Comments
 (0)