Skip to content

Commit 3892b70

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 db00199 + 1c7c792 commit 3892b70

File tree

6 files changed

+41
-125
lines changed

6 files changed

+41
-125
lines changed

compiler/rustc_expand/src/proc_macro_server.rs

+17-36
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_ast::tokenstream::{self, Spacing::*, TokenStream};
66
use rustc_ast_pretty::pprust;
77
use rustc_data_structures::fx::FxHashMap;
88
use rustc_data_structures::sync::Lrc;
9-
use rustc_errors::{Diagnostic, MultiSpan, PResult};
9+
use rustc_errors::{MultiSpan, PResult};
1010
use rustc_parse::lexer::nfc_normalize;
1111
use rustc_parse::parse_stream_from_source_str;
1212
use rustc_session::parse::ParseSess;
@@ -15,7 +15,7 @@ use rustc_span::symbol::{self, sym, Symbol};
1515
use rustc_span::{BytePos, FileName, Pos, SourceFile, Span};
1616

1717
use pm::bridge::{
18-
server, DelimSpan, ExpnGlobals, Group, Ident, LitKind, Literal, Punct, TokenTree,
18+
server, DelimSpan, Diagnostic, ExpnGlobals, Group, Ident, LitKind, Literal, Punct, TokenTree,
1919
};
2020
use pm::{Delimiter, Level, LineColumn};
2121
use std::ops::Bound;
@@ -368,8 +368,6 @@ impl server::Types for Rustc<'_, '_> {
368368
type FreeFunctions = FreeFunctions;
369369
type TokenStream = TokenStream;
370370
type SourceFile = Lrc<SourceFile>;
371-
type MultiSpan = Vec<Span>;
372-
type Diagnostic = Diagnostic;
373371
type Span = Span;
374372
type Symbol = Symbol;
375373
}
@@ -436,6 +434,21 @@ impl server::FreeFunctions for Rustc<'_, '_> {
436434
span: self.call_site,
437435
})
438436
}
437+
438+
fn emit_diagnostic(&mut self, diagnostic: Diagnostic<Self::Span>) {
439+
let mut diag =
440+
rustc_errors::Diagnostic::new(diagnostic.level.to_internal(), diagnostic.message);
441+
diag.set_span(MultiSpan::from_spans(diagnostic.spans));
442+
for child in diagnostic.children {
443+
diag.sub(
444+
child.level.to_internal(),
445+
child.message,
446+
MultiSpan::from_spans(child.spans),
447+
None,
448+
);
449+
}
450+
self.sess().span_diagnostic.emit_diagnostic(&mut diag);
451+
}
439452
}
440453

441454
impl server::TokenStream for Rustc<'_, '_> {
@@ -583,38 +596,6 @@ impl server::SourceFile for Rustc<'_, '_> {
583596
}
584597
}
585598

586-
impl server::MultiSpan for Rustc<'_, '_> {
587-
fn new(&mut self) -> Self::MultiSpan {
588-
vec![]
589-
}
590-
591-
fn push(&mut self, spans: &mut Self::MultiSpan, span: Self::Span) {
592-
spans.push(span)
593-
}
594-
}
595-
596-
impl server::Diagnostic for Rustc<'_, '_> {
597-
fn new(&mut self, level: Level, msg: &str, spans: Self::MultiSpan) -> Self::Diagnostic {
598-
let mut diag = Diagnostic::new(level.to_internal(), msg);
599-
diag.set_span(MultiSpan::from_spans(spans));
600-
diag
601-
}
602-
603-
fn sub(
604-
&mut self,
605-
diag: &mut Self::Diagnostic,
606-
level: Level,
607-
msg: &str,
608-
spans: Self::MultiSpan,
609-
) {
610-
diag.sub(level.to_internal(), msg, MultiSpan::from_spans(spans), None);
611-
}
612-
613-
fn emit(&mut self, mut diag: Self::Diagnostic) {
614-
self.sess().span_diagnostic.emit_diagnostic(&mut diag);
615-
}
616-
}
617-
618599
impl server::Span for Rustc<'_, '_> {
619600
fn debug(&mut self, span: Self::Span) -> String {
620601
if self.ecx.ecfg.span_debug {

library/proc_macro/src/bridge/client.rs

-2
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,

library/proc_macro/src/bridge/mod.rs

+13-16
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)]

library/proc_macro/src/bridge/server.rs

-2
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
}

library/proc_macro/src/diagnostic.rs

+7-14
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
}

src/tools/rust-analyzer/crates/proc-macro-srv/src/abis/abi_sysroot/ra_server.rs

+4-55
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,6 @@ pub struct SourceFile {
3737
type Level = super::proc_macro::Level;
3838
type LineColumn = super::proc_macro::LineColumn;
3939

40-
/// A structure representing a diagnostic message and associated children
41-
/// messages.
42-
#[derive(Clone, Debug)]
43-
pub struct Diagnostic {
44-
level: Level,
45-
message: String,
46-
spans: Vec<Span>,
47-
children: Vec<Diagnostic>,
48-
}
49-
50-
impl Diagnostic {
51-
/// Creates a new diagnostic with the given `level` and `message`.
52-
pub fn new<T: Into<String>>(level: Level, message: T) -> Diagnostic {
53-
Diagnostic { level, message: message.into(), spans: vec![], children: vec![] }
54-
}
55-
}
56-
5740
pub struct FreeFunctions;
5841

5942
#[derive(Default)]
@@ -65,8 +48,6 @@ impl server::Types for RustAnalyzer {
6548
type FreeFunctions = FreeFunctions;
6649
type TokenStream = TokenStream;
6750
type SourceFile = SourceFile;
68-
type MultiSpan = Vec<Span>;
69-
type Diagnostic = Diagnostic;
7051
type Span = Span;
7152
type Symbol = Symbol;
7253
}
@@ -90,6 +71,10 @@ impl server::FreeFunctions for RustAnalyzer {
9071
span: tt::TokenId::unspecified(),
9172
})
9273
}
74+
75+
fn emit_diagnostic(&mut self, _: bridge::Diagnostic<Self::Span>) {
76+
// FIXME handle diagnostic
77+
}
9378
}
9479

9580
impl server::TokenStream for RustAnalyzer {
@@ -282,30 +267,6 @@ impl server::SourceFile for RustAnalyzer {
282267
}
283268
}
284269

285-
impl server::Diagnostic for RustAnalyzer {
286-
fn new(&mut self, level: Level, msg: &str, spans: Self::MultiSpan) -> Self::Diagnostic {
287-
let mut diag = Diagnostic::new(level, msg);
288-
diag.spans = spans;
289-
diag
290-
}
291-
292-
fn sub(
293-
&mut self,
294-
_diag: &mut Self::Diagnostic,
295-
_level: Level,
296-
_msg: &str,
297-
_spans: Self::MultiSpan,
298-
) {
299-
// FIXME handle diagnostic
300-
//
301-
}
302-
303-
fn emit(&mut self, _diag: Self::Diagnostic) {
304-
// FIXME handle diagnostic
305-
// diag.emit()
306-
}
307-
}
308-
309270
impl server::Span for RustAnalyzer {
310271
fn debug(&mut self, span: Self::Span) -> String {
311272
format!("{:?}", span.0)
@@ -372,18 +333,6 @@ impl server::Span for RustAnalyzer {
372333
}
373334
}
374335

375-
impl server::MultiSpan for RustAnalyzer {
376-
fn new(&mut self) -> Self::MultiSpan {
377-
// FIXME handle span
378-
vec![]
379-
}
380-
381-
fn push(&mut self, other: &mut Self::MultiSpan, span: Self::Span) {
382-
//TODP
383-
other.push(span)
384-
}
385-
}
386-
387336
impl server::Symbol for RustAnalyzer {
388337
fn normalize_and_validate_ident(&mut self, string: &str) -> Result<Self::Symbol, ()> {
389338
// FIXME: nfc-normalize and validate idents

0 commit comments

Comments
 (0)