1 #![deny(unused_must_use)]
3 use crate::diagnostics::error::{
4 span_err, throw_invalid_attr, throw_invalid_nested_attr, throw_span_err, DiagnosticDeriveError,
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,
10 use proc_macro2::TokenStream;
11 use quote::{format_ident, quote};
12 use std::collections::HashMap;
14 use std::str::FromStr;
15 use syn::{spanned::Spanned, Attribute, Meta, MetaList, MetaNameValue, NestedMeta, Path};
16 use synstructure::{BindingInfo, Structure, VariantInfo};
18 /// Which kind of suggestion is being created?
19 #[derive(Clone, Copy)]
20 enum SubdiagnosticSuggestionKind {
23 /// `#[suggestion_short]`
25 /// `#[suggestion_hidden]`
27 /// `#[suggestion_verbose]`
31 impl FromStr for SubdiagnosticSuggestionKind {
34 fn from_str(s: &str) -> Result<Self, Self::Err> {
36 "" => Ok(SubdiagnosticSuggestionKind::Normal),
37 "_short" => Ok(SubdiagnosticSuggestionKind::Short),
38 "_hidden" => Ok(SubdiagnosticSuggestionKind::Hidden),
39 "_verbose" => Ok(SubdiagnosticSuggestionKind::Verbose),
45 impl SubdiagnosticSuggestionKind {
46 pub fn to_suggestion_style(&self) -> TokenStream {
48 SubdiagnosticSuggestionKind::Normal => {
49 quote! { rustc_errors::SuggestionStyle::ShowCode }
51 SubdiagnosticSuggestionKind::Short => {
52 quote! { rustc_errors::SuggestionStyle::HideCodeInline }
54 SubdiagnosticSuggestionKind::Hidden => {
55 quote! { rustc_errors::SuggestionStyle::HideCodeAlways }
57 SubdiagnosticSuggestionKind::Verbose => {
58 quote! { rustc_errors::SuggestionStyle::ShowAlways }
64 /// Which kind of subdiagnostic is being created from a variant?
66 enum SubdiagnosticKind {
75 /// `#[suggestion{,_short,_hidden,_verbose}]`
76 Suggestion { suggestion_kind: SubdiagnosticSuggestionKind, code: TokenStream },
77 /// `#[multipart_suggestion{,_short,_hidden,_verbose}]`
78 MultipartSuggestion { suggestion_kind: SubdiagnosticSuggestionKind },
81 impl quote::IdentFragment for SubdiagnosticKind {
82 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
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")
95 fn span(&self) -> Option<proc_macro2::Span> {
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>,
106 impl<'a> SessionSubdiagnosticDerive<'a> {
107 pub(crate) fn new(structure: Structure<'a>) -> Self {
108 let diag = format_ident!("diag");
109 Self { structure, diag }
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();
118 syn::Data::Struct(..) | syn::Data::Enum(..) => (),
119 syn::Data::Union(..) => {
122 "`#[derive(SessionSubdiagnostic)]` can only be used on structs and enums",
127 if matches!(ast.data, syn::Data::Enum(..)) {
128 for attr in &ast.attrs {
130 attr.span().unwrap(),
131 "unsupported type attribute for subdiagnostic enum",
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 });
149 let mut builder = SessionSubdiagnosticDeriveBuilder {
156 has_suggestion_parts: false,
158 builder.into_tokens().unwrap_or_else(|v| v.to_compile_error())
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};
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,
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,
193 /// Store a map of field name to its corresponding field. This is built on construction of the
195 fields: HashMap<String, TokenStream>,
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)>,
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,
208 impl<'a> HasFieldMap for SessionSubdiagnosticDeriveBuilder<'a> {
209 fn get_field_binding(&self, field: &String) -> Option<&TokenStream> {
210 self.fields.get(field)
214 impl<'a> SessionSubdiagnosticDeriveBuilder<'a> {
217 ) -> Result<Option<(SubdiagnosticKind, Path)>, DiagnosticDeriveError> {
218 let mut kind_slug = None;
220 for attr in self.variant.ast().attrs {
221 let span = attr.span().unwrap();
223 let name = attr.path.segments.last().unwrap().ident.to_string();
224 let name = name.as_str();
226 let meta = attr.parse_meta()?;
227 let Meta::List(MetaList { ref nested, .. }) = meta else {
228 throw_invalid_attr!(attr, &meta);
231 let mut kind = match name {
232 "label" => SubdiagnosticKind::Label,
233 "note" => SubdiagnosticKind::Note,
234 "help" => SubdiagnosticKind::Help,
235 "warning" => SubdiagnosticKind::Warn,
237 if let Some(suggestion_kind) =
238 name.strip_prefix("suggestion").and_then(|s| s.parse().ok())
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())
244 SubdiagnosticKind::MultipartSuggestion { suggestion_kind }
246 throw_invalid_attr!(attr, &meta);
254 let mut nested_iter = nested.into_iter();
255 if let Some(nested_attr) = nested_iter.next() {
257 NestedMeta::Meta(Meta::Path(path)) => {
258 slug.set_once((path.clone(), span));
260 NestedMeta::Meta(meta @ Meta::NameValue(_))
262 meta.path().segments.last().unwrap().ident.to_string().as_str(),
263 "code" | "applicability"
266 // Don't error for valid follow-up attributes.
269 throw_invalid_nested_attr!(attr, &nested_attr, |diag| {
271 "first argument of the attribute should be the diagnostic \
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),
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();
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")
294 _ => throw_invalid_nested_attr!(attr, &nested_attr),
299 if matches!(kind, SubdiagnosticKind::Suggestion { .. }) {
300 let formatted_str = self.build_format(&value.value(), value.span());
301 code.set_once((formatted_str, span));
306 "`code` is not a valid nested attribute of a `{}` attribute",
316 SubdiagnosticKind::Suggestion { .. }
317 | SubdiagnosticKind::MultipartSuggestion { .. }
320 Applicability::from_str(&value.value()).unwrap_or_else(|()| {
321 span_err(span, "invalid applicability").emit();
322 Applicability::Unspecified
324 self.applicability.set_once((quote! { #value }, span));
329 "`applicability` is not a valid nested attribute of a `{}` attribute",
335 _ => throw_invalid_nested_attr!(attr, &nested_attr, |diag| {
336 diag.help("only `code` and `applicability` are valid nested attributes")
341 let Some((slug, _)) = slug else {
345 "diagnostic slug must be first argument of a `#[{}(...)]` attribute",
352 SubdiagnosticKind::Suggestion { code: ref mut code_field, .. } => {
353 let Some((code, _)) = code else {
354 throw_span_err!(span, "suggestion without `code = \"...\"`");
358 SubdiagnosticKind::Label
359 | SubdiagnosticKind::Note
360 | SubdiagnosticKind::Help
361 | SubdiagnosticKind::Warn
362 | SubdiagnosticKind::MultipartSuggestion { .. } => {}
365 kind_slug.set_once(((kind, slug), span))
368 Ok(kind_slug.map(|(kind_slug, _)| kind_slug))
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");
376 let diag = &self.diag;
377 let ident = ast.ident.as_ref().unwrap();
386 /// Generates the necessary code for all attributes on a field.
387 fn generate_field_attr_code(
389 binding: &BindingInfo<'_>,
390 kind: &SubdiagnosticKind,
392 let ast = binding.ast();
393 assert!(ast.attrs.len() > 0, "field without attributes generating attr code");
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);
401 let info = FieldInfo {
403 ty: inner_ty.inner_type().unwrap_or(&ast.ty),
408 .generate_field_code_inner(kind, attr, info)
409 .unwrap_or_else(|v| v.to_compile_error());
411 inner_ty.with(binding, generated)
416 fn generate_field_code_inner(
418 kind: &SubdiagnosticKind,
421 ) -> Result<TokenStream, DiagnosticDeriveError> {
422 let meta = attr.parse_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)
428 _ => throw_invalid_attr!(attr, &meta),
432 /// Generates the code for a `[Meta::Path]`-like attribute on a field (e.g. `#[primary_span]`).
433 fn generate_field_code_inner_path(
435 kind: &SubdiagnosticKind,
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();
446 "skip_arg" => Ok(quote! {}),
448 if matches!(kind, SubdiagnosticKind::MultipartSuggestion { .. }) {
449 throw_invalid_attr!(attr, &Meta::Path(path), |diag| {
451 "multipart suggestions use one or more `#[suggestion_part]`s rather \
452 than one `#[primary_span]`",
457 report_error_if_not_applied_to_span(attr, &info)?;
459 let binding = info.binding.binding.clone();
460 self.span_field.set_once((binding, span));
464 "suggestion_part" => {
465 self.has_suggestion_parts = true;
468 SubdiagnosticKind::MultipartSuggestion { .. } => {
471 "`#[suggestion_part(...)]` attribute without `code = \"...\"`",
476 SubdiagnosticKind::Label
477 | SubdiagnosticKind::Note
478 | SubdiagnosticKind::Help
479 | SubdiagnosticKind::Warn
480 | SubdiagnosticKind::Suggestion { .. } => {
481 throw_invalid_attr!(attr, &Meta::Path(path), |diag| {
483 "`#[suggestion_part(...)]` is only valid in multipart suggestions, use `#[primary_span]` instead",
490 if let SubdiagnosticKind::Suggestion { .. }
491 | SubdiagnosticKind::MultipartSuggestion { .. } = kind
493 report_error_if_not_applied_to_applicability(attr, &info)?;
495 let binding = info.binding.binding.clone();
496 self.applicability.set_once((quote! { #binding }, span));
498 span_err(span, "`#[applicability]` is only valid on suggestions").emit();
503 _ => throw_invalid_attr!(attr, &Meta::Path(path), |diag| {
504 let span_attr = if let SubdiagnosticKind::MultipartSuggestion { .. } = kind {
510 "only `{span_attr}`, `applicability` and `skip_arg` are valid field attributes",
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(
520 kind: &SubdiagnosticKind,
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();
531 "suggestion_part" => {
532 if !matches!(kind, SubdiagnosticKind::MultipartSuggestion { .. }) {
533 throw_invalid_attr!(attr, &Meta::List(list), |diag| {
535 "`#[suggestion_part(...)]` is only valid in multipart suggestions",
540 self.has_suggestion_parts = true;
542 report_error_if_not_applied_to_span(attr, &info)?;
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);
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();
554 let Meta::NameValue(MetaNameValue { lit: syn::Lit::Str(value), .. }) = meta else {
555 throw_invalid_nested_attr!(attr, &nested_attr);
560 let formatted_str = self.build_format(&value.value(), value.span());
561 code.set_once((formatted_str, span));
563 _ => throw_invalid_nested_attr!(attr, &nested_attr, |diag| {
564 diag.help("`code` is the only valid nested attribute")
569 let Some((code, _)) = code else {
570 span_err(span, "`#[suggestion_part(...)]` attribute without `code = \"...\"`")
572 return Ok(quote! {});
574 let binding = info.binding;
576 Ok(quote! { suggestions.push((#binding, #code)); })
578 _ => throw_invalid_attr!(attr, &Meta::List(list), |diag| {
579 let span_attr = if let SubdiagnosticKind::MultipartSuggestion { .. } = kind {
585 "only `{span_attr}`, `applicability` and `skip_arg` are valid field attributes",
591 pub fn into_tokens(&mut self) -> Result<TokenStream, DiagnosticDeriveError> {
592 let Some((kind, slug)) = self.identify_kind()? else {
594 self.variant.ast().ident.span().unwrap(),
595 "subdiagnostic kind not specified"
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(); }
610 let attr_args: TokenStream = self
614 .filter(|binding| !binding.ast().attrs.is_empty())
615 .map(|binding| self.generate_field_attr_code(binding, &kind))
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,
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();
632 quote! { #diag.#name(#span, #message, #code, #applicability, #style); }
634 span_err(self.span, "suggestion without `#[primary_span]` field").emit();
635 quote! { unreachable!(); }
638 SubdiagnosticKind::MultipartSuggestion { suggestion_kind } => {
639 if !self.has_suggestion_parts {
642 "multipart suggestion without any `#[suggestion_part(...)]` fields",
647 let style = suggestion_kind.to_suggestion_style();
649 quote! { #diag.#name(#message, suggestions, #applicability, #style); }
651 SubdiagnosticKind::Label => {
652 if let Some(span) = span_field {
653 quote! { #diag.#name(#span, #message); }
655 span_err(self.span, "label without `#[primary_span]` field").emit();
656 quote! { unreachable!(); }
660 if let Some(span) = span_field {
661 quote! { #diag.#name(#span, #message); }
663 quote! { #diag.#name(#message); }
668 let plain_args: TokenStream = self
672 .filter(|binding| binding.ast().attrs.is_empty())
673 .map(|binding| self.generate_field_set_arg(binding))