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;
9
10use 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;
16
17use crate::{
18 CodeSuggestion, DiagCtxtHandle, DiagMessage, ErrCode, ErrorGuaranteed, ExplicitBug, Level,
19 MultiSpan, StashKey, Style, Substitution, SubstitutionPart, SuggestionStyle, Suggestions,
20};
21
22pub trait EmissionGuarantee: Sized {
25 type EmitResult = Self;
28
29 #[track_caller]
33 fn emit_producing_guarantee(diag: Diag<'_, Self>) -> Self::EmitResult;
34}
35
36impl EmissionGuarantee for ErrorGuaranteed {
37 fn emit_producing_guarantee(diag: Diag<'_, Self>) -> Self::EmitResult {
38 diag.emit_producing_error_guaranteed()
39 }
40}
41
42impl EmissionGuarantee for () {
43 fn emit_producing_guarantee(diag: Diag<'_, Self>) -> Self::EmitResult {
44 diag.emit_producing_nothing();
45 }
46}
47
48#[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;
52
53impl EmissionGuarantee for BugAbort {
54 type EmitResult = !;
55
56 fn emit_producing_guarantee(diag: Diag<'_, Self>) -> Self::EmitResult {
57 diag.emit_producing_nothing();
58 panic::panic_any(ExplicitBug);
59 }
60}
61
62#[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;
66
67impl EmissionGuarantee for FatalAbort {
68 type EmitResult = !;
69
70 fn emit_producing_guarantee(diag: Diag<'_, Self>) -> Self::EmitResult {
71 diag.emit_producing_nothing();
72 crate::FatalError.raise()
73 }
74}
75
76impl EmissionGuarantee for rustc_span::fatal_error::FatalError {
77 fn emit_producing_guarantee(diag: Diag<'_, Self>) -> Self::EmitResult {
78 diag.emit_producing_nothing();
79 rustc_span::fatal_error::FatalError
80 }
81}
82
83#[rustc_diagnostic_item = "Diagnostic"]
105pub trait Diagnostic<'a, G: EmissionGuarantee = ErrorGuaranteed> {
106 #[must_use]
108 #[track_caller]
109 fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G>;
110}
111
112impl<'a, T, G> Diagnostic<'a, G> for Spanned<T>
113where
114 T: Diagnostic<'a, G>,
115 G: EmissionGuarantee,
116{
117 fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G> {
118 self.node.into_diag(dcx, level).with_span(self.span)
119 }
120}
121
122impl<'a> Diagnostic<'a, ()>
123 for Box<
124 dyn for<'b> FnOnce(DiagCtxtHandle<'b>, Level) -> Diag<'b, ()> + DynSync + DynSend + 'static,
125 >
126{
127 fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
128 self(dcx, level)
129 }
130}
131
132pub struct DiagDecorator<F: FnOnce(&mut Diag<'_, ()>)>(pub F);
134
135impl<'a, F: FnOnce(&mut Diag<'_, ()>)> Diagnostic<'a, ()> for DiagDecorator<F> {
136 fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
137 let mut diag = Diag::new(dcx, level, "");
138 (self.0)(&mut diag);
139 diag
140 }
141}
142
143#[rustc_diagnostic_item = "Subdiagnostic"]
146pub trait Subdiagnostic {
147 fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>);
149}
150
151#[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}
157
158impl DiagLocation {
159 #[track_caller]
160 pub fn caller() -> Self {
161 let loc = panic::Location::caller();
162 DiagLocation { file: loc.file().into(), line: loc.line(), col: loc.column() }
163 }
164}
165
166impl fmt::Display for DiagLocation {
167 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
168 f.write_fmt(format_args!("{0}:{1}:{2}", self.file, self.line, self.col))write!(f, "{}:{}:{}", self.file, self.line, self.col)
169 }
170}
171
172#[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 pub(crate) name: String,
176 has_future_breakage: bool,
178}
179
180#[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>);
182
183impl DiagStyledString {
184 pub fn new() -> DiagStyledString {
185 DiagStyledString(::alloc::vec::Vec::new()vec![])
186 }
187 pub fn push_normal<S: Into<String>>(&mut self, t: S) {
188 self.0.push(StringPart::normal(t));
189 }
190 pub fn push_highlighted<S: Into<String>>(&mut self, t: S) {
191 self.0.push(StringPart::highlighted(t));
192 }
193 pub fn push<S: Into<String>>(&mut self, t: S, highlight: bool) {
194 if highlight {
195 self.push_highlighted(t);
196 } else {
197 self.push_normal(t);
198 }
199 }
200 pub fn normal<S: Into<String>>(t: S) -> DiagStyledString {
201 DiagStyledString(::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 }
203
204 pub fn highlighted<S: Into<String>>(t: S) -> DiagStyledString {
205 DiagStyledString(::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 }
207
208 pub fn content(&self) -> String {
209 self.0.iter().map(|x| x.content.as_str()).collect::<String>()
210 }
211}
212
213#[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}
218
219impl StringPart {
220 pub fn normal<S: Into<String>>(content: S) -> StringPart {
221 StringPart { content: content.into(), style: Style::NoStyle }
222 }
223
224 pub fn highlighted<S: Into<String>>(content: S) -> StringPart {
225 StringPart { content: content.into(), style: Style::Highlight }
226 }
227}
228
229#[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 pub(crate) level: Level,
239
240 pub messages: Vec<(DiagMessage, Style)>,
241 pub code: Option<ErrCode>,
242 pub lint_id: Option<LintExpectationId>,
243 pub span: MultiSpan,
244 pub children: Vec<Subdiag>,
245 pub suggestions: Suggestions,
246 pub args: DiagArgMap,
247
248 pub sort_span: Span,
252
253 pub is_lint: Option<IsLint>,
254
255 pub long_ty_path: Option<PathBuf>,
256 pub emitted_at: DiagLocation,
259}
260
261impl DiagInner {
262 #[track_caller]
263 pub fn new<M: Into<DiagMessage>>(level: Level, message: M) -> Self {
264 DiagInner::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 }
266
267 #[track_caller]
268 pub fn new_with_messages(level: Level, messages: Vec<(DiagMessage, Style)>) -> Self {
269 DiagInner {
270 level,
271 lint_id: None,
272 messages,
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 }
284
285 #[inline(always)]
286 pub fn level(&self) -> Level {
287 self.level
288 }
289
290 pub fn is_error(&self) -> bool {
291 match self.level {
292 Level::Bug | Level::Fatal | Level::Error | Level::DelayedBug => true,
293
294 Level::ForceWarning
295 | Level::Warning
296 | Level::Note
297 | Level::OnceNote
298 | Level::Help
299 | Level::OnceHelp
300 | Level::FailureNote
301 | Level::Allow
302 | Level::Expect => false,
303 }
304 }
305
306 pub(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 }
310
311 pub(crate) fn is_force_warn(&self) -> bool {
312 match self.level {
313 Level::ForceWarning => {
314 if !self.is_lint.is_some() {
::core::panicking::panic("assertion failed: self.is_lint.is_some()")
};assert!(self.is_lint.is_some());
315 true
316 }
317 _ => false,
318 }
319 }
320
321 pub(crate) fn sub(&mut self, level: Level, message: impl Into<DiagMessage>, span: MultiSpan) {
322 let 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 };
323 self.children.push(sub);
324 }
325
326 pub(crate) fn arg(&mut self, name: impl Into<DiagArgName>, arg: impl IntoDiagArg) {
327 let name = name.into();
328 let value = arg.into_diag_arg(&mut self.long_ty_path);
329 if 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 );
335 self.args.insert(name, value);
336 }
337
338 pub fn remove_arg(&mut self, name: &str) {
339 self.args.swap_remove(name);
340 }
341
342 pub fn emitted_at_sub_diag(&self) -> Subdiag {
343 let 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);
344 Subdiag {
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 }
350
351 fn keys(
353 &self,
354 ) -> (
355 &Level,
356 &[(DiagMessage, Style)],
357 &Option<ErrCode>,
358 &MultiSpan,
359 &[Subdiag],
360 &Suggestions,
361 Vec<(&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,
371 self.args.iter().collect(),
372 &self.is_lint,
374 )
376 }
377}
378
379impl Hash for DiagInner {
380 fn hash<H>(&self, state: &mut H)
381 where
382 H: Hasher,
383 {
384 self.keys().hash(state);
385 }
386}
387
388impl PartialEq for DiagInner {
389 fn eq(&self, other: &Self) -> bool {
390 self.keys() == other.keys()
391 }
392}
393
394#[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 {
398 pub level: Level,
399 pub messages: Vec<(DiagMessage, Style)>,
400 pub span: MultiSpan,
401}
402
403#[must_use]
416pub struct Diag<'a, G: EmissionGuarantee = ErrorGuaranteed> {
417 pub dcx: DiagCtxtHandle<'a>,
418
419 diag: Option<Box<DiagInner>>,
429
430 _marker: PhantomData<G>,
431}
432
433impl<G> !Clone for Diag<'_, G> {}
436
437const _: [(); 3 * size_of::<usize>()] =
[(); ::std::mem::size_of::<Diag<'_, ()>>()];rustc_data_structures::static_assert_size!(Diag<'_, ()>, 3 * size_of::<usize>());
438
439impl<G: EmissionGuarantee> Deref for Diag<'_, G> {
440 type Target = DiagInner;
441
442 fn deref(&self) -> &DiagInner {
443 self.diag.as_ref().unwrap()
444 }
445}
446
447impl<G: EmissionGuarantee> DerefMut for Diag<'_, G> {
448 fn deref_mut(&mut self) -> &mut DiagInner {
449 self.diag.as_mut().unwrap()
450 }
451}
452
453impl<G: EmissionGuarantee> Debug for Diag<'_, G> {
454 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
455 self.diag.fmt(f)
456 }
457}
458
459macro_rules! with_fn {
478 {
479 $with_f:ident,
480 $(#[$attrs:meta])*
481 pub fn $f:ident(&mut $self:ident, $($name:ident: $ty:ty),* $(,)?) -> &mut Self {
482 $($body:tt)*
483 }
484 } => {
485 $(#[$attrs])*
487 #[doc = concat!("See [`Diag::", stringify!($f), "()`].")]
488 pub fn $f(&mut $self, $($name: $ty),*) -> &mut Self {
489 $($body)*
490 }
491
492 $(#[$attrs])*
494 #[doc = concat!("See [`Diag::", stringify!($f), "()`].")]
495 pub fn $with_f(mut $self, $($name: $ty),*) -> Self {
496 $self.$f($($name),*);
497 $self
498 }
499 };
500}
501
502impl<'a, G: EmissionGuarantee> Diag<'a, G> {
503 #[track_caller]
504 pub fn new(dcx: DiagCtxtHandle<'a>, level: Level, message: impl Into<DiagMessage>) -> Self {
505 Self::new_diagnostic(dcx, DiagInner::new(level, message))
506 }
507
508 pub fn with_dcx(mut self, dcx: DiagCtxtHandle<'_>) -> Diag<'_, G> {
510 Diag { dcx, diag: self.diag.take(), _marker: PhantomData }
511 }
512
513 #[track_caller]
515 pub(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");
517 Self { dcx, diag: Some(Box::new(diag)), _marker: PhantomData }
518 }
519
520 #[track_caller]
531 pub fn downgrade_to_delayed_bug(&mut self) {
532 if !#[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!(
533 matches!(self.level, Level::Error | Level::DelayedBug),
534 "downgrade_to_delayed_bug: cannot downgrade {:?} to DelayedBug: not an error",
535 self.level
536 );
537 self.level = Level::DelayedBug;
538 }
539
540 #[track_caller]
548 pub fn upgrade_to_fatal(mut self) -> Diag<'a, FatalAbort> {
549 if !#[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!(
550 matches!(self.level, Level::Error),
551 "upgrade_to_fatal: cannot upgrade {:?} to Fatal: not an error",
552 self.level
553 );
554 self.level = Level::Fatal;
555
556 let diag = self.diag.take();
559 Diag { dcx: self.dcx, diag, _marker: PhantomData }
560 }
561
562 "See [`Diag::span_label()`]."
&mut Self
self
span
label
&mut Self
"See [`Diag::span_label()`]."
mut self
span
label
Self
self.span_label(span, label);
self;with_fn! { with_span_label,
563 pub fn span_label(&mut self, span: Span, label: impl Into<DiagMessage>) -> &mut Self {
576 self.span.push_span_label(span, label.into());
577 self
578 } }
579
580 "See [`Diag::span_labels()`]."
&mut Self
self
spans
label
&mut Self
"See [`Diag::span_labels()`]."
mut self
spans
label
Self
self.span_labels(spans, label);
self;with_fn! { with_span_labels,
581 pub fn span_labels(&mut self, spans: impl IntoIterator<Item = Span>, label: &str) -> &mut Self {
584 for span in spans {
585 self.span_label(span, label.to_string());
586 }
587 self
588 } }
589
590 pub fn replace_span_with(&mut self, after: Span, keep_label: bool) -> &mut Self {
591 let before = self.span.clone();
592 self.span(after);
593 for span_label in before.span_labels() {
594 if let Some(label) = span_label.label {
595 if span_label.is_primary && keep_label {
596 self.span.push_span_label(after, label);
597 } else {
598 self.span.push_span_label(span_label.span, label);
599 }
600 }
601 }
602 self
603 }
604
605 pub fn note_expected_found(
606 &mut self,
607 expected_label: &str,
608 expected: DiagStyledString,
609 found_label: &str,
610 found: DiagStyledString,
611 ) -> &mut Self {
612 self.note_expected_found_extra(
613 expected_label,
614 expected,
615 found_label,
616 found,
617 DiagStyledString::normal(""),
618 DiagStyledString::normal(""),
619 )
620 }
621
622 pub 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 {
631 let expected_label = expected_label.to_string();
632 let 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 };
637 let found_label = found_label.to_string();
638 let 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 };
643 let (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 };
648 let 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 ))];
653 msg.extend(expected.0);
654 msg.push(StringPart::normal(::alloc::__export::must_use({ ::alloc::fmt::format(format_args!("`")) })format!("`")));
655 msg.extend(expected_extra.0);
656 msg.push(StringPart::normal(::alloc::__export::must_use({ ::alloc::fmt::format(format_args!("\n")) })format!("\n")));
657 msg.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)));
658 msg.extend(found.0);
659 msg.push(StringPart::normal(::alloc::__export::must_use({ ::alloc::fmt::format(format_args!("`")) })format!("`")));
660 msg.extend(found_extra.0);
661
662 self.highlighted_note(msg);
664 self
665 }
666
667 pub fn note_trait_signature(&mut self, name: Symbol, signature: String) -> &mut Self {
668 self.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 ]);
673 self
674 }
675
676 "See [`Diag::note()`]."
&mut Self
self
msg
&mut Self
"See [`Diag::note()`]."
mut self
msg
Self
self.note(msg);
self;with_fn! { with_note,
677 pub fn note(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
679 self.sub(Level::Note, msg, MultiSpan::new());
680 self
681 } }
682
683 pub fn highlighted_note(&mut self, msg: Vec<StringPart>) -> &mut Self {
684 self.sub_with_highlights(Level::Note, msg, MultiSpan::new());
685 self
686 }
687
688 pub fn highlighted_span_note(
689 &mut self,
690 span: impl Into<MultiSpan>,
691 msg: Vec<StringPart>,
692 ) -> &mut Self {
693 self.sub_with_highlights(Level::Note, msg, span.into());
694 self
695 }
696
697 pub fn note_once(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
699 self.sub(Level::OnceNote, msg, MultiSpan::new());
700 self
701 }
702
703 "See [`Diag::span_note()`]."
&mut Self
self
sp
msg
&mut Self
"See [`Diag::span_note()`]."
mut self
sp
msg
Self
self.span_note(sp, msg);
self;with_fn! { with_span_note,
704 pub fn span_note(
707 &mut self,
708 sp: impl Into<MultiSpan>,
709 msg: impl Into<DiagMessage>,
710 ) -> &mut Self {
711 self.sub(Level::Note, msg, sp.into());
712 self
713 } }
714
715 pub fn span_note_once<S: Into<MultiSpan>>(
718 &mut self,
719 sp: S,
720 msg: impl Into<DiagMessage>,
721 ) -> &mut Self {
722 self.sub(Level::OnceNote, msg, sp.into());
723 self
724 }
725
726 "See [`Diag::warn()`]."
&mut Self
self
msg
&mut Self
"See [`Diag::warn()`]."
mut self
msg
Self
self.warn(msg);
self;with_fn! { with_warn,
727 pub fn warn(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
729 self.sub(Level::Warning, msg, MultiSpan::new());
730 self
731 } }
732
733 pub fn span_warn<S: Into<MultiSpan>>(
736 &mut self,
737 sp: S,
738 msg: impl Into<DiagMessage>,
739 ) -> &mut Self {
740 self.sub(Level::Warning, msg, sp.into());
741 self
742 }
743
744 "See [`Diag::help()`]."
&mut Self
self
msg
&mut Self
"See [`Diag::help()`]."
mut self
msg
Self
self.help(msg);
self;with_fn! { with_help,
745 pub fn help(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
747 self.sub(Level::Help, msg, MultiSpan::new());
748 self
749 } }
750
751 pub fn help_once(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
753 self.sub(Level::OnceHelp, msg, MultiSpan::new());
754 self
755 }
756
757 pub fn highlighted_help(&mut self, msg: Vec<StringPart>) -> &mut Self {
759 self.sub_with_highlights(Level::Help, msg, MultiSpan::new());
760 self
761 }
762
763 pub fn highlighted_span_help(
765 &mut self,
766 span: impl Into<MultiSpan>,
767 msg: Vec<StringPart>,
768 ) -> &mut Self {
769 self.sub_with_highlights(Level::Help, msg, span.into());
770 self
771 }
772
773 "See [`Diag::span_help()`]."
&mut Self
self
sp
msg
&mut Self
"See [`Diag::span_help()`]."
mut self
sp
msg
Self
self.span_help(sp, msg);
self;with_fn! { with_span_help,
774 pub fn span_help(
777 &mut self,
778 sp: impl Into<MultiSpan>,
779 msg: impl Into<DiagMessage>,
780 ) -> &mut Self {
781 self.sub(Level::Help, msg, sp.into());
782 self
783 } }
784
785 pub fn disable_suggestions(&mut self) -> &mut Self {
789 self.suggestions = Suggestions::Disabled;
790 self
791 }
792
793 pub fn seal_suggestions(&mut self) -> &mut Self {
798 if let Suggestions::Enabled(suggestions) = &mut self.suggestions {
799 let suggestions_slice = std::mem::take(suggestions).into_boxed_slice();
800 self.suggestions = Suggestions::Sealed(suggestions_slice);
801 }
802 self
803 }
804
805 fn push_suggestion(&mut self, suggestion: CodeSuggestion) {
810 for subst in &suggestion.substitutions {
811 for part in &subst.parts {
812 let span = part.span;
813 let call_site = span.ctxt().outer_expn_data().call_site;
814 if span.in_derive_expansion() && span.overlaps_or_adjacent(call_site) {
815 return;
817 }
818 }
819 }
820
821 if let Suggestions::Enabled(suggestions) = &mut self.suggestions {
822 suggestions.push(suggestion);
823 }
824 }
825
826 "See [`Diag::multipart_suggestion()`]."
&mut Self
self
msg
suggestion
applicability
&mut Self
"See [`Diag::multipart_suggestion()`]."
mut self
msg
suggestion
applicability
Self
self.multipart_suggestion(msg, suggestion, applicability);
self;with_fn! { with_multipart_suggestion,
827 pub fn multipart_suggestion(
830 &mut self,
831 msg: impl Into<DiagMessage>,
832 suggestion: Vec<(Span, String)>,
833 applicability: Applicability,
834 ) -> &mut Self {
835 self.multipart_suggestion_with_style(
836 msg,
837 suggestion,
838 applicability,
839 SuggestionStyle::ShowAlways,
840 )
841 } }
842
843 pub fn multipart_suggestion_with_style(
845 &mut self,
846 msg: impl Into<DiagMessage>,
847 mut suggestion: Vec<(Span, String)>,
848 applicability: Applicability,
849 style: SuggestionStyle,
850 ) -> &mut Self {
851 let mut seen = crate::FxHashSet::default();
852 suggestion.retain(|(span, msg)| seen.insert((span.lo(), span.hi(), msg.clone())));
853
854 let parts = suggestion
855 .into_iter()
856 .map(|(span, snippet)| SubstitutionPart { snippet, span })
857 .collect::<Vec<_>>();
858
859 if !!parts.is_empty() {
::core::panicking::panic("assertion failed: !parts.is_empty()")
};assert!(!parts.is_empty());
860 if 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()),
862 None,
863 "Span must not be empty and have no suggestion",
864 );
865 if 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)),
867 None,
868 "suggestion must not have overlapping parts",
869 );
870
871 self.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(),
874 style,
875 applicability,
876 });
877 self
878 }
879
880 pub fn tool_only_multipart_suggestion(
887 &mut self,
888 msg: impl Into<DiagMessage>,
889 suggestion: Vec<(Span, String)>,
890 applicability: Applicability,
891 ) -> &mut Self {
892 self.multipart_suggestion_with_style(
893 msg,
894 suggestion,
895 applicability,
896 SuggestionStyle::CompletelyHidden,
897 )
898 }
899
900 "See [`Diag::span_suggestion()`]."
&mut Self
self
sp
msg
suggestion
applicability
&mut Self
"See [`Diag::span_suggestion()`]."
mut self
sp
msg
suggestion
applicability
Self
self.span_suggestion(sp, msg, suggestion, applicability);
self;with_fn! { with_span_suggestion,
901 pub fn span_suggestion(
919 &mut self,
920 sp: Span,
921 msg: impl Into<DiagMessage>,
922 suggestion: impl ToString,
923 applicability: Applicability,
924 ) -> &mut Self {
925 self.span_suggestion_with_style(
926 sp,
927 msg,
928 suggestion,
929 applicability,
930 SuggestionStyle::ShowCode,
931 );
932 self
933 } }
934
935 "See [`Diag::span_suggestion_with_style()`]."
&mut Self
self
sp
msg
suggestion
applicability
style
&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"));
}
};
};
::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,
}])),
}]))
"See [`Diag::span_suggestion_with_style()`]."
mut self
sp
msg
suggestion
applicability
style
Self
self.span_suggestion_with_style(sp, msg, suggestion, applicability, style);
self;with_fn! { with_span_suggestion_with_style,
936 pub 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 {
945 debug_assert!(
946 !(sp.is_empty() && suggestion.to_string().is_empty()),
947 "Span must not be empty and have no suggestion"
948 );
949 self.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 });
957 self
958 } }
959
960 "See [`Diag::span_suggestion_verbose()`]."
&mut Self
self
sp
msg
suggestion
applicability
&mut Self
"See [`Diag::span_suggestion_verbose()`]."
mut self
sp
msg
suggestion
applicability
Self
self.span_suggestion_verbose(sp, msg, suggestion, applicability);
self;with_fn! { with_span_suggestion_verbose,
961 pub 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 {
969 self.span_suggestion_with_style(
970 sp,
971 msg,
972 suggestion,
973 applicability,
974 SuggestionStyle::ShowAlways,
975 );
976 self
977 } }
978
979 "See [`Diag::span_suggestions()`]."
&mut Self
self
sp
msg
suggestions
applicability
&mut Self
"See [`Diag::span_suggestions()`]."
mut self
sp
msg
suggestions
applicability
Self
self.span_suggestions(sp, msg, suggestions, applicability);
self;with_fn! { with_span_suggestions,
980 pub 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 {
989 self.span_suggestions_with_style(
990 sp,
991 msg,
992 suggestions,
993 applicability,
994 SuggestionStyle::ShowAlways,
995 )
996 } }
997
998 pub 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 {
1006 let substitutions = suggestions
1007 .into_iter()
1008 .map(|snippet| {
1009 if 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 );
1013 Substitution { 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();
1016 self.push_suggestion(CodeSuggestion {
1017 substitutions,
1018 msg: msg.into(),
1019 style,
1020 applicability,
1021 });
1022 self
1023 }
1024
1025 pub 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 {
1034 let substitutions = suggestions
1035 .into_iter()
1036 .map(|sugg| {
1037 let mut parts = sugg
1038 .into_iter()
1039 .map(|(span, snippet)| SubstitutionPart { snippet, span })
1040 .collect::<Vec<_>>();
1041
1042 parts.sort_unstable_by_key(|part| part.span);
1043
1044 if !!parts.is_empty() {
::core::panicking::panic("assertion failed: !parts.is_empty()")
};assert!(!parts.is_empty());
1045 if 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()),
1047 None,
1048 "Span must not be empty and have no suggestion",
1049 );
1050 if 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)),
1052 None,
1053 "suggestion must not have overlapping parts",
1054 );
1055
1056 Substitution { parts }
1057 })
1058 .collect();
1059
1060 self.push_suggestion(CodeSuggestion {
1061 substitutions,
1062 msg: msg.into(),
1063 style: SuggestionStyle::ShowAlways,
1064 applicability,
1065 });
1066 self
1067 }
1068
1069 "See [`Diag::span_suggestion_short()`]."
&mut Self
self
sp
msg
suggestion
applicability
&mut Self
"See [`Diag::span_suggestion_short()`]."
mut self
sp
msg
suggestion
applicability
Self
self.span_suggestion_short(sp, msg, suggestion, applicability);
self;with_fn! { with_span_suggestion_short,
1070 pub 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 {
1081 self.span_suggestion_with_style(
1082 sp,
1083 msg,
1084 suggestion,
1085 applicability,
1086 SuggestionStyle::HideCodeInline,
1087 );
1088 self
1089 } }
1090
1091 pub 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 {
1104 self.span_suggestion_with_style(
1105 sp,
1106 msg,
1107 suggestion,
1108 applicability,
1109 SuggestionStyle::HideCodeAlways,
1110 );
1111 self
1112 }
1113
1114 "See [`Diag::tool_only_span_suggestion()`]."
&mut Self
self
sp
msg
suggestion
applicability
&mut Self
"See [`Diag::tool_only_span_suggestion()`]."
mut self
sp
msg
suggestion
applicability
Self
self.tool_only_span_suggestion(sp, msg, suggestion, applicability);
self;with_fn! { with_tool_only_span_suggestion,
1115 pub 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 {
1126 self.span_suggestion_with_style(
1127 sp,
1128 msg,
1129 suggestion,
1130 applicability,
1131 SuggestionStyle::CompletelyHidden,
1132 );
1133 self
1134 } }
1135
1136 pub fn subdiagnostic(&mut self, subdiagnostic: impl Subdiagnostic) -> &mut Self {
1141 subdiagnostic.add_to_diag(self);
1142 self
1143 }
1144
1145 "See [`Diag::span()`]."
&mut Self
self
sp
&mut Self
"See [`Diag::span()`]."
mut self
sp
Self
self.span(sp);
self;with_fn! { with_span,
1146 pub fn span(&mut self, sp: impl Into<MultiSpan>) -> &mut Self {
1148 self.span = sp.into();
1149 if let Some(span) = self.span.primary_span() {
1150 self.sort_span = span;
1151 }
1152 self
1153 } }
1154
1155 pub fn is_lint(&mut self, name: String, has_future_breakage: bool) -> &mut Self {
1156 self.is_lint = Some(IsLint { name, has_future_breakage });
1157 self
1158 }
1159
1160 "See [`Diag::code()`]."
&mut Self
self
code
&mut Self
"See [`Diag::code()`]."
mut self
code
Self
self.code(code);
self;with_fn! { with_code,
1161 pub fn code(&mut self, code: ErrCode) -> &mut Self {
1163 self.code = Some(code);
1164 self
1165 } }
1166
1167 "See [`Diag::lint_id()`]."
&mut Self
self
id
&mut Self
"See [`Diag::lint_id()`]."
mut self
id
Self
self.lint_id(id);
self;with_fn! { with_lint_id,
1168 pub fn lint_id(
1170 &mut self,
1171 id: LintExpectationId,
1172 ) -> &mut Self {
1173 self.lint_id = Some(id);
1174 self
1175 } }
1176
1177 "See [`Diag::primary_message()`]."
&mut Self
self
msg
&mut Self
"See [`Diag::primary_message()`]."
mut self
msg
Self
self.primary_message(msg);
self;with_fn! { with_primary_message,
1178 pub fn primary_message(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
1180 self.messages[0] = (msg.into(), Style::NoStyle);
1181 self
1182 } }
1183
1184 "See [`Diag::arg()`]."
&mut Self
self
name
arg
&mut Self
"See [`Diag::arg()`]."
mut self
name
arg
Self
self.arg(name, arg);
self;with_fn! { with_arg,
1185 pub fn arg(
1187 &mut self,
1188 name: impl Into<DiagArgName>,
1189 arg: impl IntoDiagArg,
1190 ) -> &mut Self {
1191 self.deref_mut().arg(name, arg);
1192 self
1193 } }
1194
1195 pub fn sub(&mut self, level: Level, message: impl Into<DiagMessage>, span: MultiSpan) {
1200 self.deref_mut().sub(level, message, span);
1201 }
1202
1203 fn sub_with_highlights(&mut self, level: Level, messages: Vec<StringPart>, span: MultiSpan) {
1206 let messages = messages.into_iter().map(|m| (m.content.into(), m.style)).collect();
1207 let sub = Subdiag { level, messages, span };
1208 self.children.push(sub);
1209 }
1210
1211 fn take_diag(&mut self) -> DiagInner {
1215 if let Some(path) = &self.long_ty_path {
1216 self.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 ));
1220 self.note("consider using `--verbose` to print the full type name to the console");
1221 }
1222 *self.diag.take().unwrap()
1223 }
1224
1225 pub fn long_ty_path(&mut self) -> &mut Option<PathBuf> {
1243 &mut self.long_ty_path
1244 }
1245
1246 pub fn with_long_ty_path(mut self, long_ty_path: Option<PathBuf>) -> Self {
1247 self.long_ty_path = long_ty_path;
1248 self
1249 }
1250
1251 fn emit_producing_nothing(mut self) {
1253 let diag = self.take_diag();
1254 self.dcx.emit_diagnostic(diag);
1255 }
1256
1257 fn emit_producing_error_guaranteed(mut self) -> ErrorGuaranteed {
1259 let diag = self.take_diag();
1260
1261 if !#[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!(
1270 matches!(diag.level, Level::Error | Level::DelayedBug),
1271 "invalid diagnostic level ({:?})",
1272 diag.level,
1273 );
1274
1275 let guar = self.dcx.emit_diagnostic(diag);
1276 guar.unwrap()
1277 }
1278
1279 #[track_caller]
1281 pub fn emit(self) -> G::EmitResult {
1282 G::emit_producing_guarantee(self)
1283 }
1284
1285 #[track_caller]
1290 pub fn emit_unless_delay(mut self, delay: bool) -> G::EmitResult {
1291 if delay {
1292 self.downgrade_to_delayed_bug();
1293 }
1294 self.emit()
1295 }
1296
1297 pub fn cancel(mut self) {
1300 self.diag = None;
1301 drop(self);
1302 }
1303
1304 pub fn cancel_into_message(self) -> Option<String> {
1306 let s = self.diag.as_ref()?.messages.get(0)?.0.as_str().map(ToString::to_string);
1307 self.cancel();
1308 s
1309 }
1310
1311 pub fn stash(mut self, span: Span, key: StashKey) -> Option<ErrorGuaranteed> {
1313 let diag = self.take_diag();
1314 self.dcx.stash_diagnostic(span, key, diag)
1315 }
1316
1317 #[track_caller]
1328 pub fn delay_as_bug(mut self) -> G::EmitResult {
1329 self.downgrade_to_delayed_bug();
1330 self.emit()
1331 }
1332}
1333
1334impl<G: EmissionGuarantee> Drop for Diag<'_, G> {
1337 fn drop(&mut self) {
1338 match self.diag.take() {
1339 Some(diag) if !panicking() => {
1340 self.dcx.emit_diagnostic(DiagInner::new(
1341 Level::Bug,
1342 DiagMessage::from("the following error was constructed but not emitted"),
1343 ));
1344 self.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}
1351
1352#[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}