]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_macros/src/diagnostics/subdiagnostic.rs
Revert parts of "use derive proc macro to impl SessionDiagnostic"
[rust.git] / compiler / rustc_macros / src / diagnostics / subdiagnostic.rs
1 #![deny(unused_must_use)]
2
3 use crate::diagnostics::error::{
4     span_err, throw_invalid_attr, throw_invalid_nested_attr, throw_span_err, DiagnosticDeriveError,
5 };
6 use crate::diagnostics::utils::{
7     report_error_if_not_applied_to_applicability, report_error_if_not_applied_to_span,
8     Applicability, FieldInfo, FieldInnerTy, HasFieldMap, SetOnce,
9 };
10 use proc_macro2::TokenStream;
11 use quote::{format_ident, quote};
12 use std::collections::HashMap;
13 use std::fmt;
14 use std::str::FromStr;
15 use syn::{spanned::Spanned, Attribute, Meta, MetaList, MetaNameValue, NestedMeta, Path};
16 use synstructure::{BindingInfo, Structure, VariantInfo};
17
18 /// Which kind of suggestion is being created?
19 #[derive(Clone, Copy)]
20 enum SubdiagnosticSuggestionKind {
21     /// `#[suggestion]`
22     Normal,
23     /// `#[suggestion_short]`
24     Short,
25     /// `#[suggestion_hidden]`
26     Hidden,
27     /// `#[suggestion_verbose]`
28     Verbose,
29 }
30
31 impl FromStr for SubdiagnosticSuggestionKind {
32     type Err = ();
33
34     fn from_str(s: &str) -> Result<Self, Self::Err> {
35         match s {
36             "" => Ok(SubdiagnosticSuggestionKind::Normal),
37             "_short" => Ok(SubdiagnosticSuggestionKind::Short),
38             "_hidden" => Ok(SubdiagnosticSuggestionKind::Hidden),
39             "_verbose" => Ok(SubdiagnosticSuggestionKind::Verbose),
40             _ => Err(()),
41         }
42     }
43 }
44
45 impl SubdiagnosticSuggestionKind {
46     pub fn to_suggestion_style(&self) -> TokenStream {
47         match self {
48             SubdiagnosticSuggestionKind::Normal => {
49                 quote! { rustc_errors::SuggestionStyle::ShowCode }
50             }
51             SubdiagnosticSuggestionKind::Short => {
52                 quote! { rustc_errors::SuggestionStyle::HideCodeInline }
53             }
54             SubdiagnosticSuggestionKind::Hidden => {
55                 quote! { rustc_errors::SuggestionStyle::HideCodeAlways }
56             }
57             SubdiagnosticSuggestionKind::Verbose => {
58                 quote! { rustc_errors::SuggestionStyle::ShowAlways }
59             }
60         }
61     }
62 }
63
64 /// Which kind of subdiagnostic is being created from a variant?
65 #[derive(Clone)]
66 enum SubdiagnosticKind {
67     /// `#[label(...)]`
68     Label,
69     /// `#[note(...)]`
70     Note,
71     /// `#[help(...)]`
72     Help,
73     /// `#[warning(...)]`
74     Warn,
75     /// `#[suggestion{,_short,_hidden,_verbose}]`
76     Suggestion { suggestion_kind: SubdiagnosticSuggestionKind, code: TokenStream },
77     /// `#[multipart_suggestion{,_short,_hidden,_verbose}]`
78     MultipartSuggestion { suggestion_kind: SubdiagnosticSuggestionKind },
79 }
80
81 impl quote::IdentFragment for SubdiagnosticKind {
82     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83         match self {
84             SubdiagnosticKind::Label => write!(f, "label"),
85             SubdiagnosticKind::Note => write!(f, "note"),
86             SubdiagnosticKind::Help => write!(f, "help"),
87             SubdiagnosticKind::Warn => write!(f, "warn"),
88             SubdiagnosticKind::Suggestion { .. } => write!(f, "suggestion_with_style"),
89             SubdiagnosticKind::MultipartSuggestion { .. } => {
90                 write!(f, "multipart_suggestion_with_style")
91             }
92         }
93     }
94
95     fn span(&self) -> Option<proc_macro2::Span> {
96         None
97     }
98 }
99
100 /// The central struct for constructing the `add_to_diagnostic` method from an annotated struct.
101 pub(crate) struct SessionSubdiagnosticDerive<'a> {
102     structure: Structure<'a>,
103     diag: syn::Ident,
104 }
105
106 impl<'a> SessionSubdiagnosticDerive<'a> {
107     pub(crate) fn new(structure: Structure<'a>) -> Self {
108         let diag = format_ident!("diag");
109         Self { structure, diag }
110     }
111
112     pub(crate) fn into_tokens(self) -> TokenStream {
113         let SessionSubdiagnosticDerive { mut structure, diag } = self;
114         let implementation = {
115             let ast = structure.ast();
116             let span = ast.span().unwrap();
117             match ast.data {
118                 syn::Data::Struct(..) | syn::Data::Enum(..) => (),
119                 syn::Data::Union(..) => {
120                     span_err(
121                         span,
122                         "`#[derive(SessionSubdiagnostic)]` can only be used on structs and enums",
123                     );
124                 }
125             }
126
127             if matches!(ast.data, syn::Data::Enum(..)) {
128                 for attr in &ast.attrs {
129                     span_err(
130                         attr.span().unwrap(),
131                         "unsupported type attribute for subdiagnostic enum",
132                     )
133                     .emit();
134                 }
135             }
136
137             structure.bind_with(|_| synstructure::BindStyle::Move);
138             let variants_ = structure.each_variant(|variant| {
139                 // Build the mapping of field names to fields. This allows attributes to peek
140                 // values from other fields.
141                 let mut fields_map = HashMap::new();
142                 for binding in variant.bindings() {
143                     let field = binding.ast();
144                     if let Some(ident) = &field.ident {
145                         fields_map.insert(ident.to_string(), quote! { #binding });
146                     }
147                 }
148
149                 let mut builder = SessionSubdiagnosticDeriveBuilder {
150                     diag: &diag,
151                     variant,
152                     span,
153                     fields: fields_map,
154                     span_field: None,
155                     applicability: None,
156                     has_suggestion_parts: false,
157                 };
158                 builder.into_tokens().unwrap_or_else(|v| v.to_compile_error())
159             });
160
161             quote! {
162                 match self {
163                     #variants_
164                 }
165             }
166         };
167
168         let ret = structure.gen_impl(quote! {
169             gen impl rustc_errors::AddSubdiagnostic for @Self {
170                 fn add_to_diagnostic(self, #diag: &mut rustc_errors::Diagnostic) {
171                     use rustc_errors::{Applicability, IntoDiagnosticArg};
172                     #implementation
173                 }
174             }
175         });
176         ret
177     }
178 }
179
180 /// Tracks persistent information required for building up the call to add to the diagnostic
181 /// for the final generated method. This is a separate struct to `SessionSubdiagnosticDerive`
182 /// only to be able to destructure and split `self.builder` and the `self.structure` up to avoid a
183 /// double mut borrow later on.
184 struct SessionSubdiagnosticDeriveBuilder<'a> {
185     /// The identifier to use for the generated `DiagnosticBuilder` instance.
186     diag: &'a syn::Ident,
187
188     /// Info for the current variant (or the type if not an enum).
189     variant: &'a VariantInfo<'a>,
190     /// Span for the entire type.
191     span: proc_macro::Span,
192
193     /// Store a map of field name to its corresponding field. This is built on construction of the
194     /// derive builder.
195     fields: HashMap<String, TokenStream>,
196
197     /// Identifier for the binding to the `#[primary_span]` field.
198     span_field: Option<(proc_macro2::Ident, proc_macro::Span)>,
199     /// If a suggestion, the identifier for the binding to the `#[applicability]` field or a
200     /// `rustc_errors::Applicability::*` variant directly.
201     applicability: Option<(TokenStream, proc_macro::Span)>,
202
203     /// Set to true when a `#[suggestion_part]` field is encountered, used to generate an error
204     /// during finalization if still `false`.
205     has_suggestion_parts: bool,
206 }
207
208 impl<'a> HasFieldMap for SessionSubdiagnosticDeriveBuilder<'a> {
209     fn get_field_binding(&self, field: &String) -> Option<&TokenStream> {
210         self.fields.get(field)
211     }
212 }
213
214 impl<'a> SessionSubdiagnosticDeriveBuilder<'a> {
215     fn identify_kind(
216         &mut self,
217     ) -> Result<Option<(SubdiagnosticKind, Path)>, DiagnosticDeriveError> {
218         let mut kind_slug = None;
219
220         for attr in self.variant.ast().attrs {
221             let span = attr.span().unwrap();
222
223             let name = attr.path.segments.last().unwrap().ident.to_string();
224             let name = name.as_str();
225
226             let meta = attr.parse_meta()?;
227             let Meta::List(MetaList { ref nested, .. }) = meta else {
228                 throw_invalid_attr!(attr, &meta);
229             };
230
231             let mut kind = match name {
232                 "label" => SubdiagnosticKind::Label,
233                 "note" => SubdiagnosticKind::Note,
234                 "help" => SubdiagnosticKind::Help,
235                 "warning" => SubdiagnosticKind::Warn,
236                 _ => {
237                     if let Some(suggestion_kind) =
238                         name.strip_prefix("suggestion").and_then(|s| s.parse().ok())
239                     {
240                         SubdiagnosticKind::Suggestion { suggestion_kind, code: TokenStream::new() }
241                     } else if let Some(suggestion_kind) =
242                         name.strip_prefix("multipart_suggestion").and_then(|s| s.parse().ok())
243                     {
244                         SubdiagnosticKind::MultipartSuggestion { suggestion_kind }
245                     } else {
246                         throw_invalid_attr!(attr, &meta);
247                     }
248                 }
249             };
250
251             let mut slug = None;
252             let mut code = None;
253
254             let mut nested_iter = nested.into_iter();
255             if let Some(nested_attr) = nested_iter.next() {
256                 match nested_attr {
257                     NestedMeta::Meta(Meta::Path(path)) => {
258                         slug.set_once((path.clone(), span));
259                     }
260                     NestedMeta::Meta(meta @ Meta::NameValue(_))
261                         if matches!(
262                             meta.path().segments.last().unwrap().ident.to_string().as_str(),
263                             "code" | "applicability"
264                         ) =>
265                     {
266                         // Don't error for valid follow-up attributes.
267                     }
268                     nested_attr => {
269                         throw_invalid_nested_attr!(attr, &nested_attr, |diag| {
270                             diag.help(
271                                 "first argument of the attribute should be the diagnostic \
272                                  slug",
273                             )
274                         })
275                     }
276                 };
277             }
278
279             for nested_attr in nested_iter {
280                 let meta = match nested_attr {
281                     NestedMeta::Meta(ref meta) => meta,
282                     _ => throw_invalid_nested_attr!(attr, &nested_attr),
283                 };
284
285                 let span = meta.span().unwrap();
286                 let nested_name = meta.path().segments.last().unwrap().ident.to_string();
287                 let nested_name = nested_name.as_str();
288
289                 let value = match meta {
290                     Meta::NameValue(MetaNameValue { lit: syn::Lit::Str(value), .. }) => value,
291                     Meta::Path(_) => throw_invalid_nested_attr!(attr, &nested_attr, |diag| {
292                         diag.help("a diagnostic slug must be the first argument to the attribute")
293                     }),
294                     _ => throw_invalid_nested_attr!(attr, &nested_attr),
295                 };
296
297                 match nested_name {
298                     "code" => {
299                         if matches!(kind, SubdiagnosticKind::Suggestion { .. }) {
300                             let formatted_str = self.build_format(&value.value(), value.span());
301                             code.set_once((formatted_str, span));
302                         } else {
303                             span_err(
304                                 span,
305                                 &format!(
306                                     "`code` is not a valid nested attribute of a `{}` attribute",
307                                     name
308                                 ),
309                             )
310                             .emit();
311                         }
312                     }
313                     "applicability" => {
314                         if matches!(
315                             kind,
316                             SubdiagnosticKind::Suggestion { .. }
317                                 | SubdiagnosticKind::MultipartSuggestion { .. }
318                         ) {
319                             let value =
320                                 Applicability::from_str(&value.value()).unwrap_or_else(|()| {
321                                     span_err(span, "invalid applicability").emit();
322                                     Applicability::Unspecified
323                                 });
324                             self.applicability.set_once((quote! { #value }, span));
325                         } else {
326                             span_err(
327                                 span,
328                                 &format!(
329                                     "`applicability` is not a valid nested attribute of a `{}` attribute",
330                                     name
331                                 )
332                             ).emit();
333                         }
334                     }
335                     _ => throw_invalid_nested_attr!(attr, &nested_attr, |diag| {
336                         diag.help("only `code` and `applicability` are valid nested attributes")
337                     }),
338                 }
339             }
340
341             let Some((slug, _)) = slug else {
342                 throw_span_err!(
343                     span,
344                     &format!(
345                         "diagnostic slug must be first argument of a `#[{}(...)]` attribute",
346                         name
347                     )
348                 );
349             };
350
351             match kind {
352                 SubdiagnosticKind::Suggestion { code: ref mut code_field, .. } => {
353                     let Some((code, _)) = code else {
354                         throw_span_err!(span, "suggestion without `code = \"...\"`");
355                     };
356                     *code_field = code;
357                 }
358                 SubdiagnosticKind::Label
359                 | SubdiagnosticKind::Note
360                 | SubdiagnosticKind::Help
361                 | SubdiagnosticKind::Warn
362                 | SubdiagnosticKind::MultipartSuggestion { .. } => {}
363             }
364
365             kind_slug.set_once(((kind, slug), span))
366         }
367
368         Ok(kind_slug.map(|(kind_slug, _)| kind_slug))
369     }
370
371     /// Generates the code for a field with no attributes.
372     fn generate_field_set_arg(&mut self, binding: &BindingInfo<'_>) -> TokenStream {
373         let ast = binding.ast();
374         assert_eq!(ast.attrs.len(), 0, "field with attribute used as diagnostic arg");
375
376         let diag = &self.diag;
377         let ident = ast.ident.as_ref().unwrap();
378         quote! {
379             #diag.set_arg(
380                 stringify!(#ident),
381                 #binding
382             );
383         }
384     }
385
386     /// Generates the necessary code for all attributes on a field.
387     fn generate_field_attr_code(
388         &mut self,
389         binding: &BindingInfo<'_>,
390         kind: &SubdiagnosticKind,
391     ) -> TokenStream {
392         let ast = binding.ast();
393         assert!(ast.attrs.len() > 0, "field without attributes generating attr code");
394
395         // Abstract over `Vec<T>` and `Option<T>` fields using `FieldInnerTy`, which will
396         // apply the generated code on each element in the `Vec` or `Option`.
397         let inner_ty = FieldInnerTy::from_type(&ast.ty);
398         ast.attrs
399             .iter()
400             .map(|attr| {
401                 let info = FieldInfo {
402                     binding,
403                     ty: inner_ty.inner_type().unwrap_or(&ast.ty),
404                     span: &ast.span(),
405                 };
406
407                 let generated = self
408                     .generate_field_code_inner(kind, attr, info)
409                     .unwrap_or_else(|v| v.to_compile_error());
410
411                 inner_ty.with(binding, generated)
412             })
413             .collect()
414     }
415
416     fn generate_field_code_inner(
417         &mut self,
418         kind: &SubdiagnosticKind,
419         attr: &Attribute,
420         info: FieldInfo<'_>,
421     ) -> Result<TokenStream, DiagnosticDeriveError> {
422         let meta = attr.parse_meta()?;
423         match meta {
424             Meta::Path(path) => self.generate_field_code_inner_path(kind, attr, info, path),
425             Meta::List(list @ MetaList { .. }) => {
426                 self.generate_field_code_inner_list(kind, attr, info, list)
427             }
428             _ => throw_invalid_attr!(attr, &meta),
429         }
430     }
431
432     /// Generates the code for a `[Meta::Path]`-like attribute on a field (e.g. `#[primary_span]`).
433     fn generate_field_code_inner_path(
434         &mut self,
435         kind: &SubdiagnosticKind,
436         attr: &Attribute,
437         info: FieldInfo<'_>,
438         path: Path,
439     ) -> Result<TokenStream, DiagnosticDeriveError> {
440         let span = attr.span().unwrap();
441         let ident = &path.segments.last().unwrap().ident;
442         let name = ident.to_string();
443         let name = name.as_str();
444
445         match name {
446             "skip_arg" => Ok(quote! {}),
447             "primary_span" => {
448                 if matches!(kind, SubdiagnosticKind::MultipartSuggestion { .. }) {
449                     throw_invalid_attr!(attr, &Meta::Path(path), |diag| {
450                         diag.help(
451                             "multipart suggestions use one or more `#[suggestion_part]`s rather \
452                             than one `#[primary_span]`",
453                         )
454                     })
455                 }
456
457                 report_error_if_not_applied_to_span(attr, &info)?;
458
459                 let binding = info.binding.binding.clone();
460                 self.span_field.set_once((binding, span));
461
462                 Ok(quote! {})
463             }
464             "suggestion_part" => {
465                 self.has_suggestion_parts = true;
466
467                 match kind {
468                     SubdiagnosticKind::MultipartSuggestion { .. } => {
469                         span_err(
470                             span,
471                             "`#[suggestion_part(...)]` attribute without `code = \"...\"`",
472                         )
473                         .emit();
474                         Ok(quote! {})
475                     }
476                     SubdiagnosticKind::Label
477                     | SubdiagnosticKind::Note
478                     | SubdiagnosticKind::Help
479                     | SubdiagnosticKind::Warn
480                     | SubdiagnosticKind::Suggestion { .. } => {
481                         throw_invalid_attr!(attr, &Meta::Path(path), |diag| {
482                             diag.help(
483                                 "`#[suggestion_part(...)]` is only valid in multipart suggestions, use `#[primary_span]` instead",
484                             )
485                         });
486                     }
487                 }
488             }
489             "applicability" => {
490                 if let SubdiagnosticKind::Suggestion { .. }
491                 | SubdiagnosticKind::MultipartSuggestion { .. } = kind
492                 {
493                     report_error_if_not_applied_to_applicability(attr, &info)?;
494
495                     let binding = info.binding.binding.clone();
496                     self.applicability.set_once((quote! { #binding }, span));
497                 } else {
498                     span_err(span, "`#[applicability]` is only valid on suggestions").emit();
499                 }
500
501                 Ok(quote! {})
502             }
503             _ => throw_invalid_attr!(attr, &Meta::Path(path), |diag| {
504                 let span_attr = if let SubdiagnosticKind::MultipartSuggestion { .. } = kind {
505                     "suggestion_part"
506                 } else {
507                     "primary_span"
508                 };
509                 diag.help(format!(
510                     "only `{span_attr}`, `applicability` and `skip_arg` are valid field attributes",
511                 ))
512             }),
513         }
514     }
515
516     /// Generates the code for a `[Meta::List]`-like attribute on a field (e.g.
517     /// `#[suggestion_part(code = "...")]`).
518     fn generate_field_code_inner_list(
519         &mut self,
520         kind: &SubdiagnosticKind,
521         attr: &Attribute,
522         info: FieldInfo<'_>,
523         list: MetaList,
524     ) -> Result<TokenStream, DiagnosticDeriveError> {
525         let span = attr.span().unwrap();
526         let ident = &list.path.segments.last().unwrap().ident;
527         let name = ident.to_string();
528         let name = name.as_str();
529
530         match name {
531             "suggestion_part" => {
532                 if !matches!(kind, SubdiagnosticKind::MultipartSuggestion { .. }) {
533                     throw_invalid_attr!(attr, &Meta::List(list), |diag| {
534                         diag.help(
535                             "`#[suggestion_part(...)]` is only valid in multipart suggestions",
536                         )
537                     })
538                 }
539
540                 self.has_suggestion_parts = true;
541
542                 report_error_if_not_applied_to_span(attr, &info)?;
543
544                 let mut code = None;
545                 for nested_attr in list.nested.iter() {
546                     let NestedMeta::Meta(ref meta) = nested_attr else {
547                         throw_invalid_nested_attr!(attr, &nested_attr);
548                     };
549
550                     let span = meta.span().unwrap();
551                     let nested_name = meta.path().segments.last().unwrap().ident.to_string();
552                     let nested_name = nested_name.as_str();
553
554                     let Meta::NameValue(MetaNameValue { lit: syn::Lit::Str(value), .. }) = meta else {
555                         throw_invalid_nested_attr!(attr, &nested_attr);
556                     };
557
558                     match nested_name {
559                         "code" => {
560                             let formatted_str = self.build_format(&value.value(), value.span());
561                             code.set_once((formatted_str, span));
562                         }
563                         _ => throw_invalid_nested_attr!(attr, &nested_attr, |diag| {
564                             diag.help("`code` is the only valid nested attribute")
565                         }),
566                     }
567                 }
568
569                 let Some((code, _)) = code else {
570                     span_err(span, "`#[suggestion_part(...)]` attribute without `code = \"...\"`")
571                         .emit();
572                     return Ok(quote! {});
573                 };
574                 let binding = info.binding;
575
576                 Ok(quote! { suggestions.push((#binding, #code)); })
577             }
578             _ => throw_invalid_attr!(attr, &Meta::List(list), |diag| {
579                 let span_attr = if let SubdiagnosticKind::MultipartSuggestion { .. } = kind {
580                     "suggestion_part"
581                 } else {
582                     "primary_span"
583                 };
584                 diag.help(format!(
585                     "only `{span_attr}`, `applicability` and `skip_arg` are valid field attributes",
586                 ))
587             }),
588         }
589     }
590
591     pub fn into_tokens(&mut self) -> Result<TokenStream, DiagnosticDeriveError> {
592         let Some((kind, slug)) = self.identify_kind()? else {
593             throw_span_err!(
594                 self.variant.ast().ident.span().unwrap(),
595                 "subdiagnostic kind not specified"
596             );
597         };
598
599         let init = match &kind {
600             SubdiagnosticKind::Label
601             | SubdiagnosticKind::Note
602             | SubdiagnosticKind::Help
603             | SubdiagnosticKind::Warn
604             | SubdiagnosticKind::Suggestion { .. } => quote! {},
605             SubdiagnosticKind::MultipartSuggestion { .. } => {
606                 quote! { let mut suggestions = Vec::new(); }
607             }
608         };
609
610         let attr_args: TokenStream = self
611             .variant
612             .bindings()
613             .iter()
614             .filter(|binding| !binding.ast().attrs.is_empty())
615             .map(|binding| self.generate_field_attr_code(binding, &kind))
616             .collect();
617
618         let span_field = self.span_field.as_ref().map(|(span, _)| span);
619         let applicability = self.applicability.take().map_or_else(
620             || quote! { rustc_errors::Applicability::Unspecified },
621             |(applicability, _)| applicability,
622         );
623
624         let diag = &self.diag;
625         let name = format_ident!("{}{}", if span_field.is_some() { "span_" } else { "" }, kind);
626         let message = quote! { rustc_errors::fluent::#slug };
627         let call = match kind {
628             SubdiagnosticKind::Suggestion { suggestion_kind, code } => {
629                 if let Some(span) = span_field {
630                     let style = suggestion_kind.to_suggestion_style();
631
632                     quote! { #diag.#name(#span, #message, #code, #applicability, #style); }
633                 } else {
634                     span_err(self.span, "suggestion without `#[primary_span]` field").emit();
635                     quote! { unreachable!(); }
636                 }
637             }
638             SubdiagnosticKind::MultipartSuggestion { suggestion_kind } => {
639                 if !self.has_suggestion_parts {
640                     span_err(
641                         self.span,
642                         "multipart suggestion without any `#[suggestion_part(...)]` fields",
643                     )
644                     .emit();
645                 }
646
647                 let style = suggestion_kind.to_suggestion_style();
648
649                 quote! { #diag.#name(#message, suggestions, #applicability, #style); }
650             }
651             SubdiagnosticKind::Label => {
652                 if let Some(span) = span_field {
653                     quote! { #diag.#name(#span, #message); }
654                 } else {
655                     span_err(self.span, "label without `#[primary_span]` field").emit();
656                     quote! { unreachable!(); }
657                 }
658             }
659             _ => {
660                 if let Some(span) = span_field {
661                     quote! { #diag.#name(#span, #message); }
662                 } else {
663                     quote! { #diag.#name(#message); }
664                 }
665             }
666         };
667
668         let plain_args: TokenStream = self
669             .variant
670             .bindings()
671             .iter()
672             .filter(|binding| binding.ast().attrs.is_empty())
673             .map(|binding| self.generate_field_set_arg(binding))
674             .collect();
675
676         Ok(quote! {
677             #init
678             #attr_args
679             #call
680             #plain_args
681         })
682     }
683 }