1use std::borrow::Cow;
2use std::fmt::{self, Debug};
3use std::hash::{Hash, Hasher};
4use std::marker::PhantomData;
5use std::ops::{Deref, DerefMut};
6use std::panic;
7use std::path::PathBuf;
8use std::thread::panicking;
910use rustc_data_structures::sync::{DynSend, DynSync};
11use rustc_error_messages::{DiagArgMap, DiagArgName, DiagArgValue, IntoDiagArg};
12use rustc_lint_defs::{Applicability, LintExpectationId};
13use rustc_macros::{Decodable, Encodable};
14use rustc_span::{DUMMY_SP, Span, Spanned, Symbol};
15use tracing::debug;
1617use crate::{
18CodeSuggestion, DiagCtxtHandle, DiagMessage, ErrCode, ErrorGuaranteed, ExplicitBug, Level,
19MultiSpan, StashKey, Style, Substitution, SubstitutionPart, SuggestionStyle, Suggestions,
20};
2122/// Trait for types that `Diag::emit` can return as a "guarantee" (or "proof")
23/// token that the emission happened.
24pub trait EmissionGuarantee: Sized {
25/// This exists so that bugs and fatal errors can both result in `!` (an
26 /// abort) when emitted, but have different aborting behaviour.
27type EmitResult = Self;
2829/// Implementation of `Diag::emit`, fully controlled by each `impl` of
30 /// `EmissionGuarantee`, to make it impossible to create a value of
31 /// `Self::EmitResult` without actually performing the emission.
32#[track_caller]
33fn emit_producing_guarantee(diag: Diag<'_, Self>) -> Self::EmitResult;
34}
3536impl EmissionGuaranteefor ErrorGuaranteed {
37fn emit_producing_guarantee(diag: Diag<'_, Self>) -> Self::EmitResult {
38diag.emit_producing_error_guaranteed()
39 }
40}
4142impl EmissionGuaranteefor () {
43fn emit_producing_guarantee(diag: Diag<'_, Self>) -> Self::EmitResult {
44diag.emit_producing_nothing();
45 }
46}
4748/// Marker type which enables implementation of `create_bug` and `emit_bug` functions for
49/// bug diagnostics.
50#[derive(#[automatically_derived]
impl ::core::marker::Copy for BugAbort { }Copy, #[automatically_derived]
impl ::core::clone::Clone for BugAbort {
#[inline]
fn clone(&self) -> BugAbort { *self }
}Clone)]
51pub struct BugAbort;
5253impl EmissionGuaranteefor BugAbort {
54type EmitResult = !;
5556fn emit_producing_guarantee(diag: Diag<'_, Self>) -> Self::EmitResult {
57diag.emit_producing_nothing();
58 panic::panic_any(ExplicitBug);
59 }
60}
6162/// Marker type which enables implementation of `create_fatal` and `emit_fatal` functions for
63/// fatal diagnostics.
64#[derive(#[automatically_derived]
impl ::core::marker::Copy for FatalAbort { }Copy, #[automatically_derived]
impl ::core::clone::Clone for FatalAbort {
#[inline]
fn clone(&self) -> FatalAbort { *self }
}Clone)]
65pub struct FatalAbort;
6667impl EmissionGuaranteefor FatalAbort {
68type EmitResult = !;
6970fn emit_producing_guarantee(diag: Diag<'_, Self>) -> Self::EmitResult {
71diag.emit_producing_nothing();
72crate::FatalError.raise()
73 }
74}
7576impl EmissionGuaranteefor rustc_span::fatal_error::FatalError {
77fn emit_producing_guarantee(diag: Diag<'_, Self>) -> Self::EmitResult {
78diag.emit_producing_nothing();
79 rustc_span::fatal_error::FatalError80 }
81}
8283/// Trait implemented by error types. This is rarely implemented manually. Instead, use
84/// `#[derive(Diagnostic)]` -- see [rustc_macros::Diagnostic].
85///
86/// When implemented manually, it should be generic over the emission
87/// guarantee, i.e.:
88/// ```ignore (fragment)
89/// impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for Foo { ... }
90/// ```
91/// rather than being specific:
92/// ```ignore (fragment)
93/// impl<'a> Diagnostic<'a> for Bar { ... } // the default type param is `ErrorGuaranteed`
94/// impl<'a> Diagnostic<'a, ()> for Baz { ... }
95/// ```
96/// There are two reasons for this.
97/// - A diagnostic like `Foo` *could* be emitted at any level -- `level` is
98/// passed in to `into_diag` from outside. Even if in practice it is
99/// always emitted at a single level, we let the diagnostic creation/emission
100/// site determine the level (by using `create_err`, `emit_warn`, etc.)
101/// rather than the `Diagnostic` impl.
102/// - Derived impls are always generic, and it's good for the hand-written
103/// impls to be consistent with them.
104#[rustc_diagnostic_item = "Diagnostic"]
105pub trait Diagnostic<'a, G: EmissionGuarantee = ErrorGuaranteed> {
106/// Write out as a diagnostic out of `DiagCtxt`.
107#[must_use]
108 #[track_caller]
109fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G>;
110}
111112impl<'a, T, G> Diagnostic<'a, G> for Spanned<T>
113where
114T: Diagnostic<'a, G>,
115 G: EmissionGuarantee,
116{
117fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G> {
118self.node.into_diag(dcx, level).with_span(self.span)
119 }
120}
121122impl<'a> Diagnostic<'a, ()>
123for Box<
124dyn for<'b> FnOnce(DiagCtxtHandle<'b>, Level) -> Diag<'b, ()> + DynSync + DynSend + 'static,
125 >
126{
127fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
128self(dcx, level)
129 }
130}
131132/// Type used to emit diagnostic through a closure instead of implementing the `Diagnostic` trait.
133pub struct DiagDecorator<F: FnOnce(&mut Diag<'_, ()>)>(pub F);
134135impl<'a, F: FnOnce(&mut Diag<'_, ()>)> Diagnostic<'a, ()> for DiagDecorator<F> {
136fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
137let mut diag = Diag::new(dcx, level, "");
138 (self.0)(&mut diag);
139diag140 }
141}
142143/// Trait implemented by error types. This should not be implemented manually. Instead, use
144/// `#[derive(Subdiagnostic)]` -- see [rustc_macros::Subdiagnostic].
145#[rustc_diagnostic_item = "Subdiagnostic"]
146pub trait Subdiagnostic {
147/// Add a subdiagnostic to an existing diagnostic.
148fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>);
149}
150151#[derive(#[automatically_derived]
impl ::core::clone::Clone for DiagLocation {
#[inline]
fn clone(&self) -> DiagLocation {
DiagLocation {
file: ::core::clone::Clone::clone(&self.file),
line: ::core::clone::Clone::clone(&self.line),
col: ::core::clone::Clone::clone(&self.col),
}
}
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for DiagLocation {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field3_finish(f, "DiagLocation",
"file", &self.file, "line", &self.line, "col", &&self.col)
}
}Debug, const _: () =
{
impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
for DiagLocation {
fn encode(&self, __encoder: &mut __E) {
match *self {
DiagLocation {
file: ref __binding_0,
line: ref __binding_1,
col: ref __binding_2 } => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_2,
__encoder);
}
}
}
}
};Encodable, const _: () =
{
impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
for DiagLocation {
fn decode(__decoder: &mut __D) -> Self {
DiagLocation {
file: ::rustc_serialize::Decodable::decode(__decoder),
line: ::rustc_serialize::Decodable::decode(__decoder),
col: ::rustc_serialize::Decodable::decode(__decoder),
}
}
}
};Decodable)]
152pub struct DiagLocation {
153 file: Cow<'static, str>,
154 line: u32,
155 col: u32,
156}
157158impl DiagLocation {
159#[track_caller]
160pub fn caller() -> Self {
161let loc = panic::Location::caller();
162DiagLocation { file: loc.file().into(), line: loc.line(), col: loc.column() }
163 }
164}
165166impl fmt::Displayfor DiagLocation {
167fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
168f.write_fmt(format_args!("{0}:{1}:{2}", self.file, self.line, self.col))write!(f, "{}:{}:{}", self.file, self.line, self.col)169 }
170}
171172#[derive(#[automatically_derived]
impl ::core::clone::Clone for IsLint {
#[inline]
fn clone(&self) -> IsLint {
IsLint {
name: ::core::clone::Clone::clone(&self.name),
has_future_breakage: ::core::clone::Clone::clone(&self.has_future_breakage),
}
}
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for IsLint {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "IsLint",
"name", &self.name, "has_future_breakage",
&&self.has_future_breakage)
}
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for IsLint {
#[inline]
fn eq(&self, other: &IsLint) -> bool {
self.has_future_breakage == other.has_future_breakage &&
self.name == other.name
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for IsLint {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<String>;
let _: ::core::cmp::AssertParamIsEq<bool>;
}
}Eq, #[automatically_derived]
impl ::core::hash::Hash for IsLint {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.name, state);
::core::hash::Hash::hash(&self.has_future_breakage, state)
}
}Hash, const _: () =
{
impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
for IsLint {
fn encode(&self, __encoder: &mut __E) {
match *self {
IsLint {
name: ref __binding_0, has_future_breakage: ref __binding_1
} => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
}
}
}
}
};Encodable, const _: () =
{
impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
for IsLint {
fn decode(__decoder: &mut __D) -> Self {
IsLint {
name: ::rustc_serialize::Decodable::decode(__decoder),
has_future_breakage: ::rustc_serialize::Decodable::decode(__decoder),
}
}
}
};Decodable)]
173pub struct IsLint {
174/// The lint name.
175pub(crate) name: String,
176/// Indicates whether this lint should show up in cargo's future breakage report.
177has_future_breakage: bool,
178}
179180#[derive(#[automatically_derived]
impl ::core::fmt::Debug for DiagStyledString {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"DiagStyledString", &&self.0)
}
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for DiagStyledString {
#[inline]
fn eq(&self, other: &DiagStyledString) -> bool { self.0 == other.0 }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for DiagStyledString {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<Vec<StringPart>>;
}
}Eq)]
181pub struct DiagStyledString(pub Vec<StringPart>);
182183impl DiagStyledString {
184pub fn new() -> DiagStyledString {
185DiagStyledString(::alloc::vec::Vec::new()vec![])
186 }
187pub fn push_normal<S: Into<String>>(&mut self, t: S) {
188self.0.push(StringPart::normal(t));
189 }
190pub fn push_highlighted<S: Into<String>>(&mut self, t: S) {
191self.0.push(StringPart::highlighted(t));
192 }
193pub fn push<S: Into<String>>(&mut self, t: S, highlight: bool) {
194if highlight {
195self.push_highlighted(t);
196 } else {
197self.push_normal(t);
198 }
199 }
200pub fn normal<S: Into<String>>(t: S) -> DiagStyledString {
201DiagStyledString(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[StringPart::normal(t)]))vec![StringPart::normal(t)])
202 }
203204pub fn highlighted<S: Into<String>>(t: S) -> DiagStyledString {
205DiagStyledString(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[StringPart::highlighted(t)]))vec![StringPart::highlighted(t)])
206 }
207208pub fn content(&self) -> String {
209self.0.iter().map(|x| x.content.as_str()).collect::<String>()
210 }
211}
212213#[derive(#[automatically_derived]
impl ::core::fmt::Debug for StringPart {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "StringPart",
"content", &self.content, "style", &&self.style)
}
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for StringPart {
#[inline]
fn eq(&self, other: &StringPart) -> bool {
self.content == other.content && self.style == other.style
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for StringPart {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<String>;
let _: ::core::cmp::AssertParamIsEq<Style>;
}
}Eq)]
214pub struct StringPart {
215 content: String,
216 style: Style,
217}
218219impl StringPart {
220pub fn normal<S: Into<String>>(content: S) -> StringPart {
221StringPart { content: content.into(), style: Style::NoStyle }
222 }
223224pub fn highlighted<S: Into<String>>(content: S) -> StringPart {
225StringPart { content: content.into(), style: Style::Highlight }
226 }
227}
228229/// The main part of a diagnostic. Note that `Diag`, which wraps this type, is
230/// used for most operations, and should be used instead whenever possible.
231/// This type should only be used when `Diag`'s lifetime causes difficulties,
232/// e.g. when storing diagnostics within `DiagCtxt`.
233#[must_use]
234#[derive(#[automatically_derived]
impl ::core::clone::Clone for DiagInner {
#[inline]
fn clone(&self) -> DiagInner {
DiagInner {
level: ::core::clone::Clone::clone(&self.level),
messages: ::core::clone::Clone::clone(&self.messages),
code: ::core::clone::Clone::clone(&self.code),
lint_id: ::core::clone::Clone::clone(&self.lint_id),
span: ::core::clone::Clone::clone(&self.span),
children: ::core::clone::Clone::clone(&self.children),
suggestions: ::core::clone::Clone::clone(&self.suggestions),
args: ::core::clone::Clone::clone(&self.args),
sort_span: ::core::clone::Clone::clone(&self.sort_span),
is_lint: ::core::clone::Clone::clone(&self.is_lint),
long_ty_path: ::core::clone::Clone::clone(&self.long_ty_path),
emitted_at: ::core::clone::Clone::clone(&self.emitted_at),
}
}
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for DiagInner {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
let names: &'static _ =
&["level", "messages", "code", "lint_id", "span", "children",
"suggestions", "args", "sort_span", "is_lint",
"long_ty_path", "emitted_at"];
let values: &[&dyn ::core::fmt::Debug] =
&[&self.level, &self.messages, &self.code, &self.lint_id,
&self.span, &self.children, &self.suggestions, &self.args,
&self.sort_span, &self.is_lint, &self.long_ty_path,
&&self.emitted_at];
::core::fmt::Formatter::debug_struct_fields_finish(f, "DiagInner",
names, values)
}
}Debug, const _: () =
{
impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
for DiagInner {
fn encode(&self, __encoder: &mut __E) {
match *self {
DiagInner {
level: ref __binding_0,
messages: ref __binding_1,
code: ref __binding_2,
lint_id: ref __binding_3,
span: ref __binding_4,
children: ref __binding_5,
suggestions: ref __binding_6,
args: ref __binding_7,
sort_span: ref __binding_8,
is_lint: ref __binding_9,
long_ty_path: ref __binding_10,
emitted_at: ref __binding_11 } => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_2,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_3,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_4,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_5,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_6,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_7,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_8,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_9,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_10,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_11,
__encoder);
}
}
}
}
};Encodable, const _: () =
{
impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
for DiagInner {
fn decode(__decoder: &mut __D) -> Self {
DiagInner {
level: ::rustc_serialize::Decodable::decode(__decoder),
messages: ::rustc_serialize::Decodable::decode(__decoder),
code: ::rustc_serialize::Decodable::decode(__decoder),
lint_id: ::rustc_serialize::Decodable::decode(__decoder),
span: ::rustc_serialize::Decodable::decode(__decoder),
children: ::rustc_serialize::Decodable::decode(__decoder),
suggestions: ::rustc_serialize::Decodable::decode(__decoder),
args: ::rustc_serialize::Decodable::decode(__decoder),
sort_span: ::rustc_serialize::Decodable::decode(__decoder),
is_lint: ::rustc_serialize::Decodable::decode(__decoder),
long_ty_path: ::rustc_serialize::Decodable::decode(__decoder),
emitted_at: ::rustc_serialize::Decodable::decode(__decoder),
}
}
}
};Decodable)]
235pub struct DiagInner {
236// NOTE(eddyb) this is private to disallow arbitrary after-the-fact changes,
237 // outside of what methods in this crate themselves allow.
238pub(crate) level: Level,
239240pub messages: Vec<(DiagMessage, Style)>,
241pub code: Option<ErrCode>,
242pub lint_id: Option<LintExpectationId>,
243pub span: MultiSpan,
244pub children: Vec<Subdiag>,
245pub suggestions: Suggestions,
246pub args: DiagArgMap,
247248/// This is not used for highlighting or rendering any error message. Rather, it can be used
249 /// as a sort key to sort a buffer of diagnostics. By default, it is the primary span of
250 /// `span` if there is one. Otherwise, it is `DUMMY_SP`.
251pub sort_span: Span,
252253pub is_lint: Option<IsLint>,
254255pub long_ty_path: Option<PathBuf>,
256/// With `-Ztrack_diagnostics` enabled,
257 /// we print where in rustc this error was emitted.
258pub emitted_at: DiagLocation,
259}
260261impl DiagInner {
262#[track_caller]
263pub fn new<M: Into<DiagMessage>>(level: Level, message: M) -> Self {
264DiagInner::new_with_messages(level, ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(message.into(), Style::NoStyle)]))vec![(message.into(), Style::NoStyle)])
265 }
266267#[track_caller]
268pub fn new_with_messages(level: Level, messages: Vec<(DiagMessage, Style)>) -> Self {
269DiagInner {
270level,
271 lint_id: None,
272messages,
273 code: None,
274 span: MultiSpan::new(),
275 children: ::alloc::vec::Vec::new()vec![],
276 suggestions: Suggestions::Enabled(::alloc::vec::Vec::new()vec![]),
277 args: Default::default(),
278 sort_span: DUMMY_SP,
279 is_lint: None,
280 long_ty_path: None,
281 emitted_at: DiagLocation::caller(),
282 }
283 }
284285#[inline(always)]
286pub fn level(&self) -> Level {
287self.level
288 }
289290pub fn is_error(&self) -> bool {
291match self.level {
292 Level::Bug | Level::Fatal | Level::Error | Level::DelayedBug => true,
293294 Level::ForceWarning295 | Level::Warning296 | Level::Note297 | Level::OnceNote298 | Level::Help299 | Level::OnceHelp300 | Level::FailureNote301 | Level::Allow302 | Level::Expect => false,
303 }
304 }
305306/// Indicates whether this diagnostic should show up in cargo's future breakage report.
307pub(crate) fn has_future_breakage(&self) -> bool {
308#[allow(non_exhaustive_omitted_patterns)] match self.is_lint {
Some(IsLint { has_future_breakage: true, .. }) => true,
_ => false,
}matches!(self.is_lint, Some(IsLint { has_future_breakage: true, .. }))309 }
310311pub(crate) fn is_force_warn(&self) -> bool {
312match self.level {
313 Level::ForceWarning => {
314if !self.is_lint.is_some() {
::core::panicking::panic("assertion failed: self.is_lint.is_some()")
};assert!(self.is_lint.is_some());
315true
316}
317_ => false,
318 }
319 }
320321pub(crate) fn sub(&mut self, level: Level, message: impl Into<DiagMessage>, span: MultiSpan) {
322let sub = Subdiag { level, messages: ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(message.into(), Style::NoStyle)]))vec![(message.into(), Style::NoStyle)], span };
323self.children.push(sub);
324 }
325326pub(crate) fn arg(&mut self, name: impl Into<DiagArgName>, arg: impl IntoDiagArg) {
327let name = name.into();
328let value = arg.into_diag_arg(&mut self.long_ty_path);
329// This assertion is to avoid subdiagnostics overwriting an existing diagnostic arg.
330if true {
if !(!self.args.contains_key(&name) ||
self.args.get(&name) == Some(&value)) {
{
::core::panicking::panic_fmt(format_args!("arg {0} already exists",
name));
}
};
};debug_assert!(
331 !self.args.contains_key(&name) || self.args.get(&name) == Some(&value),
332"arg {} already exists",
333 name
334 );
335self.args.insert(name, value);
336 }
337338pub fn remove_arg(&mut self, name: &str) {
339self.args.swap_remove(name);
340 }
341342pub fn emitted_at_sub_diag(&self) -> Subdiag {
343let track = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("-Ztrack-diagnostics: created at {0}",
self.emitted_at))
})format!("-Ztrack-diagnostics: created at {}", self.emitted_at);
344Subdiag {
345 level: crate::Level::Note,
346 messages: ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(DiagMessage::Str(Cow::Owned(track)), Style::NoStyle)]))vec![(DiagMessage::Str(Cow::Owned(track)), Style::NoStyle)],
347 span: MultiSpan::new(),
348 }
349 }
350351/// Fields used for Hash, and PartialEq trait.
352fn keys(
353&self,
354 ) -> (
355&Level,
356&[(DiagMessage, Style)],
357&Option<ErrCode>,
358&MultiSpan,
359&[Subdiag],
360&Suggestions,
361Vec<(&DiagArgName, &DiagArgValue)>,
362&Option<IsLint>,
363 ) {
364 (
365&self.level,
366&self.messages,
367&self.code,
368&self.span,
369&self.children,
370&self.suggestions,
371self.args.iter().collect(),
372// omit self.sort_span
373&self.is_lint,
374// omit self.emitted_at
375)
376 }
377}
378379impl Hashfor DiagInner {
380fn hash<H>(&self, state: &mut H)
381where
382H: Hasher,
383 {
384self.keys().hash(state);
385 }
386}
387388impl PartialEqfor DiagInner {
389fn eq(&self, other: &Self) -> bool {
390self.keys() == other.keys()
391 }
392}
393394/// A "sub"-diagnostic attached to a parent diagnostic.
395/// For example, a note attached to an error.
396#[derive(#[automatically_derived]
impl ::core::clone::Clone for Subdiag {
#[inline]
fn clone(&self) -> Subdiag {
Subdiag {
level: ::core::clone::Clone::clone(&self.level),
messages: ::core::clone::Clone::clone(&self.messages),
span: ::core::clone::Clone::clone(&self.span),
}
}
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for Subdiag {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field3_finish(f, "Subdiag",
"level", &self.level, "messages", &self.messages, "span",
&&self.span)
}
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for Subdiag {
#[inline]
fn eq(&self, other: &Subdiag) -> bool {
self.level == other.level && self.messages == other.messages &&
self.span == other.span
}
}PartialEq, #[automatically_derived]
impl ::core::hash::Hash for Subdiag {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.level, state);
::core::hash::Hash::hash(&self.messages, state);
::core::hash::Hash::hash(&self.span, state)
}
}Hash, const _: () =
{
impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
for Subdiag {
fn encode(&self, __encoder: &mut __E) {
match *self {
Subdiag {
level: ref __binding_0,
messages: ref __binding_1,
span: ref __binding_2 } => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_2,
__encoder);
}
}
}
}
};Encodable, const _: () =
{
impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
for Subdiag {
fn decode(__decoder: &mut __D) -> Self {
Subdiag {
level: ::rustc_serialize::Decodable::decode(__decoder),
messages: ::rustc_serialize::Decodable::decode(__decoder),
span: ::rustc_serialize::Decodable::decode(__decoder),
}
}
}
};Decodable)]
397pub struct Subdiag {
398pub level: Level,
399pub messages: Vec<(DiagMessage, Style)>,
400pub span: MultiSpan,
401}
402403/// Used for emitting structured error messages and other diagnostic information.
404/// Wraps a `DiagInner`, adding some useful things.
405/// - The `dcx` field, allowing it to (a) emit itself, and (b) do a drop check
406/// that it has been emitted or cancelled.
407/// - The `EmissionGuarantee`, which determines the type returned from `emit`.
408///
409/// Each constructed `Diag` must be consumed by a function such as `emit`,
410/// `cancel`, `delay_as_bug`, or `into_diag`. A panic occurs if a `Diag`
411/// is dropped without being consumed by one of these functions.
412///
413/// If there is some state in a downstream crate you would like to access in
414/// the methods of `Diag` here, consider extending `DiagCtxtFlags`.
415#[must_use]
416pub struct Diag<'a, G: EmissionGuarantee = ErrorGuaranteed> {
417pub dcx: DiagCtxtHandle<'a>,
418419/// Why the `Option`? It is always `Some` until the `Diag` is consumed via
420 /// `emit`, `cancel`, etc. At that point it is consumed and replaced with
421 /// `None`. Then `drop` checks that it is `None`; if not, it panics because
422 /// a diagnostic was built but not used.
423 ///
424 /// Why the Box? `DiagInner` is a large type, and `Diag` is often used as a
425 /// return value, especially within the frequently-used `PResult` type. In
426 /// theory, return value optimization (RVO) should avoid unnecessary
427 /// copying. In practice, it does not (at the time of writing).
428diag: Option<Box<DiagInner>>,
429430 _marker: PhantomData<G>,
431}
432433// Cloning a `Diag` is a recipe for a diagnostic being emitted twice, which
434// would be bad.
435impl<G> !Clonefor Diag<'_, G> {}
436437const _: [(); 3 * size_of::<usize>()] =
[(); ::std::mem::size_of::<Diag<'_, ()>>()];rustc_data_structures::static_assert_size!(Diag<'_, ()>, 3 * size_of::<usize>());
438439impl<G: EmissionGuarantee> Dereffor Diag<'_, G> {
440type Target = DiagInner;
441442fn deref(&self) -> &DiagInner {
443self.diag.as_ref().unwrap()
444 }
445}
446447impl<G: EmissionGuarantee> DerefMutfor Diag<'_, G> {
448fn deref_mut(&mut self) -> &mut DiagInner {
449self.diag.as_mut().unwrap()
450 }
451}
452453impl<G: EmissionGuarantee> Debugfor Diag<'_, G> {
454fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
455self.diag.fmt(f)
456 }
457}
458459/// `Diag` impls many `&mut self -> &mut Self` methods. Each one modifies an
460/// existing diagnostic, either in a standalone fashion, e.g.
461/// `err.code(code);`, or in a chained fashion to make multiple modifications,
462/// e.g. `err.code(code).span(span);`.
463///
464/// This macro creates an equivalent `self -> Self` method, with a `with_`
465/// prefix. This can be used in a chained fashion when making a new diagnostic,
466/// e.g. `let err = struct_err(msg).with_code(code);`, or emitting a new
467/// diagnostic, e.g. `struct_err(msg).with_code(code).emit();`.
468///
469/// Although the latter method can be used to modify an existing diagnostic,
470/// e.g. `err = err.with_code(code);`, this should be avoided because the former
471/// method gives shorter code, e.g. `err.code(code);`.
472///
473/// Note: the `with_` methods are added only when needed. If you want to use
474/// one and it's not defined, feel free to add it.
475///
476/// Note: any doc comments must be within the `with_fn!` call.
477macro_rules!with_fn {
478 {
479$with_f:ident,
480 $(#[$attrs:meta])*
481pub fn $f:ident(&mut $self:ident, $($name:ident: $ty:ty),* $(,)?) -> &mut Self {
482 $($body:tt)*
483 }
484 } => {
485// The original function.
486$(#[$attrs])*
487#[doc = concat!("See [`Diag::", stringify!($f), "()`].")]
488pub fn $f(&mut $self, $($name: $ty),*) -> &mut Self {
489 $($body)*
490 }
491492// The `with_*` variant.
493$(#[$attrs])*
494#[doc = concat!("See [`Diag::", stringify!($f), "()`].")]
495pub fn $with_f(mut $self, $($name: $ty),*) -> Self {
496$self.$f($($name),*);
497$self
498}
499 };
500}
501502impl<'a, G: EmissionGuarantee> Diag<'a, G> {
503#[track_caller]
504pub fn new(dcx: DiagCtxtHandle<'a>, level: Level, message: impl Into<DiagMessage>) -> Self {
505Self::new_diagnostic(dcx, DiagInner::new(level, message))
506 }
507508/// Allow moving diagnostics between different error tainting contexts
509pub fn with_dcx(mut self, dcx: DiagCtxtHandle<'_>) -> Diag<'_, G> {
510Diag { dcx, diag: self.diag.take(), _marker: PhantomData }
511 }
512513/// Creates a new `Diag` with an already constructed diagnostic.
514#[track_caller]
515pub(crate) fn new_diagnostic(dcx: DiagCtxtHandle<'a>, diag: DiagInner) -> Self {
516{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_errors/src/diagnostic.rs:516",
"rustc_errors::diagnostic", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_errors/src/diagnostic.rs"),
::tracing_core::__macro_support::Option::Some(516u32),
::tracing_core::__macro_support::Option::Some("rustc_errors::diagnostic"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("Created new diagnostic")
as &dyn Value))])
});
} else { ; }
};debug!("Created new diagnostic");
517Self { dcx, diag: Some(Box::new(diag)), _marker: PhantomData }
518 }
519520/// Delay emission of this diagnostic as a bug.
521 ///
522 /// This can be useful in contexts where an error indicates a bug but
523 /// typically this only happens when other compilation errors have already
524 /// happened. In those cases this can be used to defer emission of this
525 /// diagnostic as a bug in the compiler only if no other errors have been
526 /// emitted.
527 ///
528 /// In the meantime, though, callsites are required to deal with the "bug"
529 /// locally in whichever way makes the most sense.
530#[track_caller]
531pub fn downgrade_to_delayed_bug(&mut self) {
532if !#[allow(non_exhaustive_omitted_patterns)] match self.level {
Level::Error | Level::DelayedBug => true,
_ => false,
} {
{
::core::panicking::panic_fmt(format_args!("downgrade_to_delayed_bug: cannot downgrade {0:?} to DelayedBug: not an error",
self.level));
}
};assert!(
533matches!(self.level, Level::Error | Level::DelayedBug),
534"downgrade_to_delayed_bug: cannot downgrade {:?} to DelayedBug: not an error",
535self.level
536 );
537self.level = Level::DelayedBug;
538 }
539540/// Make emitting this diagnostic fatal
541 ///
542 /// Changes the level of this diagnostic to Fatal, and importantly also changes the emission guarantee.
543 /// This is sound for errors that would otherwise be printed, but now simply exit the process instead.
544 /// This function still gives an emission guarantee, the guarantee is now just that it exits fatally.
545 /// For delayed bugs this is different, since those are buffered. If we upgrade one to fatal, another
546 /// might now be ignored.
547#[track_caller]
548pub fn upgrade_to_fatal(mut self) -> Diag<'a, FatalAbort> {
549if !#[allow(non_exhaustive_omitted_patterns)] match self.level {
Level::Error => true,
_ => false,
} {
{
::core::panicking::panic_fmt(format_args!("upgrade_to_fatal: cannot upgrade {0:?} to Fatal: not an error",
self.level));
}
};assert!(
550matches!(self.level, Level::Error),
551"upgrade_to_fatal: cannot upgrade {:?} to Fatal: not an error",
552self.level
553 );
554self.level = Level::Fatal;
555556// Take is okay since we immediately rewrap it in another diagnostic.
557 // i.e. we do emit it despite defusing the original diagnostic's drop bomb.
558let diag = self.diag.take();
559Diag { dcx: self.dcx, diag, _marker: PhantomData }
560 }
561562#[doc = r" Appends a labeled span to the diagnostic."]
#[doc = r""]
#[doc =
r" Labels are used to convey additional context for the diagnostic's primary span. They will"]
#[doc =
r" be shown together with the original diagnostic's span, *not* with spans added by"]
#[doc =
r" `span_note`, `span_help`, etc. Therefore, if the primary span is not displayable (because"]
#[doc =
r" the span is `DUMMY_SP` or the source code isn't found), labels will not be displayed"]
#[doc = r" either."]
#[doc = r""]
#[doc =
r" Implementation-wise, the label span is pushed onto the [`MultiSpan`] that was created when"]
#[doc =
r" the diagnostic was constructed. However, the label span is *not* considered a"]
#[doc =
r#" ["primary span"][`MultiSpan`]; only the `Span` supplied when creating the diagnostic is"#]
#[doc = r" primary."]
#[doc = "See [`Diag::span_label()`]."]
pub fn span_label(&mut self, span: Span, label: impl Into<DiagMessage>)
-> &mut Self {
self.span.push_span_label(span, label.into());
self
}
#[doc = r" Appends a labeled span to the diagnostic."]
#[doc = r""]
#[doc =
r" Labels are used to convey additional context for the diagnostic's primary span. They will"]
#[doc =
r" be shown together with the original diagnostic's span, *not* with spans added by"]
#[doc =
r" `span_note`, `span_help`, etc. Therefore, if the primary span is not displayable (because"]
#[doc =
r" the span is `DUMMY_SP` or the source code isn't found), labels will not be displayed"]
#[doc = r" either."]
#[doc = r""]
#[doc =
r" Implementation-wise, the label span is pushed onto the [`MultiSpan`] that was created when"]
#[doc =
r" the diagnostic was constructed. However, the label span is *not* considered a"]
#[doc =
r#" ["primary span"][`MultiSpan`]; only the `Span` supplied when creating the diagnostic is"#]
#[doc = r" primary."]
#[doc = "See [`Diag::span_label()`]."]
pub fn with_span_label(mut self, span: Span, label: impl Into<DiagMessage>)
-> Self {
self.span_label(span, label);
self
}with_fn! { with_span_label,
563/// Appends a labeled span to the diagnostic.
564 ///
565 /// Labels are used to convey additional context for the diagnostic's primary span. They will
566 /// be shown together with the original diagnostic's span, *not* with spans added by
567 /// `span_note`, `span_help`, etc. Therefore, if the primary span is not displayable (because
568 /// the span is `DUMMY_SP` or the source code isn't found), labels will not be displayed
569 /// either.
570 ///
571 /// Implementation-wise, the label span is pushed onto the [`MultiSpan`] that was created when
572 /// the diagnostic was constructed. However, the label span is *not* considered a
573 /// ["primary span"][`MultiSpan`]; only the `Span` supplied when creating the diagnostic is
574 /// primary.
575pub fn span_label(&mut self, span: Span, label: impl Into<DiagMessage>) -> &mut Self {
576self.span.push_span_label(span, label.into());
577self
578} }579580#[doc = r" Labels all the given spans with the provided label."]
#[doc = r" See [`Self::span_label()`] for more information."]
#[doc = "See [`Diag::span_labels()`]."]
pub fn span_labels(&mut self, spans: impl IntoIterator<Item = Span>,
label: &str) -> &mut Self {
for span in spans { self.span_label(span, label.to_string()); }
self
}
#[doc = r" Labels all the given spans with the provided label."]
#[doc = r" See [`Self::span_label()`] for more information."]
#[doc = "See [`Diag::span_labels()`]."]
pub fn with_span_labels(mut self, spans: impl IntoIterator<Item = Span>,
label: &str) -> Self {
self.span_labels(spans, label);
self
}with_fn! { with_span_labels,
581/// Labels all the given spans with the provided label.
582 /// See [`Self::span_label()`] for more information.
583pub fn span_labels(&mut self, spans: impl IntoIterator<Item = Span>, label: &str) -> &mut Self {
584for span in spans {
585self.span_label(span, label.to_string());
586 }
587self
588} }589590pub fn replace_span_with(&mut self, after: Span, keep_label: bool) -> &mut Self {
591let before = self.span.clone();
592self.span(after);
593for span_label in before.span_labels() {
594if let Some(label) = span_label.label {
595if span_label.is_primary && keep_label {
596self.span.push_span_label(after, label);
597 } else {
598self.span.push_span_label(span_label.span, label);
599 }
600 }
601 }
602self603 }
604605pub fn note_expected_found(
606&mut self,
607 expected_label: &str,
608 expected: DiagStyledString,
609 found_label: &str,
610 found: DiagStyledString,
611 ) -> &mut Self {
612self.note_expected_found_extra(
613expected_label,
614expected,
615found_label,
616found,
617DiagStyledString::normal(""),
618DiagStyledString::normal(""),
619 )
620 }
621622pub fn note_expected_found_extra(
623&mut self,
624 expected_label: &str,
625 expected: DiagStyledString,
626 found_label: &str,
627 found: DiagStyledString,
628 expected_extra: DiagStyledString,
629 found_extra: DiagStyledString,
630 ) -> &mut Self {
631let expected_label = expected_label.to_string();
632let expected_label = if expected_label.is_empty() {
633"expected".to_string()
634 } else {
635::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected {0}", expected_label))
})format!("expected {expected_label}")636 };
637let found_label = found_label.to_string();
638let found_label = if found_label.is_empty() {
639"found".to_string()
640 } else {
641::alloc::__export::must_use({
::alloc::fmt::format(format_args!("found {0}", found_label))
})format!("found {found_label}")642 };
643let (found_padding, expected_padding) = if expected_label.len() > found_label.len() {
644 (expected_label.len() - found_label.len(), 0)
645 } else {
646 (0, found_label.len() - expected_label.len())
647 };
648let mut msg = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[StringPart::normal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}{1} `",
" ".repeat(expected_padding), expected_label))
}))]))vec![StringPart::normal(format!(
649"{}{} `",
650" ".repeat(expected_padding),
651 expected_label
652 ))];
653msg.extend(expected.0);
654msg.push(StringPart::normal(::alloc::__export::must_use({ ::alloc::fmt::format(format_args!("`")) })format!("`")));
655msg.extend(expected_extra.0);
656msg.push(StringPart::normal(::alloc::__export::must_use({ ::alloc::fmt::format(format_args!("\n")) })format!("\n")));
657msg.push(StringPart::normal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}{1} `",
" ".repeat(found_padding), found_label))
})format!("{}{} `", " ".repeat(found_padding), found_label)));
658msg.extend(found.0);
659msg.push(StringPart::normal(::alloc::__export::must_use({ ::alloc::fmt::format(format_args!("`")) })format!("`")));
660msg.extend(found_extra.0);
661662// For now, just attach these as notes.
663self.highlighted_note(msg);
664self665 }
666667pub fn note_trait_signature(&mut self, name: Symbol, signature: String) -> &mut Self {
668self.highlighted_note(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[StringPart::normal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` from trait: `",
name))
})), StringPart::highlighted(signature),
StringPart::normal("`")]))vec![
669 StringPart::normal(format!("`{name}` from trait: `")),
670 StringPart::highlighted(signature),
671 StringPart::normal("`"),
672 ]);
673self674 }
675676#[doc = r" Add a note attached to this diagnostic."]
#[doc = "See [`Diag::note()`]."]
pub fn note(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
self.sub(Level::Note, msg, MultiSpan::new());
self
}
#[doc = r" Add a note attached to this diagnostic."]
#[doc = "See [`Diag::note()`]."]
pub fn with_note(mut self, msg: impl Into<DiagMessage>) -> Self {
self.note(msg);
self
}with_fn! { with_note,
677/// Add a note attached to this diagnostic.
678pub fn note(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
679self.sub(Level::Note, msg, MultiSpan::new());
680self
681} }682683pub fn highlighted_note(&mut self, msg: Vec<StringPart>) -> &mut Self {
684self.sub_with_highlights(Level::Note, msg, MultiSpan::new());
685self686 }
687688pub fn highlighted_span_note(
689&mut self,
690 span: impl Into<MultiSpan>,
691 msg: Vec<StringPart>,
692 ) -> &mut Self {
693self.sub_with_highlights(Level::Note, msg, span.into());
694self695 }
696697/// This is like [`Diag::note()`], but it's only printed once.
698pub fn note_once(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
699self.sub(Level::OnceNote, msg, MultiSpan::new());
700self701 }
702703#[doc = r" Prints the span with a note above it."]
#[doc = r" This is like [`Diag::note()`], but it gets its own span."]
#[doc = "See [`Diag::span_note()`]."]
pub fn span_note(&mut self, sp: impl Into<MultiSpan>,
msg: impl Into<DiagMessage>) -> &mut Self {
self.sub(Level::Note, msg, sp.into());
self
}
#[doc = r" Prints the span with a note above it."]
#[doc = r" This is like [`Diag::note()`], but it gets its own span."]
#[doc = "See [`Diag::span_note()`]."]
pub fn with_span_note(mut self, sp: impl Into<MultiSpan>,
msg: impl Into<DiagMessage>) -> Self {
self.span_note(sp, msg);
self
}with_fn! { with_span_note,
704/// Prints the span with a note above it.
705 /// This is like [`Diag::note()`], but it gets its own span.
706pub fn span_note(
707&mut self,
708 sp: impl Into<MultiSpan>,
709 msg: impl Into<DiagMessage>,
710 ) -> &mut Self {
711self.sub(Level::Note, msg, sp.into());
712self
713} }714715/// Prints the span with a note above it.
716 /// This is like [`Diag::note_once()`], but it gets its own span.
717pub fn span_note_once<S: Into<MultiSpan>>(
718&mut self,
719 sp: S,
720 msg: impl Into<DiagMessage>,
721 ) -> &mut Self {
722self.sub(Level::OnceNote, msg, sp.into());
723self724 }
725726#[doc = r" Add a warning attached to this diagnostic."]
#[doc = "See [`Diag::warn()`]."]
pub fn warn(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
self.sub(Level::Warning, msg, MultiSpan::new());
self
}
#[doc = r" Add a warning attached to this diagnostic."]
#[doc = "See [`Diag::warn()`]."]
pub fn with_warn(mut self, msg: impl Into<DiagMessage>) -> Self {
self.warn(msg);
self
}with_fn! { with_warn,
727/// Add a warning attached to this diagnostic.
728pub fn warn(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
729self.sub(Level::Warning, msg, MultiSpan::new());
730self
731} }732733/// Prints the span with a warning above it.
734 /// This is like [`Diag::warn()`], but it gets its own span.
735pub fn span_warn<S: Into<MultiSpan>>(
736&mut self,
737 sp: S,
738 msg: impl Into<DiagMessage>,
739 ) -> &mut Self {
740self.sub(Level::Warning, msg, sp.into());
741self742 }
743744#[doc = r" Add a help message attached to this diagnostic."]
#[doc = "See [`Diag::help()`]."]
pub fn help(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
self.sub(Level::Help, msg, MultiSpan::new());
self
}
#[doc = r" Add a help message attached to this diagnostic."]
#[doc = "See [`Diag::help()`]."]
pub fn with_help(mut self, msg: impl Into<DiagMessage>) -> Self {
self.help(msg);
self
}with_fn! { with_help,
745/// Add a help message attached to this diagnostic.
746pub fn help(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
747self.sub(Level::Help, msg, MultiSpan::new());
748self
749} }750751/// This is like [`Diag::help()`], but it's only printed once.
752pub fn help_once(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
753self.sub(Level::OnceHelp, msg, MultiSpan::new());
754self755 }
756757/// Add a help message attached to this diagnostic with a customizable highlighted message.
758pub fn highlighted_help(&mut self, msg: Vec<StringPart>) -> &mut Self {
759self.sub_with_highlights(Level::Help, msg, MultiSpan::new());
760self761 }
762763/// Add a help message attached to this diagnostic with a customizable highlighted message.
764pub fn highlighted_span_help(
765&mut self,
766 span: impl Into<MultiSpan>,
767 msg: Vec<StringPart>,
768 ) -> &mut Self {
769self.sub_with_highlights(Level::Help, msg, span.into());
770self771 }
772773#[doc = r" Prints the span with some help above it."]
#[doc = r" This is like [`Diag::help()`], but it gets its own span."]
#[doc = "See [`Diag::span_help()`]."]
pub fn span_help(&mut self, sp: impl Into<MultiSpan>,
msg: impl Into<DiagMessage>) -> &mut Self {
self.sub(Level::Help, msg, sp.into());
self
}
#[doc = r" Prints the span with some help above it."]
#[doc = r" This is like [`Diag::help()`], but it gets its own span."]
#[doc = "See [`Diag::span_help()`]."]
pub fn with_span_help(mut self, sp: impl Into<MultiSpan>,
msg: impl Into<DiagMessage>) -> Self {
self.span_help(sp, msg);
self
}with_fn! { with_span_help,
774/// Prints the span with some help above it.
775 /// This is like [`Diag::help()`], but it gets its own span.
776pub fn span_help(
777&mut self,
778 sp: impl Into<MultiSpan>,
779 msg: impl Into<DiagMessage>,
780 ) -> &mut Self {
781self.sub(Level::Help, msg, sp.into());
782self
783} }784785/// Disallow attaching suggestions to this diagnostic.
786 /// Any suggestions attached e.g. with the `span_suggestion_*` methods
787 /// (before and after the call to `disable_suggestions`) will be ignored.
788pub fn disable_suggestions(&mut self) -> &mut Self {
789self.suggestions = Suggestions::Disabled;
790self791 }
792793/// Prevent new suggestions from being added to this diagnostic.
794 ///
795 /// Suggestions added before the call to `.seal_suggestions()` will be preserved
796 /// and new suggestions will be ignored.
797pub fn seal_suggestions(&mut self) -> &mut Self {
798if let Suggestions::Enabled(suggestions) = &mut self.suggestions {
799let suggestions_slice = std::mem::take(suggestions).into_boxed_slice();
800self.suggestions = Suggestions::Sealed(suggestions_slice);
801 }
802self803 }
804805/// Helper for pushing to `self.suggestions`.
806 ///
807 /// A new suggestion is added if suggestions are enabled for this diagnostic.
808 /// Otherwise, they are ignored.
809fn push_suggestion(&mut self, suggestion: CodeSuggestion) {
810for subst in &suggestion.substitutions {
811for part in &subst.parts {
812let span = part.span;
813let call_site = span.ctxt().outer_expn_data().call_site;
814if span.in_derive_expansion() && span.overlaps_or_adjacent(call_site) {
815// Ignore if spans is from derive macro.
816return;
817 }
818 }
819 }
820821if let Suggestions::Enabled(suggestions) = &mut self.suggestions {
822suggestions.push(suggestion);
823 }
824 }
825826#[doc =
r" Show a suggestion that has multiple parts to it, always as its own subdiagnostic."]
#[doc =
r" In other words, multiple changes need to be applied as part of this suggestion."]
#[doc = "See [`Diag::multipart_suggestion()`]."]
pub fn multipart_suggestion(&mut self, msg: impl Into<DiagMessage>,
suggestion: Vec<(Span, String)>, applicability: Applicability)
-> &mut Self {
self.multipart_suggestion_with_style(msg, suggestion, applicability,
SuggestionStyle::ShowAlways)
}
#[doc =
r" Show a suggestion that has multiple parts to it, always as its own subdiagnostic."]
#[doc =
r" In other words, multiple changes need to be applied as part of this suggestion."]
#[doc = "See [`Diag::multipart_suggestion()`]."]
pub fn with_multipart_suggestion(mut self, msg: impl Into<DiagMessage>,
suggestion: Vec<(Span, String)>, applicability: Applicability) -> Self {
self.multipart_suggestion(msg, suggestion, applicability);
self
}with_fn! { with_multipart_suggestion,
827/// Show a suggestion that has multiple parts to it, always as its own subdiagnostic.
828 /// In other words, multiple changes need to be applied as part of this suggestion.
829pub fn multipart_suggestion(
830&mut self,
831 msg: impl Into<DiagMessage>,
832 suggestion: Vec<(Span, String)>,
833 applicability: Applicability,
834 ) -> &mut Self {
835self.multipart_suggestion_with_style(
836 msg,
837 suggestion,
838 applicability,
839 SuggestionStyle::ShowAlways,
840 )
841 } }842843/// [`Diag::multipart_suggestion()`] but you can set the [`SuggestionStyle`].
844pub fn multipart_suggestion_with_style(
845&mut self,
846 msg: impl Into<DiagMessage>,
847mut suggestion: Vec<(Span, String)>,
848 applicability: Applicability,
849 style: SuggestionStyle,
850 ) -> &mut Self {
851let mut seen = crate::FxHashSet::default();
852suggestion.retain(|(span, msg)| seen.insert((span.lo(), span.hi(), msg.clone())));
853854let parts = suggestion855 .into_iter()
856 .map(|(span, snippet)| SubstitutionPart { snippet, span })
857 .collect::<Vec<_>>();
858859if !!parts.is_empty() {
::core::panicking::panic("assertion failed: !parts.is_empty()")
};assert!(!parts.is_empty());
860if true {
match (&parts.iter().find(|part|
part.span.is_empty() && part.snippet.is_empty()), &None) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val,
::core::option::Option::Some(format_args!("Span must not be empty and have no suggestion")));
}
}
};
};debug_assert_eq!(
861 parts.iter().find(|part| part.span.is_empty() && part.snippet.is_empty()),
862None,
863"Span must not be empty and have no suggestion",
864 );
865if true {
match (&parts.array_windows().find(|[a, b]| a.span.overlaps(b.span)),
&None) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val,
::core::option::Option::Some(format_args!("suggestion must not have overlapping parts")));
}
}
};
};debug_assert_eq!(
866 parts.array_windows().find(|[a, b]| a.span.overlaps(b.span)),
867None,
868"suggestion must not have overlapping parts",
869 );
870871self.push_suggestion(CodeSuggestion {
872 substitutions: ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[Substitution { parts }]))vec![Substitution { parts }],
873 msg: msg.into(),
874style,
875applicability,
876 });
877self878 }
879880/// Prints out a message with for a multipart suggestion without showing the suggested code.
881 ///
882 /// This is intended to be used for suggestions that are obvious in what the changes need to
883 /// be from the message, showing the span label inline would be visually unpleasant
884 /// (marginally overlapping spans or multiline spans) and showing the snippet window wouldn't
885 /// improve understandability.
886pub fn tool_only_multipart_suggestion(
887&mut self,
888 msg: impl Into<DiagMessage>,
889 suggestion: Vec<(Span, String)>,
890 applicability: Applicability,
891 ) -> &mut Self {
892self.multipart_suggestion_with_style(
893msg,
894suggestion,
895applicability,
896 SuggestionStyle::CompletelyHidden,
897 )
898 }
899900#[doc = r" Prints out a message with a suggested edit of the code."]
#[doc = r""]
#[doc =
r" In case of short messages and a simple suggestion, rustc displays it as a label:"]
#[doc = r""]
#[doc = r" ```text"]
#[doc = r" try adding parentheses: `(tup.0).1`"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" The message"]
#[doc = r""]
#[doc =
r" * should not end in any punctuation (a `:` is added automatically)"]
#[doc = r#" * should not be a question (avoid language like "did you mean")"#]
#[doc =
r#" * should not contain any phrases like "the following", "as shown", etc."#]
#[doc = r#" * may look like "to do xyz, use" or "to do xyz, use abc""#]
#[doc =
r" * may contain a name of a function, variable, or type, but not whole expressions"]
#[doc = r""]
#[doc = r" See [`CodeSuggestion`] for more information."]
#[doc = "See [`Diag::span_suggestion()`]."]
pub fn span_suggestion(&mut self, sp: Span, msg: impl Into<DiagMessage>,
suggestion: impl ToString, applicability: Applicability) -> &mut Self {
self.span_suggestion_with_style(sp, msg, suggestion, applicability,
SuggestionStyle::ShowCode);
self
}
#[doc = r" Prints out a message with a suggested edit of the code."]
#[doc = r""]
#[doc =
r" In case of short messages and a simple suggestion, rustc displays it as a label:"]
#[doc = r""]
#[doc = r" ```text"]
#[doc = r" try adding parentheses: `(tup.0).1`"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" The message"]
#[doc = r""]
#[doc =
r" * should not end in any punctuation (a `:` is added automatically)"]
#[doc = r#" * should not be a question (avoid language like "did you mean")"#]
#[doc =
r#" * should not contain any phrases like "the following", "as shown", etc."#]
#[doc = r#" * may look like "to do xyz, use" or "to do xyz, use abc""#]
#[doc =
r" * may contain a name of a function, variable, or type, but not whole expressions"]
#[doc = r""]
#[doc = r" See [`CodeSuggestion`] for more information."]
#[doc = "See [`Diag::span_suggestion()`]."]
pub fn with_span_suggestion(mut self, sp: Span, msg: impl Into<DiagMessage>,
suggestion: impl ToString, applicability: Applicability) -> Self {
self.span_suggestion(sp, msg, suggestion, applicability);
self
}with_fn! { with_span_suggestion,
901/// Prints out a message with a suggested edit of the code.
902 ///
903 /// In case of short messages and a simple suggestion, rustc displays it as a label:
904 ///
905 /// ```text
906 /// try adding parentheses: `(tup.0).1`
907 /// ```
908 ///
909 /// The message
910 ///
911 /// * should not end in any punctuation (a `:` is added automatically)
912 /// * should not be a question (avoid language like "did you mean")
913 /// * should not contain any phrases like "the following", "as shown", etc.
914 /// * may look like "to do xyz, use" or "to do xyz, use abc"
915 /// * may contain a name of a function, variable, or type, but not whole expressions
916 ///
917 /// See [`CodeSuggestion`] for more information.
918pub fn span_suggestion(
919&mut self,
920 sp: Span,
921 msg: impl Into<DiagMessage>,
922 suggestion: impl ToString,
923 applicability: Applicability,
924 ) -> &mut Self {
925self.span_suggestion_with_style(
926 sp,
927 msg,
928 suggestion,
929 applicability,
930 SuggestionStyle::ShowCode,
931 );
932self
933} }934935#[doc =
r" [`Diag::span_suggestion()`] but you can set the [`SuggestionStyle`]."]
#[doc = "See [`Diag::span_suggestion_with_style()`]."]
pub fn span_suggestion_with_style(&mut self, sp: Span,
msg: impl Into<DiagMessage>, suggestion: impl ToString,
applicability: Applicability, style: SuggestionStyle) -> &mut Self {
if true {
if !!(sp.is_empty() && suggestion.to_string().is_empty()) {
{
::core::panicking::panic_fmt(format_args!("Span must not be empty and have no suggestion"));
}
};
};
self.push_suggestion(CodeSuggestion {
substitutions: ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[Substitution {
parts: ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[SubstitutionPart {
snippet: suggestion.to_string(),
span: sp,
}])),
}])),
msg: msg.into(),
style,
applicability,
});
self
}
#[doc =
r" [`Diag::span_suggestion()`] but you can set the [`SuggestionStyle`]."]
#[doc = "See [`Diag::span_suggestion_with_style()`]."]
pub fn with_span_suggestion_with_style(mut self, sp: Span,
msg: impl Into<DiagMessage>, suggestion: impl ToString,
applicability: Applicability, style: SuggestionStyle) -> Self {
self.span_suggestion_with_style(sp, msg, suggestion, applicability,
style);
self
}with_fn! { with_span_suggestion_with_style,
936/// [`Diag::span_suggestion()`] but you can set the [`SuggestionStyle`].
937pub fn span_suggestion_with_style(
938&mut self,
939 sp: Span,
940 msg: impl Into<DiagMessage>,
941 suggestion: impl ToString,
942 applicability: Applicability,
943 style: SuggestionStyle,
944 ) -> &mut Self {
945debug_assert!(
946 !(sp.is_empty() && suggestion.to_string().is_empty()),
947"Span must not be empty and have no suggestion"
948);
949self.push_suggestion(CodeSuggestion {
950 substitutions: vec![Substitution {
951 parts: vec![SubstitutionPart { snippet: suggestion.to_string(), span: sp }],
952 }],
953 msg: msg.into(),
954 style,
955 applicability,
956 });
957self
958} }959960#[doc = r" Always show the suggested change."]
#[doc = "See [`Diag::span_suggestion_verbose()`]."]
pub fn span_suggestion_verbose(&mut self, sp: Span,
msg: impl Into<DiagMessage>, suggestion: impl ToString,
applicability: Applicability) -> &mut Self {
self.span_suggestion_with_style(sp, msg, suggestion, applicability,
SuggestionStyle::ShowAlways);
self
}
#[doc = r" Always show the suggested change."]
#[doc = "See [`Diag::span_suggestion_verbose()`]."]
pub fn with_span_suggestion_verbose(mut self, sp: Span,
msg: impl Into<DiagMessage>, suggestion: impl ToString,
applicability: Applicability) -> Self {
self.span_suggestion_verbose(sp, msg, suggestion, applicability);
self
}with_fn! { with_span_suggestion_verbose,
961/// Always show the suggested change.
962pub fn span_suggestion_verbose(
963&mut self,
964 sp: Span,
965 msg: impl Into<DiagMessage>,
966 suggestion: impl ToString,
967 applicability: Applicability,
968 ) -> &mut Self {
969self.span_suggestion_with_style(
970 sp,
971 msg,
972 suggestion,
973 applicability,
974 SuggestionStyle::ShowAlways,
975 );
976self
977} }978979#[doc = r" Prints out a message with multiple suggested edits of the code."]
#[doc = r" See also [`Diag::span_suggestion()`]."]
#[doc = "See [`Diag::span_suggestions()`]."]
pub fn span_suggestions(&mut self, sp: Span, msg: impl Into<DiagMessage>,
suggestions: impl IntoIterator<Item = String>,
applicability: Applicability) -> &mut Self {
self.span_suggestions_with_style(sp, msg, suggestions, applicability,
SuggestionStyle::ShowAlways)
}
#[doc = r" Prints out a message with multiple suggested edits of the code."]
#[doc = r" See also [`Diag::span_suggestion()`]."]
#[doc = "See [`Diag::span_suggestions()`]."]
pub fn with_span_suggestions(mut self, sp: Span, msg: impl Into<DiagMessage>,
suggestions: impl IntoIterator<Item = String>,
applicability: Applicability) -> Self {
self.span_suggestions(sp, msg, suggestions, applicability);
self
}with_fn! { with_span_suggestions,
980/// Prints out a message with multiple suggested edits of the code.
981 /// See also [`Diag::span_suggestion()`].
982pub fn span_suggestions(
983&mut self,
984 sp: Span,
985 msg: impl Into<DiagMessage>,
986 suggestions: impl IntoIterator<Item = String>,
987 applicability: Applicability,
988 ) -> &mut Self {
989self.span_suggestions_with_style(
990 sp,
991 msg,
992 suggestions,
993 applicability,
994 SuggestionStyle::ShowAlways,
995 )
996 } }997998pub fn span_suggestions_with_style(
999&mut self,
1000 sp: Span,
1001 msg: impl Into<DiagMessage>,
1002 suggestions: impl IntoIterator<Item = String>,
1003 applicability: Applicability,
1004 style: SuggestionStyle,
1005 ) -> &mut Self {
1006let substitutions = suggestions1007 .into_iter()
1008 .map(|snippet| {
1009if true {
if !!(sp.is_empty() && snippet.is_empty()) {
{
::core::panicking::panic_fmt(format_args!("Span `{0:?}` must not be empty and have no suggestion",
sp));
}
};
};debug_assert!(
1010 !(sp.is_empty() && snippet.is_empty()),
1011"Span `{sp:?}` must not be empty and have no suggestion"
1012);
1013Substitution { parts: ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[SubstitutionPart { snippet, span: sp }]))vec![SubstitutionPart { snippet, span: sp }] }
1014 })
1015 .collect();
1016self.push_suggestion(CodeSuggestion {
1017substitutions,
1018 msg: msg.into(),
1019style,
1020applicability,
1021 });
1022self1023 }
10241025/// Prints out a message with multiple suggested edits of the code, where each edit consists of
1026 /// multiple parts.
1027 /// See also [`Diag::multipart_suggestion()`].
1028pub fn multipart_suggestions(
1029&mut self,
1030 msg: impl Into<DiagMessage>,
1031 suggestions: impl IntoIterator<Item = Vec<(Span, String)>>,
1032 applicability: Applicability,
1033 ) -> &mut Self {
1034let substitutions = suggestions1035 .into_iter()
1036 .map(|sugg| {
1037let mut parts = sugg1038 .into_iter()
1039 .map(|(span, snippet)| SubstitutionPart { snippet, span })
1040 .collect::<Vec<_>>();
10411042parts.sort_unstable_by_key(|part| part.span);
10431044if !!parts.is_empty() {
::core::panicking::panic("assertion failed: !parts.is_empty()")
};assert!(!parts.is_empty());
1045if true {
match (&parts.iter().find(|part|
part.span.is_empty() && part.snippet.is_empty()), &None) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val,
::core::option::Option::Some(format_args!("Span must not be empty and have no suggestion")));
}
}
};
};debug_assert_eq!(
1046 parts.iter().find(|part| part.span.is_empty() && part.snippet.is_empty()),
1047None,
1048"Span must not be empty and have no suggestion",
1049 );
1050if true {
match (&parts.array_windows().find(|[a, b]| a.span.overlaps(b.span)),
&None) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val,
::core::option::Option::Some(format_args!("suggestion must not have overlapping parts")));
}
}
};
};debug_assert_eq!(
1051 parts.array_windows().find(|[a, b]| a.span.overlaps(b.span)),
1052None,
1053"suggestion must not have overlapping parts",
1054 );
10551056Substitution { parts }
1057 })
1058 .collect();
10591060self.push_suggestion(CodeSuggestion {
1061substitutions,
1062 msg: msg.into(),
1063 style: SuggestionStyle::ShowAlways,
1064applicability,
1065 });
1066self1067 }
10681069#[doc =
r" Prints out a message with a suggested edit of the code. If the suggestion is presented"]
#[doc = r" inline, it will only show the message and not the suggestion."]
#[doc = r""]
#[doc = r" See [`CodeSuggestion`] for more information."]
#[doc = "See [`Diag::span_suggestion_short()`]."]
pub fn span_suggestion_short(&mut self, sp: Span, msg: impl Into<DiagMessage>,
suggestion: impl ToString, applicability: Applicability) -> &mut Self {
self.span_suggestion_with_style(sp, msg, suggestion, applicability,
SuggestionStyle::HideCodeInline);
self
}
#[doc =
r" Prints out a message with a suggested edit of the code. If the suggestion is presented"]
#[doc = r" inline, it will only show the message and not the suggestion."]
#[doc = r""]
#[doc = r" See [`CodeSuggestion`] for more information."]
#[doc = "See [`Diag::span_suggestion_short()`]."]
pub fn with_span_suggestion_short(mut self, sp: Span,
msg: impl Into<DiagMessage>, suggestion: impl ToString,
applicability: Applicability) -> Self {
self.span_suggestion_short(sp, msg, suggestion, applicability);
self
}with_fn! { with_span_suggestion_short,
1070/// Prints out a message with a suggested edit of the code. If the suggestion is presented
1071 /// inline, it will only show the message and not the suggestion.
1072 ///
1073 /// See [`CodeSuggestion`] for more information.
1074pub fn span_suggestion_short(
1075&mut self,
1076 sp: Span,
1077 msg: impl Into<DiagMessage>,
1078 suggestion: impl ToString,
1079 applicability: Applicability,
1080 ) -> &mut Self {
1081self.span_suggestion_with_style(
1082 sp,
1083 msg,
1084 suggestion,
1085 applicability,
1086 SuggestionStyle::HideCodeInline,
1087 );
1088self
1089} }10901091/// Prints out a message for a suggestion without showing the suggested code.
1092 ///
1093 /// This is intended to be used for suggestions that are obvious in what the changes need to
1094 /// be from the message, showing the span label inline would be visually unpleasant
1095 /// (marginally overlapping spans or multiline spans) and showing the snippet window wouldn't
1096 /// improve understandability.
1097pub fn span_suggestion_hidden(
1098&mut self,
1099 sp: Span,
1100 msg: impl Into<DiagMessage>,
1101 suggestion: impl ToString,
1102 applicability: Applicability,
1103 ) -> &mut Self {
1104self.span_suggestion_with_style(
1105sp,
1106msg,
1107suggestion,
1108applicability,
1109 SuggestionStyle::HideCodeAlways,
1110 );
1111self1112 }
11131114#[doc =
r" Adds a suggestion to the JSON output that will not be shown in the CLI."]
#[doc = r""]
#[doc =
r" This is intended to be used for suggestions that are *very* obvious in what the changes"]
#[doc =
r" need to be from the message, but we still want other tools to be able to apply them."]
#[doc = "See [`Diag::tool_only_span_suggestion()`]."]
pub fn tool_only_span_suggestion(&mut self, sp: Span,
msg: impl Into<DiagMessage>, suggestion: impl ToString,
applicability: Applicability) -> &mut Self {
self.span_suggestion_with_style(sp, msg, suggestion, applicability,
SuggestionStyle::CompletelyHidden);
self
}
#[doc =
r" Adds a suggestion to the JSON output that will not be shown in the CLI."]
#[doc = r""]
#[doc =
r" This is intended to be used for suggestions that are *very* obvious in what the changes"]
#[doc =
r" need to be from the message, but we still want other tools to be able to apply them."]
#[doc = "See [`Diag::tool_only_span_suggestion()`]."]
pub fn with_tool_only_span_suggestion(mut self, sp: Span,
msg: impl Into<DiagMessage>, suggestion: impl ToString,
applicability: Applicability) -> Self {
self.tool_only_span_suggestion(sp, msg, suggestion, applicability);
self
}with_fn! { with_tool_only_span_suggestion,
1115/// Adds a suggestion to the JSON output that will not be shown in the CLI.
1116 ///
1117 /// This is intended to be used for suggestions that are *very* obvious in what the changes
1118 /// need to be from the message, but we still want other tools to be able to apply them.
1119pub fn tool_only_span_suggestion(
1120&mut self,
1121 sp: Span,
1122 msg: impl Into<DiagMessage>,
1123 suggestion: impl ToString,
1124 applicability: Applicability,
1125 ) -> &mut Self {
1126self.span_suggestion_with_style(
1127 sp,
1128 msg,
1129 suggestion,
1130 applicability,
1131 SuggestionStyle::CompletelyHidden,
1132 );
1133self
1134} }11351136/// Add a subdiagnostic from a type that implements `Subdiagnostic` (see
1137 /// [rustc_macros::Subdiagnostic]). Performs eager formatting of any messages
1138 /// used in the subdiagnostic, so suitable for use with repeated messages (i.e. re-use of
1139 /// interpolated variables).
1140pub fn subdiagnostic(&mut self, subdiagnostic: impl Subdiagnostic) -> &mut Self {
1141subdiagnostic.add_to_diag(self);
1142self1143 }
11441145#[doc = r" Add a span."]
#[doc = "See [`Diag::span()`]."]
pub fn span(&mut self, sp: impl Into<MultiSpan>) -> &mut Self {
self.span = sp.into();
if let Some(span) = self.span.primary_span() { self.sort_span = span; }
self
}
#[doc = r" Add a span."]
#[doc = "See [`Diag::span()`]."]
pub fn with_span(mut self, sp: impl Into<MultiSpan>) -> Self {
self.span(sp);
self
}with_fn! { with_span,
1146/// Add a span.
1147pub fn span(&mut self, sp: impl Into<MultiSpan>) -> &mut Self {
1148self.span = sp.into();
1149if let Some(span) = self.span.primary_span() {
1150self.sort_span = span;
1151 }
1152self
1153} }11541155pub fn is_lint(&mut self, name: String, has_future_breakage: bool) -> &mut Self {
1156self.is_lint = Some(IsLint { name, has_future_breakage });
1157self1158 }
11591160#[doc = r" Add an error code."]
#[doc = "See [`Diag::code()`]."]
pub fn code(&mut self, code: ErrCode) -> &mut Self {
self.code = Some(code);
self
}
#[doc = r" Add an error code."]
#[doc = "See [`Diag::code()`]."]
pub fn with_code(mut self, code: ErrCode) -> Self { self.code(code); self }with_fn! { with_code,
1161/// Add an error code.
1162pub fn code(&mut self, code: ErrCode) -> &mut Self {
1163self.code = Some(code);
1164self
1165} }11661167#[doc = r" Add an argument."]
#[doc = "See [`Diag::lint_id()`]."]
pub fn lint_id(&mut self, id: LintExpectationId) -> &mut Self {
self.lint_id = Some(id);
self
}
#[doc = r" Add an argument."]
#[doc = "See [`Diag::lint_id()`]."]
pub fn with_lint_id(mut self, id: LintExpectationId) -> Self {
self.lint_id(id);
self
}with_fn! { with_lint_id,
1168/// Add an argument.
1169pub fn lint_id(
1170&mut self,
1171 id: LintExpectationId,
1172 ) -> &mut Self {
1173self.lint_id = Some(id);
1174self
1175} }11761177#[doc = r" Add a primary message."]
#[doc = "See [`Diag::primary_message()`]."]
pub fn primary_message(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
self.messages[0] = (msg.into(), Style::NoStyle);
self
}
#[doc = r" Add a primary message."]
#[doc = "See [`Diag::primary_message()`]."]
pub fn with_primary_message(mut self, msg: impl Into<DiagMessage>) -> Self {
self.primary_message(msg);
self
}with_fn! { with_primary_message,
1178/// Add a primary message.
1179pub fn primary_message(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
1180self.messages[0] = (msg.into(), Style::NoStyle);
1181self
1182} }11831184#[doc = r" Add an argument."]
#[doc = "See [`Diag::arg()`]."]
pub fn arg(&mut self, name: impl Into<DiagArgName>, arg: impl IntoDiagArg)
-> &mut Self {
self.deref_mut().arg(name, arg);
self
}
#[doc = r" Add an argument."]
#[doc = "See [`Diag::arg()`]."]
pub fn with_arg(mut self, name: impl Into<DiagArgName>, arg: impl IntoDiagArg)
-> Self {
self.arg(name, arg);
self
}with_fn! { with_arg,
1185/// Add an argument.
1186pub fn arg(
1187&mut self,
1188 name: impl Into<DiagArgName>,
1189 arg: impl IntoDiagArg,
1190 ) -> &mut Self {
1191self.deref_mut().arg(name, arg);
1192self
1193} }11941195/// Convenience function for internal use, clients should use one of the
1196 /// public methods above.
1197 ///
1198 /// Used by `proc_macro_server` for implementing `server::Diagnostic`.
1199pub fn sub(&mut self, level: Level, message: impl Into<DiagMessage>, span: MultiSpan) {
1200self.deref_mut().sub(level, message, span);
1201 }
12021203/// Convenience function for internal use, clients should use one of the
1204 /// public methods above.
1205fn sub_with_highlights(&mut self, level: Level, messages: Vec<StringPart>, span: MultiSpan) {
1206let messages = messages.into_iter().map(|m| (m.content.into(), m.style)).collect();
1207let sub = Subdiag { level, messages, span };
1208self.children.push(sub);
1209 }
12101211/// Takes the diagnostic. For use by methods that consume the Diag: `emit`,
1212 /// `cancel`, etc. Afterwards, `drop` is the only code that will be run on
1213 /// `self`.
1214fn take_diag(&mut self) -> DiagInner {
1215if let Some(path) = &self.long_ty_path {
1216self.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the full name for the type has been written to \'{0}\'",
path.display()))
})format!(
1217"the full name for the type has been written to '{}'",
1218 path.display()
1219 ));
1220self.note("consider using `--verbose` to print the full type name to the console");
1221 }
1222*self.diag.take().unwrap()
1223 }
12241225/// This method allows us to access the path of the file where "long types" are written to.
1226 ///
1227 /// When calling `Diag::emit`, as part of that we will check if a `long_ty_path` has been set,
1228 /// and if it has been then we add a note mentioning the file where the "long types" were
1229 /// written to.
1230 ///
1231 /// When calling `tcx.short_string()` after a `Diag` is constructed, the preferred way of doing
1232 /// so is `tcx.short_string(ty, diag.long_ty_path())`. The diagnostic itself is the one that
1233 /// keeps the existence of a "long type" anywhere in the diagnostic, so the note telling the
1234 /// user where we wrote the file to is only printed once at most, *and* it makes it much harder
1235 /// to forget to set it.
1236 ///
1237 /// If the diagnostic hasn't been created before a "short ty string" is created, then you should
1238 /// ensure that this method is called to set it `*diag.long_ty_path() = path`.
1239 ///
1240 /// As a rule of thumb, if you see or add at least one `tcx.short_string()` call anywhere, in a
1241 /// scope, `diag.long_ty_path()` should be called once somewhere close by.
1242pub fn long_ty_path(&mut self) -> &mut Option<PathBuf> {
1243&mut self.long_ty_path
1244 }
12451246pub fn with_long_ty_path(mut self, long_ty_path: Option<PathBuf>) -> Self {
1247self.long_ty_path = long_ty_path;
1248self1249 }
12501251/// Most `emit_producing_guarantee` functions use this as a starting point.
1252fn emit_producing_nothing(mut self) {
1253let diag = self.take_diag();
1254self.dcx.emit_diagnostic(diag);
1255 }
12561257/// `ErrorGuaranteed::emit_producing_guarantee` uses this.
1258fn emit_producing_error_guaranteed(mut self) -> ErrorGuaranteed {
1259let diag = self.take_diag();
12601261// The only error levels that produce `ErrorGuaranteed` are
1262 // `Error` and `DelayedBug`. But `DelayedBug` should never occur here
1263 // because delayed bugs have their level changed to `Bug` when they are
1264 // actually printed, so they produce an ICE.
1265 //
1266 // (Also, even though `level` isn't `pub`, the whole `DiagInner` could
1267 // be overwritten with a new one thanks to `DerefMut`. So this assert
1268 // protects against that, too.)
1269if !#[allow(non_exhaustive_omitted_patterns)] match diag.level {
Level::Error | Level::DelayedBug => true,
_ => false,
} {
{
::core::panicking::panic_fmt(format_args!("invalid diagnostic level ({0:?})",
diag.level));
}
};assert!(
1270matches!(diag.level, Level::Error | Level::DelayedBug),
1271"invalid diagnostic level ({:?})",
1272 diag.level,
1273 );
12741275let guar = self.dcx.emit_diagnostic(diag);
1276guar.unwrap()
1277 }
12781279/// Emit and consume the diagnostic.
1280#[track_caller]
1281pub fn emit(self) -> G::EmitResult {
1282 G::emit_producing_guarantee(self)
1283 }
12841285/// Emit the diagnostic unless `delay` is true,
1286 /// in which case the emission will be delayed as a bug.
1287 ///
1288 /// See `emit` and `delay_as_bug` for details.
1289#[track_caller]
1290pub fn emit_unless_delay(mut self, delay: bool) -> G::EmitResult {
1291if delay {
1292self.downgrade_to_delayed_bug();
1293 }
1294self.emit()
1295 }
12961297/// Cancel and consume the diagnostic. (A diagnostic must either be emitted or
1298 /// cancelled or it will panic when dropped).
1299pub fn cancel(mut self) {
1300self.diag = None;
1301drop(self);
1302 }
13031304/// Cancels this diagnostic and returns its first message, if it exists.
1305pub fn cancel_into_message(self) -> Option<String> {
1306let s = self.diag.as_ref()?.messages.get(0)?.0.as_str().map(ToString::to_string);
1307self.cancel();
1308s1309 }
13101311/// See `DiagCtxt::stash_diagnostic` for details.
1312pub fn stash(mut self, span: Span, key: StashKey) -> Option<ErrorGuaranteed> {
1313let diag = self.take_diag();
1314self.dcx.stash_diagnostic(span, key, diag)
1315 }
13161317/// Delay emission of this diagnostic as a bug.
1318 ///
1319 /// This can be useful in contexts where an error indicates a bug but
1320 /// typically this only happens when other compilation errors have already
1321 /// happened. In those cases this can be used to defer emission of this
1322 /// diagnostic as a bug in the compiler only if no other errors have been
1323 /// emitted.
1324 ///
1325 /// In the meantime, though, callsites are required to deal with the "bug"
1326 /// locally in whichever way makes the most sense.
1327#[track_caller]
1328pub fn delay_as_bug(mut self) -> G::EmitResult {
1329self.downgrade_to_delayed_bug();
1330self.emit()
1331 }
1332}
13331334/// Destructor bomb: every `Diag` must be consumed (emitted, cancelled, etc.)
1335/// or we emit a bug.
1336impl<G: EmissionGuarantee> Dropfor Diag<'_, G> {
1337fn drop(&mut self) {
1338match self.diag.take() {
1339Some(diag) if !panicking() => {
1340self.dcx.emit_diagnostic(DiagInner::new(
1341 Level::Bug,
1342DiagMessage::from("the following error was constructed but not emitted"),
1343 ));
1344self.dcx.emit_diagnostic(*diag);
1345{
::core::panicking::panic_fmt(format_args!("error was constructed but not emitted"));
};panic!("error was constructed but not emitted");
1346 }
1347_ => {}
1348 }
1349 }
1350}
13511352#[macro_export]
1353macro_rules!struct_span_code_err {
1354 ($dcx:expr, $span:expr, $code:expr, $($message:tt)*) => ({
1355$dcx.struct_span_err($span, format!($($message)*)).with_code($code)
1356 })
1357}