1use std::borrow::Cow;
2use std::fmt::{self, Debug};
3use std::hash::Hash;
4use std::ops::{Deref, DerefMut};
5use std::panic;
6use std::path::PathBuf;
7use std::thread::panicking;
89use rustc_ast::attr::version::RustcVersion;
10use rustc_data_structures::stable_hash::StableHasher;
11use rustc_error_messages::{DiagArgMap, DiagArgName, IntoDiagArg};
12use rustc_hashes::Hash128;
13use rustc_lint_defs::Applicability;
14use rustc_macros::{Decodable, Encodable};
15use rustc_serialize::Encodable;
16use rustc_span::{Span, Spanned, Symbol};
17use tracing::debug;
1819use crate::dedup_hash::DedupHashEncoder;
20use crate::{
21CodeSuggestion, DiagCtxtHandle, DiagMessage, ErrCode, ErrorGuaranteed, ExplicitBug, Level,
22MultiSpan, StashKey, Style, Sublevel, Substitution, SubstitutionPart, SuggestionStyle,
23Suggestions,
24};
2526/// Trait implemented by error types. This is rarely implemented manually. Instead, use
27/// `#[derive(Diagnostic)]` -- see [rustc_macros::Diagnostic].
28pub trait Diagnostic<'a> {
29/// Write out as a diagnostic out of `DiagCtxt`.
30#[must_use]
31 #[track_caller]
32fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a>;
33}
3435impl<'a, T> Diagnostic<'a> for Spanned<T>
36where
37T: Diagnostic<'a>,
38{
39fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a> {
40self.node.into_diag(dcx, level).with_span(self.span)
41 }
42}
4344/// Type used to emit diagnostic through a closure instead of implementing the `Diagnostic` trait.
45pub struct DiagDecorator<F: FnOnce(&mut Diag<'_>)>(pub F);
4647impl<'a, F: FnOnce(&mut Diag<'_>)> Diagnostic<'a> for DiagDecorator<F> {
48fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a> {
49let mut diag = Diag::new(dcx, level, "");
50 (self.0)(&mut diag);
51diag52 }
53}
5455/// Trait implemented by error types. This should not be implemented manually. Instead, use
56/// `#[derive(Subdiagnostic)]` -- see [rustc_macros::Subdiagnostic].
57pub trait Subdiagnostic {
58/// Add a subdiagnostic to an existing diagnostic.
59fn add_to_diag(self, diag: &mut Diag<'_>);
60}
6162#[derive(#[automatically_derived]
impl ::core::clone::Clone for DiagLocation {
#[inline]
fn clone(&self) -> Self {
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) {
let DiagLocation {
file: ref __binding_0,
line: ref __binding_1,
col: ref __binding_2 } = *self;
::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)]
63pub struct DiagLocation {
64 file: Cow<'static, str>,
65 line: u32,
66 col: u32,
67}
6869impl DiagLocation {
70pub fn from_location(loc: &'static panic::Location<'static>) -> Self {
71DiagLocation { file: loc.file().into(), line: loc.line(), col: loc.column() }
72 }
7374#[track_caller]
75pub fn caller() -> Self {
76Self::from_location(panic::Location::caller())
77 }
78}
7980impl fmt::Displayfor DiagLocation {
81fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82f.write_fmt(format_args!("{0}:{1}:{2}", self.file, self.line, self.col))write!(f, "{}:{}:{}", self.file, self.line, self.col)83 }
84}
8586#[derive(#[automatically_derived]
impl ::core::clone::Clone for IsLint {
#[inline]
fn clone(&self) -> Self {
IsLint {
name: ::core::clone::Clone::clone(&self.name),
has_future_breakage: ::core::clone::Clone::clone(&self.has_future_breakage),
rust_version: ::core::clone::Clone::clone(&self.rust_version),
}
}
}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_field3_finish(f, "IsLint",
"name", &self.name, "has_future_breakage",
&self.has_future_breakage, "rust_version", &&self.rust_version)
}
}Debug, #[automatically_derived]
impl ::core::marker::StructuralPartialEq for IsLint { }
#[automatically_derived]
impl ::core::cmp::PartialEq for IsLint {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.has_future_breakage == other.has_future_breakage &&
self.name == other.name &&
self.rust_version == other.rust_version
}
}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>;
let _: ::core::cmp::AssertParamIsEq<Option<RustcVersion>>;
}
}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);
::core::hash::Hash::hash(&self.rust_version, state)
}
}Hash, const _: () =
{
impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
for IsLint {
fn encode(&self, __encoder: &mut __E) {
let IsLint {
name: ref __binding_0,
has_future_breakage: ref __binding_1,
rust_version: ref __binding_2 } = *self;
::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 IsLint {
fn decode(__decoder: &mut __D) -> Self {
IsLint {
name: ::rustc_serialize::Decodable::decode(__decoder),
has_future_breakage: ::rustc_serialize::Decodable::decode(__decoder),
rust_version: ::rustc_serialize::Decodable::decode(__decoder),
}
}
}
};Decodable)]
87pub struct IsLint {
88/// The lint name.
89pub(crate) name: String,
90/// Indicates whether this lint should show up in cargo's future breakage report.
91has_future_breakage: bool,
92/// Indicates the minimum rust version this lint applies to
93rust_version: Option<RustcVersion>,
94}
9596#[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::marker::StructuralPartialEq for DiagStyledString { }
#[automatically_derived]
impl ::core::cmp::PartialEq for DiagStyledString {
#[inline]
fn eq(&self, other: &Self) -> 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)]
97pub struct DiagStyledString(pub Vec<StringPart>);
9899impl DiagStyledString {
100pub fn new() -> DiagStyledString {
101DiagStyledString(::alloc::vec::Vec::new()vec![])
102 }
103pub fn push_normal<S: Into<String>>(&mut self, t: S) {
104self.0.push(StringPart::normal(t));
105 }
106pub fn push_highlighted<S: Into<String>>(&mut self, t: S) {
107self.0.push(StringPart::highlighted(t));
108 }
109pub fn push<S: Into<String>>(&mut self, t: S, highlight: bool) {
110if highlight {
111self.push_highlighted(t);
112 } else {
113self.push_normal(t);
114 }
115 }
116pub fn normal<S: Into<String>>(t: S) -> DiagStyledString {
117DiagStyledString(::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)])
118 }
119120pub fn highlighted<S: Into<String>>(t: S) -> DiagStyledString {
121DiagStyledString(::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)])
122 }
123124pub fn content(&self) -> String {
125self.0.iter().map(|x| x.content.as_str()).collect::<String>()
126 }
127128/// Merge segments of the same style.
129pub fn compact(&mut self) {
130let segments = std::mem::take(&mut self.0);
131let mut iter = segments.into_iter();
132let Some(mut prev) = iter.next() else { return };
133while let Some(segment) = iter.next() {
134if prev.style == segment.style {
135 prev.content.push_str(&segment.content);
136 } else {
137self.0.push(prev);
138 prev = segment;
139 }
140 }
141self.0.push(prev);
142 }
143144/// Remove the middle of all long segments for shorter rendering.
145pub fn shorten(&mut self) {
146self.compact();
147/// The marker for removed text.
148const ELLIPSIS: &str = "...";
149/// How many chars at the start and end will remain.
150const PADDING: usize = 6;
151/// The distance after which it is not worth it to reduce the text.
152const DELTA: usize = 3;
153154for segment in self.0.iter_mut() {
155let char_len = segment.content.chars().count();
156if char_len > PADDING * 2 + ELLIPSIS.chars().count() + DELTA
157 && let Some((left, _)) = segment.content.char_indices().nth(PADDING)
158 && let Some((right, _)) = segment.content.char_indices().nth(char_len - PADDING)
159 {
160 segment.content.replace_range(left..right, ELLIPSIS);
161 }
162 }
163 }
164}
165166#[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::marker::StructuralPartialEq for StringPart { }
#[automatically_derived]
impl ::core::cmp::PartialEq for StringPart {
#[inline]
fn eq(&self, other: &Self) -> 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)]
167pub struct StringPart {
168 content: String,
169 style: Style,
170}
171172impl StringPart {
173pub fn normal<S: Into<String>>(content: S) -> StringPart {
174StringPart { content: content.into(), style: Style::NoStyle }
175 }
176177pub fn highlighted<S: Into<String>>(content: S) -> StringPart {
178StringPart { content: content.into(), style: Style::Highlight }
179 }
180}
181182/// The main part of a diagnostic. Note that `Diag`, which wraps this type, is
183/// used for most operations, and should be used instead whenever possible.
184/// This type should only be used when `Diag`'s lifetime causes difficulties,
185/// e.g. when storing diagnostics within `DiagCtxt`.
186#[must_use]
187#[derive(#[automatically_derived]
impl ::core::clone::Clone for DiagInner {
#[inline]
fn clone(&self) -> Self {
DiagInner {
level: ::core::clone::Clone::clone(&self.level),
messages: ::core::clone::Clone::clone(&self.messages),
code: ::core::clone::Clone::clone(&self.code),
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),
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", "span", "children", "suggestions",
"args", "is_lint", "long_ty_path", "emitted_at"];
let values: &[&dyn ::core::fmt::Debug] =
&[&self.level, &self.messages, &self.code, &self.span,
&self.children, &self.suggestions, &self.args,
&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) {
let DiagInner {
level: ref __binding_0,
messages: ref __binding_1,
code: ref __binding_2,
span: ref __binding_3,
children: ref __binding_4,
suggestions: ref __binding_5,
args: ref __binding_6,
is_lint: ref __binding_7,
long_ty_path: ref __binding_8,
emitted_at: ref __binding_9 } = *self;
::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);
}
}
};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),
span: ::rustc_serialize::Decodable::decode(__decoder),
children: ::rustc_serialize::Decodable::decode(__decoder),
suggestions: ::rustc_serialize::Decodable::decode(__decoder),
args: ::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)]
188pub struct DiagInner {
189// NOTE(eddyb) this is private to disallow arbitrary after-the-fact changes,
190 // outside of what methods in this crate themselves allow.
191pub(crate) level: Level,
192193pub messages: Vec<(DiagMessage, Style)>,
194pub code: Option<ErrCode>,
195pub span: MultiSpan,
196pub children: Vec<Subdiag>,
197pub suggestions: Suggestions,
198pub args: DiagArgMap,
199pub is_lint: Option<IsLint>,
200pub long_ty_path: Option<PathBuf>,
201/// With `-Ztrack_diagnostics` enabled,
202 /// we print where in rustc this error was emitted.
203pub emitted_at: DiagLocation,
204}
205206impl DiagInner {
207#[track_caller]
208pub fn new<M: Into<DiagMessage>>(level: Level, message: M) -> Self {
209DiagInner::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)])
210 }
211212#[track_caller]
213pub fn new_with_messages(level: Level, messages: Vec<(DiagMessage, Style)>) -> Self {
214DiagInner {
215level,
216messages,
217 code: None,
218 span: MultiSpan::new(),
219 children: ::alloc::vec::Vec::new()vec![],
220 suggestions: Suggestions::Enabled(::alloc::vec::Vec::new()vec![]),
221 args: Default::default(),
222 is_lint: None,
223 long_ty_path: None,
224 emitted_at: DiagLocation::caller(),
225 }
226 }
227228#[inline(always)]
229pub fn level(&self) -> Level {
230self.level
231 }
232233pub fn is_error(&self) -> bool {
234match self.level {
235 Level::Bug | Level::Fatal | Level::Error | Level::DelayedBug => true,
236237 Level::Warning(_) | Level::Note | Level::Help | Level::FailureNote => false,
238 }
239 }
240241/// Indicates whether this diagnostic should show up in cargo's future breakage report.
242pub(crate) fn has_future_breakage(&self) -> bool {
243#[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, .. }))244 }
245246/// Indicates the minimum rust version this lint applies to.
247pub(crate) fn rust_version(&self) -> Option<RustcVersion> {
248self.is_lint.as_ref().and_then(|is| is.rust_version)
249 }
250251pub(crate) fn sub(
252&mut self,
253 level: Sublevel,
254 message: impl Into<DiagMessage>,
255 span: MultiSpan,
256 ) {
257let 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 };
258self.children.push(sub);
259 }
260261pub(crate) fn arg(&mut self, name: impl Into<DiagArgName>, arg: impl IntoDiagArg) {
262let name = name.into();
263let value = arg.into_diag_arg(&mut self.long_ty_path);
264// This assertion is to avoid subdiagnostics overwriting an existing diagnostic arg.
265if 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!(
266 !self.args.contains_key(&name) || self.args.get(&name) == Some(&value),
267"arg {} already exists",
268 name
269 );
270self.args.insert(name, value);
271 }
272273pub fn remove_arg(&mut self, name: &str) {
274self.args.swap_remove(name);
275 }
276277pub fn emitted_at_sub_diag(&self) -> Subdiag {
278let 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);
279Subdiag {
280 level: crate::Sublevel::Note,
281 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)],
282 span: MultiSpan::new(),
283 }
284 }
285286/// Hash used to determine if two diagnostics are the same. Used by
287 /// `DiagCtxtInner::emitted_diagnostics`. Some fields are ignored for the hash.
288pub(crate) fn dedup_hash(&self) -> Hash128 {
289// Deconstruct to ensure all fields are considered.
290let DiagInner {
291 level,
292 messages,
293 code,
294 span,
295 children,
296 suggestions,
297 args,
298 is_lint,
299 long_ty_path: _, // ignore
300emitted_at: _, // ignore
301} = self;
302303let mut e = DedupHashEncoder(StableHasher::new());
304305// We use `discriminant` because we want to ignore the field within `Warning`.
306 // `Encoder`/`SpanEncoder` can't traverse a `Discriminant<T>` so we hash it directly.
307std::mem::discriminant(level).hash(&mut e.0);
308309// All the other fields are hashed via `encode`.
310messages.encode(&mut e);
311code.encode(&mut e);
312span.encode(&mut e);
313children.encode(&mut e);
314suggestions.encode(&mut e);
315args.encode(&mut e);
316is_lint.encode(&mut e);
317318e.0.finish()
319 }
320}
321322/// A "sub"-diagnostic attached to a parent diagnostic.
323/// For example, a note attached to an error.
324#[derive(#[automatically_derived]
impl ::core::clone::Clone for Subdiag {
#[inline]
fn clone(&self) -> Self {
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::marker::StructuralPartialEq for Subdiag { }
#[automatically_derived]
impl ::core::cmp::PartialEq for Subdiag {
#[inline]
fn eq(&self, other: &Self) -> 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) {
let Subdiag {
level: ref __binding_0,
messages: ref __binding_1,
span: ref __binding_2 } = *self;
::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)]
325pub struct Subdiag {
326pub level: Sublevel,
327pub messages: Vec<(DiagMessage, Style)>,
328pub span: MultiSpan,
329}
330331impl Subdiag {
332/// Hash used to determine if two subdiagnostics are the same. Used by
333 /// `DiagCtxtInner::emitted_diagnostics`.
334pub(crate) fn dedup_hash(&self) -> Hash128 {
335let mut e = DedupHashEncoder(StableHasher::new());
336self.encode(&mut e);
337e.0.finish()
338 }
339}
340341/// Used for emitting structured error messages and other diagnostic information.
342/// Wraps a `DiagInner`, adding some useful things.
343/// - The `dcx` field, allowing it to (a) emit itself, and (b) do a drop check
344/// that it has been emitted or cancelled.
345///
346/// Each constructed `Diag` must be consumed by a function such as
347/// `emit_bug`/`emit_fatal`/`emit_err`/`emit`, `cancel`, or `delay_as_bug`. A
348/// panic occurs if a `Diag` is dropped without being consumed by one of these
349/// functions.
350///
351/// If there is some state in a downstream crate you would like to access in
352/// the methods of `Diag` here, consider extending `DiagCtxtFlags`.
353#[must_use]
354pub struct Diag<'a> {
355pub dcx: DiagCtxtHandle<'a>,
356357/// Why the `Option`? It is always `Some` until the `Diag` is consumed via
358 /// `emit`, `cancel`, etc. At that point it is consumed and replaced with
359 /// `None`. Then `drop` checks that it is `None`; if not, it panics because
360 /// a diagnostic was built but not used.
361 ///
362 /// Why the Box? `DiagInner` is a large type, and `Diag` is often used as a
363 /// return value, especially within the frequently-used `PResult` type. In
364 /// theory, return value optimization (RVO) should avoid unnecessary
365 /// copying. In practice, it does not (at the time of writing).
366diag: Option<Box<DiagInner>>,
367}
368369// Cloning a `Diag` is a recipe for a diagnostic being emitted twice, which
370// would be bad.
371impl !Clonefor Diag<'_> {}
372373const _: [(); 3 * size_of::<usize>()] =
[(); ::std::mem::size_of::<Diag<'_>>()];rustc_data_structures::static_assert_size!(Diag<'_>, 3 * size_of::<usize>());
374375impl Dereffor Diag<'_> {
376type Target = DiagInner;
377378fn deref(&self) -> &DiagInner {
379self.diag.as_ref().unwrap()
380 }
381}
382383impl DerefMutfor Diag<'_> {
384fn deref_mut(&mut self) -> &mut DiagInner {
385self.diag.as_mut().unwrap()
386 }
387}
388389impl Debugfor Diag<'_> {
390fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
391self.diag.fmt(f)
392 }
393}
394395/// `Diag` impls many `&mut self -> &mut Self` methods. Each one modifies an
396/// existing diagnostic, either in a standalone fashion, e.g.
397/// `err.code(code);`, or in a chained fashion to make multiple modifications,
398/// e.g. `err.code(code).span(span);`.
399///
400/// This macro creates an equivalent `self -> Self` method, with a `with_`
401/// prefix. This can be used in a chained fashion when making a new diagnostic,
402/// e.g. `let err = struct_err(msg).with_code(code);`, or emitting a new
403/// diagnostic, e.g. `struct_err(msg).with_code(code).emit();`.
404///
405/// Although the latter method can be used to modify an existing diagnostic,
406/// e.g. `err = err.with_code(code);`, this should be avoided because the former
407/// method gives shorter code, e.g. `err.code(code);`.
408///
409/// Note: the `with_` methods are added only when needed. If you want to use
410/// one and it's not defined, feel free to add it.
411///
412/// Note: any doc comments must be within the `with_fn!` call.
413macro_rules!with_fn {
414 {
415$with_f:ident,
416 $(#[$attrs:meta])*
417pub fn $f:ident(&mut $self:ident, $($name:ident: $ty:ty),* $(,)?) -> &mut Self {
418 $($body:tt)*
419 }
420 } => {
421// The original function.
422$(#[$attrs])*
423#[doc = concat!("See [`Diag::", stringify!($f), "()`].")]
424pub fn $f(&mut $self, $($name: $ty),*) -> &mut Self {
425 $($body)*
426 }
427428// The `with_*` variant.
429$(#[$attrs])*
430#[doc = concat!("See [`Diag::", stringify!($f), "()`].")]
431pub fn $with_f(mut $self, $($name: $ty),*) -> Self {
432$self.$f($($name),*);
433$self
434}
435 };
436}
437438impl<'a> Diag<'a> {
439#[track_caller]
440pub fn new(dcx: DiagCtxtHandle<'a>, level: Level, message: impl Into<DiagMessage>) -> Self {
441Self::new_diagnostic(dcx, DiagInner::new(level, message))
442 }
443444/// Allow moving diagnostics between different error tainting contexts
445pub fn with_dcx(mut self, dcx: DiagCtxtHandle<'_>) -> Diag<'_> {
446Diag { dcx, diag: self.diag.take() }
447 }
448449/// Creates a new `Diag` with an already constructed diagnostic.
450#[track_caller]
451pub(crate) fn new_diagnostic(dcx: DiagCtxtHandle<'a>, diag: DiagInner) -> Self {
452{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/4ebd258257737d5b7635c116fa7d281627a7e937/compiler/rustc_errors/src/diagnostic.rs:452",
"rustc_errors::diagnostic", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/4ebd258257737d5b7635c116fa7d281627a7e937/compiler/rustc_errors/src/diagnostic.rs"),
::tracing_core::__macro_support::Option::Some(452u32),
::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};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("Created new diagnostic")
as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("Created new diagnostic");
453Self { dcx, diag: Some(Box::new(diag)) }
454 }
455456/// Delay emission of this diagnostic as a bug.
457 ///
458 /// This can be useful in contexts where an error indicates a bug but
459 /// typically this only happens when other compilation errors have already
460 /// happened. In those cases this can be used to defer emission of this
461 /// diagnostic as a bug in the compiler only if no other errors have been
462 /// emitted.
463 ///
464 /// In the meantime, though, callsites are required to deal with the "bug"
465 /// locally in whichever way makes the most sense.
466#[track_caller]
467pub fn downgrade_to_delayed_bug(&mut self) {
468if !#[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!(
469matches!(self.level, Level::Error | Level::DelayedBug),
470"downgrade_to_delayed_bug: cannot downgrade {:?} to DelayedBug: not an error",
471self.level
472 );
473self.level = Level::DelayedBug;
474 }
475476/// Make emitting this diagnostic fatal.
477#[track_caller]
478pub fn upgrade_to_fatal(mut self) -> Diag<'a> {
479if !#[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!(
480matches!(self.level, Level::Error),
481"upgrade_to_fatal: cannot upgrade {:?} to Fatal: not an error",
482self.level
483 );
484self.level = Level::Fatal;
485486// Take is okay since we immediately rewrap it in another diagnostic.
487 // i.e. we do emit it despite defusing the original diagnostic's drop bomb.
488let diag = self.diag.take();
489Diag { dcx: self.dcx, diag }
490 }
491492#[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,
493/// Appends a labeled span to the diagnostic.
494 ///
495 /// Labels are used to convey additional context for the diagnostic's primary span. They will
496 /// be shown together with the original diagnostic's span, *not* with spans added by
497 /// `span_note`, `span_help`, etc. Therefore, if the primary span is not displayable (because
498 /// the span is `DUMMY_SP` or the source code isn't found), labels will not be displayed
499 /// either.
500 ///
501 /// Implementation-wise, the label span is pushed onto the [`MultiSpan`] that was created when
502 /// the diagnostic was constructed. However, the label span is *not* considered a
503 /// ["primary span"][`MultiSpan`]; only the `Span` supplied when creating the diagnostic is
504 /// primary.
505pub fn span_label(&mut self, span: Span, label: impl Into<DiagMessage>) -> &mut Self {
506self.span.push_span_label(span, label.into());
507self
508} }509510#[doc = "See [`Diag::span_context()`]."]
pub fn span_context(&mut self, span: Span) -> &mut Self {
self.span.push_span_context(span);
self
}
#[doc = "See [`Diag::span_context()`]."]
pub fn with_span_context(mut self, span: Span) -> Self {
self.span_context(span);
self
}with_fn! { with_span_context,
511pub fn span_context(&mut self, span: Span) -> &mut Self {
512self.span.push_span_context(span);
513self
514} }515516#[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,
517/// Labels all the given spans with the provided label.
518 /// See [`Self::span_label()`] for more information.
519pub fn span_labels(&mut self, spans: impl IntoIterator<Item = Span>, label: &str) -> &mut Self {
520for span in spans {
521self.span_label(span, label.to_string());
522 }
523self
524} }525526pub fn replace_span_with(&mut self, after: Span, keep_label: bool) -> &mut Self {
527let before = self.span.clone();
528self.span(after);
529for span_label in before.span_labels() {
530if let Some(label) = span_label.label {
531if span_label.is_primary && keep_label {
532self.span.push_span_label(after, label);
533 } else {
534self.span.push_span_label(span_label.span, label);
535 }
536 }
537 }
538self539 }
540541pub fn note_expected_found(
542&mut self,
543 expected_label: &str,
544 expected: DiagStyledString,
545 found_label: &str,
546 found: DiagStyledString,
547 ) -> &mut Self {
548self.note_expected_found_extra(
549expected_label,
550expected,
551found_label,
552found,
553DiagStyledString::normal(""),
554DiagStyledString::normal(""),
555 )
556 }
557558pub fn note_expected_found_extra(
559&mut self,
560 expected_label: &str,
561 expected: DiagStyledString,
562 found_label: &str,
563 found: DiagStyledString,
564 expected_extra: DiagStyledString,
565 found_extra: DiagStyledString,
566 ) -> &mut Self {
567let expected_label = expected_label.to_string();
568let expected_label = if expected_label.is_empty() {
569"expected".to_string()
570 } else {
571::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected {0}", expected_label))
})format!("expected {expected_label}")572 };
573let found_label = found_label.to_string();
574let found_label = if found_label.is_empty() {
575"found".to_string()
576 } else {
577::alloc::__export::must_use({
::alloc::fmt::format(format_args!("found {0}", found_label))
})format!("found {found_label}")578 };
579let (found_padding, expected_padding) = if expected_label.len() > found_label.len() {
580 (expected_label.len() - found_label.len(), 0)
581 } else {
582 (0, found_label.len() - expected_label.len())
583 };
584let 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!(
585"{}{} `",
586" ".repeat(expected_padding),
587 expected_label
588 ))];
589msg.extend(expected.0);
590msg.push(StringPart::normal(::alloc::__export::must_use({ ::alloc::fmt::format(format_args!("`")) })format!("`")));
591msg.extend(expected_extra.0);
592msg.push(StringPart::normal(::alloc::__export::must_use({ ::alloc::fmt::format(format_args!("\n")) })format!("\n")));
593msg.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)));
594msg.extend(found.0);
595msg.push(StringPart::normal(::alloc::__export::must_use({ ::alloc::fmt::format(format_args!("`")) })format!("`")));
596msg.extend(found_extra.0);
597598// For now, just attach these as notes.
599self.highlighted_note(msg);
600self601 }
602603pub fn note_trait_signature(&mut self, name: Symbol, signature: String) -> &mut Self {
604self.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![
605 StringPart::normal(format!("`{name}` from trait: `")),
606 StringPart::highlighted(signature),
607 StringPart::normal("`"),
608 ]);
609self610 }
611612#[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(Sublevel::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,
613/// Add a note attached to this diagnostic.
614pub fn note(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
615self.sub(Sublevel::Note, msg, MultiSpan::new());
616self
617} }618619pub fn highlighted_note(&mut self, msg: Vec<StringPart>) -> &mut Self {
620self.sub_with_highlights(Sublevel::Note, msg, MultiSpan::new());
621self622 }
623624pub fn highlighted_span_note(
625&mut self,
626 span: impl Into<MultiSpan>,
627 msg: Vec<StringPart>,
628 ) -> &mut Self {
629self.sub_with_highlights(Sublevel::Note, msg, span.into());
630self631 }
632633/// This is like [`Diag::note()`], but it's only printed once.
634pub fn note_once(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
635self.sub(Sublevel::OnceNote, msg, MultiSpan::new());
636self637 }
638639#[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(Sublevel::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,
640/// Prints the span with a note above it.
641 /// This is like [`Diag::note()`], but it gets its own span.
642pub fn span_note(
643&mut self,
644 sp: impl Into<MultiSpan>,
645 msg: impl Into<DiagMessage>,
646 ) -> &mut Self {
647self.sub(Sublevel::Note, msg, sp.into());
648self
649} }650651/// Prints the span with a note above it.
652 /// This is like [`Diag::note_once()`], but it gets its own span.
653pub fn span_note_once<S: Into<MultiSpan>>(
654&mut self,
655 sp: S,
656 msg: impl Into<DiagMessage>,
657 ) -> &mut Self {
658self.sub(Sublevel::OnceNote, msg, sp.into());
659self660 }
661662#[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(Sublevel::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,
663/// Add a warning attached to this diagnostic.
664pub fn warn(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
665self.sub(Sublevel::Warning, msg, MultiSpan::new());
666self
667} }668669/// Prints the span with a warning above it.
670 /// This is like [`Diag::warn()`], but it gets its own span.
671pub fn span_warn<S: Into<MultiSpan>>(
672&mut self,
673 sp: S,
674 msg: impl Into<DiagMessage>,
675 ) -> &mut Self {
676self.sub(Sublevel::Warning, msg, sp.into());
677self678 }
679680#[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(Sublevel::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,
681/// Add a help message attached to this diagnostic.
682pub fn help(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
683self.sub(Sublevel::Help, msg, MultiSpan::new());
684self
685} }686687/// This is like [`Diag::help()`], but it's only printed once.
688pub fn help_once(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
689self.sub(Sublevel::OnceHelp, msg, MultiSpan::new());
690self691 }
692693/// Add a help message attached to this diagnostic with a customizable highlighted message.
694pub fn highlighted_help(&mut self, msg: Vec<StringPart>) -> &mut Self {
695self.sub_with_highlights(Sublevel::Help, msg, MultiSpan::new());
696self697 }
698699/// Add a help message attached to this diagnostic with a customizable highlighted message.
700pub fn highlighted_span_help(
701&mut self,
702 span: impl Into<MultiSpan>,
703 msg: Vec<StringPart>,
704 ) -> &mut Self {
705self.sub_with_highlights(Sublevel::Help, msg, span.into());
706self707 }
708709#[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(Sublevel::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,
710/// Prints the span with some help above it.
711 /// This is like [`Diag::help()`], but it gets its own span.
712pub fn span_help(
713&mut self,
714 sp: impl Into<MultiSpan>,
715 msg: impl Into<DiagMessage>,
716 ) -> &mut Self {
717self.sub(Sublevel::Help, msg, sp.into());
718self
719} }720721/// Disallow attaching suggestions to this diagnostic.
722 /// Any suggestions attached e.g. with the `span_suggestion_*` methods
723 /// (before and after the call to `disable_suggestions`) will be ignored.
724pub fn disable_suggestions(&mut self) -> &mut Self {
725self.suggestions = Suggestions::Disabled;
726self727 }
728729/// Prevent new suggestions from being added to this diagnostic.
730 ///
731 /// Suggestions added before the call to `.seal_suggestions()` will be preserved
732 /// and new suggestions will be ignored.
733pub fn seal_suggestions(&mut self) -> &mut Self {
734if let Suggestions::Enabled(suggestions) = &mut self.suggestions {
735let suggestions_slice = std::mem::take(suggestions).into_boxed_slice();
736self.suggestions = Suggestions::Sealed(suggestions_slice);
737 }
738self739 }
740741/// Helper for pushing to `self.suggestions`.
742 ///
743 /// A new suggestion is added if suggestions are enabled for this diagnostic.
744 /// Otherwise, they are ignored.
745fn push_suggestion(&mut self, suggestion: CodeSuggestion) {
746for subst in &suggestion.substitutions {
747for part in &subst.parts {
748let span = part.span;
749let call_site = span.ctxt().outer_expn_data().call_site;
750if span.in_derive_expansion() && span.overlaps_or_adjacent(call_site) {
751// Ignore if spans is from derive macro.
752return;
753 }
754 }
755 }
756757if let Suggestions::Enabled(suggestions) = &mut self.suggestions {
758suggestions.push(suggestion);
759 }
760 }
761762#[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,
763/// Show a suggestion that has multiple parts to it, always as its own subdiagnostic.
764 /// In other words, multiple changes need to be applied as part of this suggestion.
765pub fn multipart_suggestion(
766&mut self,
767 msg: impl Into<DiagMessage>,
768 suggestion: Vec<(Span, String)>,
769 applicability: Applicability,
770 ) -> &mut Self {
771self.multipart_suggestion_with_style(
772 msg,
773 suggestion,
774 applicability,
775 SuggestionStyle::ShowAlways,
776 )
777 } }778779/// [`Diag::multipart_suggestion()`] but you can set the [`SuggestionStyle`].
780pub fn multipart_suggestion_with_style(
781&mut self,
782 msg: impl Into<DiagMessage>,
783mut suggestion: Vec<(Span, String)>,
784 applicability: Applicability,
785 style: SuggestionStyle,
786 ) -> &mut Self {
787let mut seen = crate::FxHashSet::default();
788suggestion.retain(|(span, msg)| seen.insert((span.lo(), span.hi(), msg.clone())));
789790let parts = suggestion791 .into_iter()
792 .map(|(span, snippet)| SubstitutionPart { snippet, span })
793 .collect::<Vec<_>>();
794795if !!parts.is_empty() {
::core::panicking::panic("assertion failed: !parts.is_empty()")
};assert!(!parts.is_empty());
796if 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!(
797 parts.iter().find(|part| part.span.is_empty() && part.snippet.is_empty()),
798None,
799"Span must not be empty and have no suggestion",
800 );
801if 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!(
802 parts.array_windows().find(|[a, b]| a.span.overlaps(b.span)),
803None,
804"suggestion must not have overlapping parts",
805 );
806807self.push_suggestion(CodeSuggestion {
808 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 }],
809 msg: msg.into(),
810style,
811applicability,
812 });
813self814 }
815816/// Prints out a message with for a multipart suggestion without showing the suggested code.
817 ///
818 /// This is intended to be used for suggestions that are obvious in what the changes need to
819 /// be from the message, showing the span label inline would be visually unpleasant
820 /// (marginally overlapping spans or multiline spans) and showing the snippet window wouldn't
821 /// improve understandability.
822pub fn tool_only_multipart_suggestion(
823&mut self,
824 msg: impl Into<DiagMessage>,
825 suggestion: Vec<(Span, String)>,
826 applicability: Applicability,
827 ) -> &mut Self {
828self.multipart_suggestion_with_style(
829msg,
830suggestion,
831applicability,
832 SuggestionStyle::CompletelyHidden,
833 )
834 }
835836#[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,
837/// Prints out a message with a suggested edit of the code.
838 ///
839 /// In case of short messages and a simple suggestion, rustc displays it as a label:
840 ///
841 /// ```text
842 /// try adding parentheses: `(tup.0).1`
843 /// ```
844 ///
845 /// The message
846 ///
847 /// * should not end in any punctuation (a `:` is added automatically)
848 /// * should not be a question (avoid language like "did you mean")
849 /// * should not contain any phrases like "the following", "as shown", etc.
850 /// * may look like "to do xyz, use" or "to do xyz, use abc"
851 /// * may contain a name of a function, variable, or type, but not whole expressions
852 ///
853 /// See [`CodeSuggestion`] for more information.
854pub fn span_suggestion(
855&mut self,
856 sp: Span,
857 msg: impl Into<DiagMessage>,
858 suggestion: impl ToString,
859 applicability: Applicability,
860 ) -> &mut Self {
861self.span_suggestion_with_style(
862 sp,
863 msg,
864 suggestion,
865 applicability,
866 SuggestionStyle::ShowCode,
867 );
868self
869} }870871#[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,
872/// [`Diag::span_suggestion()`] but you can set the [`SuggestionStyle`].
873pub fn span_suggestion_with_style(
874&mut self,
875 sp: Span,
876 msg: impl Into<DiagMessage>,
877 suggestion: impl ToString,
878 applicability: Applicability,
879 style: SuggestionStyle,
880 ) -> &mut Self {
881debug_assert!(
882 !(sp.is_empty() && suggestion.to_string().is_empty()),
883"Span must not be empty and have no suggestion"
884);
885self.push_suggestion(CodeSuggestion {
886 substitutions: vec![Substitution {
887 parts: vec![SubstitutionPart { snippet: suggestion.to_string(), span: sp }],
888 }],
889 msg: msg.into(),
890 style,
891 applicability,
892 });
893self
894} }895896#[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,
897/// Always show the suggested change.
898pub fn span_suggestion_verbose(
899&mut self,
900 sp: Span,
901 msg: impl Into<DiagMessage>,
902 suggestion: impl ToString,
903 applicability: Applicability,
904 ) -> &mut Self {
905self.span_suggestion_with_style(
906 sp,
907 msg,
908 suggestion,
909 applicability,
910 SuggestionStyle::ShowAlways,
911 );
912self
913} }914915#[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,
916/// Prints out a message with multiple suggested edits of the code.
917 /// See also [`Diag::span_suggestion()`].
918pub fn span_suggestions(
919&mut self,
920 sp: Span,
921 msg: impl Into<DiagMessage>,
922 suggestions: impl IntoIterator<Item = String>,
923 applicability: Applicability,
924 ) -> &mut Self {
925self.span_suggestions_with_style(
926 sp,
927 msg,
928 suggestions,
929 applicability,
930 SuggestionStyle::ShowAlways,
931 )
932 } }933934pub fn span_suggestions_with_style(
935&mut self,
936 sp: Span,
937 msg: impl Into<DiagMessage>,
938 suggestions: impl IntoIterator<Item = String>,
939 applicability: Applicability,
940 style: SuggestionStyle,
941 ) -> &mut Self {
942let substitutions = suggestions943 .into_iter()
944 .map(|snippet| {
945if 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!(
946 !(sp.is_empty() && snippet.is_empty()),
947"Span `{sp:?}` must not be empty and have no suggestion"
948);
949Substitution { 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 }] }
950 })
951 .collect();
952self.push_suggestion(CodeSuggestion {
953substitutions,
954 msg: msg.into(),
955style,
956applicability,
957 });
958self959 }
960961/// Prints out a message with multiple suggested edits of the code, where each edit consists of
962 /// multiple parts.
963 /// See also [`Diag::multipart_suggestion()`].
964pub fn multipart_suggestions(
965&mut self,
966 msg: impl Into<DiagMessage>,
967 suggestions: impl IntoIterator<Item = Vec<(Span, String)>>,
968 applicability: Applicability,
969 ) -> &mut Self {
970let substitutions = suggestions971 .into_iter()
972 .map(|sugg| {
973let mut parts = sugg974 .into_iter()
975 .map(|(span, snippet)| SubstitutionPart { snippet, span })
976 .collect::<Vec<_>>();
977978parts.sort_unstable_by_key(|part| part.span.lo_hi());
979980if !!parts.is_empty() {
::core::panicking::panic("assertion failed: !parts.is_empty()")
};assert!(!parts.is_empty());
981if 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!(
982 parts.iter().find(|part| part.span.is_empty() && part.snippet.is_empty()),
983None,
984"Span must not be empty and have no suggestion",
985 );
986if 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!(
987 parts.array_windows().find(|[a, b]| a.span.overlaps(b.span)),
988None,
989"suggestion must not have overlapping parts",
990 );
991992Substitution { parts }
993 })
994 .collect();
995996self.push_suggestion(CodeSuggestion {
997substitutions,
998 msg: msg.into(),
999 style: SuggestionStyle::ShowAlways,
1000applicability,
1001 });
1002self1003 }
10041005#[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,
1006/// Prints out a message with a suggested edit of the code. If the suggestion is presented
1007 /// inline, it will only show the message and not the suggestion.
1008 ///
1009 /// See [`CodeSuggestion`] for more information.
1010pub fn span_suggestion_short(
1011&mut self,
1012 sp: Span,
1013 msg: impl Into<DiagMessage>,
1014 suggestion: impl ToString,
1015 applicability: Applicability,
1016 ) -> &mut Self {
1017self.span_suggestion_with_style(
1018 sp,
1019 msg,
1020 suggestion,
1021 applicability,
1022 SuggestionStyle::HideCodeInline,
1023 );
1024self
1025} }10261027/// Prints out a message for a suggestion without showing the suggested code.
1028 ///
1029 /// This is intended to be used for suggestions that are obvious in what the changes need to
1030 /// be from the message, showing the span label inline would be visually unpleasant
1031 /// (marginally overlapping spans or multiline spans) and showing the snippet window wouldn't
1032 /// improve understandability.
1033pub fn span_suggestion_hidden(
1034&mut self,
1035 sp: Span,
1036 msg: impl Into<DiagMessage>,
1037 suggestion: impl ToString,
1038 applicability: Applicability,
1039 ) -> &mut Self {
1040self.span_suggestion_with_style(
1041sp,
1042msg,
1043suggestion,
1044applicability,
1045 SuggestionStyle::HideCodeAlways,
1046 );
1047self1048 }
10491050#[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,
1051/// Adds a suggestion to the JSON output that will not be shown in the CLI.
1052 ///
1053 /// This is intended to be used for suggestions that are *very* obvious in what the changes
1054 /// need to be from the message, but we still want other tools to be able to apply them.
1055pub fn tool_only_span_suggestion(
1056&mut self,
1057 sp: Span,
1058 msg: impl Into<DiagMessage>,
1059 suggestion: impl ToString,
1060 applicability: Applicability,
1061 ) -> &mut Self {
1062self.span_suggestion_with_style(
1063 sp,
1064 msg,
1065 suggestion,
1066 applicability,
1067 SuggestionStyle::CompletelyHidden,
1068 );
1069self
1070} }10711072/// Add a subdiagnostic from a type that implements `Subdiagnostic` (see
1073 /// [rustc_macros::Subdiagnostic]). Performs eager formatting of any messages
1074 /// used in the subdiagnostic, so suitable for use with repeated messages (i.e. re-use of
1075 /// interpolated variables).
1076pub fn subdiagnostic(&mut self, subdiagnostic: impl Subdiagnostic) -> &mut Self {
1077subdiagnostic.add_to_diag(self);
1078self1079 }
10801081#[doc = r" Add a span."]
#[doc = "See [`Diag::span()`]."]
pub fn span(&mut self, sp: impl Into<MultiSpan>) -> &mut Self {
self.span = sp.into();
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,
1082/// Add a span.
1083pub fn span(&mut self, sp: impl Into<MultiSpan>) -> &mut Self {
1084self.span = sp.into();
1085self
1086} }10871088pub fn is_lint(
1089&mut self,
1090 name: String,
1091 has_future_breakage: bool,
1092 rust_version: Option<RustcVersion>,
1093 ) -> &mut Self {
1094self.is_lint = Some(IsLint { name, has_future_breakage, rust_version });
1095self1096 }
10971098#[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,
1099/// Add an error code.
1100pub fn code(&mut self, code: ErrCode) -> &mut Self {
1101self.code = Some(code);
1102self
1103} }11041105#[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,
1106/// Add a primary message.
1107pub fn primary_message(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
1108self.messages[0] = (msg.into(), Style::NoStyle);
1109self
1110} }11111112#[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,
1113/// Add an argument.
1114pub fn arg(
1115&mut self,
1116 name: impl Into<DiagArgName>,
1117 arg: impl IntoDiagArg,
1118 ) -> &mut Self {
1119self.deref_mut().arg(name, arg);
1120self
1121} }11221123/// Convenience function for internal use, clients should use one of the
1124 /// public methods above.
1125 ///
1126 /// Used by `proc_macro_server` for implementing `server::Diagnostic`.
1127pub fn sub(&mut self, level: Sublevel, message: impl Into<DiagMessage>, span: MultiSpan) {
1128self.deref_mut().sub(level, message, span);
1129 }
11301131/// Convenience function for internal use, clients should use one of the
1132 /// public methods above.
1133fn sub_with_highlights(&mut self, level: Sublevel, messages: Vec<StringPart>, span: MultiSpan) {
1134let messages = messages.into_iter().map(|m| (m.content.into(), m.style)).collect();
1135let sub = Subdiag { level, messages, span };
1136self.children.push(sub);
1137 }
11381139/// Takes the diagnostic. For use by methods that consume the Diag: `emit`,
1140 /// `cancel`, etc. Afterwards, `drop` is the only code that will be run on
1141 /// `self`.
1142fn take_diag(&mut self) -> DiagInner {
1143if let Some(path) = &self.long_ty_path {
1144self.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the full name for the type has been written to \'{0}\'",
path.display()))
})format!(
1145"the full name for the type has been written to '{}'",
1146 path.display()
1147 ));
1148self.note("consider using `--verbose` to print the full type name to the console");
1149 }
1150*self.diag.take().unwrap()
1151 }
11521153/// This method allows us to access the path of the file where "long types" are written to.
1154 ///
1155 /// When calling `Diag::emit`, as part of that we will check if a `long_ty_path` has been set,
1156 /// and if it has been then we add a note mentioning the file where the "long types" were
1157 /// written to.
1158 ///
1159 /// When calling `tcx.short_string()` after a `Diag` is constructed, the preferred way of doing
1160 /// so is `tcx.short_string(ty, diag.long_ty_path())`. The diagnostic itself is the one that
1161 /// keeps the existence of a "long type" anywhere in the diagnostic, so the note telling the
1162 /// user where we wrote the file to is only printed once at most, *and* it makes it much harder
1163 /// to forget to set it.
1164 ///
1165 /// If the diagnostic hasn't been created before a "short ty string" is created, then you should
1166 /// ensure that this method is called to set it `*diag.long_ty_path() = path`.
1167 ///
1168 /// As a rule of thumb, if you see or add at least one `tcx.short_string()` call anywhere, in a
1169 /// scope, `diag.long_ty_path()` should be called once somewhere close by.
1170pub fn long_ty_path(&mut self) -> &mut Option<PathBuf> {
1171&mut self.long_ty_path
1172 }
11731174pub fn with_long_ty_path(mut self, long_ty_path: Option<PathBuf>) -> Self {
1175self.long_ty_path = long_ty_path;
1176self1177 }
11781179/// Emit the diagnostic. Will also abort appropriately if the level is `Bug` or `Fatal`.
1180#[track_caller]
1181pub fn emit(mut self) {
1182let level = self.level; // get level before taking the inner diag
1183let diag = self.take_diag();
1184self.dcx.emit_diagnostic(diag);
11851186match level {
1187 Level::Bug => panic::panic_any(ExplicitBug),
1188 Level::Fatal => crate::FatalError.raise(),
1189_ => {}
1190 }
1191 }
11921193/// Use this on a `Bug` diagnostic if you need the `!` return type. Otherwise `emit` suffices.
1194 /// Aborts if used on a non-`Bug` diagnostic.
1195#[track_caller]
1196pub fn emit_bug(self) -> ! {
1197{
match (&self.level, &Level::Bug) {
(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::None);
}
}
}
};assert_eq!(self.level, Level::Bug);
1198self.emit();
1199::core::panicking::panic("internal error: entered unreachable code");unreachable!(); // `emit` will have aborted
1200}
12011202/// Use this on a `Fatal` diagnostic if you need the `!` return type. Otherwise `emit`
1203 /// suffices. Aborts if used on a non-`Fatal` diagnostic.
1204#[track_caller]
1205pub fn emit_fatal(self) -> ! {
1206{
match (&self.level, &Level::Fatal) {
(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::None);
}
}
}
};assert_eq!(self.level, Level::Fatal);
1207self.emit();
1208::core::panicking::panic("internal error: entered unreachable code");unreachable!(); // `emit` will have aborted
1209}
12101211/// Use this on an `Error`/`DelayedBug` diagnostic if you need the `ErrorGuaranteed` return
1212 /// type. Otherwise `emit` suffices. Aborts if used on a non-`Error`/`DelayedBug` diagnostic.
1213#[track_caller]
1214pub fn emit_err(mut self) -> ErrorGuaranteed {
1215let diag = self.take_diag();
12161217// The only error levels that should reach here are `Error` and `DelayedBug`.
1218 // (Also, even though `level` isn't `pub`, the whole `DiagInner` could
1219 // be overwritten with a new one thanks to `DerefMut`. So this assert
1220 // protects against that, too.)
1221if !#[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!(
1222matches!(diag.level, Level::Error | Level::DelayedBug),
1223"invalid diagnostic level ({:?})",
1224 diag.level,
1225 );
12261227let guar = self.dcx.emit_diagnostic(diag);
1228guar.unwrap()
1229 }
12301231/// Emit the diagnostic unless `delay` is true,
1232 /// in which case the emission will be delayed as a bug.
1233 ///
1234 /// See `emit` and `delay_as_bug` for details.
1235#[track_caller]
1236pub fn emit_err_unless_delay(mut self, delay: bool) -> ErrorGuaranteed {
1237if delay {
1238self.downgrade_to_delayed_bug();
1239 }
1240self.emit_err()
1241 }
12421243/// Cancel and consume the diagnostic. (A diagnostic must either be emitted or
1244 /// cancelled or it will panic when dropped).
1245pub fn cancel(mut self) {
1246self.diag = None;
1247drop(self);
1248 }
12491250/// Cancels this diagnostic and returns its first message, if it exists.
1251pub fn cancel_into_message(self) -> Option<String> {
1252let s = self.diag.as_ref()?.messages.get(0)?.0.as_str().map(ToString::to_string);
1253self.cancel();
1254s1255 }
12561257/// See `DiagCtxtHandle::stash_diagnostic` for details.
1258pub fn stash(mut self, span: Span, key: StashKey) -> Option<ErrorGuaranteed> {
1259let diag = self.take_diag();
1260self.dcx.stash_diagnostic(span, key, diag)
1261 }
12621263/// Delay emission of this diagnostic as a bug.
1264 ///
1265 /// This can be useful in contexts where an error indicates a bug but
1266 /// typically this only happens when other compilation errors have already
1267 /// happened. In those cases this can be used to defer emission of this
1268 /// diagnostic as a bug in the compiler only if no other errors have been
1269 /// emitted.
1270 ///
1271 /// In the meantime, though, callsites are required to deal with the "bug"
1272 /// locally in whichever way makes the most sense.
1273#[track_caller]
1274pub fn delay_as_bug(mut self) -> ErrorGuaranteed {
1275self.downgrade_to_delayed_bug();
1276self.emit_err()
1277 }
1278}
12791280/// Destructor bomb: every `Diag` must be consumed (emitted, cancelled, etc.)
1281/// or we emit a bug.
1282impl Dropfor Diag<'_> {
1283fn drop(&mut self) {
1284match self.diag.take() {
1285Some(diag) if !panicking() => {
1286self.dcx.emit_diagnostic(DiagInner::new(
1287 Level::Bug,
1288DiagMessage::from("the following error was constructed but not emitted"),
1289 ));
1290self.dcx.emit_diagnostic(*diag);
1291{
::core::panicking::panic_fmt(format_args!("error was constructed but not emitted"));
};panic!("error was constructed but not emitted");
1292 }
1293_ => {}
1294 }
1295 }
1296}
12971298#[macro_export]
1299macro_rules!struct_span_code_err {
1300 ($dcx:expr, $span:expr, $code:expr, $($message:tt)*) => ({
1301$dcx.struct_span_err($span, format!($($message)*)).with_code($code)
1302 })
1303}