Skip to content

Commit 08b5505

Browse files
committed
Auto merge of #100210 - mystor:proc_macro_diag_struct, r=eddyb
proc_macro/bridge: send diagnostics over the bridge as a struct This removes some RPC when creating and emitting diagnostics, and simplifies the bridge slightly. After this change, there are no remaining methods which take advantage of the support for `&mut` references to objects in the store as arguments, meaning that support for them could technically be removed if we wanted. The only remaining uses of immutable references into the store are `TokenStream` and `SourceFile`. r? `@eddyb`
2 parents 6a3c976 + 5e23469 commit 08b5505

File tree

4 files changed

+20
-34
lines changed

4 files changed

+20
-34
lines changed

proc_macro/src/bridge/client.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,6 @@ define_handles! {
176176
FreeFunctions,
177177
TokenStream,
178178
SourceFile,
179-
MultiSpan,
180-
Diagnostic,
181179

182180
'interned:
183181
Span,

proc_macro/src/bridge/mod.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ macro_rules! with_api {
5757
fn track_env_var(var: &str, value: Option<&str>);
5858
fn track_path(path: &str);
5959
fn literal_from_str(s: &str) -> Result<Literal<$S::Span, $S::Symbol>, ()>;
60+
fn emit_diagnostic(diagnostic: Diagnostic<$S::Span>);
6061
},
6162
TokenStream {
6263
fn drop($self: $S::TokenStream);
@@ -87,22 +88,6 @@ macro_rules! with_api {
8788
fn path($self: &$S::SourceFile) -> String;
8889
fn is_real($self: &$S::SourceFile) -> bool;
8990
},
90-
MultiSpan {
91-
fn drop($self: $S::MultiSpan);
92-
fn new() -> $S::MultiSpan;
93-
fn push($self: &mut $S::MultiSpan, span: $S::Span);
94-
},
95-
Diagnostic {
96-
fn drop($self: $S::Diagnostic);
97-
fn new(level: Level, msg: &str, span: $S::MultiSpan) -> $S::Diagnostic;
98-
fn sub(
99-
$self: &mut $S::Diagnostic,
100-
level: Level,
101-
msg: &str,
102-
span: $S::MultiSpan,
103-
);
104-
fn emit($self: $S::Diagnostic);
105-
},
10691
Span {
10792
fn debug($self: $S::Span) -> String;
10893
fn source_file($self: $S::Span) -> $S::SourceFile;
@@ -510,6 +495,18 @@ compound_traits!(
510495
}
511496
);
512497

498+
#[derive(Clone, Debug)]
499+
pub struct Diagnostic<Span> {
500+
pub level: Level,
501+
pub message: String,
502+
pub spans: Vec<Span>,
503+
pub children: Vec<Diagnostic<Span>>,
504+
}
505+
506+
compound_traits!(
507+
struct Diagnostic<Span> { level, message, spans, children }
508+
);
509+
513510
/// Globals provided alongside the initial inputs for a macro expansion.
514511
/// Provides values such as spans which are used frequently to avoid RPC.
515512
#[derive(Clone)]

proc_macro/src/bridge/server.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ pub trait Types {
1111
type FreeFunctions: 'static;
1212
type TokenStream: 'static + Clone;
1313
type SourceFile: 'static + Clone;
14-
type MultiSpan: 'static;
15-
type Diagnostic: 'static;
1614
type Span: 'static + Copy + Eq + Hash;
1715
type Symbol: 'static;
1816
}

proc_macro/src/diagnostic.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -161,22 +161,15 @@ impl Diagnostic {
161161
/// Emit the diagnostic.
162162
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
163163
pub fn emit(self) {
164-
fn to_internal(spans: Vec<Span>) -> crate::bridge::client::MultiSpan {
165-
let mut multi_span = crate::bridge::client::MultiSpan::new();
166-
for span in spans {
167-
multi_span.push(span.0);
164+
fn to_internal(diag: Diagnostic) -> crate::bridge::Diagnostic<crate::bridge::client::Span> {
165+
crate::bridge::Diagnostic {
166+
level: diag.level,
167+
message: diag.message,
168+
spans: diag.spans.into_iter().map(|s| s.0).collect(),
169+
children: diag.children.into_iter().map(to_internal).collect(),
168170
}
169-
multi_span
170171
}
171172

172-
let mut diag = crate::bridge::client::Diagnostic::new(
173-
self.level,
174-
&self.message[..],
175-
to_internal(self.spans),
176-
);
177-
for c in self.children {
178-
diag.sub(c.level, &c.message[..], to_internal(c.spans));
179-
}
180-
diag.emit();
173+
crate::bridge::client::FreeFunctions::emit_diagnostic(to_internal(self));
181174
}
182175
}

0 commit comments

Comments
 (0)