solar_interface/diagnostics/
builder.rs1use super::{
6 Applicability, BugAbort, Diag, DiagCtxt, DiagId, DiagMsg, ErrorGuaranteed, ExplicitBug,
7 FatalAbort, Level, MultiSpan, Span, Style, SuggestionStyle,
8};
9use solar_data_structures::Never;
10use std::{
11 fmt,
12 marker::PhantomData,
13 ops::{Deref, DerefMut},
14 panic::Location,
15};
16
17pub trait EmissionGuarantee: Sized {
23 type EmitResult;
26
27 #[track_caller]
31 fn emit_producing_guarantee(db: &mut DiagBuilder<'_, Self>) -> Self::EmitResult;
32}
33
34impl EmissionGuarantee for ErrorGuaranteed {
35 type EmitResult = Self;
36
37 fn emit_producing_guarantee(db: &mut DiagBuilder<'_, Self>) -> Self::EmitResult {
38 let guar = db.emit_producing_error_guaranteed();
39
40 assert!(
44 db.is_error(),
45 "emitted non-error ({:?}) diagnostic from `DiagBuilder<ErrorGuaranteed>`",
46 db.level,
47 );
48
49 guar.unwrap_err()
50 }
51}
52
53impl EmissionGuarantee for () {
54 type EmitResult = Self;
55
56 fn emit_producing_guarantee(db: &mut DiagBuilder<'_, Self>) -> Self::EmitResult {
57 db.emit_producing_nothing();
58 }
59}
60
61impl EmissionGuarantee for BugAbort {
62 type EmitResult = Never;
63
64 fn emit_producing_guarantee(db: &mut DiagBuilder<'_, Self>) -> Self::EmitResult {
65 db.emit_producing_nothing();
66 std::panic::panic_any(ExplicitBug);
67 }
68}
69
70impl EmissionGuarantee for FatalAbort {
71 type EmitResult = Never;
72
73 fn emit_producing_guarantee(db: &mut DiagBuilder<'_, Self>) -> Self::EmitResult {
74 db.emit_producing_nothing();
75 std::panic::panic_any(Self);
76 }
77}
78
79#[must_use = "diagnostics must be emitted or cancelled"]
84pub struct DiagBuilder<'a, G: EmissionGuarantee> {
85 diagnostic: Option<Box<(Diag, &'a DiagCtxt)>>,
90
91 _marker: PhantomData<G>,
92}
93
94impl<G: EmissionGuarantee> fmt::Debug for DiagBuilder<'_, G> {
95 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
96 self.diagnostic.as_ref().unwrap().0.fmt(f)
97 }
98}
99
100impl<G: EmissionGuarantee> Deref for DiagBuilder<'_, G> {
101 type Target = Diag;
102
103 #[inline]
104 fn deref(&self) -> &Self::Target {
105 &self.diagnostic.as_ref().unwrap().0
106 }
107}
108
109impl<G: EmissionGuarantee> DerefMut for DiagBuilder<'_, G> {
110 #[inline]
111 fn deref_mut(&mut self) -> &mut Self::Target {
112 &mut self.diagnostic.as_mut().unwrap().0
113 }
114}
115
116impl<G: EmissionGuarantee> Drop for DiagBuilder<'_, G> {
117 #[track_caller]
118 fn drop(&mut self) {
119 if let Some(mut diagnostic) = self.diagnostic.take()
120 && !std::thread::panicking()
121 {
122 let (diag, dcx) = &mut *diagnostic;
123 let _ = dcx.emit_diagnostic(Diag::new(
124 Level::Bug,
125 "the following error was constructed but not emitted",
126 ));
127 let _ = dcx.emit_diagnostic_without_consuming(diag);
128 panic!("error was constructed but not emitted");
129 }
130 }
131}
132
133impl<'a, G: EmissionGuarantee> DiagBuilder<'a, G> {
134 #[inline(never)]
136 #[track_caller]
137 pub fn new<M: Into<DiagMsg>>(dcx: &'a DiagCtxt, level: Level, msg: M) -> Self {
138 Self { diagnostic: Some(Box::new((Diag::new(level, msg), dcx))), _marker: PhantomData }
139 }
140
141 #[inline]
143 pub fn dcx(&self) -> &DiagCtxt {
144 self.diagnostic.as_ref().unwrap().1
145 }
146
147 #[track_caller]
149 #[inline(never)]
150 pub fn emit(mut self) -> G::EmitResult {
151 if self.dcx().track_diagnostics() {
152 self.locations_note(Location::caller());
153 }
154 self.consume_no_panic(G::emit_producing_guarantee)
155 }
156
157 fn emit_producing_nothing(&mut self) {
158 let _ = self.emit_producing_error_guaranteed();
159 }
160
161 fn emit_producing_error_guaranteed(&mut self) -> Result<(), ErrorGuaranteed> {
162 let (diag, dcx) = &mut **self.diagnostic.as_mut().unwrap();
163 dcx.emit_diagnostic_without_consuming(diag)
164 }
165
166 #[inline]
169 pub fn cancel(self) {
170 self.consume_no_panic(|_| {});
171 }
172
173 pub fn cancel_into_message(self) -> Option<String> {
175 let message = self.messages.first().map(|(message, _)| message.to_string());
176 self.cancel();
177 message
178 }
179
180 fn consume_no_panic<R>(mut self, f: impl FnOnce(&mut Self) -> R) -> R {
181 let r = f(&mut self);
182 self.diagnostic = None;
183 r
184 }
185}
186
187macro_rules! forward {
189 (
190 $(
191 $(#[$attrs:meta])*
192 $vis:vis fn $n:ident($($name:ident: $ty:ty),* $(,)?);
193 )*
194 ) => {
195 $(
196 $(#[$attrs])*
197 #[doc = concat!("See [`Diag::", stringify!($n), "()`].")]
198 #[inline(never)]
199 $vis fn $n(mut self, $($name: $ty),*) -> Self {
200 self.diagnostic.as_mut().unwrap().0.$n($($name),*);
201 self
202 }
203 )*
204 };
205}
206
207impl<G: EmissionGuarantee> DiagBuilder<'_, G> {
209 forward! {
210 pub fn span(span: impl Into<MultiSpan>);
211 pub fn code(code: impl Into<DiagId>);
212 pub fn primary_message(msg: impl Into<DiagMsg>);
213
214 pub fn span_label(span: Span, label: impl Into<DiagMsg>);
215 pub fn span_labels(spans: impl IntoIterator<Item = Span>, label: impl Into<DiagMsg>);
216 pub fn replace_span_with(after: Span, keep_label: bool);
217
218 pub fn warn(msg: impl Into<DiagMsg>);
219 pub fn span_warn(span: impl Into<MultiSpan>, msg: impl Into<DiagMsg>);
220
221 pub fn note(msg: impl Into<DiagMsg>);
222 pub fn span_note(span: impl Into<MultiSpan>, msg: impl Into<DiagMsg>);
223 pub fn highlighted_note(messages: Vec<(impl Into<DiagMsg>, Style)>);
224 pub fn highlighted_span_note(
225 span: impl Into<MultiSpan>,
226 messages: Vec<(impl Into<DiagMsg>, Style)>
227 );
228 pub fn note_once(msg: impl Into<DiagMsg>);
229 pub fn span_note_once(span: impl Into<MultiSpan>, msg: impl Into<DiagMsg>);
230
231 pub fn help(msg: impl Into<DiagMsg>);
232 pub fn help_once(msg: impl Into<DiagMsg>);
233 pub fn highlighted_help(messages: Vec<(impl Into<DiagMsg>, Style)>);
234 pub fn highlighted_span_help(
235 span: impl Into<MultiSpan>,
236 messages: Vec<(impl Into<DiagMsg>, Style)>
237 );
238 pub fn span_help(span: impl Into<MultiSpan>, msg: impl Into<DiagMsg>);
239
240 pub fn disable_suggestions();
241 pub fn seal_suggestions();
242
243 pub fn span_suggestion(
244 span: Span,
245 msg: impl Into<DiagMsg>,
246 suggestion: impl Into<DiagMsg>,
247 applicability: Applicability,
248 );
249 pub fn span_suggestion_with_style(
250 span: Span,
251 msg: impl Into<DiagMsg>,
252 suggestion: impl Into<DiagMsg>,
253 applicability: Applicability,
254 style: SuggestionStyle
255 );
256 pub fn span_suggestion_verbose(
257 span: Span,
258 msg: impl Into<DiagMsg>,
259 suggestion: impl Into<DiagMsg>,
260 applicability: Applicability,
261 );
262 pub fn span_suggestions(
263 span: Span,
264 msg: impl Into<DiagMsg>,
265 suggestions: impl IntoIterator<Item = DiagMsg>,
266 applicability: Applicability,
267 );
268 pub fn span_suggestions_with_style(
269 span: Span,
270 msg: impl Into<DiagMsg>,
271 suggestions: impl IntoIterator<Item = DiagMsg>,
272 applicability: Applicability,
273 style: SuggestionStyle,
274 );
275 pub fn span_suggestion_short(
276 span: Span,
277 msg: impl Into<DiagMsg>,
278 suggestion: impl Into<DiagMsg>,
279 applicability: Applicability,
280 );
281 pub fn span_suggestion_hidden(
282 span: Span,
283 msg: impl Into<DiagMsg>,
284 suggestion: impl Into<DiagMsg>,
285 applicability: Applicability,
286 );
287 pub fn tool_only_span_suggestion(
288 span: Span,
289 msg: impl Into<DiagMsg>,
290 suggestion: impl Into<DiagMsg>,
291 applicability: Applicability,
292 );
293 pub fn multipart_suggestion(
294 msg: impl Into<DiagMsg>,
295 substitutions: Vec<(Span, DiagMsg)>,
296 applicability: Applicability,
297 );
298 pub fn multipart_suggestion_with_style(
299 msg: impl Into<DiagMsg>,
300 substitutions: Vec<(Span, DiagMsg)>,
301 applicability: Applicability,
302 style: SuggestionStyle
303 );
304 pub fn tool_only_multipart_suggestion(
305 msg: impl Into<DiagMsg>,
306 substitutions: Vec<(Span, DiagMsg)>,
307 applicability: Applicability,
308 );
309 pub fn multipart_suggestions(
310 msg: impl Into<DiagMsg>,
311 suggestions: impl IntoIterator<Item = Vec<(Span, DiagMsg)>>,
312 applicability: Applicability,
313 );
314 }
315}