1 #![deny(unused_must_use)]
3 use crate::diagnostics::error::{
4 invalid_attr, span_err, throw_invalid_attr, throw_invalid_nested_attr, throw_span_err,
7 use crate::diagnostics::utils::{
8 build_field_mapping, new_code_ident, report_error_if_not_applied_to_applicability,
9 report_error_if_not_applied_to_span, FieldInfo, FieldInnerTy, FieldMap, HasFieldMap, SetOnce,
10 SpannedOption, SubdiagnosticKind,
12 use proc_macro2::TokenStream;
13 use quote::{format_ident, quote};
14 use syn::{spanned::Spanned, Attribute, Meta, MetaList, MetaNameValue, NestedMeta, Path};
15 use synstructure::{BindingInfo, Structure, VariantInfo};
17 /// The central struct for constructing the `add_to_diagnostic` method from an annotated struct.
18 pub(crate) struct SubdiagnosticDeriveBuilder {
23 impl SubdiagnosticDeriveBuilder {
24 pub(crate) fn new() -> Self {
25 let diag = format_ident!("diag");
26 let f = format_ident!("f");
30 pub(crate) fn into_tokens<'a>(self, mut structure: Structure<'a>) -> TokenStream {
31 let implementation = {
32 let ast = structure.ast();
33 let span = ast.span().unwrap();
35 syn::Data::Struct(..) | syn::Data::Enum(..) => (),
36 syn::Data::Union(..) => {
39 "`#[derive(Subdiagnostic)]` can only be used on structs and enums",
44 if matches!(ast.data, syn::Data::Enum(..)) {
45 for attr in &ast.attrs {
48 "unsupported type attribute for subdiagnostic enum",
54 structure.bind_with(|_| synstructure::BindStyle::Move);
55 let variants_ = structure.each_variant(|variant| {
56 let mut builder = SubdiagnosticDeriveVariantBuilder {
60 formatting_init: TokenStream::new(),
61 fields: build_field_mapping(variant),
64 has_suggestion_parts: false,
66 builder.into_tokens().unwrap_or_else(|v| v.to_compile_error())
76 let diag = &self.diag;
78 let ret = structure.gen_impl(quote! {
79 gen impl rustc_errors::AddToDiagnostic for @Self {
80 fn add_to_diagnostic_with<__F>(self, #diag: &mut rustc_errors::Diagnostic, #f: __F)
83 &mut rustc_errors::Diagnostic,
84 rustc_errors::SubdiagnosticMessage
85 ) -> rustc_errors::SubdiagnosticMessage,
87 use rustc_errors::{Applicability, IntoDiagnosticArg};
96 /// Tracks persistent information required for building up the call to add to the diagnostic
97 /// for the final generated method. This is a separate struct to `SubdiagnosticDerive`
98 /// only to be able to destructure and split `self.builder` and the `self.structure` up to avoid a
99 /// double mut borrow later on.
100 struct SubdiagnosticDeriveVariantBuilder<'parent, 'a> {
101 /// The identifier to use for the generated `DiagnosticBuilder` instance.
102 parent: &'parent SubdiagnosticDeriveBuilder,
104 /// Info for the current variant (or the type if not an enum).
105 variant: &'a VariantInfo<'a>,
106 /// Span for the entire type.
107 span: proc_macro::Span,
109 /// Initialization of format strings for code suggestions.
110 formatting_init: TokenStream,
112 /// Store a map of field name to its corresponding field. This is built on construction of the
116 /// Identifier for the binding to the `#[primary_span]` field.
117 span_field: SpannedOption<proc_macro2::Ident>,
119 /// The binding to the `#[applicability]` field, if present.
120 applicability: SpannedOption<TokenStream>,
122 /// Set to true when a `#[suggestion_part]` field is encountered, used to generate an error
123 /// during finalization if still `false`.
124 has_suggestion_parts: bool,
127 impl<'parent, 'a> HasFieldMap for SubdiagnosticDeriveVariantBuilder<'parent, 'a> {
128 fn get_field_binding(&self, field: &String) -> Option<&TokenStream> {
129 self.fields.get(field)
133 /// Provides frequently-needed information about the diagnostic kinds being derived for this type.
134 #[derive(Clone, Copy, Debug)]
135 struct KindsStatistics {
136 has_multipart_suggestion: bool,
137 all_multipart_suggestions: bool,
138 has_normal_suggestion: bool,
139 all_applicabilities_static: bool,
142 impl<'a> FromIterator<&'a SubdiagnosticKind> for KindsStatistics {
143 fn from_iter<T: IntoIterator<Item = &'a SubdiagnosticKind>>(kinds: T) -> Self {
145 has_multipart_suggestion: false,
146 all_multipart_suggestions: true,
147 has_normal_suggestion: false,
148 all_applicabilities_static: true,
152 if let SubdiagnosticKind::MultipartSuggestion { applicability: None, .. }
153 | SubdiagnosticKind::Suggestion { applicability: None, .. } = kind
155 ret.all_applicabilities_static = false;
157 if let SubdiagnosticKind::MultipartSuggestion { .. } = kind {
158 ret.has_multipart_suggestion = true;
160 ret.all_multipart_suggestions = false;
163 if let SubdiagnosticKind::Suggestion { .. } = kind {
164 ret.has_normal_suggestion = true;
171 impl<'parent, 'a> SubdiagnosticDeriveVariantBuilder<'parent, 'a> {
172 fn identify_kind(&mut self) -> Result<Vec<(SubdiagnosticKind, Path)>, DiagnosticDeriveError> {
173 let mut kind_slugs = vec![];
175 for attr in self.variant.ast().attrs {
176 let (kind, slug) = SubdiagnosticKind::from_attr(attr, self)?;
178 let Some(slug) = slug else {
179 let name = attr.path.segments.last().unwrap().ident.to_string();
180 let name = name.as_str();
183 attr.span().unwrap(),
185 "diagnostic slug must be first argument of a `#[{}(...)]` attribute",
191 kind_slugs.push((kind, slug));
197 /// Generates the code for a field with no attributes.
198 fn generate_field_set_arg(&mut self, binding: &BindingInfo<'_>) -> TokenStream {
199 let ast = binding.ast();
200 assert_eq!(ast.attrs.len(), 0, "field with attribute used as diagnostic arg");
202 let diag = &self.parent.diag;
203 let ident = ast.ident.as_ref().unwrap();
204 // strip `r#` prefix, if present
205 let ident = format_ident!("{}", ident);
215 /// Generates the necessary code for all attributes on a field.
216 fn generate_field_attr_code(
218 binding: &BindingInfo<'_>,
219 kind_stats: KindsStatistics,
221 let ast = binding.ast();
222 assert!(ast.attrs.len() > 0, "field without attributes generating attr code");
224 // Abstract over `Vec<T>` and `Option<T>` fields using `FieldInnerTy`, which will
225 // apply the generated code on each element in the `Vec` or `Option`.
226 let inner_ty = FieldInnerTy::from_type(&ast.ty);
230 let info = FieldInfo {
232 ty: inner_ty.inner_type().unwrap_or(&ast.ty),
237 .generate_field_code_inner(kind_stats, attr, info, inner_ty.will_iterate())
238 .unwrap_or_else(|v| v.to_compile_error());
240 inner_ty.with(binding, generated)
245 fn generate_field_code_inner(
247 kind_stats: KindsStatistics,
250 clone_suggestion_code: bool,
251 ) -> Result<TokenStream, DiagnosticDeriveError> {
252 let meta = attr.parse_meta()?;
254 Meta::Path(path) => self.generate_field_code_inner_path(kind_stats, attr, info, path),
255 Meta::List(list @ MetaList { .. }) => self.generate_field_code_inner_list(
260 clone_suggestion_code,
262 _ => throw_invalid_attr!(attr, &meta),
266 /// Generates the code for a `[Meta::Path]`-like attribute on a field (e.g. `#[primary_span]`).
267 fn generate_field_code_inner_path(
269 kind_stats: KindsStatistics,
273 ) -> Result<TokenStream, DiagnosticDeriveError> {
274 let span = attr.span().unwrap();
275 let ident = &path.segments.last().unwrap().ident;
276 let name = ident.to_string();
277 let name = name.as_str();
280 "skip_arg" => Ok(quote! {}),
282 if kind_stats.has_multipart_suggestion {
283 invalid_attr(attr, &Meta::Path(path))
285 "multipart suggestions use one or more `#[suggestion_part]`s rather \
286 than one `#[primary_span]`",
290 report_error_if_not_applied_to_span(attr, &info)?;
292 let binding = info.binding.binding.clone();
293 self.span_field.set_once(binding, span);
298 "suggestion_part" => {
299 self.has_suggestion_parts = true;
301 if kind_stats.has_multipart_suggestion {
302 span_err(span, "`#[suggestion_part(...)]` attribute without `code = \"...\"`")
305 invalid_attr(attr, &Meta::Path(path))
307 "`#[suggestion_part(...)]` is only valid in multipart suggestions, \
308 use `#[primary_span]` instead",
316 if kind_stats.has_multipart_suggestion || kind_stats.has_normal_suggestion {
317 report_error_if_not_applied_to_applicability(attr, &info)?;
319 if kind_stats.all_applicabilities_static {
322 "`#[applicability]` has no effect if all `#[suggestion]`/\
323 `#[multipart_suggestion]` attributes have a static \
324 `applicability = \"...\"`",
328 let binding = info.binding.binding.clone();
329 self.applicability.set_once(quote! { #binding }, span);
331 span_err(span, "`#[applicability]` is only valid on suggestions").emit();
337 let mut span_attrs = vec![];
338 if kind_stats.has_multipart_suggestion {
339 span_attrs.push("suggestion_part");
341 if !kind_stats.all_multipart_suggestions {
342 span_attrs.push("primary_span")
345 invalid_attr(attr, &Meta::Path(path))
347 "only `{}`, `applicability` and `skip_arg` are valid field attributes",
348 span_attrs.join(", ")
357 /// Generates the code for a `[Meta::List]`-like attribute on a field (e.g.
358 /// `#[suggestion_part(code = "...")]`).
359 fn generate_field_code_inner_list(
361 kind_stats: KindsStatistics,
365 clone_suggestion_code: bool,
366 ) -> Result<TokenStream, DiagnosticDeriveError> {
367 let span = attr.span().unwrap();
368 let ident = &list.path.segments.last().unwrap().ident;
369 let name = ident.to_string();
370 let name = name.as_str();
373 "suggestion_part" => {
374 if !kind_stats.has_multipart_suggestion {
375 throw_invalid_attr!(attr, &Meta::List(list), |diag| {
377 "`#[suggestion_part(...)]` is only valid in multipart suggestions",
382 self.has_suggestion_parts = true;
384 report_error_if_not_applied_to_span(attr, &info)?;
387 for nested_attr in list.nested.iter() {
388 let NestedMeta::Meta(ref meta) = nested_attr else {
389 throw_invalid_nested_attr!(attr, &nested_attr);
392 let span = meta.span().unwrap();
393 let nested_name = meta.path().segments.last().unwrap().ident.to_string();
394 let nested_name = nested_name.as_str();
396 let Meta::NameValue(MetaNameValue { lit: syn::Lit::Str(value), .. }) = meta else {
397 throw_invalid_nested_attr!(attr, &nested_attr);
402 let formatted_str = self.build_format(&value.value(), value.span());
403 let code_field = new_code_ident();
404 code.set_once((code_field, formatted_str), span);
406 _ => throw_invalid_nested_attr!(attr, &nested_attr, |diag| {
407 diag.help("`code` is the only valid nested attribute")
412 let Some((code_field, formatted_str)) = code.value() else {
413 span_err(span, "`#[suggestion_part(...)]` attribute without `code = \"...\"`")
415 return Ok(quote! {});
417 let binding = info.binding;
419 self.formatting_init.extend(quote! { let #code_field = #formatted_str; });
420 let code_field = if clone_suggestion_code {
421 quote! { #code_field.clone() }
423 quote! { #code_field }
425 Ok(quote! { suggestions.push((#binding, #code_field)); })
427 _ => throw_invalid_attr!(attr, &Meta::List(list), |diag| {
428 let mut span_attrs = vec![];
429 if kind_stats.has_multipart_suggestion {
430 span_attrs.push("suggestion_part");
432 if !kind_stats.all_multipart_suggestions {
433 span_attrs.push("primary_span")
436 "only `{}`, `applicability` and `skip_arg` are valid field attributes",
437 span_attrs.join(", ")
443 pub fn into_tokens(&mut self) -> Result<TokenStream, DiagnosticDeriveError> {
444 let kind_slugs = self.identify_kind()?;
445 if kind_slugs.is_empty() {
447 self.variant.ast().ident.span().unwrap(),
448 "subdiagnostic kind not specified"
452 let kind_stats: KindsStatistics = kind_slugs.iter().map(|(kind, _slug)| kind).collect();
454 let init = if kind_stats.has_multipart_suggestion {
455 quote! { let mut suggestions = Vec::new(); }
460 let attr_args: TokenStream = self
464 .filter(|binding| !binding.ast().attrs.is_empty())
465 .map(|binding| self.generate_field_attr_code(binding, kind_stats))
468 let span_field = self.span_field.value_ref();
470 let diag = &self.parent.diag;
471 let f = &self.parent.f;
472 let mut calls = TokenStream::new();
473 for (kind, slug) in kind_slugs {
474 let message = format_ident!("__message");
475 calls.extend(quote! { let #message = #f(#diag, rustc_errors::fluent::#slug.into()); });
477 let name = format_ident!("{}{}", if span_field.is_some() { "span_" } else { "" }, kind);
478 let call = match kind {
479 SubdiagnosticKind::Suggestion {
485 self.formatting_init.extend(code_init);
487 let applicability = applicability
489 .map(|a| quote! { #a })
490 .or_else(|| self.applicability.take().value())
491 .unwrap_or_else(|| quote! { rustc_errors::Applicability::Unspecified });
493 if let Some(span) = span_field {
494 let style = suggestion_kind.to_suggestion_style();
495 quote! { #diag.#name(#span, #message, #code_field, #applicability, #style); }
497 span_err(self.span, "suggestion without `#[primary_span]` field").emit();
498 quote! { unreachable!(); }
501 SubdiagnosticKind::MultipartSuggestion { suggestion_kind, applicability } => {
502 let applicability = applicability
504 .map(|a| quote! { #a })
505 .or_else(|| self.applicability.take().value())
506 .unwrap_or_else(|| quote! { rustc_errors::Applicability::Unspecified });
508 if !self.has_suggestion_parts {
511 "multipart suggestion without any `#[suggestion_part(...)]` fields",
516 let style = suggestion_kind.to_suggestion_style();
518 quote! { #diag.#name(#message, suggestions, #applicability, #style); }
520 SubdiagnosticKind::Label => {
521 if let Some(span) = span_field {
522 quote! { #diag.#name(#span, #message); }
524 span_err(self.span, "label without `#[primary_span]` field").emit();
525 quote! { unreachable!(); }
529 if let Some(span) = span_field {
530 quote! { #diag.#name(#span, #message); }
532 quote! { #diag.#name(#message); }
540 let plain_args: TokenStream = self
544 .filter(|binding| binding.ast().attrs.is_empty())
545 .map(|binding| self.generate_field_set_arg(binding))
548 let formatting_init = &self.formatting_init;