2019-09-23 02:45:21 +00:00
|
|
|
use crate::{Diagnostic, DiagnosticId, DiagnosticStyledString};
|
2020-08-13 19:41:52 +00:00
|
|
|
use crate::{Handler, Level, StashKey};
|
|
|
|
use rustc_lint_defs::Applicability;
|
Highlight and simplify mismatched types
Shorten mismatched types errors by replacing subtypes that are not
different with `_`, and highlighting only the subtypes that are
different.
Given a file
```rust
struct X<T1, T2> {
x: T1,
y: T2,
}
fn foo() -> X<X<String, String>, String> {
X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
}
fn bar() -> Option<String> {
"".to_string()
}
```
provide the following output
```rust
error[E0308]: mismatched types
--> file.rs:6:5
|
6 | X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found {integer}
|
= note: expected type `X<X<_, std::string::String>, _>`
^^^^^^^^^^^^^^^^^^^ // < highlighted
found type `X<X<_, {integer}>, _>`
^^^^^^^^^ // < highlighted
error[E0308]: mismatched types
--> file.rs:6:5
|
10 | "".to_string()
| ^^^^^^^^^^^^^^ expected struct `std::option::Option`, found `std::string::String`
|
= note: expected type `Option<std::string::String>`
^^^^^^^ ^ // < highlighted
found type `std::string::String`
```
2017-02-17 22:31:59 +00:00
|
|
|
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::{MultiSpan, Span};
|
2016-10-11 16:26:32 +00:00
|
|
|
use std::fmt::{self, Debug};
|
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
use std::thread::panicking;
|
2020-08-14 06:05:01 +00:00
|
|
|
use tracing::debug;
|
2016-10-11 16:26:32 +00:00
|
|
|
|
|
|
|
/// Used for emitting structured error messages and other diagnostic information.
|
2018-09-15 04:27:55 +00:00
|
|
|
///
|
|
|
|
/// If there is some state in a downstream crate you would like to
|
|
|
|
/// access in the methods of `DiagnosticBuilder` here, consider
|
|
|
|
/// extending `HandlerFlags`, accessed via `self.handler.flags`.
|
2016-10-11 16:26:32 +00:00
|
|
|
#[must_use]
|
|
|
|
#[derive(Clone)]
|
2019-09-11 22:29:17 +00:00
|
|
|
pub struct DiagnosticBuilder<'a>(Box<DiagnosticBuilderInner<'a>>);
|
|
|
|
|
|
|
|
/// This is a large type, and often used as a return value, especially within
|
|
|
|
/// the frequently-used `PResult` type. In theory, return value optimization
|
|
|
|
/// (RVO) should avoid unnecessary copying. In practice, it does not (at the
|
|
|
|
/// time of writing). The split between `DiagnosticBuilder` and
|
|
|
|
/// `DiagnosticBuilderInner` exists to avoid many `memcpy` calls.
|
|
|
|
#[must_use]
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct DiagnosticBuilderInner<'a> {
|
|
|
|
handler: &'a Handler,
|
2016-10-11 16:26:32 +00:00
|
|
|
diagnostic: Diagnostic,
|
2018-07-26 21:53:15 +00:00
|
|
|
allow_suggestions: bool,
|
2016-10-11 16:26:32 +00:00
|
|
|
}
|
|
|
|
|
2020-12-15 04:03:19 +00:00
|
|
|
/// This is a helper macro for [`forward!`] that allows automatically adding documentation
|
|
|
|
/// that uses tokens from [`forward!`]'s input.
|
|
|
|
macro_rules! forward_inner_docs {
|
|
|
|
($e:expr => $i:item) => {
|
|
|
|
#[doc = $e]
|
|
|
|
$i
|
2021-01-09 17:00:45 +00:00
|
|
|
};
|
2020-12-15 04:03:19 +00:00
|
|
|
}
|
|
|
|
|
2016-10-11 16:26:32 +00:00
|
|
|
/// In general, the `DiagnosticBuilder` uses deref to allow access to
|
|
|
|
/// the fields and methods of the embedded `diagnostic` in a
|
2019-02-08 13:53:55 +00:00
|
|
|
/// transparent way. *However,* many of the methods are intended to
|
2016-10-11 16:26:32 +00:00
|
|
|
/// be used in a chained way, and hence ought to return `self`. In
|
|
|
|
/// that case, we can't just naively forward to the method on the
|
|
|
|
/// `diagnostic`, because the return type would be a `&Diagnostic`
|
|
|
|
/// instead of a `&DiagnosticBuilder<'a>`. This `forward!` macro makes
|
|
|
|
/// it easy to declare such methods on the builder.
|
|
|
|
macro_rules! forward {
|
|
|
|
// Forward pattern for &self -> &Self
|
2019-01-17 15:18:56 +00:00
|
|
|
(
|
|
|
|
$(#[$attrs:meta])*
|
2019-02-24 12:59:44 +00:00
|
|
|
pub fn $n:ident(&self, $($name:ident: $ty:ty),* $(,)?) -> &Self
|
2019-01-17 15:18:56 +00:00
|
|
|
) => {
|
|
|
|
$(#[$attrs])*
|
2020-12-15 04:03:19 +00:00
|
|
|
forward_inner_docs!(concat!("See [`Diagnostic::", stringify!($n), "()`].") =>
|
2016-10-11 16:26:32 +00:00
|
|
|
pub fn $n(&self, $($name: $ty),*) -> &Self {
|
|
|
|
self.diagnostic.$n($($name),*);
|
|
|
|
self
|
2020-12-15 04:03:19 +00:00
|
|
|
});
|
2016-10-11 16:26:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Forward pattern for &mut self -> &mut Self
|
2019-01-17 15:18:56 +00:00
|
|
|
(
|
|
|
|
$(#[$attrs:meta])*
|
2019-02-24 12:59:44 +00:00
|
|
|
pub fn $n:ident(&mut self, $($name:ident: $ty:ty),* $(,)?) -> &mut Self
|
2019-01-17 15:18:56 +00:00
|
|
|
) => {
|
|
|
|
$(#[$attrs])*
|
2020-12-15 04:03:19 +00:00
|
|
|
forward_inner_docs!(concat!("See [`Diagnostic::", stringify!($n), "()`].") =>
|
2016-10-11 16:26:32 +00:00
|
|
|
pub fn $n(&mut self, $($name: $ty),*) -> &mut Self {
|
2019-09-11 22:29:17 +00:00
|
|
|
self.0.diagnostic.$n($($name),*);
|
2016-10-11 16:26:32 +00:00
|
|
|
self
|
2020-12-15 04:03:19 +00:00
|
|
|
});
|
2016-10-11 16:26:32 +00:00
|
|
|
};
|
|
|
|
|
2020-09-26 21:20:14 +00:00
|
|
|
// Forward pattern for &mut self -> &mut Self, with generic parameters.
|
2019-01-17 15:18:56 +00:00
|
|
|
(
|
|
|
|
$(#[$attrs:meta])*
|
2020-09-26 21:20:14 +00:00
|
|
|
pub fn $n:ident<$($generic:ident: $bound:path),*>(
|
2019-01-17 15:18:56 +00:00
|
|
|
&mut self,
|
|
|
|
$($name:ident: $ty:ty),*
|
2019-02-24 12:59:44 +00:00
|
|
|
$(,)?
|
2019-01-17 15:18:56 +00:00
|
|
|
) -> &mut Self
|
|
|
|
) => {
|
|
|
|
$(#[$attrs])*
|
2020-12-15 04:03:19 +00:00
|
|
|
forward_inner_docs!(concat!("See [`Diagnostic::", stringify!($n), "()`].") =>
|
2020-09-26 21:20:14 +00:00
|
|
|
pub fn $n<$($generic: $bound),*>(&mut self, $($name: $ty),*) -> &mut Self {
|
2019-09-11 22:29:17 +00:00
|
|
|
self.0.diagnostic.$n($($name),*);
|
2016-10-11 16:26:32 +00:00
|
|
|
self
|
2020-12-15 04:03:19 +00:00
|
|
|
});
|
2016-10-11 16:26:32 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Deref for DiagnosticBuilder<'a> {
|
|
|
|
type Target = Diagnostic;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Diagnostic {
|
2019-09-11 22:29:17 +00:00
|
|
|
&self.0.diagnostic
|
2016-10-11 16:26:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> DerefMut for DiagnosticBuilder<'a> {
|
|
|
|
fn deref_mut(&mut self) -> &mut Diagnostic {
|
2019-09-11 22:29:17 +00:00
|
|
|
&mut self.0.diagnostic
|
2016-10-11 16:26:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> DiagnosticBuilder<'a> {
|
|
|
|
/// Emit the diagnostic.
|
|
|
|
pub fn emit(&mut self) {
|
2019-09-07 14:03:15 +00:00
|
|
|
self.0.handler.emit_diagnostic(&self);
|
2018-01-02 19:00:12 +00:00
|
|
|
self.cancel();
|
|
|
|
}
|
|
|
|
|
2019-03-11 15:43:27 +00:00
|
|
|
/// Emit the diagnostic unless `delay` is true,
|
|
|
|
/// in which case the emission will be delayed as a bug.
|
|
|
|
///
|
|
|
|
/// See `emit` and `delay_as_bug` for details.
|
|
|
|
pub fn emit_unless(&mut self, delay: bool) {
|
2020-02-01 23:47:58 +00:00
|
|
|
if delay {
|
|
|
|
self.delay_as_bug();
|
|
|
|
} else {
|
|
|
|
self.emit();
|
|
|
|
}
|
2019-03-11 15:43:27 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 02:45:21 +00:00
|
|
|
/// Stashes diagnostic for possible later improvement in a different,
|
|
|
|
/// later stage of the compiler. The diagnostic can be accessed with
|
2020-12-15 04:03:19 +00:00
|
|
|
/// the provided `span` and `key` through [`Handler::steal_diagnostic()`].
|
2019-09-23 02:45:21 +00:00
|
|
|
///
|
|
|
|
/// As with `buffer`, this is unless the handler has disabled such buffering.
|
|
|
|
pub fn stash(self, span: Span, key: StashKey) {
|
|
|
|
if let Some((diag, handler)) = self.into_diagnostic() {
|
|
|
|
handler.stash_diagnostic(span, key, diag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts the builder to a `Diagnostic` for later emission,
|
|
|
|
/// unless handler has disabled such buffering.
|
|
|
|
pub fn into_diagnostic(mut self) -> Option<(Diagnostic, &'a Handler)> {
|
2019-12-22 22:42:04 +00:00
|
|
|
if self.0.handler.flags.dont_buffer_diagnostics
|
|
|
|
|| self.0.handler.flags.treat_err_as_bug.is_some()
|
2019-03-07 16:09:41 +00:00
|
|
|
{
|
2018-09-15 04:27:55 +00:00
|
|
|
self.emit();
|
2019-09-23 02:45:21 +00:00
|
|
|
return None;
|
2018-09-15 04:27:55 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 02:45:21 +00:00
|
|
|
let handler = self.0.handler;
|
|
|
|
|
2020-03-02 00:47:49 +00:00
|
|
|
// We must use `Level::Cancelled` for `dummy` to avoid an ICE about an
|
|
|
|
// unused diagnostic.
|
|
|
|
let dummy = Diagnostic::new(Level::Cancelled, "");
|
|
|
|
let diagnostic = std::mem::replace(&mut self.0.diagnostic, dummy);
|
|
|
|
|
2018-09-06 11:07:14 +00:00
|
|
|
// Logging here is useful to help track down where in logs an error was
|
|
|
|
// actually emitted.
|
|
|
|
debug!("buffer: diagnostic={:?}", diagnostic);
|
2019-09-23 02:45:21 +00:00
|
|
|
|
|
|
|
Some((diagnostic, handler))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Buffers the diagnostic for later emission,
|
|
|
|
/// unless handler has disabled such buffering.
|
|
|
|
pub fn buffer(self, buffered_diagnostics: &mut Vec<Diagnostic>) {
|
|
|
|
buffered_diagnostics.extend(self.into_diagnostic().map(|(diag, _)| diag));
|
2018-07-18 21:10:08 +00:00
|
|
|
}
|
|
|
|
|
2017-08-28 09:56:43 +00:00
|
|
|
/// Convenience function for internal use, clients should use one of the
|
|
|
|
/// span_* methods instead.
|
|
|
|
pub fn sub<S: Into<MultiSpan>>(
|
|
|
|
&mut self,
|
|
|
|
level: Level,
|
|
|
|
message: &str,
|
|
|
|
span: Option<S>,
|
|
|
|
) -> &mut Self {
|
2020-03-22 11:43:19 +00:00
|
|
|
let span = span.map(|s| s.into()).unwrap_or_else(MultiSpan::new);
|
2019-09-11 22:29:17 +00:00
|
|
|
self.0.diagnostic.sub(level, message, span, None);
|
2017-08-28 09:56:43 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-08-23 19:52:22 +00:00
|
|
|
/// Delay emission of this diagnostic as a bug.
|
|
|
|
///
|
|
|
|
/// This can be useful in contexts where an error indicates a bug but
|
|
|
|
/// typically this only happens when other compilation errors have already
|
|
|
|
/// happened. In those cases this can be used to defer emission of this
|
|
|
|
/// diagnostic as a bug in the compiler only if no other errors have been
|
|
|
|
/// emitted.
|
|
|
|
///
|
|
|
|
/// In the meantime, though, callsites are required to deal with the "bug"
|
|
|
|
/// locally in whichever way makes the most sense.
|
|
|
|
pub fn delay_as_bug(&mut self) {
|
|
|
|
self.level = Level::Bug;
|
2019-09-11 22:29:17 +00:00
|
|
|
self.0.handler.delay_as_bug(self.0.diagnostic.clone());
|
2017-08-23 19:52:22 +00:00
|
|
|
self.cancel();
|
|
|
|
}
|
|
|
|
|
2020-11-16 03:39:51 +00:00
|
|
|
/// Appends a labeled span to the diagnostic.
|
2020-08-11 07:42:25 +00:00
|
|
|
///
|
2020-11-16 03:39:51 +00:00
|
|
|
/// Labels are used to convey additional context for the diagnostic's primary span. They will
|
|
|
|
/// be shown together with the original diagnostic's span, *not* with spans added by
|
|
|
|
/// `span_note`, `span_help`, etc. Therefore, if the primary span is not displayable (because
|
|
|
|
/// the span is `DUMMY_SP` or the source code isn't found), labels will not be displayed
|
|
|
|
/// either.
|
2020-08-11 07:42:25 +00:00
|
|
|
///
|
2020-11-16 03:39:51 +00:00
|
|
|
/// Implementation-wise, the label span is pushed onto the [`MultiSpan`] that was created when
|
|
|
|
/// the diagnostic was constructed. However, the label span is *not* considered a
|
|
|
|
/// ["primary span"][`MultiSpan`]; only the `Span` supplied when creating the diagnostic is
|
|
|
|
/// primary.
|
2020-01-19 01:14:51 +00:00
|
|
|
pub fn span_label(&mut self, span: Span, label: impl Into<String>) -> &mut Self {
|
2019-09-11 22:29:17 +00:00
|
|
|
self.0.diagnostic.span_label(span, label);
|
2017-05-04 12:17:23 +00:00
|
|
|
self
|
|
|
|
}
|
2016-10-11 16:26:32 +00:00
|
|
|
|
2020-01-19 01:14:51 +00:00
|
|
|
/// Labels all the given spans with the provided label.
|
2020-12-15 04:03:19 +00:00
|
|
|
/// See [`Diagnostic::span_label()`] for more information.
|
2020-01-19 01:14:51 +00:00
|
|
|
pub fn span_labels(
|
|
|
|
&mut self,
|
|
|
|
spans: impl IntoIterator<Item = Span>,
|
|
|
|
label: impl AsRef<str>,
|
|
|
|
) -> &mut Self {
|
|
|
|
let label = label.as_ref();
|
|
|
|
for span in spans {
|
|
|
|
self.0.diagnostic.span_label(span, label);
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-11-13 22:16:56 +00:00
|
|
|
forward!(pub fn note_expected_found(
|
|
|
|
&mut self,
|
|
|
|
expected_label: &dyn fmt::Display,
|
|
|
|
expected: DiagnosticStyledString,
|
|
|
|
found_label: &dyn fmt::Display,
|
|
|
|
found: DiagnosticStyledString,
|
|
|
|
) -> &mut Self);
|
2016-10-11 16:26:32 +00:00
|
|
|
|
2019-11-13 22:16:56 +00:00
|
|
|
forward!(pub fn note_expected_found_extra(
|
|
|
|
&mut self,
|
|
|
|
expected_label: &dyn fmt::Display,
|
|
|
|
expected: DiagnosticStyledString,
|
|
|
|
found_label: &dyn fmt::Display,
|
|
|
|
found: DiagnosticStyledString,
|
|
|
|
expected_extra: &dyn fmt::Display,
|
|
|
|
found_extra: &dyn fmt::Display,
|
|
|
|
) -> &mut Self);
|
2016-10-11 16:26:32 +00:00
|
|
|
|
2020-12-15 04:05:31 +00:00
|
|
|
forward!(pub fn note_unsuccessful_coercion(
|
2019-11-13 22:16:56 +00:00
|
|
|
&mut self,
|
|
|
|
expected: DiagnosticStyledString,
|
|
|
|
found: DiagnosticStyledString,
|
|
|
|
) -> &mut Self);
|
2019-01-08 21:14:04 +00:00
|
|
|
|
2016-10-11 16:26:32 +00:00
|
|
|
forward!(pub fn note(&mut self, msg: &str) -> &mut Self);
|
2019-11-13 22:16:56 +00:00
|
|
|
forward!(pub fn span_note<S: Into<MultiSpan>>(
|
|
|
|
&mut self,
|
|
|
|
sp: S,
|
|
|
|
msg: &str,
|
|
|
|
) -> &mut Self);
|
2016-10-11 16:26:32 +00:00
|
|
|
forward!(pub fn warn(&mut self, msg: &str) -> &mut Self);
|
|
|
|
forward!(pub fn span_warn<S: Into<MultiSpan>>(&mut self, sp: S, msg: &str) -> &mut Self);
|
2019-04-25 08:48:25 +00:00
|
|
|
forward!(pub fn help(&mut self, msg: &str) -> &mut Self);
|
2019-11-13 22:16:56 +00:00
|
|
|
forward!(pub fn span_help<S: Into<MultiSpan>>(
|
|
|
|
&mut self,
|
|
|
|
sp: S,
|
|
|
|
msg: &str,
|
|
|
|
) -> &mut Self);
|
2018-09-15 11:58:25 +00:00
|
|
|
|
2020-12-15 04:03:19 +00:00
|
|
|
/// See [`Diagnostic::multipart_suggestion()`].
|
2019-01-25 21:03:27 +00:00
|
|
|
pub fn multipart_suggestion(
|
|
|
|
&mut self,
|
|
|
|
msg: &str,
|
|
|
|
suggestion: Vec<(Span, String)>,
|
|
|
|
applicability: Applicability,
|
|
|
|
) -> &mut Self {
|
2019-09-11 22:29:17 +00:00
|
|
|
if !self.0.allow_suggestions {
|
2019-12-22 22:42:04 +00:00
|
|
|
return self;
|
2018-09-15 11:44:28 +00:00
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
self.0.diagnostic.multipart_suggestion(msg, suggestion, applicability);
|
2018-09-15 11:44:28 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-12-15 04:03:19 +00:00
|
|
|
/// See [`Diagnostic::multipart_suggestions()`].
|
2020-05-29 16:24:59 +00:00
|
|
|
pub fn multipart_suggestions(
|
|
|
|
&mut self,
|
|
|
|
msg: &str,
|
|
|
|
suggestions: Vec<Vec<(Span, String)>>,
|
|
|
|
applicability: Applicability,
|
|
|
|
) -> &mut Self {
|
|
|
|
if !self.0.allow_suggestions {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
self.0.diagnostic.multipart_suggestions(msg, suggestions, applicability);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-12-15 04:03:19 +00:00
|
|
|
/// See [`Diagnostic::tool_only_multipart_suggestion()`].
|
2019-02-11 19:16:22 +00:00
|
|
|
pub fn tool_only_multipart_suggestion(
|
|
|
|
&mut self,
|
|
|
|
msg: &str,
|
|
|
|
suggestion: Vec<(Span, String)>,
|
|
|
|
applicability: Applicability,
|
|
|
|
) -> &mut Self {
|
2019-09-11 22:29:17 +00:00
|
|
|
if !self.0.allow_suggestions {
|
2019-12-22 22:42:04 +00:00
|
|
|
return self;
|
2019-02-11 19:16:22 +00:00
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
self.0.diagnostic.tool_only_multipart_suggestion(msg, suggestion, applicability);
|
2019-02-11 19:16:22 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-12-15 04:03:19 +00:00
|
|
|
/// See [`Diagnostic::span_suggestion()`].
|
2019-01-25 21:03:27 +00:00
|
|
|
pub fn span_suggestion(
|
|
|
|
&mut self,
|
|
|
|
sp: Span,
|
|
|
|
msg: &str,
|
|
|
|
suggestion: String,
|
|
|
|
applicability: Applicability,
|
|
|
|
) -> &mut Self {
|
2019-09-11 22:29:17 +00:00
|
|
|
if !self.0.allow_suggestions {
|
2019-12-22 22:42:04 +00:00
|
|
|
return self;
|
2018-07-26 21:53:15 +00:00
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
self.0.diagnostic.span_suggestion(sp, msg, suggestion, applicability);
|
2018-07-26 21:53:15 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-12-15 04:03:19 +00:00
|
|
|
/// See [`Diagnostic::span_suggestions()`].
|
2019-01-25 21:03:27 +00:00
|
|
|
pub fn span_suggestions(
|
|
|
|
&mut self,
|
|
|
|
sp: Span,
|
|
|
|
msg: &str,
|
|
|
|
suggestions: impl Iterator<Item = String>,
|
|
|
|
applicability: Applicability,
|
|
|
|
) -> &mut Self {
|
2019-09-11 22:29:17 +00:00
|
|
|
if !self.0.allow_suggestions {
|
2019-12-22 22:42:04 +00:00
|
|
|
return self;
|
2018-07-26 21:53:15 +00:00
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
self.0.diagnostic.span_suggestions(sp, msg, suggestions, applicability);
|
2018-07-26 21:53:15 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-12-15 04:03:19 +00:00
|
|
|
/// See [`Diagnostic::span_suggestion_short()`].
|
2019-01-25 21:03:27 +00:00
|
|
|
pub fn span_suggestion_short(
|
|
|
|
&mut self,
|
|
|
|
sp: Span,
|
|
|
|
msg: &str,
|
|
|
|
suggestion: String,
|
|
|
|
applicability: Applicability,
|
|
|
|
) -> &mut Self {
|
2019-09-11 22:29:17 +00:00
|
|
|
if !self.0.allow_suggestions {
|
2019-12-22 22:42:04 +00:00
|
|
|
return self;
|
2018-07-26 21:53:15 +00:00
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
self.0.diagnostic.span_suggestion_short(sp, msg, suggestion, applicability);
|
2018-07-26 21:53:15 +00:00
|
|
|
self
|
|
|
|
}
|
2019-02-08 10:50:53 +00:00
|
|
|
|
2020-12-15 04:03:19 +00:00
|
|
|
/// See [`Diagnostic::span_suggestion_verbose()`].
|
2020-03-29 18:31:58 +00:00
|
|
|
pub fn span_suggestion_verbose(
|
|
|
|
&mut self,
|
|
|
|
sp: Span,
|
|
|
|
msg: &str,
|
|
|
|
suggestion: String,
|
|
|
|
applicability: Applicability,
|
|
|
|
) -> &mut Self {
|
|
|
|
if !self.0.allow_suggestions {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
self.0.diagnostic.span_suggestion_verbose(sp, msg, suggestion, applicability);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-12-15 04:03:19 +00:00
|
|
|
/// See [`Diagnostic::span_suggestion_hidden()`].
|
2019-02-08 10:50:53 +00:00
|
|
|
pub fn span_suggestion_hidden(
|
|
|
|
&mut self,
|
|
|
|
sp: Span,
|
|
|
|
msg: &str,
|
|
|
|
suggestion: String,
|
|
|
|
applicability: Applicability,
|
|
|
|
) -> &mut Self {
|
2019-09-11 22:29:17 +00:00
|
|
|
if !self.0.allow_suggestions {
|
2019-12-22 22:42:04 +00:00
|
|
|
return self;
|
2019-02-08 10:50:53 +00:00
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
self.0.diagnostic.span_suggestion_hidden(sp, msg, suggestion, applicability);
|
2019-02-08 10:50:53 +00:00
|
|
|
self
|
2019-02-09 11:39:08 +00:00
|
|
|
}
|
|
|
|
|
2020-12-15 04:03:19 +00:00
|
|
|
/// See [`Diagnostic::tool_only_span_suggestion()`] for more information.
|
2019-02-09 11:39:08 +00:00
|
|
|
pub fn tool_only_span_suggestion(
|
|
|
|
&mut self,
|
|
|
|
sp: Span,
|
|
|
|
msg: &str,
|
|
|
|
suggestion: String,
|
|
|
|
applicability: Applicability,
|
|
|
|
) -> &mut Self {
|
2019-09-11 22:29:17 +00:00
|
|
|
if !self.0.allow_suggestions {
|
2019-12-22 22:42:04 +00:00
|
|
|
return self;
|
2019-02-09 11:39:08 +00:00
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
self.0.diagnostic.tool_only_span_suggestion(sp, msg, suggestion, applicability);
|
2019-02-09 11:39:08 +00:00
|
|
|
self
|
2019-02-08 10:50:53 +00:00
|
|
|
}
|
|
|
|
|
2020-09-26 21:20:14 +00:00
|
|
|
forward!(pub fn set_primary_message<M: Into<String>>(&mut self, msg: M) -> &mut Self);
|
2016-10-11 16:26:32 +00:00
|
|
|
forward!(pub fn set_span<S: Into<MultiSpan>>(&mut self, sp: S) -> &mut Self);
|
2017-10-27 06:21:22 +00:00
|
|
|
forward!(pub fn code(&mut self, s: DiagnosticId) -> &mut Self);
|
2016-10-11 16:26:32 +00:00
|
|
|
|
2020-12-15 04:03:19 +00:00
|
|
|
/// Allow attaching suggestions this diagnostic.
|
|
|
|
/// If this is set to `false`, then any suggestions attached with the `span_suggestion_*`
|
|
|
|
/// methods after this is set to `false` will be ignored.
|
2018-07-26 21:53:15 +00:00
|
|
|
pub fn allow_suggestions(&mut self, allow: bool) -> &mut Self {
|
2019-09-11 22:29:17 +00:00
|
|
|
self.0.allow_suggestions = allow;
|
2018-07-26 21:53:15 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2016-10-11 16:26:32 +00:00
|
|
|
/// Convenience function for internal use, clients should use one of the
|
2020-12-15 04:03:19 +00:00
|
|
|
/// `struct_*` methods on [`Handler`].
|
2019-09-07 14:20:56 +00:00
|
|
|
crate fn new(handler: &'a Handler, level: Level, message: &str) -> DiagnosticBuilder<'a> {
|
2016-10-11 16:26:32 +00:00
|
|
|
DiagnosticBuilder::new_with_code(handler, level, None, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convenience function for internal use, clients should use one of the
|
2020-12-15 04:03:19 +00:00
|
|
|
/// `struct_*` methods on [`Handler`].
|
2019-12-22 22:42:04 +00:00
|
|
|
crate fn new_with_code(
|
|
|
|
handler: &'a Handler,
|
|
|
|
level: Level,
|
|
|
|
code: Option<DiagnosticId>,
|
|
|
|
message: &str,
|
|
|
|
) -> DiagnosticBuilder<'a> {
|
2017-08-12 22:37:28 +00:00
|
|
|
let diagnostic = Diagnostic::new_with_code(level, code, message);
|
|
|
|
DiagnosticBuilder::new_diagnostic(handler, diagnostic)
|
|
|
|
}
|
|
|
|
|
2017-08-23 19:52:22 +00:00
|
|
|
/// Creates a new `DiagnosticBuilder` with an already constructed
|
|
|
|
/// diagnostic.
|
2019-12-22 22:42:04 +00:00
|
|
|
crate fn new_diagnostic(handler: &'a Handler, diagnostic: Diagnostic) -> DiagnosticBuilder<'a> {
|
2020-01-31 12:24:57 +00:00
|
|
|
debug!("Created new diagnostic");
|
2019-09-11 22:29:17 +00:00
|
|
|
DiagnosticBuilder(Box::new(DiagnosticBuilderInner {
|
2018-07-26 21:53:15 +00:00
|
|
|
handler,
|
|
|
|
diagnostic,
|
|
|
|
allow_suggestions: true,
|
2019-09-11 22:29:17 +00:00
|
|
|
}))
|
2016-10-11 16:26:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Debug for DiagnosticBuilder<'a> {
|
2019-02-06 18:53:01 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-09-11 22:29:17 +00:00
|
|
|
self.0.diagnostic.fmt(f)
|
2016-10-11 16:26:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-15 19:45:21 +00:00
|
|
|
/// Destructor bomb - a `DiagnosticBuilder` must be either emitted or canceled
|
2017-05-18 23:20:48 +00:00
|
|
|
/// or we emit a bug.
|
2016-10-11 16:26:32 +00:00
|
|
|
impl<'a> Drop for DiagnosticBuilder<'a> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if !panicking() && !self.cancelled() {
|
2019-07-11 23:59:19 +00:00
|
|
|
let mut db = DiagnosticBuilder::new(
|
2019-09-11 22:29:17 +00:00
|
|
|
self.0.handler,
|
2019-07-11 23:59:19 +00:00
|
|
|
Level::Bug,
|
|
|
|
"the following error was constructed but not emitted",
|
|
|
|
);
|
2016-10-11 16:26:32 +00:00
|
|
|
db.emit();
|
2019-07-11 23:59:19 +00:00
|
|
|
self.emit();
|
2016-10-11 16:26:32 +00:00
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-31 20:25:16 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! struct_span_err {
|
|
|
|
($session:expr, $span:expr, $code:ident, $($message:tt)*) => ({
|
|
|
|
$session.struct_span_err_with_code(
|
|
|
|
$span,
|
|
|
|
&format!($($message)*),
|
|
|
|
$crate::error_code!($code),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! error_code {
|
2020-01-18 20:53:53 +00:00
|
|
|
($code:ident) => {{ $crate::DiagnosticId::Error(stringify!($code).to_owned()) }};
|
2019-12-31 20:25:16 +00:00
|
|
|
}
|