diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs index bc8c8a444058d..3bcbeddf95c80 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs @@ -820,16 +820,20 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { if matches!(obligation.cause.code(), ObligationCauseCode::FunctionArg { .. }) && obligation.cause.span.can_be_used_for_suggestions() { + let (span, sugg) = if let Some(snippet) = + self.tcx.sess.source_map().span_to_snippet(obligation.cause.span).ok() + && snippet.starts_with("|") + { + (obligation.cause.span, format!("({snippet})({args})")) + } else { + (obligation.cause.span.shrink_to_hi(), format!("({args})")) + }; + // When the obligation error has been ensured to have been caused by // an argument, the `obligation.cause.span` points at the expression // of the argument, so we can provide a suggestion. Otherwise, we give // a more general note. - err.span_suggestion_verbose( - obligation.cause.span.shrink_to_hi(), - msg, - format!("({args})"), - Applicability::HasPlaceholders, - ); + err.span_suggestion_verbose(span, msg, sugg, Applicability::HasPlaceholders); } else if let DefIdOrName::DefId(def_id) = def_id_or_name { let name = match self.tcx.hir_get_if_local(def_id) { Some(hir::Node::Expr(hir::Expr { diff --git a/tests/ui/const-generics/adt_const_params/const_param_ty_bad.stderr b/tests/ui/const-generics/adt_const_params/const_param_ty_bad.stderr index 4c8a5e46751c9..c05584ef909ac 100644 --- a/tests/ui/const-generics/adt_const_params/const_param_ty_bad.stderr +++ b/tests/ui/const-generics/adt_const_params/const_param_ty_bad.stderr @@ -32,8 +32,9 @@ LL | fn check(_: impl std::marker::UnsizedConstParamTy) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `check` help: use parentheses to call this closure | -LL | check(|| {}()); - | ++ +LL - check(|| {}); +LL + check((|| {})()); + | error[E0277]: `fn()` can't be used as a const parameter type --> $DIR/const_param_ty_bad.rs:9:11 diff --git a/tests/ui/suggestions/use-parentheses-to-call-closure-issue-145404.fixed b/tests/ui/suggestions/use-parentheses-to-call-closure-issue-145404.fixed new file mode 100644 index 0000000000000..2835743fca201 --- /dev/null +++ b/tests/ui/suggestions/use-parentheses-to-call-closure-issue-145404.fixed @@ -0,0 +1,13 @@ +//@ run-rustfix + +use std::fmt::Display; + +struct S; + +impl S { + fn call(&self, _: impl Display) {} +} + +fn main() { + S.call((|| "hello")()); //~ ERROR [E0277] +} diff --git a/tests/ui/suggestions/use-parentheses-to-call-closure-issue-145404.rs b/tests/ui/suggestions/use-parentheses-to-call-closure-issue-145404.rs new file mode 100644 index 0000000000000..848629a67001a --- /dev/null +++ b/tests/ui/suggestions/use-parentheses-to-call-closure-issue-145404.rs @@ -0,0 +1,13 @@ +//@ run-rustfix + +use std::fmt::Display; + +struct S; + +impl S { + fn call(&self, _: impl Display) {} +} + +fn main() { + S.call(|| "hello"); //~ ERROR [E0277] +} diff --git a/tests/ui/suggestions/use-parentheses-to-call-closure-issue-145404.stderr b/tests/ui/suggestions/use-parentheses-to-call-closure-issue-145404.stderr new file mode 100644 index 0000000000000..cb6df5af7fb16 --- /dev/null +++ b/tests/ui/suggestions/use-parentheses-to-call-closure-issue-145404.stderr @@ -0,0 +1,23 @@ +error[E0277]: `{closure@$DIR/use-parentheses-to-call-closure-issue-145404.rs:12:12: 12:14}` doesn't implement `std::fmt::Display` + --> $DIR/use-parentheses-to-call-closure-issue-145404.rs:12:12 + | +LL | S.call(|| "hello"); + | ---- ^^^^^^^^^^ unsatisfied trait bound + | | + | required by a bound introduced by this call + | + = help: the trait `std::fmt::Display` is not implemented for closure `{closure@$DIR/use-parentheses-to-call-closure-issue-145404.rs:12:12: 12:14}` +note: required by a bound in `S::call` + --> $DIR/use-parentheses-to-call-closure-issue-145404.rs:8:28 + | +LL | fn call(&self, _: impl Display) {} + | ^^^^^^^ required by this bound in `S::call` +help: use parentheses to call this closure + | +LL - S.call(|| "hello"); +LL + S.call((|| "hello")()); + | + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0277`.