]>
Commit | Line | Data |
---|---|---|
ee023bcb | 1 | use crate::{Diagnostic, DiagnosticId, DiagnosticStyledString, ErrorGuaranteed}; |
29967ef6 XL |
2 | use crate::{Handler, Level, StashKey}; |
3 | use rustc_lint_defs::Applicability; | |
cc61c64b | 4 | |
dfeec247 | 5 | use rustc_span::{MultiSpan, Span}; |
c30ab7b3 | 6 | use std::fmt::{self, Debug}; |
ee023bcb | 7 | use std::marker::PhantomData; |
c30ab7b3 SL |
8 | use std::ops::{Deref, DerefMut}; |
9 | use std::thread::panicking; | |
3dfed10e | 10 | use tracing::debug; |
c30ab7b3 SL |
11 | |
12 | /// Used for emitting structured error messages and other diagnostic information. | |
0bf4aa26 XL |
13 | /// |
14 | /// If there is some state in a downstream crate you would like to | |
15 | /// access in the methods of `DiagnosticBuilder` here, consider | |
16 | /// extending `HandlerFlags`, accessed via `self.handler.flags`. | |
c30ab7b3 SL |
17 | #[must_use] |
18 | #[derive(Clone)] | |
ee023bcb FG |
19 | pub struct DiagnosticBuilder<'a, G: EmissionGuarantee> { |
20 | inner: DiagnosticBuilderInner<'a>, | |
21 | _marker: PhantomData<G>, | |
22 | } | |
23 | ||
24 | /// This type exists only for `DiagnosticBuilder::forget_guarantee`, because it: | |
25 | /// 1. lacks the `G` parameter and therefore `DiagnosticBuilder<G1>` can be | |
26 | /// converted into `DiagnosticBuilder<G2>` while reusing the `inner` field | |
27 | /// 2. can implement the `Drop` "bomb" instead of `DiagnosticBuilder`, as it | |
28 | /// contains all of the data (`state` + `diagnostic`) of `DiagnosticBuilder` | |
29 | /// | |
30 | /// The `diagnostic` field is not `Copy` and can't be moved out of whichever | |
31 | /// type implements the `Drop` "bomb", but because of the above two facts, that | |
32 | /// never needs to happen - instead, the whole `inner: DiagnosticBuilderInner` | |
33 | /// can be moved out of a `DiagnosticBuilder` and into another. | |
34 | #[must_use] | |
35 | #[derive(Clone)] | |
36 | struct DiagnosticBuilderInner<'a> { | |
37 | state: DiagnosticBuilderState<'a>, | |
5099ac24 FG |
38 | |
39 | /// `Diagnostic` is a large type, and `DiagnosticBuilder` is often used as a | |
40 | /// return value, especially within the frequently-used `PResult` type. | |
41 | /// In theory, return value optimization (RVO) should avoid unnecessary | |
42 | /// copying. In practice, it does not (at the time of writing). | |
43 | diagnostic: Box<Diagnostic>, | |
c30ab7b3 SL |
44 | } |
45 | ||
ee023bcb FG |
46 | #[derive(Clone)] |
47 | enum DiagnosticBuilderState<'a> { | |
48 | /// Initial state of a `DiagnosticBuilder`, before `.emit()` or `.cancel()`. | |
49 | /// | |
50 | /// The `Diagnostic` will be emitted through this `Handler`. | |
51 | Emittable(&'a Handler), | |
52 | ||
53 | /// State of a `DiagnosticBuilder`, after `.emit()` or *during* `.cancel()`. | |
54 | /// | |
55 | /// The `Diagnostic` will be ignored when calling `.emit()`, and it can be | |
56 | /// assumed that `.emit()` was previously called, to end up in this state. | |
57 | /// | |
58 | /// While this is also used by `.cancel()`, this state is only observed by | |
59 | /// the `Drop` `impl` of `DiagnosticBuilderInner`, as `.cancel()` takes | |
60 | /// `self` by-value specifically to prevent any attempts to `.emit()`. | |
61 | /// | |
62 | // FIXME(eddyb) currently this doesn't prevent extending the `Diagnostic`, | |
63 | // despite that being potentially lossy, if important information is added | |
64 | // *after* the original `.emit()` call. | |
65 | AlreadyEmittedOrDuringCancellation, | |
66 | } | |
67 | ||
68 | // `DiagnosticBuilderState` should be pointer-sized. | |
69 | rustc_data_structures::static_assert_size!( | |
70 | DiagnosticBuilderState<'_>, | |
71 | std::mem::size_of::<&Handler>() | |
72 | ); | |
73 | ||
74 | /// Trait for types that `DiagnosticBuilder::emit` can return as a "guarantee" | |
75 | /// (or "proof") token that the emission happened. | |
76 | pub trait EmissionGuarantee: Sized { | |
77 | /// Implementation of `DiagnosticBuilder::emit`, fully controlled by each | |
78 | /// `impl` of `EmissionGuarantee`, to make it impossible to create a value | |
79 | /// of `Self` without actually performing the emission. | |
80 | #[track_caller] | |
81 | fn diagnostic_builder_emit_producing_guarantee(db: &mut DiagnosticBuilder<'_, Self>) -> Self; | |
82 | } | |
83 | ||
84 | /// Private module for sealing the `IsError` helper trait. | |
85 | mod sealed_level_is_error { | |
86 | use crate::Level; | |
87 | ||
88 | /// Sealed helper trait for statically checking that a `Level` is an error. | |
89 | crate trait IsError<const L: Level> {} | |
90 | ||
91 | impl IsError<{ Level::Bug }> for () {} | |
92 | impl IsError<{ Level::DelayedBug }> for () {} | |
93 | impl IsError<{ Level::Fatal }> for () {} | |
94 | // NOTE(eddyb) `Level::Error { lint: true }` is also an error, but lints | |
95 | // don't need error guarantees, as their levels are always dynamic. | |
96 | impl IsError<{ Level::Error { lint: false } }> for () {} | |
97 | } | |
98 | ||
99 | impl<'a> DiagnosticBuilder<'a, ErrorGuaranteed> { | |
100 | /// Convenience function for internal use, clients should use one of the | |
101 | /// `struct_*` methods on [`Handler`]. | |
102 | crate fn new_guaranteeing_error<const L: Level>(handler: &'a Handler, message: &str) -> Self | |
103 | where | |
104 | (): sealed_level_is_error::IsError<L>, | |
105 | { | |
106 | Self { | |
107 | inner: DiagnosticBuilderInner { | |
108 | state: DiagnosticBuilderState::Emittable(handler), | |
109 | diagnostic: Box::new(Diagnostic::new_with_code(L, None, message)), | |
110 | }, | |
111 | _marker: PhantomData, | |
112 | } | |
113 | } | |
114 | ||
115 | /// Discard the guarantee `.emit()` would return, in favor of having the | |
116 | /// type `DiagnosticBuilder<'a, ()>`. This may be necessary whenever there | |
117 | /// is a common codepath handling both errors and warnings. | |
118 | pub fn forget_guarantee(self) -> DiagnosticBuilder<'a, ()> { | |
119 | DiagnosticBuilder { inner: self.inner, _marker: PhantomData } | |
120 | } | |
121 | } | |
122 | ||
123 | // FIXME(eddyb) make `ErrorGuaranteed` impossible to create outside `.emit()`. | |
124 | impl EmissionGuarantee for ErrorGuaranteed { | |
125 | fn diagnostic_builder_emit_producing_guarantee(db: &mut DiagnosticBuilder<'_, Self>) -> Self { | |
126 | match db.inner.state { | |
127 | // First `.emit()` call, the `&Handler` is still available. | |
128 | DiagnosticBuilderState::Emittable(handler) => { | |
129 | db.inner.state = DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation; | |
130 | ||
131 | let guar = handler.emit_diagnostic(&mut db.inner.diagnostic); | |
132 | ||
133 | // Only allow a guarantee if the `level` wasn't switched to a | |
134 | // non-error - the field isn't `pub`, but the whole `Diagnostic` | |
135 | // can be overwritten with a new one, thanks to `DerefMut`. | |
136 | assert!( | |
137 | db.inner.diagnostic.is_error(), | |
138 | "emitted non-error ({:?}) diagnostic \ | |
139 | from `DiagnosticBuilder<ErrorGuaranteed>`", | |
140 | db.inner.diagnostic.level, | |
141 | ); | |
142 | guar.unwrap() | |
143 | } | |
144 | // `.emit()` was previously called, disallowed from repeating it, | |
145 | // but can take advantage of the previous `.emit()`'s guarantee | |
146 | // still being applicable (i.e. as a form of idempotency). | |
147 | DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation => { | |
148 | // Only allow a guarantee if the `level` wasn't switched to a | |
149 | // non-error - the field isn't `pub`, but the whole `Diagnostic` | |
150 | // can be overwritten with a new one, thanks to `DerefMut`. | |
151 | assert!( | |
152 | db.inner.diagnostic.is_error(), | |
153 | "`DiagnosticBuilder<ErrorGuaranteed>`'s diagnostic \ | |
154 | became non-error ({:?}), after original `.emit()`", | |
155 | db.inner.diagnostic.level, | |
156 | ); | |
157 | ErrorGuaranteed::unchecked_claim_error_was_emitted() | |
158 | } | |
159 | } | |
160 | } | |
161 | } | |
162 | ||
163 | impl<'a> DiagnosticBuilder<'a, ()> { | |
164 | /// Convenience function for internal use, clients should use one of the | |
165 | /// `struct_*` methods on [`Handler`]. | |
166 | crate fn new(handler: &'a Handler, level: Level, message: &str) -> Self { | |
167 | let diagnostic = Diagnostic::new_with_code(level, None, message); | |
168 | Self::new_diagnostic(handler, diagnostic) | |
169 | } | |
170 | ||
171 | /// Creates a new `DiagnosticBuilder` with an already constructed | |
172 | /// diagnostic. | |
173 | crate fn new_diagnostic(handler: &'a Handler, diagnostic: Diagnostic) -> Self { | |
174 | debug!("Created new diagnostic"); | |
175 | Self { | |
176 | inner: DiagnosticBuilderInner { | |
177 | state: DiagnosticBuilderState::Emittable(handler), | |
178 | diagnostic: Box::new(diagnostic), | |
179 | }, | |
180 | _marker: PhantomData, | |
181 | } | |
182 | } | |
183 | } | |
184 | ||
185 | // FIXME(eddyb) should there be a `Option<ErrorGuaranteed>` impl as well? | |
186 | impl EmissionGuarantee for () { | |
187 | fn diagnostic_builder_emit_producing_guarantee(db: &mut DiagnosticBuilder<'_, Self>) -> Self { | |
188 | match db.inner.state { | |
189 | // First `.emit()` call, the `&Handler` is still available. | |
190 | DiagnosticBuilderState::Emittable(handler) => { | |
191 | db.inner.state = DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation; | |
192 | ||
193 | handler.emit_diagnostic(&mut db.inner.diagnostic); | |
194 | } | |
195 | // `.emit()` was previously called, disallowed from repeating it. | |
196 | DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation => {} | |
197 | } | |
198 | } | |
199 | } | |
200 | ||
201 | impl<'a> DiagnosticBuilder<'a, !> { | |
202 | /// Convenience function for internal use, clients should use one of the | |
203 | /// `struct_*` methods on [`Handler`]. | |
204 | crate fn new_fatal(handler: &'a Handler, message: &str) -> Self { | |
205 | let diagnostic = Diagnostic::new_with_code(Level::Fatal, None, message); | |
206 | Self::new_diagnostic_fatal(handler, diagnostic) | |
207 | } | |
208 | ||
209 | /// Creates a new `DiagnosticBuilder` with an already constructed | |
210 | /// diagnostic. | |
211 | crate fn new_diagnostic_fatal(handler: &'a Handler, diagnostic: Diagnostic) -> Self { | |
212 | debug!("Created new diagnostic"); | |
213 | Self { | |
214 | inner: DiagnosticBuilderInner { | |
215 | state: DiagnosticBuilderState::Emittable(handler), | |
216 | diagnostic: Box::new(diagnostic), | |
217 | }, | |
218 | _marker: PhantomData, | |
219 | } | |
220 | } | |
221 | } | |
222 | ||
223 | impl EmissionGuarantee for ! { | |
224 | fn diagnostic_builder_emit_producing_guarantee(db: &mut DiagnosticBuilder<'_, Self>) -> Self { | |
225 | match db.inner.state { | |
226 | // First `.emit()` call, the `&Handler` is still available. | |
227 | DiagnosticBuilderState::Emittable(handler) => { | |
228 | db.inner.state = DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation; | |
229 | ||
230 | handler.emit_diagnostic(&mut db.inner.diagnostic); | |
231 | } | |
232 | // `.emit()` was previously called, disallowed from repeating it. | |
233 | DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation => {} | |
234 | } | |
235 | // Then fatally error, returning `!` | |
236 | crate::FatalError.raise() | |
237 | } | |
238 | } | |
239 | ||
c30ab7b3 SL |
240 | /// In general, the `DiagnosticBuilder` uses deref to allow access to |
241 | /// the fields and methods of the embedded `diagnostic` in a | |
9fa01778 | 242 | /// transparent way. *However,* many of the methods are intended to |
c30ab7b3 SL |
243 | /// be used in a chained way, and hence ought to return `self`. In |
244 | /// that case, we can't just naively forward to the method on the | |
245 | /// `diagnostic`, because the return type would be a `&Diagnostic` | |
246 | /// instead of a `&DiagnosticBuilder<'a>`. This `forward!` macro makes | |
247 | /// it easy to declare such methods on the builder. | |
248 | macro_rules! forward { | |
249 | // Forward pattern for &self -> &Self | |
9fa01778 XL |
250 | ( |
251 | $(#[$attrs:meta])* | |
252 | pub fn $n:ident(&self, $($name:ident: $ty:ty),* $(,)?) -> &Self | |
253 | ) => { | |
254 | $(#[$attrs])* | |
6a06907d | 255 | #[doc = concat!("See [`Diagnostic::", stringify!($n), "()`].")] |
c30ab7b3 SL |
256 | pub fn $n(&self, $($name: $ty),*) -> &Self { |
257 | self.diagnostic.$n($($name),*); | |
258 | self | |
6a06907d | 259 | } |
c30ab7b3 SL |
260 | }; |
261 | ||
262 | // Forward pattern for &mut self -> &mut Self | |
9fa01778 XL |
263 | ( |
264 | $(#[$attrs:meta])* | |
265 | pub fn $n:ident(&mut self, $($name:ident: $ty:ty),* $(,)?) -> &mut Self | |
266 | ) => { | |
267 | $(#[$attrs])* | |
6a06907d | 268 | #[doc = concat!("See [`Diagnostic::", stringify!($n), "()`].")] |
c30ab7b3 | 269 | pub fn $n(&mut self, $($name: $ty),*) -> &mut Self { |
ee023bcb | 270 | self.inner.diagnostic.$n($($name),*); |
c30ab7b3 | 271 | self |
6a06907d | 272 | } |
c30ab7b3 SL |
273 | }; |
274 | } | |
275 | ||
ee023bcb | 276 | impl<G: EmissionGuarantee> Deref for DiagnosticBuilder<'_, G> { |
c30ab7b3 SL |
277 | type Target = Diagnostic; |
278 | ||
279 | fn deref(&self) -> &Diagnostic { | |
ee023bcb | 280 | &self.inner.diagnostic |
c30ab7b3 SL |
281 | } |
282 | } | |
283 | ||
ee023bcb | 284 | impl<G: EmissionGuarantee> DerefMut for DiagnosticBuilder<'_, G> { |
c30ab7b3 | 285 | fn deref_mut(&mut self) -> &mut Diagnostic { |
ee023bcb | 286 | &mut self.inner.diagnostic |
c30ab7b3 SL |
287 | } |
288 | } | |
289 | ||
ee023bcb | 290 | impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> { |
c30ab7b3 | 291 | /// Emit the diagnostic. |
ee023bcb FG |
292 | #[track_caller] |
293 | pub fn emit(&mut self) -> G { | |
294 | G::diagnostic_builder_emit_producing_guarantee(self) | |
2c00a5a8 XL |
295 | } |
296 | ||
48663c56 XL |
297 | /// Emit the diagnostic unless `delay` is true, |
298 | /// in which case the emission will be delayed as a bug. | |
299 | /// | |
300 | /// See `emit` and `delay_as_bug` for details. | |
ee023bcb FG |
301 | #[track_caller] |
302 | pub fn emit_unless(&mut self, delay: bool) -> G { | |
74b04a01 | 303 | if delay { |
ee023bcb | 304 | self.downgrade_to_delayed_bug(); |
74b04a01 | 305 | } |
ee023bcb FG |
306 | self.emit() |
307 | } | |
308 | ||
309 | /// Cancel the diagnostic (a structured diagnostic must either be emitted or | |
310 | /// cancelled or it will panic when dropped). | |
311 | /// | |
312 | /// This method takes `self` by-value to disallow calling `.emit()` on it, | |
313 | /// which may be expected to *guarantee* the emission of an error, either | |
314 | /// at the time of the call, or through a prior `.emit()` call. | |
315 | pub fn cancel(mut self) { | |
316 | self.inner.state = DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation; | |
317 | drop(self); | |
48663c56 XL |
318 | } |
319 | ||
e74abb32 XL |
320 | /// Stashes diagnostic for possible later improvement in a different, |
321 | /// later stage of the compiler. The diagnostic can be accessed with | |
fc512014 | 322 | /// the provided `span` and `key` through [`Handler::steal_diagnostic()`]. |
e74abb32 XL |
323 | /// |
324 | /// As with `buffer`, this is unless the handler has disabled such buffering. | |
325 | pub fn stash(self, span: Span, key: StashKey) { | |
326 | if let Some((diag, handler)) = self.into_diagnostic() { | |
327 | handler.stash_diagnostic(span, key, diag); | |
328 | } | |
329 | } | |
330 | ||
331 | /// Converts the builder to a `Diagnostic` for later emission, | |
ee023bcb | 332 | /// unless handler has disabled such buffering, or `.emit()` was called. |
e74abb32 | 333 | pub fn into_diagnostic(mut self) -> Option<(Diagnostic, &'a Handler)> { |
ee023bcb FG |
334 | let handler = match self.inner.state { |
335 | // No `.emit()` calls, the `&Handler` is still available. | |
336 | DiagnosticBuilderState::Emittable(handler) => handler, | |
337 | // `.emit()` was previously called, nothing we can do. | |
338 | DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation => { | |
339 | return None; | |
340 | } | |
341 | }; | |
342 | ||
343 | if handler.flags.dont_buffer_diagnostics || handler.flags.treat_err_as_bug.is_some() { | |
0bf4aa26 | 344 | self.emit(); |
e74abb32 | 345 | return None; |
0bf4aa26 XL |
346 | } |
347 | ||
ee023bcb FG |
348 | // Take the `Diagnostic` by replacing it with a dummy. |
349 | let dummy = Diagnostic::new(Level::Allow, ""); | |
350 | let diagnostic = std::mem::replace(&mut *self.inner.diagnostic, dummy); | |
e74abb32 | 351 | |
ee023bcb FG |
352 | // Disable the ICE on `Drop`. |
353 | self.cancel(); | |
dfeec247 | 354 | |
0bf4aa26 XL |
355 | // Logging here is useful to help track down where in logs an error was |
356 | // actually emitted. | |
357 | debug!("buffer: diagnostic={:?}", diagnostic); | |
e74abb32 XL |
358 | |
359 | Some((diagnostic, handler)) | |
360 | } | |
361 | ||
362 | /// Buffers the diagnostic for later emission, | |
363 | /// unless handler has disabled such buffering. | |
364 | pub fn buffer(self, buffered_diagnostics: &mut Vec<Diagnostic>) { | |
365 | buffered_diagnostics.extend(self.into_diagnostic().map(|(diag, _)| diag)); | |
c30ab7b3 SL |
366 | } |
367 | ||
3b2f2976 XL |
368 | /// Delay emission of this diagnostic as a bug. |
369 | /// | |
370 | /// This can be useful in contexts where an error indicates a bug but | |
371 | /// typically this only happens when other compilation errors have already | |
372 | /// happened. In those cases this can be used to defer emission of this | |
373 | /// diagnostic as a bug in the compiler only if no other errors have been | |
374 | /// emitted. | |
375 | /// | |
376 | /// In the meantime, though, callsites are required to deal with the "bug" | |
377 | /// locally in whichever way makes the most sense. | |
ee023bcb | 378 | #[track_caller] |
3b2f2976 | 379 | pub fn delay_as_bug(&mut self) { |
ee023bcb FG |
380 | self.downgrade_to_delayed_bug(); |
381 | self.emit(); | |
3b2f2976 XL |
382 | } |
383 | ||
ee023bcb FG |
384 | forward!( |
385 | #[track_caller] | |
386 | pub fn downgrade_to_delayed_bug(&mut self,) -> &mut Self | |
387 | ); | |
388 | ||
389 | forward!( | |
fc512014 | 390 | /// Appends a labeled span to the diagnostic. |
3dfed10e | 391 | /// |
fc512014 XL |
392 | /// Labels are used to convey additional context for the diagnostic's primary span. They will |
393 | /// be shown together with the original diagnostic's span, *not* with spans added by | |
394 | /// `span_note`, `span_help`, etc. Therefore, if the primary span is not displayable (because | |
395 | /// the span is `DUMMY_SP` or the source code isn't found), labels will not be displayed | |
396 | /// either. | |
3dfed10e | 397 | /// |
fc512014 XL |
398 | /// Implementation-wise, the label span is pushed onto the [`MultiSpan`] that was created when |
399 | /// the diagnostic was constructed. However, the label span is *not* considered a | |
400 | /// ["primary span"][`MultiSpan`]; only the `Span` supplied when creating the diagnostic is | |
401 | /// primary. | |
ee023bcb | 402 | pub fn span_label(&mut self, span: Span, label: impl Into<String>) -> &mut Self); |
c30ab7b3 | 403 | |
ee023bcb | 404 | forward!( |
74b04a01 | 405 | /// Labels all the given spans with the provided label. |
fc512014 | 406 | /// See [`Diagnostic::span_label()`] for more information. |
74b04a01 XL |
407 | pub fn span_labels( |
408 | &mut self, | |
409 | spans: impl IntoIterator<Item = Span>, | |
410 | label: impl AsRef<str>, | |
ee023bcb | 411 | ) -> &mut Self); |
74b04a01 | 412 | |
60c5eb7d XL |
413 | forward!(pub fn note_expected_found( |
414 | &mut self, | |
415 | expected_label: &dyn fmt::Display, | |
416 | expected: DiagnosticStyledString, | |
417 | found_label: &dyn fmt::Display, | |
418 | found: DiagnosticStyledString, | |
419 | ) -> &mut Self); | |
c30ab7b3 | 420 | |
60c5eb7d XL |
421 | forward!(pub fn note_expected_found_extra( |
422 | &mut self, | |
423 | expected_label: &dyn fmt::Display, | |
424 | expected: DiagnosticStyledString, | |
425 | found_label: &dyn fmt::Display, | |
426 | found: DiagnosticStyledString, | |
427 | expected_extra: &dyn fmt::Display, | |
428 | found_extra: &dyn fmt::Display, | |
429 | ) -> &mut Self); | |
c30ab7b3 | 430 | |
fc512014 | 431 | forward!(pub fn note_unsuccessful_coercion( |
60c5eb7d XL |
432 | &mut self, |
433 | expected: DiagnosticStyledString, | |
434 | found: DiagnosticStyledString, | |
435 | ) -> &mut Self); | |
e74abb32 | 436 | |
c30ab7b3 | 437 | forward!(pub fn note(&mut self, msg: &str) -> &mut Self); |
ee023bcb FG |
438 | forward!(pub fn note_once(&mut self, msg: &str) -> &mut Self); |
439 | forward!(pub fn span_note( | |
440 | &mut self, | |
441 | sp: impl Into<MultiSpan>, | |
442 | msg: &str, | |
443 | ) -> &mut Self); | |
444 | forward!(pub fn span_note_once( | |
60c5eb7d | 445 | &mut self, |
ee023bcb | 446 | sp: impl Into<MultiSpan>, |
60c5eb7d XL |
447 | msg: &str, |
448 | ) -> &mut Self); | |
c30ab7b3 | 449 | forward!(pub fn warn(&mut self, msg: &str) -> &mut Self); |
ee023bcb | 450 | forward!(pub fn span_warn(&mut self, sp: impl Into<MultiSpan>, msg: &str) -> &mut Self); |
48663c56 | 451 | forward!(pub fn help(&mut self, msg: &str) -> &mut Self); |
ee023bcb | 452 | forward!(pub fn span_help( |
60c5eb7d | 453 | &mut self, |
ee023bcb | 454 | sp: impl Into<MultiSpan>, |
60c5eb7d XL |
455 | msg: &str, |
456 | ) -> &mut Self); | |
ee023bcb | 457 | forward!(pub fn help_use_latest_edition(&mut self,) -> &mut Self); |
c295e0f8 | 458 | forward!(pub fn set_is_lint(&mut self,) -> &mut Self); |
0bf4aa26 | 459 | |
5099ac24 FG |
460 | forward!(pub fn disable_suggestions(&mut self,) -> &mut Self); |
461 | ||
462 | forward!(pub fn multipart_suggestion( | |
9fa01778 XL |
463 | &mut self, |
464 | msg: &str, | |
465 | suggestion: Vec<(Span, String)>, | |
466 | applicability: Applicability, | |
5099ac24 FG |
467 | ) -> &mut Self); |
468 | forward!(pub fn multipart_suggestion_verbose( | |
c295e0f8 XL |
469 | &mut self, |
470 | msg: &str, | |
471 | suggestion: Vec<(Span, String)>, | |
472 | applicability: Applicability, | |
5099ac24 FG |
473 | ) -> &mut Self); |
474 | forward!(pub fn tool_only_multipart_suggestion( | |
94b46f34 XL |
475 | &mut self, |
476 | msg: &str, | |
0bf4aa26 | 477 | suggestion: Vec<(Span, String)>, |
9fa01778 | 478 | applicability: Applicability, |
5099ac24 FG |
479 | ) -> &mut Self); |
480 | forward!(pub fn span_suggestion( | |
9fa01778 XL |
481 | &mut self, |
482 | sp: Span, | |
483 | msg: &str, | |
484 | suggestion: String, | |
485 | applicability: Applicability, | |
5099ac24 FG |
486 | ) -> &mut Self); |
487 | forward!(pub fn span_suggestions( | |
9fa01778 XL |
488 | &mut self, |
489 | sp: Span, | |
490 | msg: &str, | |
491 | suggestions: impl Iterator<Item = String>, | |
492 | applicability: Applicability, | |
5099ac24 FG |
493 | ) -> &mut Self); |
494 | forward!(pub fn multipart_suggestions( | |
94222f64 XL |
495 | &mut self, |
496 | msg: &str, | |
497 | suggestions: impl Iterator<Item = Vec<(Span, String)>>, | |
498 | applicability: Applicability, | |
5099ac24 FG |
499 | ) -> &mut Self); |
500 | forward!(pub fn span_suggestion_short( | |
9fa01778 XL |
501 | &mut self, |
502 | sp: Span, | |
503 | msg: &str, | |
504 | suggestion: String, | |
505 | applicability: Applicability, | |
5099ac24 FG |
506 | ) -> &mut Self); |
507 | forward!(pub fn span_suggestion_verbose( | |
ba9703b0 XL |
508 | &mut self, |
509 | sp: Span, | |
510 | msg: &str, | |
511 | suggestion: String, | |
512 | applicability: Applicability, | |
5099ac24 FG |
513 | ) -> &mut Self); |
514 | forward!(pub fn span_suggestion_hidden( | |
9fa01778 XL |
515 | &mut self, |
516 | sp: Span, | |
517 | msg: &str, | |
518 | suggestion: String, | |
519 | applicability: Applicability, | |
5099ac24 FG |
520 | ) -> &mut Self); |
521 | forward!(pub fn tool_only_span_suggestion( | |
9fa01778 XL |
522 | &mut self, |
523 | sp: Span, | |
524 | msg: &str, | |
525 | suggestion: String, | |
526 | applicability: Applicability, | |
5099ac24 | 527 | ) -> &mut Self); |
9fa01778 | 528 | |
ee023bcb FG |
529 | forward!(pub fn set_primary_message(&mut self, msg: impl Into<String>) -> &mut Self); |
530 | forward!(pub fn set_span(&mut self, sp: impl Into<MultiSpan>) -> &mut Self); | |
abe05a73 | 531 | forward!(pub fn code(&mut self, s: DiagnosticId) -> &mut Self); |
c30ab7b3 SL |
532 | } |
533 | ||
ee023bcb | 534 | impl<G: EmissionGuarantee> Debug for DiagnosticBuilder<'_, G> { |
9fa01778 | 535 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
ee023bcb | 536 | self.inner.diagnostic.fmt(f) |
c30ab7b3 SL |
537 | } |
538 | } | |
539 | ||
ee023bcb | 540 | /// Destructor bomb - a `DiagnosticBuilder` must be either emitted or cancelled |
7cac9316 | 541 | /// or we emit a bug. |
ee023bcb | 542 | impl Drop for DiagnosticBuilderInner<'_> { |
c30ab7b3 | 543 | fn drop(&mut self) { |
ee023bcb FG |
544 | match self.state { |
545 | // No `.emit()` or `.cancel()` calls. | |
546 | DiagnosticBuilderState::Emittable(handler) => { | |
547 | if !panicking() { | |
548 | handler.emit_diagnostic(&mut Diagnostic::new( | |
549 | Level::Bug, | |
550 | "the following error was constructed but not emitted", | |
551 | )); | |
552 | handler.emit_diagnostic(&mut self.diagnostic); | |
553 | panic!(); | |
554 | } | |
555 | } | |
556 | // `.emit()` was previously called, or maybe we're during `.cancel()`. | |
557 | DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation => {} | |
c30ab7b3 SL |
558 | } |
559 | } | |
560 | } | |
dfeec247 XL |
561 | |
562 | #[macro_export] | |
563 | macro_rules! struct_span_err { | |
564 | ($session:expr, $span:expr, $code:ident, $($message:tt)*) => ({ | |
565 | $session.struct_span_err_with_code( | |
566 | $span, | |
567 | &format!($($message)*), | |
568 | $crate::error_code!($code), | |
569 | ) | |
570 | }) | |
571 | } | |
572 | ||
573 | #[macro_export] | |
574 | macro_rules! error_code { | |
575 | ($code:ident) => {{ $crate::DiagnosticId::Error(stringify!($code).to_owned()) }}; | |
576 | } |