From 64d98f8ee037282c35007b64c2649055c56af1db Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:19:03 +0200 Subject: Merging upstream version 1.68.2+dfsg1. Signed-off-by: Daniel Baumann --- .../rustc_macros/src/diagnostics/diagnostic.rs | 14 ++---- .../src/diagnostics/diagnostic_builder.rs | 53 +++++++++------------- compiler/rustc_macros/src/diagnostics/error.rs | 16 +++---- compiler/rustc_macros/src/diagnostics/fluent.rs | 4 +- .../rustc_macros/src/diagnostics/subdiagnostic.rs | 5 +- compiler/rustc_macros/src/diagnostics/utils.rs | 10 ++-- 6 files changed, 42 insertions(+), 60 deletions(-) (limited to 'compiler/rustc_macros/src/diagnostics') diff --git a/compiler/rustc_macros/src/diagnostics/diagnostic.rs b/compiler/rustc_macros/src/diagnostics/diagnostic.rs index 684835d8c..9ff944864 100644 --- a/compiler/rustc_macros/src/diagnostics/diagnostic.rs +++ b/compiler/rustc_macros/src/diagnostics/diagnostic.rs @@ -39,10 +39,8 @@ impl<'a> DiagnosticDerive<'a> { let init = match builder.slug.value_ref() { None => { span_err(builder.span, "diagnostic slug not specified") - .help(format!( - "specify the slug as the first argument to the `#[diag(...)]` \ - attribute, such as `#[diag(hir_analysis_example_error)]`", - )) + .help("specify the slug as the first argument to the `#[diag(...)]` \ + attribute, such as `#[diag(hir_analysis_example_error)]`") .emit(); return DiagnosticDeriveError::ErrorHandled.to_compile_error(); } @@ -133,10 +131,8 @@ impl<'a> LintDiagnosticDerive<'a> { match builder.slug.value_ref() { None => { span_err(builder.span, "diagnostic slug not specified") - .help(format!( - "specify the slug as the first argument to the attribute, such as \ - `#[diag(compiletest_example)]`", - )) + .help("specify the slug as the first argument to the attribute, such as \ + `#[diag(compiletest_example)]`") .emit(); DiagnosticDeriveError::ErrorHandled.to_compile_error() } @@ -192,7 +188,7 @@ impl Mismatch { let crate_name = std::env::var("CARGO_CRATE_NAME").ok()?; // If we're not in a "rustc_" crate, bail. - let Some(("rustc", slug_prefix)) = crate_name.split_once("_") else { return None }; + let Some(("rustc", slug_prefix)) = crate_name.split_once('_') else { return None }; let slug_name = slug.segments.first()?.ident.to_string(); if !slug_name.starts_with(slug_prefix) { diff --git a/compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs b/compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs index 9f2ac5112..e405462bb 100644 --- a/compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs +++ b/compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs @@ -372,45 +372,36 @@ impl<'a> DiagnosticDeriveVariantBuilder<'a> { } } (Meta::Path(_), "subdiagnostic") => { - return Ok(quote! { #diag.subdiagnostic(#binding); }); - } - (Meta::NameValue(_), "subdiagnostic") => { - throw_invalid_attr!(attr, &meta, |diag| { - diag.help("`eager` is the only supported nested attribute for `subdiagnostic`") - }) + if FieldInnerTy::from_type(&info.binding.ast().ty).will_iterate() { + let DiagnosticDeriveKind::Diagnostic { handler } = &self.parent.kind else { + // No eager translation for lints. + return Ok(quote! { #diag.subdiagnostic(#binding); }); + }; + return Ok(quote! { #diag.eager_subdiagnostic(#handler, #binding); }); + } else { + return Ok(quote! { #diag.subdiagnostic(#binding); }); + } } (Meta::List(MetaList { ref nested, .. }), "subdiagnostic") => { - if nested.len() != 1 { + if nested.len() == 1 + && let Some(NestedMeta::Meta(Meta::Path(path))) = nested.first() + && path.is_ident("eager") { + let handler = match &self.parent.kind { + DiagnosticDeriveKind::Diagnostic { handler } => handler, + DiagnosticDeriveKind::LintDiagnostic => { + throw_invalid_attr!(attr, &meta, |diag| { + diag.help("eager subdiagnostics are not supported on lints") + }) + } + }; + return Ok(quote! { #diag.eager_subdiagnostic(#handler, #binding); }); + } else { throw_invalid_attr!(attr, &meta, |diag| { diag.help( "`eager` is the only supported nested attribute for `subdiagnostic`", ) }) } - - let handler = match &self.parent.kind { - DiagnosticDeriveKind::Diagnostic { handler } => handler, - DiagnosticDeriveKind::LintDiagnostic => { - throw_invalid_attr!(attr, &meta, |diag| { - diag.help("eager subdiagnostics are not supported on lints") - }) - } - }; - - let nested_attr = nested.first().expect("pop failed for single element list"); - match nested_attr { - NestedMeta::Meta(meta @ Meta::Path(_)) - if meta.path().segments.last().unwrap().ident.to_string().as_str() - == "eager" => - { - return Ok(quote! { #diag.eager_subdiagnostic(#handler, #binding); }); - } - _ => { - throw_invalid_nested_attr!(attr, nested_attr, |diag| { - diag.help("`eager` is the only supported nested attribute for `subdiagnostic`") - }) - } - } } _ => (), } diff --git a/compiler/rustc_macros/src/diagnostics/error.rs b/compiler/rustc_macros/src/diagnostics/error.rs index 4612f54e4..2d62d5931 100644 --- a/compiler/rustc_macros/src/diagnostics/error.rs +++ b/compiler/rustc_macros/src/diagnostics/error.rs @@ -76,11 +76,11 @@ pub(crate) fn invalid_attr(attr: &Attribute, meta: &Meta) -> Diagnostic { let span = attr.span().unwrap(); let path = path_to_string(&attr.path); match meta { - Meta::Path(_) => span_err(span, &format!("`#[{}]` is not a valid attribute", path)), + Meta::Path(_) => span_err(span, &format!("`#[{path}]` is not a valid attribute")), Meta::NameValue(_) => { - span_err(span, &format!("`#[{} = ...]` is not a valid attribute", path)) + span_err(span, &format!("`#[{path} = ...]` is not a valid attribute")) } - Meta::List(_) => span_err(span, &format!("`#[{}(...)]` is not a valid attribute", path)), + Meta::List(_) => span_err(span, &format!("`#[{path}(...)]` is not a valid attribute")), } } @@ -107,7 +107,7 @@ pub(crate) fn invalid_nested_attr(attr: &Attribute, nested: &NestedMeta) -> Diag let meta = match nested { syn::NestedMeta::Meta(meta) => meta, syn::NestedMeta::Lit(_) => { - return span_err(span, &format!("`#[{}(\"...\")]` is not a valid attribute", name)); + return span_err(span, &format!("`#[{name}(\"...\")]` is not a valid attribute")); } }; @@ -115,13 +115,11 @@ pub(crate) fn invalid_nested_attr(attr: &Attribute, nested: &NestedMeta) -> Diag let path = path_to_string(meta.path()); match meta { Meta::NameValue(..) => { - span_err(span, &format!("`#[{}({} = ...)]` is not a valid attribute", name, path)) - } - Meta::Path(..) => { - span_err(span, &format!("`#[{}({})]` is not a valid attribute", name, path)) + span_err(span, &format!("`#[{name}({path} = ...)]` is not a valid attribute")) } + Meta::Path(..) => span_err(span, &format!("`#[{name}({path})]` is not a valid attribute")), Meta::List(..) => { - span_err(span, &format!("`#[{}({}(...))]` is not a valid attribute", name, path)) + span_err(span, &format!("`#[{name}({path}(...))]` is not a valid attribute")) } } } diff --git a/compiler/rustc_macros/src/diagnostics/fluent.rs b/compiler/rustc_macros/src/diagnostics/fluent.rs index 3e447c94e..32338f9df 100644 --- a/compiler/rustc_macros/src/diagnostics/fluent.rs +++ b/compiler/rustc_macros/src/diagnostics/fluent.rs @@ -178,7 +178,7 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok opt: Default::default(), }; let dl = DisplayList::from(snippet); - eprintln!("{}\n", dl); + eprintln!("{dl}\n"); } continue; } @@ -265,7 +265,7 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok Diagnostic::spanned( path_span, Level::Error, - format!("overrides existing {}: `{}`", kind, id), + format!("overrides existing {kind}: `{id}`"), ) .span_help(previous_defns[&id], "previously defined in this resource") .emit(); diff --git a/compiler/rustc_macros/src/diagnostics/subdiagnostic.rs b/compiler/rustc_macros/src/diagnostics/subdiagnostic.rs index 446aebe4f..baffd3cec 100644 --- a/compiler/rustc_macros/src/diagnostics/subdiagnostic.rs +++ b/compiler/rustc_macros/src/diagnostics/subdiagnostic.rs @@ -29,7 +29,7 @@ impl SubdiagnosticDeriveBuilder { Self { diag, f } } - pub(crate) fn into_tokens<'a>(self, mut structure: Structure<'a>) -> TokenStream { + pub(crate) fn into_tokens(self, mut structure: Structure<'_>) -> TokenStream { let implementation = { let ast = structure.ast(); let span = ast.span().unwrap(); @@ -198,8 +198,7 @@ impl<'parent, 'a> SubdiagnosticDeriveVariantBuilder<'parent, 'a> { throw_span_err!( attr.span().unwrap(), &format!( - "diagnostic slug must be first argument of a `#[{}(...)]` attribute", - name + "diagnostic slug must be first argument of a `#[{name}(...)]` attribute" ) ); }; diff --git a/compiler/rustc_macros/src/diagnostics/utils.rs b/compiler/rustc_macros/src/diagnostics/utils.rs index da9023352..6f52a3de1 100644 --- a/compiler/rustc_macros/src/diagnostics/utils.rs +++ b/compiler/rustc_macros/src/diagnostics/utils.rs @@ -322,7 +322,7 @@ pub(crate) trait HasFieldMap { None => { span_err( span.unwrap(), - &format!("`{}` doesn't refer to a field on this type", field), + &format!("`{field}` doesn't refer to a field on this type"), ) .emit(); quote! { @@ -385,7 +385,7 @@ impl quote::ToTokens for Applicability { /// Build the mapping of field names to fields. This allows attributes to peek values from /// other fields. -pub(super) fn build_field_mapping<'v>(variant: &VariantInfo<'v>) -> HashMap { +pub(super) fn build_field_mapping(variant: &VariantInfo<'_>) -> HashMap { let mut fields_map = FieldMap::new(); for binding in variant.bindings() { if let Some(ident) = &binding.ast().ident { @@ -603,8 +603,7 @@ impl SubdiagnosticKind { if suggestion_kind != SuggestionKind::Normal { invalid_attr(attr, &meta) .help(format!( - r#"Use `#[suggestion(..., style = "{}")]` instead"#, - suggestion_kind + r#"Use `#[suggestion(..., style = "{suggestion_kind}")]` instead"# )) .emit(); } @@ -621,8 +620,7 @@ impl SubdiagnosticKind { if suggestion_kind != SuggestionKind::Normal { invalid_attr(attr, &meta) .help(format!( - r#"Use `#[multipart_suggestion(..., style = "{}")]` instead"#, - suggestion_kind + r#"Use `#[multipart_suggestion(..., style = "{suggestion_kind}")]` instead"# )) .emit(); } -- cgit v1.2.3