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