1use std::ops::Range;
2
3use rustc_errors::E0232;
4use rustc_hir::AttrPath;
5use rustc_hir::attrs::diagnostic::{
6 Directive, Filter, FilterFormatString, Flag, FormatArg, FormatString, LitOrArg, Name,
7 NameValue, Piece, Predicate,
8};
9use rustc_macros::Diagnostic;
10use rustc_parse_format::{
11 Argument, FormatSpec, ParseError, ParseMode, Parser, Piece as RpfPiece, Position,
12};
13use rustc_session::lint::builtin::{
14 MALFORMED_DIAGNOSTIC_ATTRIBUTES, MALFORMED_DIAGNOSTIC_FORMAT_LITERALS,
15};
16use rustc_span::{Ident, InnerSpan, Span, Symbol, kw, sym};
17use thin_vec::{ThinVec, thin_vec};
18
19use crate::context::AcceptContext;
20use crate::errors::{
21 FormatWarning, IgnoredDiagnosticOption, MalFormedDiagnosticAttributeLint,
22 MissingOptionsForDiagnosticAttribute, NonMetaItemDiagnosticAttribute, WrappedParserError,
23};
24use crate::parser::{ArgParser, MetaItemListParser, MetaItemOrLitParser, MetaItemParser};
25
26pub(crate) mod check_cfg;
27pub(crate) mod do_not_recommend;
28pub(crate) mod on_const;
29pub(crate) mod on_move;
30pub(crate) mod on_unimplemented;
31pub(crate) mod on_unknown;
32pub(crate) mod on_unmatch_args;
33
34#[derive(#[automatically_derived]
impl ::core::marker::Copy for Mode { }Copy, #[automatically_derived]
impl ::core::clone::Clone for Mode {
#[inline]
fn clone(&self) -> Mode { *self }
}Clone)]
35pub(crate) enum Mode {
36 RustcOnUnimplemented,
38 DiagnosticOnUnimplemented,
40 DiagnosticOnConst,
42 DiagnosticOnMove,
44 DiagnosticOnUnknown,
46 DiagnosticOnUnmatchArgs,
48}
49
50impl Mode {
51 fn as_str(&self) -> &'static str {
52 match self {
53 Self::RustcOnUnimplemented => "rustc_on_unimplemented",
54 Self::DiagnosticOnUnimplemented => "diagnostic::on_unimplemented",
55 Self::DiagnosticOnConst => "diagnostic::on_const",
56 Self::DiagnosticOnMove => "diagnostic::on_move",
57 Self::DiagnosticOnUnknown => "diagnostic::on_unknown",
58 Self::DiagnosticOnUnmatchArgs => "diagnostic::on_unmatch_args",
59 }
60 }
61
62 fn expected_options(&self) -> &'static str {
63 const DEFAULT: &str =
64 "at least one of the `message`, `note` and `label` options are expected";
65 match self {
66 Self::RustcOnUnimplemented => {
67 "see <https://rustc-dev-guide.rust-lang.org/diagnostics.html#rustc_on_unimplemented>"
68 }
69 Self::DiagnosticOnUnimplemented => DEFAULT,
70 Self::DiagnosticOnConst => DEFAULT,
71 Self::DiagnosticOnMove => DEFAULT,
72 Self::DiagnosticOnUnknown => DEFAULT,
73 Self::DiagnosticOnUnmatchArgs => DEFAULT,
74 }
75 }
76
77 fn allowed_options(&self) -> &'static str {
78 const DEFAULT: &str = "only `message`, `note` and `label` are allowed as options";
79 match self {
80 Self::RustcOnUnimplemented => {
81 "see <https://rustc-dev-guide.rust-lang.org/diagnostics.html#rustc_on_unimplemented>"
82 }
83 Self::DiagnosticOnUnimplemented => DEFAULT,
84 Self::DiagnosticOnConst => DEFAULT,
85 Self::DiagnosticOnMove => DEFAULT,
86 Self::DiagnosticOnUnknown => DEFAULT,
87 Self::DiagnosticOnUnmatchArgs => DEFAULT,
88 }
89 }
90
91 fn allowed_format_arguments(&self) -> &'static str {
92 match self {
93 Self::RustcOnUnimplemented => {
94 "see <https://rustc-dev-guide.rust-lang.org/diagnostics.html#rustc_on_unimplemented> for allowed format arguments"
95 }
96 Self::DiagnosticOnUnimplemented => {
97 "only `Self` and generics of the trait are allowed as a format argument"
98 }
99 Self::DiagnosticOnConst => {
100 "only `Self` and generics of the implementation are allowed as a format argument"
101 }
102 Self::DiagnosticOnMove => {
103 "only `This`, `Self` and generics of the type are allowed as a format argument"
104 }
105 Self::DiagnosticOnUnknown => {
106 "only `This` is allowed as a format argument, referring to the failed import"
107 }
108 Self::DiagnosticOnUnmatchArgs => {
109 "only `This` is allowed as a format argument, referring to the macro's name"
110 }
111 }
112 }
113}
114
115fn merge_directives(
116 cx: &mut AcceptContext<'_, '_>,
117 first: &mut Option<(Span, Directive)>,
118 later: (Span, Directive),
119) {
120 if let Some((_, first)) = first {
121 if first.is_rustc_attr || later.1.is_rustc_attr {
122 cx.emit_err(DupesNotAllowed);
123 }
124
125 merge(cx, &mut first.message, later.1.message, sym::message);
126 merge(cx, &mut first.label, later.1.label, sym::label);
127 first.notes.extend(later.1.notes);
128 } else {
129 *first = Some(later);
130 }
131}
132
133fn merge<T>(
134 cx: &mut AcceptContext<'_, '_>,
135 first: &mut Option<(Span, T)>,
136 later: Option<(Span, T)>,
137 option_name: Symbol,
138) {
139 match (first, later) {
140 (Some(_) | None, None) => {}
141 (Some((first_span, _)), Some((later_span, _))) => {
142 let first_span = *first_span;
143 cx.emit_lint(
144 MALFORMED_DIAGNOSTIC_ATTRIBUTES,
145 IgnoredDiagnosticOption { first_span, later_span, option_name },
146 later_span,
147 );
148 }
149 (first @ None, Some(later)) => {
150 first.get_or_insert(later);
151 }
152 }
153}
154
155fn parse_list<'p>(
156 cx: &mut AcceptContext<'_, '_>,
157 args: &'p ArgParser,
158 mode: Mode,
159) -> Option<&'p MetaItemListParser> {
160 let span = cx.attr_span;
161 match args {
162 ArgParser::List(items) if items.len() != 0 => return Some(items),
163 ArgParser::List(list) => {
164 cx.emit_lint(
168 MALFORMED_DIAGNOSTIC_ATTRIBUTES,
169 NonMetaItemDiagnosticAttribute,
170 list.span,
171 );
172 }
173 ArgParser::NoArgs => {
174 cx.emit_lint(
175 MALFORMED_DIAGNOSTIC_ATTRIBUTES,
176 MissingOptionsForDiagnosticAttribute {
177 attribute: mode.as_str(),
178 options: mode.expected_options(),
179 },
180 span,
181 );
182 }
183 ArgParser::NameValue(_) => {
184 cx.emit_lint(
185 MALFORMED_DIAGNOSTIC_ATTRIBUTES,
186 MalFormedDiagnosticAttributeLint {
187 attribute: mode.as_str(),
188 options: mode.allowed_options(),
189 span,
190 },
191 span,
192 );
193 }
194 }
195 None
196}
197
198fn parse_directive_items<'p>(
199 cx: &mut AcceptContext<'_, '_>,
200 mode: Mode,
201 items: impl Iterator<Item = &'p MetaItemOrLitParser>,
202 is_root: bool,
203) -> Option<Directive> {
204 let mut message: Option<(Span, _)> = None;
205 let mut label: Option<(Span, _)> = None;
206 let mut notes = ThinVec::new();
207 let mut parent_label = None;
208 let mut filters = ThinVec::new();
209
210 for item in items {
211 let span = item.span();
212
213 macro malformed() {{
214 cx.emit_lint(
215 MALFORMED_DIAGNOSTIC_ATTRIBUTES,
216 MalFormedDiagnosticAttributeLint {
217 attribute: mode.as_str(),
218 options: mode.allowed_options(),
219 span,
220 },
221 span,
222 );
223 continue;
224 }}
225
226 macro or_malformed($($code:tt)*) {{
227 let Some(ret) = (
228 try {
229 $($code)*
230 }
231 ) else {
232 malformed!()
233 };
234 ret
235 }}
236
237 macro duplicate($name: ident, $($first_span:tt)*) {{
238 let first_span = $($first_span)*;
239 cx.emit_lint(
240 MALFORMED_DIAGNOSTIC_ATTRIBUTES,
241 IgnoredDiagnosticOption {
242 first_span,
243 later_span: span,
244 option_name: $name,
245 },
246 span,
247 );
248 }}
249
250 let item: &MetaItemParser = {
let Some(ret) =
(try {
item.meta_item()?
}) else {
{
cx.emit_lint(MALFORMED_DIAGNOSTIC_ATTRIBUTES,
MalFormedDiagnosticAttributeLint {
attribute: mode.as_str(),
options: mode.allowed_options(),
span,
}, span);
continue;
}
};
ret
}or_malformed!(item.meta_item()?);
251 let name = {
let Some(ret) =
(try {
item.ident()?
}) else {
{
cx.emit_lint(MALFORMED_DIAGNOSTIC_ATTRIBUTES,
MalFormedDiagnosticAttributeLint {
attribute: mode.as_str(),
options: mode.allowed_options(),
span,
}, span);
continue;
}
};
ret
}or_malformed!(item.ident()?).name;
252
253 let value: Option<Ident> = match item.args().as_name_value() {
261 Some(nv) => Some({
let Some(ret) =
(try {
nv.value_as_ident()?
}) else {
{
cx.emit_lint(MALFORMED_DIAGNOSTIC_ATTRIBUTES,
MalFormedDiagnosticAttributeLint {
attribute: mode.as_str(),
options: mode.allowed_options(),
span,
}, span);
continue;
}
};
ret
}or_malformed!(nv.value_as_ident()?)),
262 None => None,
263 };
264
265 let mut parse_format = |input: Ident| {
266 let snippet = cx.sess.source_map().span_to_snippet(input.span).ok();
267 let is_snippet = snippet.is_some();
268 match parse_format_string(input.name, snippet, input.span, mode) {
269 Ok((f, warnings)) => {
270 for warning in warnings {
271 let (FormatWarning::InvalidSpecifier { span }
272 | FormatWarning::PositionalArgument { span }
273 | FormatWarning::IndexedArgument { span }
274 | FormatWarning::DisallowedPlaceholder { span, .. }) = warning;
275 cx.emit_lint(MALFORMED_DIAGNOSTIC_FORMAT_LITERALS, warning, span);
276 }
277
278 f
279 }
280 Err(e) => {
281 cx.emit_lint(
282 MALFORMED_DIAGNOSTIC_FORMAT_LITERALS,
283 WrappedParserError {
284 description: e.description,
285 label: e.label,
286 span: slice_span(input.span, e.span.clone(), is_snippet),
287 },
288 input.span,
289 );
290 FormatString {
292 input: input.name,
293 span: input.span,
294 pieces: {
let len = [()].len();
let mut vec = ::thin_vec::ThinVec::with_capacity(len);
vec.push(Piece::Lit(input.name));
vec
}thin_vec![Piece::Lit(input.name)],
295 }
296 }
297 }
298 };
299 match (mode, name) {
300 (_, sym::message) => {
301 let value = {
let Some(ret) =
(try {
value?
}) else {
{
cx.emit_lint(MALFORMED_DIAGNOSTIC_ATTRIBUTES,
MalFormedDiagnosticAttributeLint {
attribute: mode.as_str(),
options: mode.allowed_options(),
span,
}, span);
continue;
}
};
ret
}or_malformed!(value?);
302 if let Some(message) = &message {
303 {
let first_span = message.0;
cx.emit_lint(MALFORMED_DIAGNOSTIC_ATTRIBUTES,
IgnoredDiagnosticOption {
first_span,
later_span: span,
option_name: name,
}, span);
}duplicate!(name, message.0)
304 } else {
305 message = Some((item.span(), parse_format(value)));
306 }
307 }
308 (_, sym::label) => {
309 let value = {
let Some(ret) =
(try {
value?
}) else {
{
cx.emit_lint(MALFORMED_DIAGNOSTIC_ATTRIBUTES,
MalFormedDiagnosticAttributeLint {
attribute: mode.as_str(),
options: mode.allowed_options(),
span,
}, span);
continue;
}
};
ret
}or_malformed!(value?);
310 if let Some(label) = &label {
311 {
let first_span = label.0;
cx.emit_lint(MALFORMED_DIAGNOSTIC_ATTRIBUTES,
IgnoredDiagnosticOption {
first_span,
later_span: span,
option_name: name,
}, span);
}duplicate!(name, label.0)
312 } else {
313 label = Some((item.span(), parse_format(value)));
314 }
315 }
316 (_, sym::note) => {
317 let value = {
let Some(ret) =
(try {
value?
}) else {
{
cx.emit_lint(MALFORMED_DIAGNOSTIC_ATTRIBUTES,
MalFormedDiagnosticAttributeLint {
attribute: mode.as_str(),
options: mode.allowed_options(),
span,
}, span);
continue;
}
};
ret
}or_malformed!(value?);
318 notes.push(parse_format(value))
319 }
320 (Mode::RustcOnUnimplemented, sym::parent_label) => {
321 let value = {
let Some(ret) =
(try {
value?
}) else {
{
cx.emit_lint(MALFORMED_DIAGNOSTIC_ATTRIBUTES,
MalFormedDiagnosticAttributeLint {
attribute: mode.as_str(),
options: mode.allowed_options(),
span,
}, span);
continue;
}
};
ret
}or_malformed!(value?);
322 if parent_label.is_none() {
323 parent_label = Some(parse_format(value));
324 } else {
325 {
let first_span = span;
cx.emit_lint(MALFORMED_DIAGNOSTIC_ATTRIBUTES,
IgnoredDiagnosticOption {
first_span,
later_span: span,
option_name: name,
}, span);
}duplicate!(name, span)
326 }
327 }
328 (Mode::RustcOnUnimplemented, sym::on) => {
329 if is_root {
330 let items = {
let Some(ret) =
(try {
item.args().as_list()?
}) else {
{
cx.emit_lint(MALFORMED_DIAGNOSTIC_ATTRIBUTES,
MalFormedDiagnosticAttributeLint {
attribute: mode.as_str(),
options: mode.allowed_options(),
span,
}, span);
continue;
}
};
ret
}or_malformed!(item.args().as_list()?);
331 let mut iter = items.mixed();
332 let filter: &MetaItemOrLitParser = match iter.next() {
333 Some(c) => c,
334 None => {
335 cx.emit_err(InvalidOnClause::Empty { span });
336 continue;
337 }
338 };
339
340 let filter = parse_filter(filter);
341
342 if items.len() < 2 {
343 {
cx.emit_lint(MALFORMED_DIAGNOSTIC_ATTRIBUTES,
MalFormedDiagnosticAttributeLint {
attribute: mode.as_str(),
options: mode.allowed_options(),
span,
}, span);
continue;
};malformed!();
346 }
347
348 match filter {
349 Ok(filter) => {
350 let directive =
351 {
let Some(ret) =
(try {
parse_directive_items(cx, mode, iter, false)?
}) else {
{
cx.emit_lint(MALFORMED_DIAGNOSTIC_ATTRIBUTES,
MalFormedDiagnosticAttributeLint {
attribute: mode.as_str(),
options: mode.allowed_options(),
span,
}, span);
continue;
}
};
ret
}or_malformed!(parse_directive_items(cx, mode, iter, false)?);
352 filters.push((filter, directive));
353 }
354 Err(e) => {
355 cx.emit_err(e);
356 }
357 }
358 } else {
359 {
cx.emit_lint(MALFORMED_DIAGNOSTIC_ATTRIBUTES,
MalFormedDiagnosticAttributeLint {
attribute: mode.as_str(),
options: mode.allowed_options(),
span,
}, span);
continue;
};malformed!();
360 }
361 }
362
363 _other => {
364 {
cx.emit_lint(MALFORMED_DIAGNOSTIC_ATTRIBUTES,
MalFormedDiagnosticAttributeLint {
attribute: mode.as_str(),
options: mode.allowed_options(),
span,
}, span);
continue;
};malformed!();
365 }
366 }
367 }
368
369 Some(Directive {
370 is_rustc_attr: #[allow(non_exhaustive_omitted_patterns)] match mode {
Mode::RustcOnUnimplemented => true,
_ => false,
}matches!(mode, Mode::RustcOnUnimplemented),
371 filters,
372 message,
373 label,
374 notes,
375 parent_label,
376 })
377}
378
379pub(crate) fn parse_format_string(
380 input: Symbol,
381 snippet: Option<String>,
382 span: Span,
383 mode: Mode,
384) -> Result<(FormatString, Vec<FormatWarning>), ParseError> {
385 let s = input.as_str();
386 let mut parser = Parser::new(s, None, snippet, false, ParseMode::Diagnostic);
387 let pieces: Vec<_> = parser.by_ref().collect();
388
389 if let Some(err) = parser.errors.into_iter().next() {
390 return Err(err);
391 }
392 let mut warnings = Vec::new();
393
394 let pieces = pieces
395 .into_iter()
396 .map(|piece| match piece {
397 RpfPiece::Lit(lit) => Piece::Lit(Symbol::intern(lit)),
398 RpfPiece::NextArgument(arg) => {
399 Piece::Arg(parse_arg(&arg, mode, &mut warnings, span, parser.is_source_literal))
400 }
401 })
402 .collect();
403
404 Ok((FormatString { input, pieces, span }, warnings))
405}
406
407fn parse_arg(
408 arg: &Argument<'_>,
409 mode: Mode,
410 warnings: &mut Vec<FormatWarning>,
411 input_span: Span,
412 is_source_literal: bool,
413) -> FormatArg {
414 let span = slice_span(input_span, arg.position_span.clone(), is_source_literal);
415
416 let mut check_format = true;
417
418 let ret = match arg.position {
419 Position::ArgumentNamed(name) => match (mode, Symbol::intern(name)) {
421 (Mode::RustcOnUnimplemented, sym::ItemContext) => FormatArg::ItemContext,
422
423 (Mode::RustcOnUnimplemented, sym::This) => match arg.format.ty {
425 "resolved" => {
426 check_format = false;
427 FormatArg::ThisResolved
428 }
429 "path" => {
430 check_format = false;
431 FormatArg::ThisPath
432 }
433 _ => FormatArg::This,
434 },
435
436 (
442 Mode::DiagnosticOnUnknown | Mode::DiagnosticOnMove | Mode::DiagnosticOnUnmatchArgs,
443 sym::This,
444 ) => FormatArg::This,
445
446 (
452 Mode::RustcOnUnimplemented
453 | Mode::DiagnosticOnUnimplemented
454 | Mode::DiagnosticOnMove
455 | Mode::DiagnosticOnConst,
456 kw::SelfUpper,
457 ) => FormatArg::SelfUpper,
458
459 (
465 Mode::RustcOnUnimplemented
466 | Mode::DiagnosticOnUnimplemented
467 | Mode::DiagnosticOnMove
468 | Mode::DiagnosticOnConst,
469 generic_param,
470 ) => FormatArg::GenericParam { generic_param, span },
471
472 (Mode::DiagnosticOnUnknown | Mode::DiagnosticOnUnmatchArgs, as_is) => {
474 warnings.push(FormatWarning::DisallowedPlaceholder {
475 span,
476 attr: mode.as_str(),
477 allowed: mode.allowed_format_arguments(),
478 });
479 FormatArg::AsIs(Symbol::intern(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{{{0}}}", as_is))
})format!("{{{as_is}}}")))
480 }
481 },
482
483 Position::ArgumentIs(idx) => {
485 warnings.push(FormatWarning::IndexedArgument { span });
486 FormatArg::AsIs(Symbol::intern(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{{{0}}}", idx))
})format!("{{{idx}}}")))
487 }
488 Position::ArgumentImplicitlyIs(_) => {
489 warnings.push(FormatWarning::PositionalArgument { span });
490 FormatArg::AsIs(sym::empty_braces)
491 }
492 };
493 if check_format {
494 warn_on_format_spec(&arg.format, warnings, input_span, is_source_literal);
495 }
496 ret
497}
498
499fn warn_on_format_spec(
502 spec: &FormatSpec<'_>,
503 warnings: &mut Vec<FormatWarning>,
504 input_span: Span,
505 is_source_literal: bool,
506) {
507 if let Some(ty_span) = &spec.ty_span {
508 let span = slice_span(input_span, ty_span.clone(), is_source_literal);
509 warnings.push(FormatWarning::InvalidSpecifier { span })
510 }
511}
512
513fn slice_span(input: Span, Range { start, end }: Range<usize>, is_source_literal: bool) -> Span {
514 if is_source_literal { input.from_inner(InnerSpan { start, end }) } else { input }
515}
516
517pub(crate) fn parse_filter(input: &MetaItemOrLitParser) -> Result<Filter, InvalidOnClause> {
518 let span = input.span();
519 let pred = parse_predicate(input)?;
520 Ok(Filter { span, pred })
521}
522
523fn parse_predicate(input: &MetaItemOrLitParser) -> Result<Predicate, InvalidOnClause> {
524 let Some(meta_item) = input.meta_item() else {
525 return Err(InvalidOnClause::UnsupportedLiteral { span: input.span() });
526 };
527
528 let Some(predicate) = meta_item.ident() else {
529 return Err(InvalidOnClause::ExpectedIdentifier {
530 span: meta_item.path().span(),
531 path: meta_item.path().get_attribute_path(),
532 });
533 };
534
535 match meta_item.args() {
536 ArgParser::List(mis) => match predicate.name {
537 sym::any => Ok(Predicate::Any(parse_predicate_sequence(mis)?)),
538 sym::all => Ok(Predicate::All(parse_predicate_sequence(mis)?)),
539 sym::not => {
540 if let Some(single) = mis.as_single() {
541 Ok(Predicate::Not(Box::new(parse_predicate(single)?)))
542 } else {
543 Err(InvalidOnClause::ExpectedOnePredInNot { span: mis.span })
544 }
545 }
546 invalid_pred => {
547 Err(InvalidOnClause::InvalidPredicate { span: predicate.span, invalid_pred })
548 }
549 },
550 ArgParser::NameValue(p) => {
551 let Some(value) = p.value_as_ident() else {
552 return Err(InvalidOnClause::UnsupportedLiteral { span: p.args_span() });
553 };
554 let name = parse_name(predicate.name);
555 let value = parse_filter_format(value.name);
556 let kv = NameValue { name, value };
557 Ok(Predicate::Match(kv))
558 }
559 ArgParser::NoArgs => {
560 let flag = parse_flag(predicate)?;
561 Ok(Predicate::Flag(flag))
562 }
563 }
564}
565
566fn parse_predicate_sequence(
567 sequence: &MetaItemListParser,
568) -> Result<ThinVec<Predicate>, InvalidOnClause> {
569 sequence.mixed().map(parse_predicate).collect()
570}
571
572fn parse_flag(Ident { name, span }: Ident) -> Result<Flag, InvalidOnClause> {
573 match name {
574 sym::crate_local => Ok(Flag::CrateLocal),
575 sym::direct => Ok(Flag::Direct),
576 sym::from_desugaring => Ok(Flag::FromDesugaring),
577 invalid_flag => Err(InvalidOnClause::InvalidFlag { invalid_flag, span }),
578 }
579}
580
581fn parse_name(name: Symbol) -> Name {
582 match name {
583 kw::SelfUpper => Name::SelfUpper,
584 sym::from_desugaring => Name::FromDesugaring,
585 sym::cause => Name::Cause,
586 generic => Name::GenericArg(generic),
587 }
588}
589
590fn parse_filter_format(input: Symbol) -> FilterFormatString {
591 let pieces = Parser::new(input.as_str(), None, None, false, ParseMode::Diagnostic)
592 .map(|p| match p {
593 RpfPiece::Lit(s) => LitOrArg::Lit(Symbol::intern(s)),
594 RpfPiece::NextArgument(a) => match a.position {
596 Position::ArgumentNamed(
604 arg @ ("integer" | "integral" | "float" | "union" | "enum" | "struct"),
605 ) => LitOrArg::Lit(Symbol::intern(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{{{0}}}", arg))
})format!("{{{arg}}}"))),
606
607 Position::ArgumentNamed(arg) => LitOrArg::Arg(Symbol::intern(arg)),
608 Position::ArgumentImplicitlyIs(_) => LitOrArg::Lit(sym::empty_braces),
609 Position::ArgumentIs(idx) => LitOrArg::Lit(Symbol::intern(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{{{0}}}", idx))
})format!("{{{idx}}}"))),
610 },
611 })
612 .collect();
613 FilterFormatString { pieces }
614}
615
616#[derive(const _: () =
{
impl<'_sess, G> rustc_errors::Diagnostic<'_sess, G> for
InvalidOnClause where G: rustc_errors::EmissionGuarantee {
#[track_caller]
fn into_diag(self, dcx: rustc_errors::DiagCtxtHandle<'_sess>,
level: rustc_errors::Level) -> rustc_errors::Diag<'_sess, G> {
match self {
InvalidOnClause::Empty { span: __binding_0 } => {
let mut diag =
rustc_errors::Diag::new(dcx, level,
rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("empty `on`-clause in `#[rustc_on_unimplemented]`")));
diag.code(E0232);
;
diag.span(__binding_0);
diag.span_label(__binding_0,
rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("empty `on`-clause here")));
diag
}
InvalidOnClause::ExpectedOnePredInNot { span: __binding_0 }
=> {
let mut diag =
rustc_errors::Diag::new(dcx, level,
rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("expected a single predicate in `not(..)`")));
diag.code(E0232);
;
diag.span(__binding_0);
diag.span_label(__binding_0,
rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("unexpected quantity of predicates here")));
diag
}
InvalidOnClause::UnsupportedLiteral { span: __binding_0 } =>
{
let mut diag =
rustc_errors::Diag::new(dcx, level,
rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("literals inside `on`-clauses are not supported")));
diag.code(E0232);
;
diag.span(__binding_0);
diag.span_label(__binding_0,
rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("unexpected literal here")));
diag
}
InvalidOnClause::ExpectedIdentifier {
span: __binding_0, path: __binding_1 } => {
let mut diag =
rustc_errors::Diag::new(dcx, level,
rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("expected an identifier inside this `on`-clause")));
diag.code(E0232);
;
diag.arg("path", __binding_1);
diag.span(__binding_0);
diag.span_label(__binding_0,
rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("expected an identifier here, not `{$path}`")));
diag
}
InvalidOnClause::InvalidPredicate {
span: __binding_0, invalid_pred: __binding_1 } => {
let mut diag =
rustc_errors::Diag::new(dcx, level,
rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("this predicate is invalid")));
diag.code(E0232);
;
diag.arg("invalid_pred", __binding_1);
diag.span(__binding_0);
diag.span_label(__binding_0,
rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("expected one of `any`, `all` or `not` here, not `{$invalid_pred}`")));
diag
}
InvalidOnClause::InvalidFlag {
span: __binding_0, invalid_flag: __binding_1 } => {
let mut diag =
rustc_errors::Diag::new(dcx, level,
rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("invalid flag in `on`-clause")));
diag.code(E0232);
;
diag.arg("invalid_flag", __binding_1);
diag.span(__binding_0);
diag.span_label(__binding_0,
rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("expected one of the `crate_local`, `direct` or `from_desugaring` flags, not `{$invalid_flag}`")));
diag
}
}
}
}
};Diagnostic)]
617pub(crate) enum InvalidOnClause {
618 #[diag("empty `on`-clause in `#[rustc_on_unimplemented]`", code = E0232)]
619 Empty {
620 #[primary_span]
621 #[label("empty `on`-clause here")]
622 span: Span,
623 },
624 #[diag("expected a single predicate in `not(..)`", code = E0232)]
625 ExpectedOnePredInNot {
626 #[primary_span]
627 #[label("unexpected quantity of predicates here")]
628 span: Span,
629 },
630 #[diag("literals inside `on`-clauses are not supported", code = E0232)]
631 UnsupportedLiteral {
632 #[primary_span]
633 #[label("unexpected literal here")]
634 span: Span,
635 },
636 #[diag("expected an identifier inside this `on`-clause", code = E0232)]
637 ExpectedIdentifier {
638 #[primary_span]
639 #[label("expected an identifier here, not `{$path}`")]
640 span: Span,
641 path: AttrPath,
642 },
643 #[diag("this predicate is invalid", code = E0232)]
644 InvalidPredicate {
645 #[primary_span]
646 #[label("expected one of `any`, `all` or `not` here, not `{$invalid_pred}`")]
647 span: Span,
648 invalid_pred: Symbol,
649 },
650 #[diag("invalid flag in `on`-clause", code = E0232)]
651 InvalidFlag {
652 #[primary_span]
653 #[label(
654 "expected one of the `crate_local`, `direct` or `from_desugaring` flags, not `{$invalid_flag}`"
655 )]
656 span: Span,
657 invalid_flag: Symbol,
658 },
659}
660
661#[derive(const _: () =
{
impl<'_sess, G> rustc_errors::Diagnostic<'_sess, G> for
DupesNotAllowed where G: rustc_errors::EmissionGuarantee {
#[track_caller]
fn into_diag(self, dcx: rustc_errors::DiagCtxtHandle<'_sess>,
level: rustc_errors::Level) -> rustc_errors::Diag<'_sess, G> {
match self {
DupesNotAllowed => {
let mut diag =
rustc_errors::Diag::new(dcx, level,
rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("using multiple `rustc_on_unimplemented` (or mixing it with `diagnostic::on_unimplemented`) is not supported")));
;
diag
}
}
}
}
};Diagnostic)]
662#[diag(
663 "using multiple `rustc_on_unimplemented` (or mixing it with `diagnostic::on_unimplemented`) is not supported"
664)]
665pub(crate) struct DupesNotAllowed;