pub struct Diag<'a, G: EmissionGuarantee = ErrorGuaranteed> {
pub dcx: DiagCtxtHandle<'a>,
diag: Option<Box<DiagInner>>,
_marker: PhantomData<G>,
}
Expand description
Used for emitting structured error messages and other diagnostic information. Wraps a DiagInner
, adding some useful things.
dcx
field, allowing it to (a) emit itself, and (b) do a drop check that it has been emitted or cancelled.EmissionGuarantee
, which determines the type returned from emit
.Each constructed Diag
must be consumed by a function such as emit
, cancel
, delay_as_bug
, or into_diag
. A panic occurs if a Diag
is dropped without being consumed by one of these functions.
If there is some state in a downstream crate you would like to access in the methods of Diag
here, consider extending DiagCtxtFlags
.
Why the Option
? It is always Some
until the Diag
is consumed via emit
, cancel
, etc. At that point it is consumed and replaced with None
. Then drop
checks that it is None
; if not, it panics because a diagnostic was built but not used.
Why the Box? DiagInner
is a large type, and Diag
is 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).
Allow moving diagnostics between different error tainting contexts
SourceCreates a new Diag
with an already constructed diagnostic.
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.
SourceAppends a labeled span to the diagnostic.
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.
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â; only the Span
supplied when creating the diagnostic is primary. See Diag::span_label()
.
Appends a labeled span to the diagnostic.
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.
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â; only the Span
supplied when creating the diagnostic is primary. See Diag::span_label()
.
Labels all the given spans with the provided label. See Self::span_label()
for more information. See Diag::span_labels()
.
Labels all the given spans with the provided label. See Self::span_label()
for more information. See Diag::span_labels()
.
Add a note attached to this diagnostic. See Diag::note()
.
Add a note attached to this diagnostic. See Diag::note()
.
This is like Diag::note()
, but itâs only printed once.
Prints the span with a note above it. This is like Diag::note()
, but it gets its own span. See Diag::span_note()
.
Prints the span with a note above it. This is like Diag::note()
, but it gets its own span. See Diag::span_note()
.
Prints the span with a note above it. This is like Diag::note_once()
, but it gets its own span.
Add a warning attached to this diagnostic. See Diag::warn()
.
Add a warning attached to this diagnostic. See Diag::warn()
.
Prints the span with a warning above it. This is like Diag::warn()
, but it gets its own span.
Add a help message attached to this diagnostic. See Diag::help()
.
Add a help message attached to this diagnostic. See Diag::help()
.
This is like Diag::help()
, but itâs only printed once.
Add a help message attached to this diagnostic with a customizable highlighted message.
SourceAdd a help message attached to this diagnostic with a customizable highlighted message.
SourcePrints the span with some help above it. This is like Diag::help()
, but it gets its own span. See Diag::span_help()
.
Prints the span with some help above it. This is like Diag::help()
, but it gets its own span. See Diag::span_help()
.
Disallow attaching suggestions to this diagnostic. Any suggestions attached e.g. with the span_suggestion_*
methods (before and after the call to disable_suggestions
) will be ignored.
Prevent new suggestions from being added to this diagnostic.
Suggestions added before the call to .seal_suggestions()
will be preserved and new suggestions will be ignored.
Helper for pushing to self.suggestions
.
A new suggestion is added if suggestions are enabled for this diagnostic. Otherwise, they are ignored.
SourceShow a suggestion that has multiple parts to it. In other words, multiple changes need to be applied as part of this suggestion. See Diag::multipart_suggestion()
.
Show a suggestion that has multiple parts to it. In other words, multiple changes need to be applied as part of this suggestion. See Diag::multipart_suggestion()
.
Show a suggestion that has multiple parts to it, always as its own subdiagnostic. In other words, multiple changes need to be applied as part of this suggestion.
SourceDiag::multipart_suggestion()
but you can set the SuggestionStyle
.
Prints out a message with for a multipart suggestion without showing the suggested code.
This is intended to be used for suggestions that are obvious in what the changes need to be from the message, showing the span label inline would be visually unpleasant (marginally overlapping spans or multiline spans) and showing the snippet window wouldnât improve understandability.
SourcePrints out a message with a suggested edit of the code.
In case of short messages and a simple suggestion, rustc displays it as a label:
try adding parentheses: `(tup.0).1`
The message
:
is added automatically)See CodeSuggestion
for more information. See Diag::span_suggestion()
.
Prints out a message with a suggested edit of the code.
In case of short messages and a simple suggestion, rustc displays it as a label:
try adding parentheses: `(tup.0).1`
The message
:
is added automatically)See CodeSuggestion
for more information. See Diag::span_suggestion()
.
Diag::span_suggestion()
but you can set the SuggestionStyle
.
Always show the suggested change. See Diag::span_suggestion_verbose()
.
Always show the suggested change. See Diag::span_suggestion_verbose()
.
Prints out a message with multiple suggested edits of the code. See also Diag::span_suggestion()
. See Diag::span_suggestions()
.
Prints out a message with multiple suggested edits of the code. See also Diag::span_suggestion()
. See Diag::span_suggestions()
.
Prints out a message with multiple suggested edits of the code, where each edit consists of multiple parts. See also Diag::multipart_suggestion()
.
Prints out a message with a suggested edit of the code. If the suggestion is presented inline, it will only show the message and not the suggestion.
See CodeSuggestion
for more information. See Diag::span_suggestion_short()
.
Prints out a message with a suggested edit of the code. If the suggestion is presented inline, it will only show the message and not the suggestion.
See CodeSuggestion
for more information. See Diag::span_suggestion_short()
.
Prints out a message for a suggestion without showing the suggested code.
This is intended to be used for suggestions that are obvious in what the changes need to be from the message, showing the span label inline would be visually unpleasant (marginally overlapping spans or multiline spans) and showing the snippet window wouldnât improve understandability.
SourceAdds a suggestion to the JSON output that will not be shown in the CLI.
This is intended to be used for suggestions that are very obvious in what the changes need to be from the message, but we still want other tools to be able to apply them. See Diag::tool_only_span_suggestion()
.
Adds a suggestion to the JSON output that will not be shown in the CLI.
This is intended to be used for suggestions that are very obvious in what the changes need to be from the message, but we still want other tools to be able to apply them. See Diag::tool_only_span_suggestion()
.
Add a subdiagnostic from a type that implements Subdiagnostic
(see rustc_macros::Subdiagnostic). Performs eager translation of any translatable messages used in the subdiagnostic, so suitable for use with repeated messages (i.e. re-use of interpolated variables).
Fluent variables are not namespaced from each other, so when Diagnostic
s and Subdiagnostic
s use the same variable name, one value will clobber the other. Eagerly translating the diagnostic uses the variables defined right then, before the clobbering occurs.
Add a span. See Diag::span()
.
Add a span. See Diag::span()
.
Add an error code. See Diag::code()
.
Add an error code. See Diag::code()
.
Add an argument. See Diag::lint_id()
.
Add an argument. See Diag::lint_id()
.
Add a primary message. See Diag::primary_message()
.
Add a primary message. See Diag::primary_message()
.
Add an argument. See Diag::arg()
.
Add an argument. See Diag::arg()
.
Helper function that takes a SubdiagMessage
and returns a DiagMessage
by combining it with the primary message of the diagnostic (if translatable, otherwise it just passes the userâs string along).
Convenience function for internal use, clients should use one of the public methods above.
Used by proc_macro_server
for implementing server::Diagnostic
.
Convenience function for internal use, clients should use one of the public methods above.
SourceTakes the diagnostic. For use by methods that consume the Diag: emit
, cancel
, etc. Afterwards, drop
is the only code that will be run on self
.
This method allows us to access the path of the file where âlong typesâ are written to.
When calling Diag::emit
, as part of that we will check if a long_ty_path
has been set, and if it has been then we add a note mentioning the file where the âlong typesâ were written to.
When calling tcx.short_string()
after a Diag
is constructed, the preferred way of doing so is tcx.short_string(ty, diag.long_ty_path())
. The diagnostic itself is the one that keeps the existence of a âlong typeâ anywhere in the diagnostic, so the note telling the user where we wrote the file to is only printed once at most, and it makes it much harder to forget to set it.
If the diagnostic hasnât been created before a âshort ty stringâ is created, then you should ensure that this method is called to set it *diag.long_ty_path() = path
.
As a rule of thumb, if you see or add at least one tcx.short_string()
call anywhere, in a scope, diag.long_ty_path()
should be called once somewhere close by.
Most emit_producing_guarantee
functions use this as a starting point.
ErrorGuaranteed::emit_producing_guarantee
uses this.
Emit and consume the diagnostic.
SourceEmit 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.
Cancel and consume the diagnostic. (A diagnostic must either be emitted or cancelled or it will panic when dropped).
SourceSee DiagCtxt::stash_diagnostic
for details.
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.
SourceNote: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...)
attributes. Please see the Rust Reference's âType Layoutâ chapter for details on type layout guarantees.
Size: 24 bytes
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4