1 use crate::diagnostic::IntoDiagnosticArg;
3 Diagnostic, DiagnosticId, DiagnosticMessage, DiagnosticStyledString, ErrorGuaranteed,
6 use crate::{Handler, Level, MultiSpan, StashKey};
7 use rustc_lint_defs::Applicability;
8 use rustc_span::source_map::Spanned;
12 use std::fmt::{self, Debug};
13 use std::marker::PhantomData;
14 use std::ops::{Deref, DerefMut};
15 use std::thread::panicking;
17 /// Trait implemented by error types. This should not be implemented manually. Instead, use
18 /// `#[derive(Diagnostic)]` -- see [rustc_macros::Diagnostic].
19 #[cfg_attr(bootstrap, rustc_diagnostic_item = "SessionDiagnostic")]
20 #[cfg_attr(not(bootstrap), rustc_diagnostic_item = "IntoDiagnostic")]
21 pub trait IntoDiagnostic<'a, T: EmissionGuarantee = ErrorGuaranteed> {
22 /// Write out as a diagnostic out of `Handler`.
24 fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, T>;
27 impl<'a, T, E> IntoDiagnostic<'a, E> for Spanned<T>
29 T: IntoDiagnostic<'a, E>,
32 fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, E> {
33 let mut diag = self.node.into_diagnostic(handler);
34 diag.set_span(self.span);
39 /// Used for emitting structured error messages and other diagnostic information.
41 /// If there is some state in a downstream crate you would like to
42 /// access in the methods of `DiagnosticBuilder` here, consider
43 /// extending `HandlerFlags`, accessed via `self.handler.flags`.
46 pub struct DiagnosticBuilder<'a, G: EmissionGuarantee> {
47 inner: DiagnosticBuilderInner<'a>,
48 _marker: PhantomData<G>,
51 /// This type exists only for `DiagnosticBuilder::forget_guarantee`, because it:
52 /// 1. lacks the `G` parameter and therefore `DiagnosticBuilder<G1>` can be
53 /// converted into `DiagnosticBuilder<G2>` while reusing the `inner` field
54 /// 2. can implement the `Drop` "bomb" instead of `DiagnosticBuilder`, as it
55 /// contains all of the data (`state` + `diagnostic`) of `DiagnosticBuilder`
57 /// The `diagnostic` field is not `Copy` and can't be moved out of whichever
58 /// type implements the `Drop` "bomb", but because of the above two facts, that
59 /// never needs to happen - instead, the whole `inner: DiagnosticBuilderInner`
60 /// can be moved out of a `DiagnosticBuilder` and into another.
63 struct DiagnosticBuilderInner<'a> {
64 state: DiagnosticBuilderState<'a>,
66 /// `Diagnostic` is a large type, and `DiagnosticBuilder` is often used as a
67 /// return value, especially within the frequently-used `PResult` type.
68 /// In theory, return value optimization (RVO) should avoid unnecessary
69 /// copying. In practice, it does not (at the time of writing).
70 diagnostic: Box<Diagnostic>,
74 enum DiagnosticBuilderState<'a> {
75 /// Initial state of a `DiagnosticBuilder`, before `.emit()` or `.cancel()`.
77 /// The `Diagnostic` will be emitted through this `Handler`.
78 Emittable(&'a Handler),
80 /// State of a `DiagnosticBuilder`, after `.emit()` or *during* `.cancel()`.
82 /// The `Diagnostic` will be ignored when calling `.emit()`, and it can be
83 /// assumed that `.emit()` was previously called, to end up in this state.
85 /// While this is also used by `.cancel()`, this state is only observed by
86 /// the `Drop` `impl` of `DiagnosticBuilderInner`, as `.cancel()` takes
87 /// `self` by-value specifically to prevent any attempts to `.emit()`.
89 // FIXME(eddyb) currently this doesn't prevent extending the `Diagnostic`,
90 // despite that being potentially lossy, if important information is added
91 // *after* the original `.emit()` call.
92 AlreadyEmittedOrDuringCancellation,
95 // `DiagnosticBuilderState` should be pointer-sized.
96 rustc_data_structures::static_assert_size!(
97 DiagnosticBuilderState<'_>,
98 std::mem::size_of::<&Handler>()
101 /// Trait for types that `DiagnosticBuilder::emit` can return as a "guarantee"
102 /// (or "proof") token that the emission happened.
103 pub trait EmissionGuarantee: Sized {
104 /// Implementation of `DiagnosticBuilder::emit`, fully controlled by each
105 /// `impl` of `EmissionGuarantee`, to make it impossible to create a value
106 /// of `Self` without actually performing the emission.
108 fn diagnostic_builder_emit_producing_guarantee(db: &mut DiagnosticBuilder<'_, Self>) -> Self;
110 /// Creates a new `DiagnosticBuilder` that will return this type of guarantee.
112 fn make_diagnostic_builder(
114 msg: impl Into<DiagnosticMessage>,
115 ) -> DiagnosticBuilder<'_, Self>;
118 /// Private module for sealing the `IsError` helper trait.
119 mod sealed_level_is_error {
122 /// Sealed helper trait for statically checking that a `Level` is an error.
123 pub(crate) trait IsError<const L: Level> {}
125 impl IsError<{ Level::Bug }> for () {}
126 impl IsError<{ Level::DelayedBug }> for () {}
127 impl IsError<{ Level::Fatal }> for () {}
128 // NOTE(eddyb) `Level::Error { lint: true }` is also an error, but lints
129 // don't need error guarantees, as their levels are always dynamic.
130 impl IsError<{ Level::Error { lint: false } }> for () {}
133 impl<'a> DiagnosticBuilder<'a, ErrorGuaranteed> {
134 /// Convenience function for internal use, clients should use one of the
135 /// `struct_*` methods on [`Handler`].
137 pub(crate) fn new_guaranteeing_error<M: Into<DiagnosticMessage>, const L: Level>(
138 handler: &'a Handler,
142 (): sealed_level_is_error::IsError<L>,
145 inner: DiagnosticBuilderInner {
146 state: DiagnosticBuilderState::Emittable(handler),
147 diagnostic: Box::new(Diagnostic::new_with_code(L, None, message)),
149 _marker: PhantomData,
153 /// Discard the guarantee `.emit()` would return, in favor of having the
154 /// type `DiagnosticBuilder<'a, ()>`. This may be necessary whenever there
155 /// is a common codepath handling both errors and warnings.
156 pub fn forget_guarantee(self) -> DiagnosticBuilder<'a, ()> {
157 DiagnosticBuilder { inner: self.inner, _marker: PhantomData }
161 // FIXME(eddyb) make `ErrorGuaranteed` impossible to create outside `.emit()`.
162 impl EmissionGuarantee for ErrorGuaranteed {
163 fn diagnostic_builder_emit_producing_guarantee(db: &mut DiagnosticBuilder<'_, Self>) -> Self {
164 match db.inner.state {
165 // First `.emit()` call, the `&Handler` is still available.
166 DiagnosticBuilderState::Emittable(handler) => {
167 db.inner.state = DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation;
169 let guar = handler.emit_diagnostic(&mut db.inner.diagnostic);
171 // Only allow a guarantee if the `level` wasn't switched to a
172 // non-error - the field isn't `pub`, but the whole `Diagnostic`
173 // can be overwritten with a new one, thanks to `DerefMut`.
175 db.inner.diagnostic.is_error(),
176 "emitted non-error ({:?}) diagnostic \
177 from `DiagnosticBuilder<ErrorGuaranteed>`",
178 db.inner.diagnostic.level,
182 // `.emit()` was previously called, disallowed from repeating it,
183 // but can take advantage of the previous `.emit()`'s guarantee
184 // still being applicable (i.e. as a form of idempotency).
185 DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation => {
186 // Only allow a guarantee if the `level` wasn't switched to a
187 // non-error - the field isn't `pub`, but the whole `Diagnostic`
188 // can be overwritten with a new one, thanks to `DerefMut`.
190 db.inner.diagnostic.is_error(),
191 "`DiagnosticBuilder<ErrorGuaranteed>`'s diagnostic \
192 became non-error ({:?}), after original `.emit()`",
193 db.inner.diagnostic.level,
195 ErrorGuaranteed::unchecked_claim_error_was_emitted()
201 fn make_diagnostic_builder(
203 msg: impl Into<DiagnosticMessage>,
204 ) -> DiagnosticBuilder<'_, Self> {
205 DiagnosticBuilder::new_guaranteeing_error::<_, { Level::Error { lint: false } }>(
211 impl<'a> DiagnosticBuilder<'a, ()> {
212 /// Convenience function for internal use, clients should use one of the
213 /// `struct_*` methods on [`Handler`].
215 pub(crate) fn new<M: Into<DiagnosticMessage>>(
216 handler: &'a Handler,
220 let diagnostic = Diagnostic::new_with_code(level, None, message);
221 Self::new_diagnostic(handler, diagnostic)
224 /// Creates a new `DiagnosticBuilder` with an already constructed
227 pub(crate) fn new_diagnostic(handler: &'a Handler, diagnostic: Diagnostic) -> Self {
228 debug!("Created new diagnostic");
230 inner: DiagnosticBuilderInner {
231 state: DiagnosticBuilderState::Emittable(handler),
232 diagnostic: Box::new(diagnostic),
234 _marker: PhantomData,
239 // FIXME(eddyb) should there be a `Option<ErrorGuaranteed>` impl as well?
240 impl EmissionGuarantee for () {
241 fn diagnostic_builder_emit_producing_guarantee(db: &mut DiagnosticBuilder<'_, Self>) -> Self {
242 match db.inner.state {
243 // First `.emit()` call, the `&Handler` is still available.
244 DiagnosticBuilderState::Emittable(handler) => {
245 db.inner.state = DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation;
247 handler.emit_diagnostic(&mut db.inner.diagnostic);
249 // `.emit()` was previously called, disallowed from repeating it.
250 DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation => {}
254 fn make_diagnostic_builder(
256 msg: impl Into<DiagnosticMessage>,
257 ) -> DiagnosticBuilder<'_, Self> {
258 DiagnosticBuilder::new(handler, Level::Warning(None), msg)
262 /// Marker type which enables implementation of `create_note` and `emit_note` functions for
263 /// note-without-error struct diagnostics.
264 #[derive(Copy, Clone)]
267 impl<'a> DiagnosticBuilder<'a, Noted> {
268 /// Convenience function for internal use, clients should use one of the
269 /// `struct_*` methods on [`Handler`].
270 pub(crate) fn new_note(handler: &'a Handler, message: impl Into<DiagnosticMessage>) -> Self {
271 let diagnostic = Diagnostic::new_with_code(Level::Note, None, message);
272 Self::new_diagnostic_note(handler, diagnostic)
275 /// Creates a new `DiagnosticBuilder` with an already constructed
277 pub(crate) fn new_diagnostic_note(handler: &'a Handler, diagnostic: Diagnostic) -> Self {
278 debug!("Created new diagnostic");
280 inner: DiagnosticBuilderInner {
281 state: DiagnosticBuilderState::Emittable(handler),
282 diagnostic: Box::new(diagnostic),
284 _marker: PhantomData,
289 impl EmissionGuarantee for Noted {
290 fn diagnostic_builder_emit_producing_guarantee(db: &mut DiagnosticBuilder<'_, Self>) -> Self {
291 match db.inner.state {
292 // First `.emit()` call, the `&Handler` is still available.
293 DiagnosticBuilderState::Emittable(handler) => {
294 db.inner.state = DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation;
295 handler.emit_diagnostic(&mut db.inner.diagnostic);
297 // `.emit()` was previously called, disallowed from repeating it.
298 DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation => {}
304 fn make_diagnostic_builder(
306 msg: impl Into<DiagnosticMessage>,
307 ) -> DiagnosticBuilder<'_, Self> {
308 DiagnosticBuilder::new_note(handler, msg)
312 impl<'a> DiagnosticBuilder<'a, !> {
313 /// Convenience function for internal use, clients should use one of the
314 /// `struct_*` methods on [`Handler`].
316 pub(crate) fn new_fatal(handler: &'a Handler, message: impl Into<DiagnosticMessage>) -> Self {
317 let diagnostic = Diagnostic::new_with_code(Level::Fatal, None, message);
318 Self::new_diagnostic_fatal(handler, diagnostic)
321 /// Creates a new `DiagnosticBuilder` with an already constructed
323 pub(crate) fn new_diagnostic_fatal(handler: &'a Handler, diagnostic: Diagnostic) -> Self {
324 debug!("Created new diagnostic");
326 inner: DiagnosticBuilderInner {
327 state: DiagnosticBuilderState::Emittable(handler),
328 diagnostic: Box::new(diagnostic),
330 _marker: PhantomData,
335 impl EmissionGuarantee for ! {
336 fn diagnostic_builder_emit_producing_guarantee(db: &mut DiagnosticBuilder<'_, Self>) -> Self {
337 match db.inner.state {
338 // First `.emit()` call, the `&Handler` is still available.
339 DiagnosticBuilderState::Emittable(handler) => {
340 db.inner.state = DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation;
342 handler.emit_diagnostic(&mut db.inner.diagnostic);
344 // `.emit()` was previously called, disallowed from repeating it.
345 DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation => {}
347 // Then fatally error, returning `!`
348 crate::FatalError.raise()
351 fn make_diagnostic_builder(
353 msg: impl Into<DiagnosticMessage>,
354 ) -> DiagnosticBuilder<'_, Self> {
355 DiagnosticBuilder::new_fatal(handler, msg)
359 /// In general, the `DiagnosticBuilder` uses deref to allow access to
360 /// the fields and methods of the embedded `diagnostic` in a
361 /// transparent way. *However,* many of the methods are intended to
362 /// be used in a chained way, and hence ought to return `self`. In
363 /// that case, we can't just naively forward to the method on the
364 /// `diagnostic`, because the return type would be a `&Diagnostic`
365 /// instead of a `&DiagnosticBuilder<'a>`. This `forward!` macro makes
366 /// it easy to declare such methods on the builder.
367 macro_rules! forward {
368 // Forward pattern for &mut self -> &mut Self
371 pub fn $n:ident(&mut self, $($name:ident: $ty:ty),* $(,)?) -> &mut Self
374 #[doc = concat!("See [`Diagnostic::", stringify!($n), "()`].")]
375 pub fn $n(&mut self, $($name: $ty),*) -> &mut Self {
376 self.inner.diagnostic.$n($($name),*);
382 impl<G: EmissionGuarantee> Deref for DiagnosticBuilder<'_, G> {
383 type Target = Diagnostic;
385 fn deref(&self) -> &Diagnostic {
386 &self.inner.diagnostic
390 impl<G: EmissionGuarantee> DerefMut for DiagnosticBuilder<'_, G> {
391 fn deref_mut(&mut self) -> &mut Diagnostic {
392 &mut self.inner.diagnostic
396 impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
397 /// Emit the diagnostic.
399 pub fn emit(&mut self) -> G {
400 G::diagnostic_builder_emit_producing_guarantee(self)
403 /// Emit the diagnostic unless `delay` is true,
404 /// in which case the emission will be delayed as a bug.
406 /// See `emit` and `delay_as_bug` for details.
408 pub fn emit_unless(&mut self, delay: bool) -> G {
410 self.downgrade_to_delayed_bug();
415 /// Cancel the diagnostic (a structured diagnostic must either be emitted or
416 /// cancelled or it will panic when dropped).
418 /// This method takes `self` by-value to disallow calling `.emit()` on it,
419 /// which may be expected to *guarantee* the emission of an error, either
420 /// at the time of the call, or through a prior `.emit()` call.
421 pub fn cancel(mut self) {
422 self.inner.state = DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation;
426 /// Stashes diagnostic for possible later improvement in a different,
427 /// later stage of the compiler. The diagnostic can be accessed with
428 /// the provided `span` and `key` through [`Handler::steal_diagnostic()`].
430 /// As with `buffer`, this is unless the handler has disabled such buffering.
431 pub fn stash(self, span: Span, key: StashKey) {
432 if let Some((diag, handler)) = self.into_diagnostic() {
433 handler.stash_diagnostic(span, key, diag);
437 /// Converts the builder to a `Diagnostic` for later emission,
438 /// unless handler has disabled such buffering, or `.emit()` was called.
439 pub fn into_diagnostic(mut self) -> Option<(Diagnostic, &'a Handler)> {
440 let handler = match self.inner.state {
441 // No `.emit()` calls, the `&Handler` is still available.
442 DiagnosticBuilderState::Emittable(handler) => handler,
443 // `.emit()` was previously called, nothing we can do.
444 DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation => {
449 if handler.flags.dont_buffer_diagnostics || handler.flags.treat_err_as_bug.is_some() {
454 // Take the `Diagnostic` by replacing it with a dummy.
455 let dummy = Diagnostic::new(Level::Allow, DiagnosticMessage::Str("".to_string()));
456 let diagnostic = std::mem::replace(&mut *self.inner.diagnostic, dummy);
458 // Disable the ICE on `Drop`.
461 // Logging here is useful to help track down where in logs an error was
463 debug!("buffer: diagnostic={:?}", diagnostic);
465 Some((diagnostic, handler))
468 /// Buffers the diagnostic for later emission,
469 /// unless handler has disabled such buffering.
470 pub fn buffer(self, buffered_diagnostics: &mut Vec<Diagnostic>) {
471 buffered_diagnostics.extend(self.into_diagnostic().map(|(diag, _)| diag));
474 /// Delay emission of this diagnostic as a bug.
476 /// This can be useful in contexts where an error indicates a bug but
477 /// typically this only happens when other compilation errors have already
478 /// happened. In those cases this can be used to defer emission of this
479 /// diagnostic as a bug in the compiler only if no other errors have been
482 /// In the meantime, though, callsites are required to deal with the "bug"
483 /// locally in whichever way makes the most sense.
485 pub fn delay_as_bug(&mut self) -> G {
486 self.downgrade_to_delayed_bug();
492 pub fn downgrade_to_delayed_bug(&mut self,) -> &mut Self
496 /// Appends a labeled span to the diagnostic.
498 /// Labels are used to convey additional context for the diagnostic's primary span. They will
499 /// be shown together with the original diagnostic's span, *not* with spans added by
500 /// `span_note`, `span_help`, etc. Therefore, if the primary span is not displayable (because
501 /// the span is `DUMMY_SP` or the source code isn't found), labels will not be displayed
504 /// Implementation-wise, the label span is pushed onto the [`MultiSpan`] that was created when
505 /// the diagnostic was constructed. However, the label span is *not* considered a
506 /// ["primary span"][`MultiSpan`]; only the `Span` supplied when creating the diagnostic is
508 pub fn span_label(&mut self, span: Span, label: impl Into<SubdiagnosticMessage>) -> &mut Self);
511 /// Labels all the given spans with the provided label.
512 /// See [`Diagnostic::span_label()`] for more information.
515 spans: impl IntoIterator<Item = Span>,
516 label: impl AsRef<str>,
519 forward!(pub fn note_expected_found(
521 expected_label: &dyn fmt::Display,
522 expected: DiagnosticStyledString,
523 found_label: &dyn fmt::Display,
524 found: DiagnosticStyledString,
527 forward!(pub fn note_expected_found_extra(
529 expected_label: &dyn fmt::Display,
530 expected: DiagnosticStyledString,
531 found_label: &dyn fmt::Display,
532 found: DiagnosticStyledString,
533 expected_extra: &dyn fmt::Display,
534 found_extra: &dyn fmt::Display,
537 forward!(pub fn note_unsuccessful_coercion(
539 expected: DiagnosticStyledString,
540 found: DiagnosticStyledString,
543 forward!(pub fn note(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self);
544 forward!(pub fn note_once(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self);
545 forward!(pub fn span_note(
547 sp: impl Into<MultiSpan>,
548 msg: impl Into<SubdiagnosticMessage>,
550 forward!(pub fn span_note_once(
552 sp: impl Into<MultiSpan>,
553 msg: impl Into<SubdiagnosticMessage>,
555 forward!(pub fn warn(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self);
556 forward!(pub fn span_warn(
558 sp: impl Into<MultiSpan>,
559 msg: impl Into<SubdiagnosticMessage>,
561 forward!(pub fn help(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self);
562 forward!(pub fn span_help(
564 sp: impl Into<MultiSpan>,
565 msg: impl Into<SubdiagnosticMessage>,
567 forward!(pub fn help_use_latest_edition(&mut self,) -> &mut Self);
568 forward!(pub fn set_is_lint(&mut self,) -> &mut Self);
570 forward!(pub fn disable_suggestions(&mut self,) -> &mut Self);
571 forward!(pub fn clear_suggestions(&mut self,) -> &mut Self);
573 forward!(pub fn multipart_suggestion(
575 msg: impl Into<SubdiagnosticMessage>,
576 suggestion: Vec<(Span, String)>,
577 applicability: Applicability,
579 forward!(pub fn multipart_suggestion_verbose(
581 msg: impl Into<SubdiagnosticMessage>,
582 suggestion: Vec<(Span, String)>,
583 applicability: Applicability,
585 forward!(pub fn tool_only_multipart_suggestion(
587 msg: impl Into<SubdiagnosticMessage>,
588 suggestion: Vec<(Span, String)>,
589 applicability: Applicability,
591 forward!(pub fn span_suggestion(
594 msg: impl Into<SubdiagnosticMessage>,
595 suggestion: impl ToString,
596 applicability: Applicability,
598 forward!(pub fn span_suggestions(
601 msg: impl Into<SubdiagnosticMessage>,
602 suggestions: impl IntoIterator<Item = String>,
603 applicability: Applicability,
605 forward!(pub fn multipart_suggestions(
607 msg: impl Into<SubdiagnosticMessage>,
608 suggestions: impl IntoIterator<Item = Vec<(Span, String)>>,
609 applicability: Applicability,
611 forward!(pub fn span_suggestion_short(
614 msg: impl Into<SubdiagnosticMessage>,
615 suggestion: impl ToString,
616 applicability: Applicability,
618 forward!(pub fn span_suggestion_verbose(
621 msg: impl Into<SubdiagnosticMessage>,
622 suggestion: impl ToString,
623 applicability: Applicability,
625 forward!(pub fn span_suggestion_hidden(
628 msg: impl Into<SubdiagnosticMessage>,
629 suggestion: impl ToString,
630 applicability: Applicability,
632 forward!(pub fn tool_only_span_suggestion(
635 msg: impl Into<SubdiagnosticMessage>,
636 suggestion: impl ToString,
637 applicability: Applicability,
640 forward!(pub fn set_primary_message(&mut self, msg: impl Into<DiagnosticMessage>) -> &mut Self);
641 forward!(pub fn set_span(&mut self, sp: impl Into<MultiSpan>) -> &mut Self);
642 forward!(pub fn code(&mut self, s: DiagnosticId) -> &mut Self);
643 forward!(pub fn set_arg(
645 name: impl Into<Cow<'static, str>>,
646 arg: impl IntoDiagnosticArg,
649 forward!(pub fn subdiagnostic(
651 subdiagnostic: impl crate::AddToDiagnostic
655 impl<G: EmissionGuarantee> Debug for DiagnosticBuilder<'_, G> {
656 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
657 self.inner.diagnostic.fmt(f)
661 /// Destructor bomb - a `DiagnosticBuilder` must be either emitted or cancelled
662 /// or we emit a bug.
663 impl Drop for DiagnosticBuilderInner<'_> {
666 // No `.emit()` or `.cancel()` calls.
667 DiagnosticBuilderState::Emittable(handler) => {
669 handler.emit_diagnostic(&mut Diagnostic::new(
671 DiagnosticMessage::Str(
672 "the following error was constructed but not emitted".to_string(),
675 handler.emit_diagnostic(&mut self.diagnostic);
676 panic!("error was constructed but not emitted");
679 // `.emit()` was previously called, or maybe we're during `.cancel()`.
680 DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation => {}
686 macro_rules! struct_span_err {
687 ($session:expr, $span:expr, $code:ident, $($message:tt)*) => ({
688 $session.struct_span_err_with_code(
690 &format!($($message)*),
691 $crate::error_code!($code),
697 macro_rules! error_code {
698 ($code:ident) => {{ $crate::DiagnosticId::Error(stringify!($code).to_owned()) }};