From 4d47b07353e2d5014f08dc5ff9b913c1df1e2c58 Mon Sep 17 00:00:00 2001 From: arferreira Date: Wed, 11 Mar 2026 22:39:51 -0400 Subject: [PATCH 1/2] Detect existing turbofish on method calls to suppress useless suggestion --- .../error_reporting/infer/need_type_info.rs | 5 +++- .../inference/useless-turbofish-suggestion.rs | 28 +++++++++++++++++++ .../useless-turbofish-suggestion.stderr | 20 +++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 tests/ui/inference/useless-turbofish-suggestion.rs create mode 100644 tests/ui/inference/useless-turbofish-suggestion.stderr 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 80e97d36c23f0..629bd30d88e8e 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 @@ -1030,12 +1030,15 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> { let args = self.node_args_opt(expr.hir_id)?; let span = tcx.hir_span(segment.hir_id); let insert_span = segment.ident.span.shrink_to_hi().with_hi(span.hi()); + let have_turbofish = segment.args.is_some_and(|args| { + args.args.iter().any(|arg| arg.is_ty_or_const()) + }); InsertableGenericArgs { insert_span, args, generics_def_id: def_id, def_id, - have_turbofish: false, + have_turbofish, } }; return Box::new(insertable.into_iter()); diff --git a/tests/ui/inference/useless-turbofish-suggestion.rs b/tests/ui/inference/useless-turbofish-suggestion.rs new file mode 100644 index 0000000000000..ed86df6296cad --- /dev/null +++ b/tests/ui/inference/useless-turbofish-suggestion.rs @@ -0,0 +1,28 @@ +// Regression test for #153732. +// +// When a method call already has turbofish type arguments, don't suggest +// rewriting them — the suggestion just rewrites user syntax into +// fully-qualified form without resolving anything. +// +// The span still points at the method name rather than the unresolved `_`; +// fixing that is left as future work. + +struct S; + +impl S { + fn f(self, _a: A) -> B { + todo!() + } +} + +fn with_turbofish() { + S.f::(42); + //~^ ERROR type annotations needed +} + +fn without_turbofish() { + S.f(42); + //~^ ERROR type annotations needed +} + +fn main() {} diff --git a/tests/ui/inference/useless-turbofish-suggestion.stderr b/tests/ui/inference/useless-turbofish-suggestion.stderr new file mode 100644 index 0000000000000..25b05801aa470 --- /dev/null +++ b/tests/ui/inference/useless-turbofish-suggestion.stderr @@ -0,0 +1,20 @@ +error[E0282]: type annotations needed + --> $DIR/useless-turbofish-suggestion.rs:19:7 + | +LL | S.f::(42); + | ^ cannot infer type of the type parameter `B` declared on the method `f` + +error[E0282]: type annotations needed + --> $DIR/useless-turbofish-suggestion.rs:24:7 + | +LL | S.f(42); + | ^ cannot infer type of the type parameter `B` declared on the method `f` + | +help: consider specifying the generic arguments + | +LL | S.f::(42); + | ++++++++++ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0282`. From 6e954d376a84ac6da5006abba7800d5663c51639 Mon Sep 17 00:00:00 2001 From: arferreira Date: Thu, 12 Mar 2026 06:28:54 -0400 Subject: [PATCH 2/2] Bless issue-23041 test after turbofish suggestion suppression --- tests/ui/issues/issue-23041.stderr | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tests/ui/issues/issue-23041.stderr b/tests/ui/issues/issue-23041.stderr index bd0e457fa9da5..4d7266c7c98d7 100644 --- a/tests/ui/issues/issue-23041.stderr +++ b/tests/ui/issues/issue-23041.stderr @@ -3,12 +3,6 @@ error[E0282]: type annotations needed | LL | b.downcast_ref::_>(); | ^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the method `downcast_ref` - | -help: consider specifying the generic argument - | -LL - b.downcast_ref::_>(); -LL + b.downcast_ref:: _>(); - | error: aborting due to 1 previous error