Skip to content

Commit

Permalink
Auto merge of #125778 - estebank:issue-67100, r=compiler-errors
Browse files Browse the repository at this point in the history
Use parenthetical notation for `Fn` traits

Always use the `Fn(T) -> R` format when printing closure traits instead of `Fn<(T,), Output = R>`.

Address #67100:

```
error[E0277]: expected a `Fn()` closure, found `F`
 --> file.rs:6:13
  |
6 |     call_fn(f)
  |     ------- ^ expected an `Fn()` closure, found `F`
  |     |
  |     required by a bound introduced by this call
  |
  = note: wrap the `F` in a closure with no arguments: `|| { /* code */ }`
note: required by a bound in `call_fn`
 --> file.rs:1:15
  |
1 | fn call_fn<F: Fn() -> ()>(f: &F) {
  |               ^^^^^^^^^^ required by this bound in `call_fn`
help: consider further restricting this bound
  |
5 | fn call_any<F: std::any::Any + Fn()>(f: &F) {
  |                              ++++++
```
  • Loading branch information
bors committed Jun 3, 2024
2 parents 621e957 + e6bd6c2 commit 1d52972
Show file tree
Hide file tree
Showing 43 changed files with 89 additions and 88 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1361,7 +1361,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
match *predicate.self_ty().kind() {
ty::Param(param_ty) => Ok((
generics.type_param(param_ty, tcx),
predicate.trait_ref.print_only_trait_path().to_string(),
predicate.trait_ref.print_trait_sugared().to_string(),
)),
_ => Err(()),
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/check_consts/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
if let Some(generics) = tcx.hir_node_by_def_id(caller).generics() {
let constraint = with_no_trimmed_paths!(format!(
"~const {}",
trait_ref.print_only_trait_path()
trait_ref.print_trait_sugared(),
));
suggest_constraining_type_param(
tcx,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/coherence/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ fn infringing_fields_error(
if let ty::Param(_) = ty.kind() {
bounds.push((
format!("{ty}"),
trait_ref.print_only_trait_path().to_string(),
trait_ref.print_trait_sugared().to_string(),
Some(trait_ref.def_id),
));
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3202,7 +3202,7 @@ define_print_and_forward_display! {
if let ty::PredicatePolarity::Negative = self.0.polarity {
p!("!")
}
p!(print(self.0.trait_ref.print_only_trait_path()));
p!(print(self.0.trait_ref.print_trait_sugared()));
}

PrintClosureAsImpl<'tcx> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ use rustc_macros::extension;
use rustc_middle::hir::map;
use rustc_middle::traits::IsConstable;
use rustc_middle::ty::error::TypeError::{self, Sorts};
use rustc_middle::ty::print::PrintPolyTraitRefExt;
use rustc_middle::ty::{
self, suggest_arbitrary_trait_bound, suggest_constraining_type_param, AdtKind, GenericArgs,
InferTy, IsSuggestable, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable,
TypeVisitableExt, TypeckResults, Upcast,
InferTy, IsSuggestable, ToPolyTraitRef, Ty, TyCtxt, TypeFoldable, TypeFolder,
TypeSuperFoldable, TypeVisitableExt, TypeckResults, Upcast,
};
use rustc_middle::{bug, span_bug};
use rustc_span::def_id::LocalDefId;
Expand Down Expand Up @@ -219,15 +220,15 @@ pub fn suggest_restriction<'tcx, G: EmissionGuarantee>(
(_, None) => predicate_constraint(hir_generics, trait_pred.upcast(tcx)),
(None, Some((ident, []))) => (
ident.span.shrink_to_hi(),
format!(": {}", trait_pred.print_modifiers_and_trait_path()),
format!(": {}", trait_pred.to_poly_trait_ref().print_trait_sugared()),
),
(_, Some((_, [.., bounds]))) => (
bounds.span().shrink_to_hi(),
format!(" + {}", trait_pred.print_modifiers_and_trait_path()),
format!(" + {}", trait_pred.to_poly_trait_ref().print_trait_sugared()),
),
(Some(_), Some((_, []))) => (
hir_generics.span.shrink_to_hi(),
format!(": {}", trait_pred.print_modifiers_and_trait_path()),
format!(": {}", trait_pred.to_poly_trait_ref().print_trait_sugared()),
),
};

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/async-await/async-fn/impl-header.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ error[E0277]: expected a `FnMut()` closure, found `F`
LL | impl async Fn<()> for F {}
| ^ expected an `FnMut()` closure, found `F`
|
= help: the trait `FnMut<()>` is not implemented for `F`
= help: the trait `FnMut()` is not implemented for `F`
= note: wrap the `F` in a closure with no arguments: `|| { /* code */ }`
note: required by a bound in `Fn`
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/closures/closure-expected.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | let y = x.or_else(4);
| |
| required by a bound introduced by this call
|
= help: the trait `FnOnce<()>` is not implemented for `{integer}`
= help: the trait `FnOnce()` is not implemented for `{integer}`
= note: wrap the `{integer}` in a closure with no arguments: `|| { /* code */ }`
note: required by a bound in `Option::<T>::or_else`
--> $SRC_DIR/core/src/option.rs:LL:COL
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/closures/coerce-unsafe-to-closure.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | let x: Option<&[u8]> = Some("foo").map(std::mem::transmute);
| |
| required by a bound introduced by this call
|
= help: the trait `FnOnce<(&str,)>` is not implemented for fn item `unsafe extern "rust-intrinsic" fn(_) -> _ {transmute::<_, _>}`
= help: the trait `FnOnce(&str)` is not implemented for fn item `unsafe extern "rust-intrinsic" fn(_) -> _ {transmute::<_, _>}`
= note: unsafe function cannot be called generically without an unsafe block
note: required by a bound in `Option::<T>::map`
--> $SRC_DIR/core/src/option.rs:LL:COL
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/consts/fn_trait_refs.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ LL | f()
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
help: consider further restricting this bound
|
LL | T: ~const Fn<()> + ~const Destruct + ~const std::ops::Fn<()>,
| +++++++++++++++++++++++++
LL | T: ~const Fn<()> + ~const Destruct + ~const Fn(),
| +++++++++++++
help: add `#![feature(effects)]` to the crate attributes to enable
|
LL + #![feature(effects)]
Expand All @@ -132,8 +132,8 @@ LL | f()
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
help: consider further restricting this bound
|
LL | T: ~const FnMut<()> + ~const Destruct + ~const std::ops::FnMut<()>,
| ++++++++++++++++++++++++++++
LL | T: ~const FnMut<()> + ~const Destruct + ~const FnMut(),
| ++++++++++++++++
help: add `#![feature(effects)]` to the crate attributes to enable
|
LL + #![feature(effects)]
Expand All @@ -157,8 +157,8 @@ LL | f()
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
help: consider further restricting this bound
|
LL | T: ~const FnOnce<()> + ~const std::ops::FnOnce<()>,
| +++++++++++++++++++++++++++++
LL | T: ~const FnOnce<()> + ~const FnOnce(),
| +++++++++++++++++
help: add `#![feature(effects)]` to the crate attributes to enable
|
LL + #![feature(effects)]
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/consts/unstable-const-fn-in-libcore.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ LL | Opt::None => f(),
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
help: consider further restricting this bound
|
LL | const fn unwrap_or_else<F: ~const FnOnce() -> T + ~const std::ops::FnOnce<()>>(self, f: F) -> T {
| +++++++++++++++++++++++++++++
LL | const fn unwrap_or_else<F: ~const FnOnce() -> T + ~const FnOnce()>(self, f: F) -> T {
| +++++++++++++++++
help: add `#![feature(effects)]` to the crate attributes to enable
|
LL + #![feature(effects)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ LL | || }
LL | | });
| |______^ expected an `FnOnce(&bool)` closure, found `bool`
|
= help: the trait `for<'a> FnOnce<(&'a bool,)>` is not implemented for `bool`
= help: the trait `for<'a> FnOnce(&'a bool)` is not implemented for `bool`
note: required by a bound in `Option::<T>::filter`
--> $SRC_DIR/core/src/option.rs:LL:COL
help: you might have meant to create the closure instead of a block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ LL | | Some(x * 2)
LL | | });
| |_____^ expected an `FnOnce({integer})` closure, found `Option<usize>`
|
= help: the trait `FnOnce<({integer},)>` is not implemented for `Option<usize>`
= help: the trait `FnOnce({integer})` is not implemented for `Option<usize>`
note: required by a bound in `Option::<T>::and_then`
--> $SRC_DIR/core/src/option.rs:LL:COL
help: you might have meant to open the closure body instead of placing a closure within a block
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/extern/extern-wrong-value-type.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | is_fn(f);
| |
| required by a bound introduced by this call
|
= help: the trait `Fn<()>` is not implemented for fn item `extern "C" fn() {f}`
= help: the trait `Fn()` is not implemented for fn item `extern "C" fn() {f}`
= note: wrap the `extern "C" fn() {f}` in a closure with no arguments: `|| { /* code */ }`
note: required by a bound in `is_fn`
--> $DIR/extern-wrong-value-type.rs:4:28
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ error[E0277]: expected a `FnMut()` closure, found `Foo`
LL | impl Fn<()> for Foo {
| ^^^ expected an `FnMut()` closure, found `Foo`
|
= help: the trait `FnMut<()>` is not implemented for `Foo`
= help: the trait `FnMut()` is not implemented for `Foo`
= note: wrap the `Foo` in a closure with no arguments: `|| { /* code */ }`
note: required by a bound in `Fn`
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
Expand Down Expand Up @@ -149,7 +149,7 @@ error[E0277]: expected a `FnOnce()` closure, found `Bar`
LL | impl FnMut<()> for Bar {
| ^^^ expected an `FnOnce()` closure, found `Bar`
|
= help: the trait `FnOnce<()>` is not implemented for `Bar`
= help: the trait `FnOnce()` is not implemented for `Bar`
= note: wrap the `Bar` in a closure with no arguments: `|| { /* code */ }`
note: required by a bound in `FnMut`
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/fn/fn-trait-formatting.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ LL | needs_fn(1);
| |
| required by a bound introduced by this call
|
= help: the trait `Fn<(isize,)>` is not implemented for `{integer}`
= help: the trait `Fn(isize)` is not implemented for `{integer}`
note: required by a bound in `needs_fn`
--> $DIR/fn-trait-formatting.rs:1:31
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/fn/issue-39259.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ error[E0277]: expected a `FnMut(u32)` closure, found `S`
LL | impl Fn(u32) -> u32 for S {
| ^ expected an `FnMut(u32)` closure, found `S`
|
= help: the trait `FnMut<(u32,)>` is not implemented for `S`
= help: the trait `FnMut(u32)` is not implemented for `S`
note: required by a bound in `Fn`
--> $SRC_DIR/core/src/ops/function.rs:LL:COL

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ LL | type F<'a>: Fn() -> u32;
| ^^^^^^^^^^^ required by this bound in `Fun::F`
help: consider restricting type parameter `T`
|
LL | impl<T: std::ops::Fn<()>> Fun for T {
| ++++++++++++++++++
LL | impl<T: Fn()> Fun for T {
| ++++++

error: aborting due to 1 previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ LL | type F<'a>: Fn() -> u32;
| ^^^^^^^^^^^ required by this bound in `Fun::F`
help: consider restricting type parameter `T`
|
LL | impl<T: std::ops::Fn<()>> Fun for T {
| ++++++++++++++++++
LL | impl<T: Fn()> Fun for T {
| ++++++

error: aborting due to 1 previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ LL | type F<'a>: Fn() -> u32;
| ^^^^^^^^^^^ required by this bound in `Fun::F`
help: consider restricting type parameter `T`
|
LL | impl<T: std::ops::Fn<()>> Fun for T {
| ++++++++++++++++++
LL | impl<T: Fn()> Fun for T {
| ++++++

error: aborting due to 1 previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ LL | type F<'a>: Fn() -> u32;
| ^^^^^^^^^^^ required by this bound in `Fun::F`
help: consider restricting type parameter `T`
|
LL | impl<T: std::ops::Fn<()>> Fun for T {
| ++++++++++++++++++
LL | impl<T: Fn()> Fun for T {
| ++++++

error: aborting due to 1 previous error

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/impl-trait/normalize-tait-in-const.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ LL | fun(filter_positive());
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
help: consider further restricting this bound
|
LL | const fn with_positive<F: ~const for<'a> Fn(&'a Alias<'a>) + ~const Destruct + ~const std::ops::Fn<(&Alias<'_>,)>>(fun: F) {
| ++++++++++++++++++++++++++++++++++++
LL | const fn with_positive<F: ~const for<'a> Fn(&'a Alias<'a>) + ~const Destruct + ~const Fn(&Alias<'_>)>(fun: F) {
| +++++++++++++++++++++++
help: add `#![feature(effects)]` to the crate attributes to enable
|
LL + #![feature(effects)]
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/intrinsics/const-eval-select-bad.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ LL | const_eval_select((), 42, 0xDEADBEEF);
| |
| required by a bound introduced by this call
|
= help: the trait `FnOnce<()>` is not implemented for `{integer}`
= help: the trait `FnOnce()` is not implemented for `{integer}`
= note: wrap the `{integer}` in a closure with no arguments: `|| { /* code */ }`
note: required by a bound in `const_eval_select`
--> $SRC_DIR/core/src/intrinsics.rs:LL:COL
Expand All @@ -37,7 +37,7 @@ LL | const_eval_select((), 42, 0xDEADBEEF);
| |
| required by a bound introduced by this call
|
= help: the trait `FnOnce<()>` is not implemented for `{integer}`
= help: the trait `FnOnce()` is not implemented for `{integer}`
= note: wrap the `{integer}` in a closure with no arguments: `|| { /* code */ }`
note: required by a bound in `const_eval_select`
--> $SRC_DIR/core/src/intrinsics.rs:LL:COL
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/issues/issue-22034.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0277]: expected a `Fn()` closure, found `()`
LL | &mut *(ptr as *mut dyn Fn())
| ^^^ expected an `Fn()` closure, found `()`
|
= help: the trait `Fn<()>` is not implemented for `()`
= help: the trait `Fn()` is not implemented for `()`
= note: wrap the `()` in a closure with no arguments: `|| { /* code */ }`
= note: required for the cast from `*mut ()` to `*mut dyn Fn()`

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/issues/issue-23966.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | "".chars().fold(|_, _| (), ());
| |
| required by a bound introduced by this call
|
= help: the trait `FnMut<(_, char)>` is not implemented for `()`
= help: the trait `FnMut(_, char)` is not implemented for `()`
note: required by a bound in `fold`
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL

Expand Down
10 changes: 5 additions & 5 deletions tests/ui/lifetimes/issue-76168-hr-outlives-3.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ LL | | F:,
LL | | for<'a> <i32 as FnOnce<(&'a mut i32,)>>::Output: Future<Output = ()> + 'a,
| |__________________________________________________________________________^ expected an `FnOnce(&'a mut i32)` closure, found `i32`
|
= help: the trait `for<'a> FnOnce<(&'a mut i32,)>` is not implemented for `i32`
= help: the trait `for<'a> FnOnce(&'a mut i32)` is not implemented for `i32`

error[E0277]: expected a `FnOnce(&'a mut i32)` closure, found `i32`
--> $DIR/issue-76168-hr-outlives-3.rs:6:10
|
LL | async fn wrapper<F>(f: F)
| ^^^^^^^ expected an `FnOnce(&'a mut i32)` closure, found `i32`
|
= help: the trait `for<'a> FnOnce<(&'a mut i32,)>` is not implemented for `i32`
= help: the trait `for<'a> FnOnce(&'a mut i32)` is not implemented for `i32`

error[E0277]: expected a `FnOnce(&'a mut i32)` closure, found `i32`
--> $DIR/issue-76168-hr-outlives-3.rs:6:1
Expand All @@ -32,7 +32,7 @@ LL | | F:,
LL | | for<'a> <i32 as FnOnce<(&'a mut i32,)>>::Output: Future<Output = ()> + 'a,
| |__________________________________________________________________________^ expected an `FnOnce(&'a mut i32)` closure, found `i32`
|
= help: the trait `for<'a> FnOnce<(&'a mut i32,)>` is not implemented for `i32`
= help: the trait `for<'a> FnOnce(&'a mut i32)` is not implemented for `i32`

error[E0277]: expected a `FnOnce(&'a mut i32)` closure, found `i32`
--> $DIR/issue-76168-hr-outlives-3.rs:6:1
Expand All @@ -46,7 +46,7 @@ LL | | F:,
LL | | for<'a> <i32 as FnOnce<(&'a mut i32,)>>::Output: Future<Output = ()> + 'a,
| |__________________________________________________________________________^ expected an `FnOnce(&'a mut i32)` closure, found `i32`
|
= help: the trait `for<'a> FnOnce<(&'a mut i32,)>` is not implemented for `i32`
= help: the trait `for<'a> FnOnce(&'a mut i32)` is not implemented for `i32`
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error[E0277]: expected a `FnOnce(&'a mut i32)` closure, found `i32`
Expand All @@ -59,7 +59,7 @@ LL | | &mut i;
LL | | }
| |_^ expected an `FnOnce(&'a mut i32)` closure, found `i32`
|
= help: the trait `for<'a> FnOnce<(&'a mut i32,)>` is not implemented for `i32`
= help: the trait `for<'a> FnOnce(&'a mut i32)` is not implemented for `i32`

error: aborting due to 5 previous errors

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/lifetimes/issue-95023.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ error[E0277]: expected a `FnMut(&isize)` closure, found `Error`
LL | impl Fn(&isize) for Error {
| ^^^^^ expected an `FnMut(&isize)` closure, found `Error`
|
= help: the trait `FnMut<(&isize,)>` is not implemented for `Error`
= help: the trait `FnMut(&isize)` is not implemented for `Error`
note: required by a bound in `Fn`
--> $SRC_DIR/core/src/ops/function.rs:LL:COL

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ LL | let _ = produces_string().and_then(takes_str_but_wrong_abi);
| |
| required by a bound introduced by this call
|
= help: the trait `FnOnce<(String,)>` is not implemented for fn item `for<'a> extern "C" fn(&'a str) -> Option<()> {takes_str_but_wrong_abi}`
= help: the trait `FnOnce(String)` is not implemented for fn item `for<'a> extern "C" fn(&'a str) -> Option<()> {takes_str_but_wrong_abi}`
note: required by a bound in `Option::<T>::and_then`
--> $SRC_DIR/core/src/option.rs:LL:COL

Expand All @@ -38,7 +38,7 @@ LL | let _ = produces_string().and_then(takes_str_but_unsafe);
| |
| required by a bound introduced by this call
|
= help: the trait `FnOnce<(String,)>` is not implemented for fn item `for<'a> unsafe fn(&'a str) -> Option<()> {takes_str_but_unsafe}`
= help: the trait `FnOnce(String)` is not implemented for fn item `for<'a> unsafe fn(&'a str) -> Option<()> {takes_str_but_unsafe}`
= note: unsafe function cannot be called generically without an unsafe block
note: required by a bound in `Option::<T>::and_then`
--> $SRC_DIR/core/src/option.rs:LL:COL
Expand Down
Loading

0 comments on commit 1d52972

Please sign in to comment.