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, is_doc_comment, new_code_ident,
9 report_error_if_not_applied_to_applicability, report_error_if_not_applied_to_span, FieldInfo,
10 FieldInnerTy, FieldMap, HasFieldMap, SetOnce, 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 {
46 // Always allow documentation comments.
47 if is_doc_comment(attr) {
53 "unsupported type attribute for subdiagnostic enum",
59 structure.bind_with(|_| synstructure::BindStyle::Move);
60 let variants_ = structure.each_variant(|variant| {
61 let mut builder = SubdiagnosticDeriveVariantBuilder {
65 formatting_init: TokenStream::new(),
66 fields: build_field_mapping(variant),
69 has_suggestion_parts: false,
71 builder.into_tokens().unwrap_or_else(|v| v.to_compile_error())
81 let diag = &self.diag;
83 let ret = structure.gen_impl(quote! {
84 gen impl rustc_errors::AddToDiagnostic for @Self {
85 fn add_to_diagnostic_with<__F>(self, #diag: &mut rustc_errors::Diagnostic, #f: __F)
88 &mut rustc_errors::Diagnostic,
89 rustc_errors::SubdiagnosticMessage
90 ) -> rustc_errors::SubdiagnosticMessage,
92 use rustc_errors::{Applicability, IntoDiagnosticArg};
101 /// Tracks persistent information required for building up the call to add to the diagnostic
102 /// for the final generated method. This is a separate struct to `SubdiagnosticDerive`
103 /// only to be able to destructure and split `self.builder` and the `self.structure` up to avoid a
104 /// double mut borrow later on.
105 struct SubdiagnosticDeriveVariantBuilder<'parent, 'a> {
106 /// The identifier to use for the generated `DiagnosticBuilder` instance.
107 parent: &'parent SubdiagnosticDeriveBuilder,
109 /// Info for the current variant (or the type if not an enum).
110 variant: &'a VariantInfo<'a>,
111 /// Span for the entire type.
112 span: proc_macro::Span,
114 /// Initialization of format strings for code suggestions.
115 formatting_init: TokenStream,
117 /// Store a map of field name to its corresponding field. This is built on construction of the
121 /// Identifier for the binding to the `#[primary_span]` field.
122 span_field: SpannedOption<proc_macro2::Ident>,
124 /// The binding to the `#[applicability]` field, if present.
125 applicability: SpannedOption<TokenStream>,
127 /// Set to true when a `#[suggestion_part]` field is encountered, used to generate an error
128 /// during finalization if still `false`.
129 has_suggestion_parts: bool,
132 impl<'parent, 'a> HasFieldMap for SubdiagnosticDeriveVariantBuilder<'parent, 'a> {
133 fn get_field_binding(&self, field: &String) -> Option<&TokenStream> {
134 self.fields.get(field)
138 /// Provides frequently-needed information about the diagnostic kinds being derived for this type.
139 #[derive(Clone, Copy, Debug)]
140 struct KindsStatistics {
141 has_multipart_suggestion: bool,
142 all_multipart_suggestions: bool,
143 has_normal_suggestion: bool,
144 all_applicabilities_static: bool,
147 impl<'a> FromIterator<&'a SubdiagnosticKind> for KindsStatistics {
148 fn from_iter<T: IntoIterator<Item = &'a SubdiagnosticKind>>(kinds: T) -> Self {
150 has_multipart_suggestion: false,
151 all_multipart_suggestions: true,
152 has_normal_suggestion: false,
153 all_applicabilities_static: true,
157 if let SubdiagnosticKind::MultipartSuggestion { applicability: None, .. }
158 | SubdiagnosticKind::Suggestion { applicability: None, .. } = kind
160 ret.all_applicabilities_static = false;
162 if let SubdiagnosticKind::MultipartSuggestion { .. } = kind {
163 ret.has_multipart_suggestion = true;
165 ret.all_multipart_suggestions = false;
168 if let SubdiagnosticKind::Suggestion { .. } = kind {
169 ret.has_normal_suggestion = true;
176 impl<'parent, 'a> SubdiagnosticDeriveVariantBuilder<'parent, 'a> {
177 fn identify_kind(&mut self) -> Result<Vec<(SubdiagnosticKind, Path)>, DiagnosticDeriveError> {
178 let mut kind_slugs = vec![];
180 for attr in self.variant.ast().attrs {
181 let Some((kind, slug)) = SubdiagnosticKind::from_attr(attr, self)? else {
182 // Some attributes aren't errors - like documentation comments - but also aren't
187 let Some(slug) = slug else {
188 let name = attr.path.segments.last().unwrap().ident.to_string();
189 let name = name.as_str();
192 attr.span().unwrap(),
194 "diagnostic slug must be first argument of a `#[{}(...)]` attribute",
200 kind_slugs.push((kind, slug));
206 /// Generates the code for a field with no attributes.
207 fn generate_field_set_arg(&mut self, binding: &BindingInfo<'_>) -> TokenStream {
208 let ast = binding.ast();
209 assert_eq!(ast.attrs.len(), 0, "field with attribute used as diagnostic arg");
211 let diag = &self.parent.diag;
212 let ident = ast.ident.as_ref().unwrap();
213 // strip `r#` prefix, if present
214 let ident = format_ident!("{}", ident);
224 /// Generates the necessary code for all attributes on a field.
225 fn generate_field_attr_code(
227 binding: &BindingInfo<'_>,
228 kind_stats: KindsStatistics,
230 let ast = binding.ast();
231 assert!(ast.attrs.len() > 0, "field without attributes generating attr code");
233 // Abstract over `Vec<T>` and `Option<T>` fields using `FieldInnerTy`, which will
234 // apply the generated code on each element in the `Vec` or `Option`.
235 let inner_ty = FieldInnerTy::from_type(&ast.ty);
239 // Always allow documentation comments.
240 if is_doc_comment(attr) {
244 let info = FieldInfo {
246 ty: inner_ty.inner_type().unwrap_or(&ast.ty),
251 .generate_field_code_inner(kind_stats, attr, info, inner_ty.will_iterate())
252 .unwrap_or_else(|v| v.to_compile_error());
254 inner_ty.with(binding, generated)
259 fn generate_field_code_inner(
261 kind_stats: KindsStatistics,
264 clone_suggestion_code: bool,
265 ) -> Result<TokenStream, DiagnosticDeriveError> {
266 let meta = attr.parse_meta()?;
268 Meta::Path(path) => self.generate_field_code_inner_path(kind_stats, attr, info, path),
269 Meta::List(list @ MetaList { .. }) => self.generate_field_code_inner_list(
274 clone_suggestion_code,
276 _ => throw_invalid_attr!(attr, &meta),
280 /// Generates the code for a `[Meta::Path]`-like attribute on a field (e.g. `#[primary_span]`).
281 fn generate_field_code_inner_path(
283 kind_stats: KindsStatistics,
287 ) -> Result<TokenStream, DiagnosticDeriveError> {
288 let span = attr.span().unwrap();
289 let ident = &path.segments.last().unwrap().ident;
290 let name = ident.to_string();
291 let name = name.as_str();
294 "skip_arg" => Ok(quote! {}),
296 if kind_stats.has_multipart_suggestion {
297 invalid_attr(attr, &Meta::Path(path))
299 "multipart suggestions use one or more `#[suggestion_part]`s rather \
300 than one `#[primary_span]`",
304 report_error_if_not_applied_to_span(attr, &info)?;
306 let binding = info.binding.binding.clone();
307 self.span_field.set_once(binding, span);
312 "suggestion_part" => {
313 self.has_suggestion_parts = true;
315 if kind_stats.has_multipart_suggestion {
316 span_err(span, "`#[suggestion_part(...)]` attribute without `code = \"...\"`")
319 invalid_attr(attr, &Meta::Path(path))
321 "`#[suggestion_part(...)]` is only valid in multipart suggestions, \
322 use `#[primary_span]` instead",
330 if kind_stats.has_multipart_suggestion || kind_stats.has_normal_suggestion {
331 report_error_if_not_applied_to_applicability(attr, &info)?;
333 if kind_stats.all_applicabilities_static {
336 "`#[applicability]` has no effect if all `#[suggestion]`/\
337 `#[multipart_suggestion]` attributes have a static \
338 `applicability = \"...\"`",
342 let binding = info.binding.binding.clone();
343 self.applicability.set_once(quote! { #binding }, span);
345 span_err(span, "`#[applicability]` is only valid on suggestions").emit();
351 let mut span_attrs = vec![];
352 if kind_stats.has_multipart_suggestion {
353 span_attrs.push("suggestion_part");
355 if !kind_stats.all_multipart_suggestions {
356 span_attrs.push("primary_span")
359 invalid_attr(attr, &Meta::Path(path))
361 "only `{}`, `applicability` and `skip_arg` are valid field attributes",
362 span_attrs.join(", ")
371 /// Generates the code for a `[Meta::List]`-like attribute on a field (e.g.
372 /// `#[suggestion_part(code = "...")]`).
373 fn generate_field_code_inner_list(
375 kind_stats: KindsStatistics,
379 clone_suggestion_code: bool,
380 ) -> Result<TokenStream, DiagnosticDeriveError> {
381 let span = attr.span().unwrap();
382 let ident = &list.path.segments.last().unwrap().ident;
383 let name = ident.to_string();
384 let name = name.as_str();
387 "suggestion_part" => {
388 if !kind_stats.has_multipart_suggestion {
389 throw_invalid_attr!(attr, &Meta::List(list), |diag| {
391 "`#[suggestion_part(...)]` is only valid in multipart suggestions",
396 self.has_suggestion_parts = true;
398 report_error_if_not_applied_to_span(attr, &info)?;
401 for nested_attr in list.nested.iter() {
402 let NestedMeta::Meta(ref meta) = nested_attr else {
403 throw_invalid_nested_attr!(attr, &nested_attr);
406 let span = meta.span().unwrap();
407 let nested_name = meta.path().segments.last().unwrap().ident.to_string();
408 let nested_name = nested_name.as_str();
410 let Meta::NameValue(MetaNameValue { lit: syn::Lit::Str(value), .. }) = meta else {
411 throw_invalid_nested_attr!(attr, &nested_attr);
416 let formatted_str = self.build_format(&value.value(), value.span());
417 let code_field = new_code_ident();
418 code.set_once((code_field, formatted_str), span);
420 _ => throw_invalid_nested_attr!(attr, &nested_attr, |diag| {
421 diag.help("`code` is the only valid nested attribute")
426 let Some((code_field, formatted_str)) = code.value() else {
427 span_err(span, "`#[suggestion_part(...)]` attribute without `code = \"...\"`")
429 return Ok(quote! {});
431 let binding = info.binding;
433 self.formatting_init.extend(quote! { let #code_field = #formatted_str; });
434 let code_field = if clone_suggestion_code {
435 quote! { #code_field.clone() }
437 quote! { #code_field }
439 Ok(quote! { suggestions.push((#binding, #code_field)); })
441 _ => throw_invalid_attr!(attr, &Meta::List(list), |diag| {
442 let mut span_attrs = vec![];
443 if kind_stats.has_multipart_suggestion {
444 span_attrs.push("suggestion_part");
446 if !kind_stats.all_multipart_suggestions {
447 span_attrs.push("primary_span")
450 "only `{}`, `applicability` and `skip_arg` are valid field attributes",
451 span_attrs.join(", ")
457 pub fn into_tokens(&mut self) -> Result<TokenStream, DiagnosticDeriveError> {
458 let kind_slugs = self.identify_kind()?;
459 if kind_slugs.is_empty() {
461 self.variant.ast().ident.span().unwrap(),
462 "subdiagnostic kind not specified"
466 let kind_stats: KindsStatistics = kind_slugs.iter().map(|(kind, _slug)| kind).collect();
468 let init = if kind_stats.has_multipart_suggestion {
469 quote! { let mut suggestions = Vec::new(); }
474 let attr_args: TokenStream = self
478 .filter(|binding| !binding.ast().attrs.is_empty())
479 .map(|binding| self.generate_field_attr_code(binding, kind_stats))
482 let span_field = self.span_field.value_ref();
484 let diag = &self.parent.diag;
485 let f = &self.parent.f;
486 let mut calls = TokenStream::new();
487 for (kind, slug) in kind_slugs {
488 let message = format_ident!("__message");
489 calls.extend(quote! { let #message = #f(#diag, rustc_errors::fluent::#slug.into()); });
491 let name = format_ident!("{}{}", if span_field.is_some() { "span_" } else { "" }, kind);
492 let call = match kind {
493 SubdiagnosticKind::Suggestion {
499 self.formatting_init.extend(code_init);
501 let applicability = applicability
503 .map(|a| quote! { #a })
504 .or_else(|| self.applicability.take().value())
505 .unwrap_or_else(|| quote! { rustc_errors::Applicability::Unspecified });
507 if let Some(span) = span_field {
508 let style = suggestion_kind.to_suggestion_style();
509 quote! { #diag.#name(#span, #message, #code_field, #applicability, #style); }
511 span_err(self.span, "suggestion without `#[primary_span]` field").emit();
512 quote! { unreachable!(); }
515 SubdiagnosticKind::MultipartSuggestion { suggestion_kind, applicability } => {
516 let applicability = applicability
518 .map(|a| quote! { #a })
519 .or_else(|| self.applicability.take().value())
520 .unwrap_or_else(|| quote! { rustc_errors::Applicability::Unspecified });
522 if !self.has_suggestion_parts {
525 "multipart suggestion without any `#[suggestion_part(...)]` fields",
530 let style = suggestion_kind.to_suggestion_style();
532 quote! { #diag.#name(#message, suggestions, #applicability, #style); }
534 SubdiagnosticKind::Label => {
535 if let Some(span) = span_field {
536 quote! { #diag.#name(#span, #message); }
538 span_err(self.span, "label without `#[primary_span]` field").emit();
539 quote! { unreachable!(); }
543 if let Some(span) = span_field {
544 quote! { #diag.#name(#span, #message); }
546 quote! { #diag.#name(#message); }
554 let plain_args: TokenStream = self
558 .filter(|binding| binding.ast().attrs.is_empty())
559 .map(|binding| self.generate_field_set_arg(binding))
562 let formatting_init = &self.formatting_init;