Skip to content

Commit

Permalink
Auto merge of rust-lang#116439 - compiler-errors:on-unimplemented, r=…
Browse files Browse the repository at this point in the history
…davidtwco

Pretty print `Fn` traits in `rustc_on_unimplemented`

I don't think that users really ever should need to think about `Fn*` traits' tupled args for a simple trait error.

r? diagnostics
  • Loading branch information
bors committed Nov 3, 2023
2 parents 2429818 + c83f642 commit 1d6f05f
Show file tree
Hide file tree
Showing 61 changed files with 169 additions and 154 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ symbols! {
ToOwned,
ToString,
TokenStream,
Trait,
Try,
TryCaptureGeneric,
TryCapturePrintable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ static ALLOWED_FORMAT_SYMBOLS: &[Symbol] = &[
sym::float,
sym::_Self,
sym::crate_local,
sym::Trait,
];

impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
Expand Down Expand Up @@ -183,6 +184,19 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
flags.push((sym::cause, Some("MainFunctionType".to_string())));
}

if let Some(kind) = self.tcx.fn_trait_kind_from_def_id(trait_ref.def_id)
&& let ty::Tuple(args) = trait_ref.args.type_at(1).kind()
{
let args = args
.iter()
.map(|ty| ty.to_string())
.collect::<Vec<_>>()
.join(", ");
flags.push((sym::Trait, Some(format!("{}({args})", kind.as_str()))));
} else {
flags.push((sym::Trait, Some(trait_ref.print_only_trait_path().to_string())));
}

// Add all types without trimmed paths or visible paths, ensuring they end up with
// their "canonical" def path.
ty::print::with_no_trimmed_paths!(ty::print::with_no_visible_paths!({
Expand Down
24 changes: 12 additions & 12 deletions library/core/src/ops/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ use crate::marker::Tuple;
#[lang = "fn"]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_paren_sugar]
#[rustc_on_unimplemented(
#[cfg_attr(not(bootstrap), rustc_on_unimplemented(
on(
Args = "()",
note = "wrap the `{Self}` in a closure with no arguments: `|| {{ /* code */ }}`"
Expand All @@ -67,9 +67,9 @@ use crate::marker::Tuple;
// SAFETY: tidy is not smart enough to tell that the below unsafe block is a string
label = "call the function in a closure: `|| unsafe {{ /* code */ }}`"
),
message = "expected a `{Fn}<{Args}>` closure, found `{Self}`",
label = "expected an `Fn<{Args}>` closure, found `{Self}`"
)]
message = "expected a `{Trait}` closure, found `{Self}`",
label = "expected an `{Trait}` closure, found `{Self}`"
))]
#[fundamental] // so that regex can rely that `&str: !FnMut`
#[must_use = "closures are lazy and do nothing unless called"]
// FIXME(effects) #[const_trait]
Expand Down Expand Up @@ -143,7 +143,7 @@ pub trait Fn<Args: Tuple>: FnMut<Args> {
#[lang = "fn_mut"]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_paren_sugar]
#[rustc_on_unimplemented(
#[cfg_attr(not(bootstrap), rustc_on_unimplemented(
on(
Args = "()",
note = "wrap the `{Self}` in a closure with no arguments: `|| {{ /* code */ }}`"
Expand All @@ -154,9 +154,9 @@ pub trait Fn<Args: Tuple>: FnMut<Args> {
// SAFETY: tidy is not smart enough to tell that the below unsafe block is a string
label = "call the function in a closure: `|| unsafe {{ /* code */ }}`"
),
message = "expected a `{FnMut}<{Args}>` closure, found `{Self}`",
label = "expected an `FnMut<{Args}>` closure, found `{Self}`"
)]
message = "expected a `{Trait}` closure, found `{Self}`",
label = "expected an `{Trait}` closure, found `{Self}`"
))]
#[fundamental] // so that regex can rely that `&str: !FnMut`
#[must_use = "closures are lazy and do nothing unless called"]
// FIXME(effects) #[const_trait]
Expand Down Expand Up @@ -222,7 +222,7 @@ pub trait FnMut<Args: Tuple>: FnOnce<Args> {
#[lang = "fn_once"]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_paren_sugar]
#[rustc_on_unimplemented(
#[cfg_attr(not(bootstrap), rustc_on_unimplemented(
on(
Args = "()",
note = "wrap the `{Self}` in a closure with no arguments: `|| {{ /* code */ }}`"
Expand All @@ -233,9 +233,9 @@ pub trait FnMut<Args: Tuple>: FnOnce<Args> {
// SAFETY: tidy is not smart enough to tell that the below unsafe block is a string
label = "call the function in a closure: `|| unsafe {{ /* code */ }}`"
),
message = "expected a `{FnOnce}<{Args}>` closure, found `{Self}`",
label = "expected an `FnOnce<{Args}>` closure, found `{Self}`"
)]
message = "expected a `{Trait}` closure, found `{Self}`",
label = "expected an `{Trait}` closure, found `{Self}`"
))]
#[fundamental] // so that regex can rely that `&str: !FnMut`
#[must_use = "closures are lazy and do nothing unless called"]
// FIXME(effects) #[const_trait]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/closures/closure-expected.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fn main() {
let x = Some(1);
let y = x.or_else(4);
//~^ ERROR expected a `FnOnce<()>` closure, found `{integer}`
//~^ ERROR expected a `FnOnce()` closure, found `{integer}`
}
4 changes: 2 additions & 2 deletions tests/ui/closures/closure-expected.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0277]: expected a `FnOnce<()>` closure, found `{integer}`
error[E0277]: expected a `FnOnce()` closure, found `{integer}`
--> $DIR/closure-expected.rs:3:23
|
LL | let y = x.or_else(4);
| ------- ^ expected an `FnOnce<()>` closure, found `{integer}`
| ------- ^ expected an `FnOnce()` closure, found `{integer}`
| |
| required by a bound introduced by this call
|
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
@@ -1,4 +1,4 @@
error[E0277]: expected a `FnOnce<(&str,)>` closure, found `unsafe extern "rust-intrinsic" fn(_) -> _ {transmute::<_, _>}`
error[E0277]: expected a `FnOnce(&str)` closure, found `unsafe extern "rust-intrinsic" fn(_) -> _ {transmute::<_, _>}`
--> $DIR/coerce-unsafe-to-closure.rs:2:44
|
LL | let x: Option<&[u8]> = Some("foo").map(std::mem::transmute);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fn main() {
let number = 2;
Some(true).filter({ //~ ERROR expected a `FnOnce<(&bool,)>` closure, found `bool`
Some(true).filter({ //~ ERROR expected a `FnOnce(&bool)` closure, found `bool`
if number % 2 == 0 {
number == 0
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0277]: expected a `FnOnce<(&bool,)>` closure, found `bool`
error[E0277]: expected a `FnOnce(&bool)` closure, found `bool`
--> $DIR/block_instead_of_closure_in_arg.rs:3:23
|
LL | Some(true).filter({
Expand All @@ -12,7 +12,7 @@ LL | || number != 0
LL | || }
| ||_________- this tail expression is of type `bool`
LL | | });
| |______^ expected an `FnOnce<(&bool,)>` closure, found `bool`
| |______^ expected an `FnOnce(&bool)` closure, found `bool`
|
= help: the trait `for<'a> FnOnce<(&'a bool,)>` is not implemented for `bool`
note: required by a bound in `Option::<T>::filter`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const x: usize =42;
fn main() {
let p = Some(45).and_then({|x| //~ ERROR expected a `FnOnce<({integer},)>` closure, found `Option<usize>`
let p = Some(45).and_then({|x| //~ ERROR expected a `FnOnce({integer})` closure, found `Option<usize>`
1 + 1;
Some(x * 2)
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0277]: expected a `FnOnce<({integer},)>` closure, found `Option<usize>`
error[E0277]: expected a `FnOnce({integer})` closure, found `Option<usize>`
--> $DIR/ruby_style_closure_successful_parse.rs:3:31
|
LL | let p = Some(45).and_then({|x|
Expand All @@ -9,7 +9,7 @@ LL | | 1 + 1;
LL | | Some(x * 2)
| | ----------- this tail expression is of type `Option<usize>`
LL | | });
| |_____^ expected an `FnOnce<({integer},)>` closure, found `Option<usize>`
| |_____^ expected an `FnOnce({integer})` closure, found `Option<usize>`
|
= help: the trait `FnOnce<({integer},)>` is not implemented for `Option<usize>`
note: required by a bound in `Option::<T>::and_then`
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/extern/extern-wrong-value-type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ fn main() {
// extern functions are extern "C" fn
let _x: extern "C" fn() = f; // OK
is_fn(f);
//~^ ERROR expected a `Fn<()>` closure, found `extern "C" fn() {f}`
//~^ ERROR expected a `Fn()` closure, found `extern "C" fn() {f}`
}
4 changes: 2 additions & 2 deletions tests/ui/extern/extern-wrong-value-type.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0277]: expected a `Fn<()>` closure, found `extern "C" fn() {f}`
error[E0277]: expected a `Fn()` closure, found `extern "C" fn() {f}`
--> $DIR/extern-wrong-value-type.rs:9:11
|
LL | is_fn(f);
| ----- ^ expected an `Fn<()>` closure, found `extern "C" fn() {f}`
| ----- ^ expected an `Fn()` closure, found `extern "C" fn() {f}`
| |
| required by a bound introduced by this call
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/fn/fn-trait-formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ fn main() {
//~| found struct `Box<dyn FnMut() -> isize>`

needs_fn(1);
//~^ ERROR expected a `Fn<(isize,)>` closure, found `{integer}`
//~^ ERROR expected a `Fn(isize)` closure, found `{integer}`
}
4 changes: 2 additions & 2 deletions tests/ui/fn/fn-trait-formatting.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ LL | let _: () = Box::new(|| -> isize { unimplemented!() }) as Box<dyn FnMut
= note: expected unit type `()`
found struct `Box<dyn FnMut() -> isize>`

error[E0277]: expected a `Fn<(isize,)>` closure, found `{integer}`
error[E0277]: expected a `Fn(isize)` closure, found `{integer}`
--> $DIR/fn-trait-formatting.rs:19:14
|
LL | needs_fn(1);
| -------- ^ expected an `Fn<(isize,)>` closure, found `{integer}`
| -------- ^ expected an `Fn(isize)` closure, found `{integer}`
| |
| required by a bound introduced by this call
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ trait Fun {

impl<T> Fun for T {
type F<'a> = Self;
//~^ ERROR expected a `Fn<()>` closure, found `T`
//~^ ERROR expected a `Fn()` closure, found `T`
}

fn main() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0277]: expected a `Fn<()>` closure, found `T`
error[E0277]: expected a `Fn()` closure, found `T`
--> $DIR/issue-68642-broken-llvm-ir.rs:12:18
|
LL | type F<'a> = Self;
| ^^^^ expected an `Fn<()>` closure, found `T`
| ^^^^ expected an `Fn()` closure, found `T`
|
= note: wrap the `T` in a closure with no arguments: `|| { /* code */ }`
note: required by a bound in `Fun::F`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ trait Fun {

impl<T> Fun for T {
type F<'a> = Self;
//~^ ERROR expected a `Fn<()>` closure, found `T`
//~^ ERROR expected a `Fn()` closure, found `T`
}

pub fn main() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0277]: expected a `Fn<()>` closure, found `T`
error[E0277]: expected a `Fn()` closure, found `T`
--> $DIR/issue-68643-broken-mir.rs:12:18
|
LL | type F<'a> = Self;
| ^^^^ expected an `Fn<()>` closure, found `T`
| ^^^^ expected an `Fn()` closure, found `T`
|
= note: wrap the `T` in a closure with no arguments: `|| { /* code */ }`
note: required by a bound in `Fun::F`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ trait Fun {

impl<T> Fun for T {
type F<'a> = Self;
//~^ ERROR expected a `Fn<()>` closure, found `T`
//~^ ERROR expected a `Fn()` closure, found `T`
}

fn main() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0277]: expected a `Fn<()>` closure, found `T`
error[E0277]: expected a `Fn()` closure, found `T`
--> $DIR/issue-68644-codegen-selection.rs:12:18
|
LL | type F<'a> = Self;
| ^^^^ expected an `Fn<()>` closure, found `T`
| ^^^^ expected an `Fn()` closure, found `T`
|
= note: wrap the `T` in a closure with no arguments: `|| { /* code */ }`
note: required by a bound in `Fun::F`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ trait Fun {

impl<T> Fun for T {
type F<'a> = Self;
//~^ ERROR expected a `Fn<()>` closure, found `T`
//~^ ERROR expected a `Fn()` closure, found `T`
}

fn main() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0277]: expected a `Fn<()>` closure, found `T`
error[E0277]: expected a `Fn()` closure, found `T`
--> $DIR/issue-68645-codegen-fulfillment.rs:12:18
|
LL | type F<'a> = Self;
| ^^^^ expected an `Fn<()>` closure, found `T`
| ^^^^ expected an `Fn()` closure, found `T`
|
= note: wrap the `T` in a closure with no arguments: `|| { /* code */ }`
note: required by a bound in `Fun::F`
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/higher-ranked/trait-bounds/fn-ptr.classic.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0277]: expected a `Fn<(&'w (),)>` closure, found `fn(&'w ())`
error[E0277]: expected a `Fn(&'w ())` closure, found `fn(&'w ())`
--> $DIR/fn-ptr.rs:12:5
|
LL | ice();
| ^^^ expected an `Fn<(&'w (),)>` closure, found `fn(&'w ())`
| ^^^ expected an `Fn(&'w ())` closure, found `fn(&'w ())`
|
= help: the trait `for<'w> Fn<(&'w (),)>` is not implemented for `fn(&'w ())`
note: required by a bound in `ice`
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/higher-ranked/trait-bounds/fn-ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ where

fn main() {
ice();
//[classic]~^ ERROR expected a `Fn<(&'w (),)>` closure, found `fn(&'w ())`
//[classic]~^ ERROR expected a `Fn(&'w ())` closure, found `fn(&'w ())`
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0277]: expected a `Fn<(<_ as ATC<'a>>::Type,)>` closure, found `F`
error[E0277]: expected a `Fn(<_ as ATC<'a>>::Type)` closure, found `F`
--> $DIR/issue-62529-3.rs:25:14
|
LL | call(f, ());
| ---- ^ expected an `Fn<(<_ as ATC<'a>>::Type,)>` closure, found `F`
| ---- ^ expected an `Fn(<_ as ATC<'a>>::Type)` closure, found `F`
| |
| required by a bound introduced by this call
|
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/implied-bounds/issue-100690.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ impl<'a, T: 'a> Handle<'a, T, UIView<'a, T>, Result<(), io::Error>> for TUIHandl
F: FnOnce(&mut UIView<'a, T>) -> Result<(), io::Error> + Send + 'static,
{
real_dispatch(f)
//~^ ERROR expected a `FnOnce<(&mut UIView<'_, T>,)>` closure, found `F`
//~| NOTE expected an `FnOnce<(&mut UIView<'_, T>,)>` closure, found `F`
//~^ ERROR expected a `FnOnce(&mut UIView<'_, T>)` closure, found `F`
//~| NOTE expected an `FnOnce(&mut UIView<'_, T>)` closure, found `F`
//~| NOTE expected a closure with arguments
//~| NOTE required by a bound introduced by this call
}
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/implied-bounds/issue-100690.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0277]: expected a `FnOnce<(&mut UIView<'_, T>,)>` closure, found `F`
error[E0277]: expected a `FnOnce(&mut UIView<'_, T>)` closure, found `F`
--> $DIR/issue-100690.rs:37:23
|
LL | real_dispatch(f)
| ------------- ^ expected an `FnOnce<(&mut UIView<'_, T>,)>` closure, found `F`
| ------------- ^ expected an `FnOnce(&mut UIView<'_, T>)` closure, found `F`
| |
| required by a bound introduced by this call
|
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/intrinsics/const-eval-select-bad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const fn not_fn_items() {
//~^ ERROR this argument must be a function item
//~| ERROR this argument must be a function item
const_eval_select((), 42, 0xDEADBEEF);
//~^ ERROR expected a `FnOnce<()>` closure
//~| ERROR expected a `FnOnce<()>` closure
//~^ ERROR expected a `FnOnce()` closure
//~| ERROR expected a `FnOnce()` closure
//~| ERROR this argument must be a function item
//~| ERROR this argument must be a function item
}
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/intrinsics/const-eval-select-bad.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ LL | const_eval_select((), 42, 0xDEADBEEF);
= note: expected a function item, found {integer}
= help: consult the documentation on `const_eval_select` for more information

error[E0277]: expected a `FnOnce<()>` closure, found `{integer}`
error[E0277]: expected a `FnOnce()` closure, found `{integer}`
--> $DIR/const-eval-select-bad.rs:10:27
|
LL | const_eval_select((), 42, 0xDEADBEEF);
| ----------------- ^^ expected an `FnOnce<()>` closure, found `{integer}`
| ----------------- ^^ expected an `FnOnce()` closure, found `{integer}`
| |
| required by a bound introduced by this call
|
Expand All @@ -47,11 +47,11 @@ LL | const_eval_select((), 42, 0xDEADBEEF);
= note: expected a function item, found {integer}
= help: consult the documentation on `const_eval_select` for more information

error[E0277]: expected a `FnOnce<()>` closure, found `{integer}`
error[E0277]: expected a `FnOnce()` closure, found `{integer}`
--> $DIR/const-eval-select-bad.rs:10:31
|
LL | const_eval_select((), 42, 0xDEADBEEF);
| ----------------- ^^^^^^^^^^ expected an `FnOnce<()>` closure, found `{integer}`
| ----------------- ^^^^^^^^^^ expected an `FnOnce()` closure, found `{integer}`
| |
| required by a bound introduced by this call
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/issues/issue-22034.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ fn main() {
let ptr: *mut () = core::ptr::null_mut();
let _: &mut dyn Fn() = unsafe {
&mut *(ptr as *mut dyn Fn())
//~^ ERROR expected a `Fn<()>` closure, found `()`
//~^ ERROR expected a `Fn()` closure, found `()`
};
}
4 changes: 2 additions & 2 deletions tests/ui/issues/issue-22034.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0277]: expected a `Fn<()>` closure, found `()`
error[E0277]: expected a `Fn()` closure, found `()`
--> $DIR/issue-22034.rs:8:16
|
LL | &mut *(ptr as *mut dyn Fn())
| ^^^ expected an `Fn<()>` closure, found `()`
| ^^^ expected an `Fn()` closure, found `()`
|
= help: the trait `Fn<()>` is not implemented for `()`
= note: wrap the `()` in a closure with no arguments: `|| { /* code */ }`
Expand Down
Loading

0 comments on commit 1d6f05f

Please sign in to comment.