1 use crate::{Diagnostic, DiagnosticId, DiagnosticStyledString}
;
2 use crate::{Applicability, Level, Handler, StashKey}
;
4 use std
::fmt
::{self, Debug}
;
5 use std
::ops
::{Deref, DerefMut}
;
6 use std
::thread
::panicking
;
7 use syntax_pos
::{MultiSpan, Span}
;
10 /// Used for emitting structured error messages and other diagnostic information.
12 /// If there is some state in a downstream crate you would like to
13 /// access in the methods of `DiagnosticBuilder` here, consider
14 /// extending `HandlerFlags`, accessed via `self.handler.flags`.
17 pub struct DiagnosticBuilder
<'a
>(Box
<DiagnosticBuilderInner
<'a
>>);
19 /// This is a large type, and often used as a return value, especially within
20 /// the frequently-used `PResult` type. In theory, return value optimization
21 /// (RVO) should avoid unnecessary copying. In practice, it does not (at the
22 /// time of writing). The split between `DiagnosticBuilder` and
23 /// `DiagnosticBuilderInner` exists to avoid many `memcpy` calls.
26 struct DiagnosticBuilderInner
<'a
> {
28 diagnostic
: Diagnostic
,
29 allow_suggestions
: bool
,
32 /// In general, the `DiagnosticBuilder` uses deref to allow access to
33 /// the fields and methods of the embedded `diagnostic` in a
34 /// transparent way. *However,* many of the methods are intended to
35 /// be used in a chained way, and hence ought to return `self`. In
36 /// that case, we can't just naively forward to the method on the
37 /// `diagnostic`, because the return type would be a `&Diagnostic`
38 /// instead of a `&DiagnosticBuilder<'a>`. This `forward!` macro makes
39 /// it easy to declare such methods on the builder.
40 macro_rules
! forward
{
41 // Forward pattern for &self -> &Self
44 pub fn $n
:ident(&self, $
($name
:ident
: $ty
:ty
),* $
(,)?
) -> &Self
47 pub fn $
n(&self, $
($name
: $ty
),*) -> &Self {
48 self.diagnostic
.$
n($
($name
),*);
53 // Forward pattern for &mut self -> &mut Self
56 pub fn $n
:ident(&mut self, $
($name
:ident
: $ty
:ty
),* $
(,)?
) -> &mut Self
59 pub fn $
n(&mut self, $
($name
: $ty
),*) -> &mut Self {
60 self.0.diagnostic
.$
n($
($name
),*);
65 // Forward pattern for &mut self -> &mut Self, with S: Into<MultiSpan>
66 // type parameter. No obvious way to make this more generic.
69 pub fn $n
:ident
<S
: Into
<MultiSpan
>>(
71 $
($name
:ident
: $ty
:ty
),*
76 pub fn $n
<S
: Into
<MultiSpan
>>(&mut self, $
($name
: $ty
),*) -> &mut Self {
77 self.0.diagnostic
.$
n($
($name
),*);
83 impl<'a
> Deref
for DiagnosticBuilder
<'a
> {
84 type Target
= Diagnostic
;
86 fn deref(&self) -> &Diagnostic
{
91 impl<'a
> DerefMut
for DiagnosticBuilder
<'a
> {
92 fn deref_mut(&mut self) -> &mut Diagnostic
{
93 &mut self.0.diagnostic
97 impl<'a
> DiagnosticBuilder
<'a
> {
98 /// Emit the diagnostic.
99 pub fn emit(&mut self) {
100 self.0.handler
.emit_diagnostic(&self);
104 /// Emit the diagnostic unless `delay` is true,
105 /// in which case the emission will be delayed as a bug.
107 /// See `emit` and `delay_as_bug` for details.
108 pub fn emit_unless(&mut self, delay
: bool
) {
116 /// Stashes diagnostic for possible later improvement in a different,
117 /// later stage of the compiler. The diagnostic can be accessed with
118 /// the provided `span` and `key` through `.steal_diagnostic` on `Handler`.
120 /// As with `buffer`, this is unless the handler has disabled such buffering.
121 pub fn stash(self, span
: Span
, key
: StashKey
) {
122 if let Some((diag
, handler
)) = self.into_diagnostic() {
123 handler
.stash_diagnostic(span
, key
, diag
);
127 /// Converts the builder to a `Diagnostic` for later emission,
128 /// unless handler has disabled such buffering.
129 pub fn into_diagnostic(mut self) -> Option
<(Diagnostic
, &'a Handler
)> {
130 if self.0.handler
.flags
.dont_buffer_diagnostics
||
131 self.0.handler
.flags
.treat_err_as_bug
.is_some()
137 let handler
= self.0.handler
;
139 // We need to use `ptr::read` because `DiagnosticBuilder` implements `Drop`.
142 diagnostic
= std
::ptr
::read(&self.0.diagnostic
);
143 std
::mem
::forget(self);
145 // Logging here is useful to help track down where in logs an error was
147 debug
!("buffer: diagnostic={:?}", diagnostic
);
149 Some((diagnostic
, handler
))
152 /// Buffers the diagnostic for later emission,
153 /// unless handler has disabled such buffering.
154 pub fn buffer(self, buffered_diagnostics
: &mut Vec
<Diagnostic
>) {
155 buffered_diagnostics
.extend(self.into_diagnostic().map(|(diag
, _
)| diag
));
158 /// Convenience function for internal use, clients should use one of the
159 /// span_* methods instead.
160 pub fn sub
<S
: Into
<MultiSpan
>>(
166 let span
= span
.map(|s
| s
.into()).unwrap_or_else(|| MultiSpan
::new());
167 self.0.diagnostic
.sub(level
, message
, span
, None
);
171 /// Delay emission of this diagnostic as a bug.
173 /// This can be useful in contexts where an error indicates a bug but
174 /// typically this only happens when other compilation errors have already
175 /// happened. In those cases this can be used to defer emission of this
176 /// diagnostic as a bug in the compiler only if no other errors have been
179 /// In the meantime, though, callsites are required to deal with the "bug"
180 /// locally in whichever way makes the most sense.
181 pub fn delay_as_bug(&mut self) {
182 self.level
= Level
::Bug
;
183 self.0.handler
.delay_as_bug(self.0.diagnostic
.clone());
187 /// Adds a span/label to be included in the resulting snippet.
188 /// This is pushed onto the `MultiSpan` that was created when the
189 /// diagnostic was first built. If you don't call this function at
190 /// all, and you just supplied a `Span` to create the diagnostic,
191 /// then the snippet will just include that `Span`, which is
192 /// called the primary span.
193 pub fn span_label
<T
: Into
<String
>>(&mut self, span
: Span
, label
: T
) -> &mut Self {
194 self.0.diagnostic
.span_label(span
, label
);
198 forward
!(pub fn note_expected_found(
200 expected_label
: &dyn fmt
::Display
,
201 expected
: DiagnosticStyledString
,
202 found_label
: &dyn fmt
::Display
,
203 found
: DiagnosticStyledString
,
206 forward
!(pub fn note_expected_found_extra(
208 expected_label
: &dyn fmt
::Display
,
209 expected
: DiagnosticStyledString
,
210 found_label
: &dyn fmt
::Display
,
211 found
: DiagnosticStyledString
,
212 expected_extra
: &dyn fmt
::Display
,
213 found_extra
: &dyn fmt
::Display
,
216 forward
!(pub fn note_unsuccessfull_coercion(
218 expected
: DiagnosticStyledString
,
219 found
: DiagnosticStyledString
,
222 forward
!(pub fn note(&mut self, msg
: &str) -> &mut Self);
223 forward
!(pub fn span_note
<S
: Into
<MultiSpan
>>(
228 forward
!(pub fn warn(&mut self, msg
: &str) -> &mut Self);
229 forward
!(pub fn span_warn
<S
: Into
<MultiSpan
>>(&mut self, sp
: S
, msg
: &str) -> &mut Self);
230 forward
!(pub fn help(&mut self, msg
: &str) -> &mut Self);
231 forward
!(pub fn span_help
<S
: Into
<MultiSpan
>>(
237 pub fn multipart_suggestion(
240 suggestion
: Vec
<(Span
, String
)>,
241 applicability
: Applicability
,
243 if !self.0.allow_suggestions
{
246 self.0.diagnostic
.multipart_suggestion(
254 pub fn tool_only_multipart_suggestion(
257 suggestion
: Vec
<(Span
, String
)>,
258 applicability
: Applicability
,
260 if !self.0.allow_suggestions
{
263 self.0.diagnostic
.tool_only_multipart_suggestion(
271 pub fn span_suggestion(
276 applicability
: Applicability
,
278 if !self.0.allow_suggestions
{
281 self.0.diagnostic
.span_suggestion(
290 pub fn span_suggestions(
294 suggestions
: impl Iterator
<Item
= String
>,
295 applicability
: Applicability
,
297 if !self.0.allow_suggestions
{
300 self.0.diagnostic
.span_suggestions(
309 pub fn span_suggestion_short(
314 applicability
: Applicability
,
316 if !self.0.allow_suggestions
{
319 self.0.diagnostic
.span_suggestion_short(
328 pub fn span_suggestion_hidden(
333 applicability
: Applicability
,
335 if !self.0.allow_suggestions
{
338 self.0.diagnostic
.span_suggestion_hidden(
347 pub fn tool_only_span_suggestion(
352 applicability
: Applicability
,
354 if !self.0.allow_suggestions
{
357 self.0.diagnostic
.tool_only_span_suggestion(
366 forward
!(pub fn set_span
<S
: Into
<MultiSpan
>>(&mut self, sp
: S
) -> &mut Self);
367 forward
!(pub fn code(&mut self, s
: DiagnosticId
) -> &mut Self);
369 pub fn allow_suggestions(&mut self, allow
: bool
) -> &mut Self {
370 self.0.allow_suggestions
= allow
;
374 /// Convenience function for internal use, clients should use one of the
375 /// struct_* methods on Handler.
376 crate fn new(handler
: &'a Handler
, level
: Level
, message
: &str) -> DiagnosticBuilder
<'a
> {
377 DiagnosticBuilder
::new_with_code(handler
, level
, None
, message
)
380 /// Convenience function for internal use, clients should use one of the
381 /// struct_* methods on Handler.
382 crate fn new_with_code(handler
: &'a Handler
,
384 code
: Option
<DiagnosticId
>,
386 -> DiagnosticBuilder
<'a
> {
387 let diagnostic
= Diagnostic
::new_with_code(level
, code
, message
);
388 DiagnosticBuilder
::new_diagnostic(handler
, diagnostic
)
391 /// Creates a new `DiagnosticBuilder` with an already constructed
393 crate fn new_diagnostic(handler
: &'a Handler
, diagnostic
: Diagnostic
)
394 -> DiagnosticBuilder
<'a
> {
395 DiagnosticBuilder(Box
::new(DiagnosticBuilderInner
{
398 allow_suggestions
: true,
403 impl<'a
> Debug
for DiagnosticBuilder
<'a
> {
404 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
405 self.0.diagnostic
.fmt(f
)
409 /// Destructor bomb - a `DiagnosticBuilder` must be either emitted or canceled
410 /// or we emit a bug.
411 impl<'a
> Drop
for DiagnosticBuilder
<'a
> {
413 if !panicking() && !self.cancelled() {
414 let mut db
= DiagnosticBuilder
::new(
417 "the following error was constructed but not emitted",