From 77bbbfdb59c24448126c4681784c682c60015c34 Mon Sep 17 00:00:00 2001 From: Lucas Sunsi Abreu Date: Sun, 23 Aug 2026 07:50:43 -0300 Subject: [PATCH 1/2] Add reproduction test --- .../derive-attribute-annotation-needed.rs | 36 +++++++++++++++++++ .../derive-attribute-annotation-needed.rs | 21 +++++++++++ .../derive-attribute-annotation-needed.stderr | 25 +++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 tests/ui/inference/need_type_info/auxiliary/derive-attribute-annotation-needed.rs create mode 100644 tests/ui/inference/need_type_info/derive-attribute-annotation-needed.rs create mode 100644 tests/ui/inference/need_type_info/derive-attribute-annotation-needed.stderr diff --git a/tests/ui/inference/need_type_info/auxiliary/derive-attribute-annotation-needed.rs b/tests/ui/inference/need_type_info/auxiliary/derive-attribute-annotation-needed.rs new file mode 100644 index 0000000000000..27f5a4c1f2475 --- /dev/null +++ b/tests/ui/inference/need_type_info/auxiliary/derive-attribute-annotation-needed.rs @@ -0,0 +1,36 @@ +//@ edition: 2018 +#![feature(proc_macro_quote)] +use proc_macro::{TokenStream, Ident, TokenTree, quote}; + +#[proc_macro_derive(Serialize, attributes(serde))] +pub fn serialize(input: TokenStream) -> TokenStream { + assert_eq!( + input.to_string(), + "pub struct Matrix { #[serde(serialize_with = \"f\")] matrix: (), }" + ); + + let TokenTree::Group(group) = input.into_iter().last().unwrap() else { + panic!("unexpected group"); + }; + + let TokenTree::Group(group) = group.stream().into_iter().nth(1).unwrap() else { + panic!("expected group"); + }; + + let TokenTree::Group(group) = group.stream().into_iter().nth(1).unwrap() else { + panic!("expected group"); + }; + + let TokenTree::Literal(lit) = group.stream().into_iter().nth(2).unwrap() else { + panic!("expected literal"); + }; + + let ident = Ident::new("f", lit.span()); + + quote! { + fn serialize() { + $ident(); + } + } + .into() +} diff --git a/tests/ui/inference/need_type_info/derive-attribute-annotation-needed.rs b/tests/ui/inference/need_type_info/derive-attribute-annotation-needed.rs new file mode 100644 index 0000000000000..35771bc6aff55 --- /dev/null +++ b/tests/ui/inference/need_type_info/derive-attribute-annotation-needed.rs @@ -0,0 +1,21 @@ +//@ proc-macro: derive-attribute-annotation-needed.rs +//@ edition: 2018 + + +use derive_attribute_annotation_needed::Serialize; + +fn f() {} + +#[derive(Serialize)] +pub struct Matrix { + #[serde(serialize_with = "f")] + //~^ ERROR type annotations needed + //~| HELP consider specifying a concrete type for the type parameter `T` + matrix: (), +} + +fn main() { + f(); + //~^ ERROR type annotations needed + //~| HELP consider specifying a concrete type for the type parameter `T` +} diff --git a/tests/ui/inference/need_type_info/derive-attribute-annotation-needed.stderr b/tests/ui/inference/need_type_info/derive-attribute-annotation-needed.stderr new file mode 100644 index 0000000000000..975eb3d0e73c0 --- /dev/null +++ b/tests/ui/inference/need_type_info/derive-attribute-annotation-needed.stderr @@ -0,0 +1,25 @@ +error[E0282]: type annotations needed + --> $DIR/derive-attribute-annotation-needed.rs:11:30 + | +LL | #[serde(serialize_with = "f")] + | ^^^ cannot infer type of the type parameter `T` declared on the function `f` + | +help: consider specifying a concrete type for the type parameter `T` + | +LL | #[serde(serialize_with = "f"::)] + | ++++++++++++++ + +error[E0282]: type annotations needed + --> $DIR/derive-attribute-annotation-needed.rs:18:5 + | +LL | f(); + | ^ cannot infer type of the type parameter `T` declared on the function `f` + | +help: consider specifying a concrete type for the type parameter `T` + | +LL | f::(); + | ++++++++++++++ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0282`. From 3d5f17362dd996c8ff722cc2ac2fdf3b91b683d5 Mon Sep 17 00:00:00 2001 From: Lucas Sunsi Abreu Date: Thu, 20 Aug 2026 16:12:53 -0300 Subject: [PATCH 2/2] Add condition to avoid suggestion on expansion --- .../src/error_reporting/infer/need_type_info.rs | 6 +++++- .../need_type_info/derive-attribute-annotation-needed.rs | 1 - .../derive-attribute-annotation-needed.stderr | 7 +------ 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs index 903c3998fd4b6..38c6be6069e9a 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs @@ -519,11 +519,13 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { }; let mut local_visitor = FindInferSourceVisitor::new(self, typeck_results, term, ty); + let mut body_from_expansion = false; if let Some(body) = self.tcx.hir_maybe_body_owned_by(self.tcx.typeck_root_def_id_local(body_def_id)) { let expr = body.value; local_visitor.visit_expr(expr); + body_from_expansion = body.value.span.from_expansion(); } let Some(InferSource { span, kind }) = local_visitor.infer_source else { @@ -566,6 +568,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { term, &arg_data, typeck_results, + body_from_expansion, span, ); @@ -705,6 +708,7 @@ impl<'tcx> InferSourceKind<'tcx> { term: Term<'tcx>, arg_data: &'local InferenceDiagnosticsData, typeck_results: &TypeckResults<'tcx>, + body_from_expansion: bool, span: Span, ) -> Option> where @@ -799,7 +803,7 @@ impl<'tcx> InferSourceKind<'tcx> { p.into_buffer() }; - let suggestion = if have_turbofish { + let suggestion = if have_turbofish || body_from_expansion { None } else if generic_args.len() == 1 && used_fallback { match param.kind { diff --git a/tests/ui/inference/need_type_info/derive-attribute-annotation-needed.rs b/tests/ui/inference/need_type_info/derive-attribute-annotation-needed.rs index 35771bc6aff55..1ac00dbcbd4b1 100644 --- a/tests/ui/inference/need_type_info/derive-attribute-annotation-needed.rs +++ b/tests/ui/inference/need_type_info/derive-attribute-annotation-needed.rs @@ -10,7 +10,6 @@ fn f() {} pub struct Matrix { #[serde(serialize_with = "f")] //~^ ERROR type annotations needed - //~| HELP consider specifying a concrete type for the type parameter `T` matrix: (), } diff --git a/tests/ui/inference/need_type_info/derive-attribute-annotation-needed.stderr b/tests/ui/inference/need_type_info/derive-attribute-annotation-needed.stderr index 975eb3d0e73c0..ebffce7148672 100644 --- a/tests/ui/inference/need_type_info/derive-attribute-annotation-needed.stderr +++ b/tests/ui/inference/need_type_info/derive-attribute-annotation-needed.stderr @@ -3,14 +3,9 @@ error[E0282]: type annotations needed | LL | #[serde(serialize_with = "f")] | ^^^ cannot infer type of the type parameter `T` declared on the function `f` - | -help: consider specifying a concrete type for the type parameter `T` - | -LL | #[serde(serialize_with = "f"::)] - | ++++++++++++++ error[E0282]: type annotations needed - --> $DIR/derive-attribute-annotation-needed.rs:18:5 + --> $DIR/derive-attribute-annotation-needed.rs:17:5 | LL | f(); | ^ cannot infer type of the type parameter `T` declared on the function `f`