Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion compiler/rustc_hir_typeck/src/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use tracing::{debug, instrument};
use super::method::MethodCallee;
use super::method::probe::ProbeScope;
use super::{Expectation, FnCtxt, TupleArgumentsFlag};
use crate::expr_use_visitor::TypeInformationCtxt;
use crate::{errors, fluent_generated};

/// Checks that it is legal to call methods of the trait corresponding
Expand Down Expand Up @@ -122,7 +123,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
);
}

let guar = self.report_invalid_callee(call_expr, callee_expr, expr_ty, arg_exprs);
let guar = self.report_invalid_callee(
call_expr,
callee_expr,
expr_ty,
arg_exprs,
expected,
);
Ty::new_error(self.tcx, guar)
}

Expand Down Expand Up @@ -677,6 +684,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
callee_expr: &'tcx hir::Expr<'tcx>,
callee_ty: Ty<'tcx>,
arg_exprs: &'tcx [hir::Expr<'tcx>],
expected: Expectation<'tcx>,
) -> ErrorGuaranteed {
// Callee probe fails when APIT references errors, so suppress those
// errors here.
Expand Down Expand Up @@ -806,6 +814,33 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
err.span_label(def_span, "the callable type is defined here");
}
} else {
// Suggest adding <Param>: Fn(...) [-> RetType]
if callee_ty.has_non_region_param() {
let arg_types: Vec<Ty<'tcx>> = arg_exprs
.iter()
.map(|arg| self.typeck_results.borrow().expr_ty(arg))
.collect();
let args_tuple = Ty::new_tup(self.tcx(), &arg_types);

let fn_def_id = self.tcx().require_lang_item(LangItem::Fn, callee_expr.span);
Copy link
Member

@SparrowLii SparrowLii Sep 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There several function traits like Fn, Fnmut or FnOnce. It's hard to know which one is most suitable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My original use-case was that there is no bound at all. In that case, we really can't guess much, I suppose, so using Fn as the most related option seemed reasonable to me.

let trait_ref =
ty::TraitRef::new(self.tcx(), fn_def_id, [callee_ty, args_tuple]);

let trait_pred =
ty::TraitPredicate { trait_ref, polarity: ty::PredicatePolarity::Positive };

let associated_ty = expected
.to_option(self)
// We do not want to suggest e.g. `-> _`
.filter(|ty| !ty.has_infer())
.map(|ty| ("Output", ty));
self.err_ctxt().suggest_restricting_param_bound(
&mut err,
ty::Binder::dummy(trait_pred),
associated_ty,
self.body_id,
);
}
err.span_label(call_expr.span, "call expression requires function");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,12 +388,16 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
);

if let Some((name, term)) = associated_ty {
// FIXME: this case overlaps with code in TyCtxt::note_and_explain_type_err.
// That should be extracted into a helper function.
if let Some(stripped) = constraint.strip_suffix('>') {
constraint = format!("{stripped}, {name} = {term}>");
if self.tcx.is_fn_trait(trait_pred.skip_binder().trait_ref.def_id) {
constraint.push_str(&format!(" -> {term}"));
} else {
constraint.push_str(&format!("<{name} = {term}>"));
// FIXME: this case overlaps with code in TyCtxt::note_and_explain_type_err.
// That should be extracted into a helper function.
if let Some(stripped) = constraint.strip_suffix('>') {
constraint = format!("{stripped}, {name} = {term}>");
} else {
constraint.push_str(&format!("<{name} = {term}>"));
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions tests/ui/issues/issue-21701.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ LL | let y = t();
| ^--
| |
| call expression requires function
|
help: consider restricting type parameter `U` with trait `Fn`
|
LL | fn foo<U: Fn()>(t: U) {
| ++++++

error[E0618]: expected function, found struct `Bar`
--> $DIR/issue-21701.rs:9:13
Expand Down
26 changes: 26 additions & 0 deletions tests/ui/suggestions/suggest-call-on-generic-param.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
fn return_type<T>(t: T) {
let x: u32 = t(1);
//~^ ERROR: expected function, found `T` [E0618]
}

fn unknown_return_type<T>(t: T) {
let x = t();
//~^ ERROR: expected function, found `T` [E0618]
}

fn nested_return_type<T>(t: Vec<T>) {
t();
//~^ ERROR: expected function, found `Vec<T>` [E0618]
}

fn no_return_type<T>(t: T) {
t(1, 2, true);
//~^ ERROR: expected function, found `T` [E0618]
}

fn existing_bound<T: Copy>(t: T) {
t(false);
//~^ ERROR: expected function, found `T` [E0618]
}

fn main() {}
78 changes: 78 additions & 0 deletions tests/ui/suggestions/suggest-call-on-generic-param.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
error[E0618]: expected function, found `T`
--> $DIR/suggest-call-on-generic-param.rs:2:18
|
LL | fn return_type<T>(t: T) {
| - `t` has type `T`
LL | let x: u32 = t(1);
| ^---
| |
| call expression requires function
|
help: consider restricting type parameter `T` with trait `Fn`
|
LL | fn return_type<T: Fn(i32) -> u32>(t: T) {
| ++++++++++++++++

error[E0618]: expected function, found `T`
--> $DIR/suggest-call-on-generic-param.rs:7:13
|
LL | fn unknown_return_type<T>(t: T) {
| - `t` has type `T`
LL | let x = t();
| ^--
| |
| call expression requires function
|
help: consider restricting type parameter `T` with trait `Fn`
|
LL | fn unknown_return_type<T: Fn()>(t: T) {
| ++++++

error[E0618]: expected function, found `Vec<T>`
--> $DIR/suggest-call-on-generic-param.rs:12:5
|
LL | fn nested_return_type<T>(t: Vec<T>) {
| - `t` has type `Vec<T>`
LL | t();
| ^--
| |
| call expression requires function
|
help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement
|
LL | fn nested_return_type<T>(t: Vec<T>) where Vec<T>: Fn() {
| ++++++++++++++++++

error[E0618]: expected function, found `T`
--> $DIR/suggest-call-on-generic-param.rs:17:5
|
LL | fn no_return_type<T>(t: T) {
| - `t` has type `T`
LL | t(1, 2, true);
| ^------------
| |
| call expression requires function
|
help: consider restricting type parameter `T` with trait `Fn`
|
LL | fn no_return_type<T: Fn(i32, i32, bool)>(t: T) {
| ++++++++++++++++++++

error[E0618]: expected function, found `T`
--> $DIR/suggest-call-on-generic-param.rs:22:5
|
LL | fn existing_bound<T: Copy>(t: T) {
| - `t` has type `T`
LL | t(false);
| ^-------
| |
| call expression requires function
|
help: consider further restricting type parameter `T` with trait `Fn`
|
LL | fn existing_bound<T: Copy + Fn(bool)>(t: T) {
| ++++++++++

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0618`.
Loading