Skip to content

Commit 32abbdc

Browse files
Pretty print Fn traits in rustc_on_unimplemented
1 parent 4910642 commit 32abbdc

File tree

55 files changed

+151
-136
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+151
-136
lines changed

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ symbols! {
295295
ToOwned,
296296
ToString,
297297
TokenStream,
298+
Trait,
298299
Try,
299300
TryCaptureGeneric,
300301
TryCapturePrintable,

compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ static ALLOWED_FORMAT_SYMBOLS: &[Symbol] = &[
5050
sym::float,
5151
sym::_Self,
5252
sym::crate_local,
53+
sym::Trait,
5354
];
5455

5556
impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
@@ -180,6 +181,19 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
180181
flags.push((sym::cause, Some("MainFunctionType".to_string())));
181182
}
182183

184+
if let Some(kind) = self.tcx.fn_trait_kind_from_def_id(trait_ref.def_id)
185+
&& let ty::Tuple(args) = trait_ref.args.type_at(1).kind()
186+
{
187+
let args = args
188+
.iter()
189+
.map(|ty| ty.to_string())
190+
.collect::<Vec<_>>()
191+
.join(", ");
192+
flags.push((sym::Trait, Some(format!("{}({args})", kind.as_str()))));
193+
} else {
194+
flags.push((sym::Trait, Some(trait_ref.print_only_trait_path().to_string())));
195+
}
196+
183197
// Add all types without trimmed paths.
184198
ty::print::with_no_trimmed_paths!({
185199
let generics = self.tcx.generics_of(def_id);

library/core/src/ops/function.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ use crate::marker::Tuple;
5656
#[lang = "fn"]
5757
#[stable(feature = "rust1", since = "1.0.0")]
5858
#[rustc_paren_sugar]
59-
#[rustc_on_unimplemented(
59+
#[cfg_attr(not(bootstrap), rustc_on_unimplemented(
6060
on(
6161
Args = "()",
6262
note = "wrap the `{Self}` in a closure with no arguments: `|| {{ /* code */ }}`"
@@ -67,9 +67,9 @@ use crate::marker::Tuple;
6767
// SAFETY: tidy is not smart enough to tell that the below unsafe block is a string
6868
label = "call the function in a closure: `|| unsafe {{ /* code */ }}`"
6969
),
70-
message = "expected a `{Fn}<{Args}>` closure, found `{Self}`",
71-
label = "expected an `Fn<{Args}>` closure, found `{Self}`"
72-
)]
70+
message = "expected a `{Trait}` closure, found `{Self}`",
71+
label = "expected an `{Trait}` closure, found `{Self}`"
72+
))]
7373
#[fundamental] // so that regex can rely that `&str: !FnMut`
7474
#[must_use = "closures are lazy and do nothing unless called"]
7575
// FIXME(effects) #[const_trait]
@@ -143,7 +143,7 @@ pub trait Fn<Args: Tuple>: FnMut<Args> {
143143
#[lang = "fn_mut"]
144144
#[stable(feature = "rust1", since = "1.0.0")]
145145
#[rustc_paren_sugar]
146-
#[rustc_on_unimplemented(
146+
#[cfg_attr(not(bootstrap), rustc_on_unimplemented(
147147
on(
148148
Args = "()",
149149
note = "wrap the `{Self}` in a closure with no arguments: `|| {{ /* code */ }}`"
@@ -154,9 +154,9 @@ pub trait Fn<Args: Tuple>: FnMut<Args> {
154154
// SAFETY: tidy is not smart enough to tell that the below unsafe block is a string
155155
label = "call the function in a closure: `|| unsafe {{ /* code */ }}`"
156156
),
157-
message = "expected a `{FnMut}<{Args}>` closure, found `{Self}`",
158-
label = "expected an `FnMut<{Args}>` closure, found `{Self}`"
159-
)]
157+
message = "expected a `{Trait}` closure, found `{Self}`",
158+
label = "expected an `{Trait}` closure, found `{Self}`"
159+
))]
160160
#[fundamental] // so that regex can rely that `&str: !FnMut`
161161
#[must_use = "closures are lazy and do nothing unless called"]
162162
// FIXME(effects) #[const_trait]
@@ -222,7 +222,7 @@ pub trait FnMut<Args: Tuple>: FnOnce<Args> {
222222
#[lang = "fn_once"]
223223
#[stable(feature = "rust1", since = "1.0.0")]
224224
#[rustc_paren_sugar]
225-
#[rustc_on_unimplemented(
225+
#[cfg_attr(not(bootstrap), rustc_on_unimplemented(
226226
on(
227227
Args = "()",
228228
note = "wrap the `{Self}` in a closure with no arguments: `|| {{ /* code */ }}`"
@@ -233,9 +233,9 @@ pub trait FnMut<Args: Tuple>: FnOnce<Args> {
233233
// SAFETY: tidy is not smart enough to tell that the below unsafe block is a string
234234
label = "call the function in a closure: `|| unsafe {{ /* code */ }}`"
235235
),
236-
message = "expected a `{FnOnce}<{Args}>` closure, found `{Self}`",
237-
label = "expected an `FnOnce<{Args}>` closure, found `{Self}`"
238-
)]
236+
message = "expected a `{Trait}` closure, found `{Self}`",
237+
label = "expected an `{Trait}` closure, found `{Self}`"
238+
))]
239239
#[fundamental] // so that regex can rely that `&str: !FnMut`
240240
#[must_use = "closures are lazy and do nothing unless called"]
241241
// FIXME(effects) #[const_trait]

tests/ui/closures/closure-expected.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fn main() {
22
let x = Some(1);
33
let y = x.or_else(4);
4-
//~^ ERROR expected a `FnOnce<()>` closure, found `{integer}`
4+
//~^ ERROR expected a `FnOnce()` closure, found `{integer}`
55
}

tests/ui/closures/closure-expected.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0277]: expected a `FnOnce<()>` closure, found `{integer}`
1+
error[E0277]: expected a `FnOnce()` closure, found `{integer}`
22
--> $DIR/closure-expected.rs:3:23
33
|
44
LL | let y = x.or_else(4);
5-
| ------- ^ expected an `FnOnce<()>` closure, found `{integer}`
5+
| ------- ^ expected an `FnOnce()` closure, found `{integer}`
66
| |
77
| required by a bound introduced by this call
88
|

tests/ui/closures/coerce-unsafe-to-closure.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0277]: expected a `FnOnce<(&str,)>` closure, found `unsafe extern "rust-intrinsic" fn(_) -> _ {transmute::<_, _>}`
1+
error[E0277]: expected a `FnOnce(&str)` closure, found `unsafe extern "rust-intrinsic" fn(_) -> _ {transmute::<_, _>}`
22
--> $DIR/coerce-unsafe-to-closure.rs:2:44
33
|
44
LL | let x: Option<&[u8]> = Some("foo").map(std::mem::transmute);

tests/ui/extern/extern-wrong-value-type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ fn main() {
77
// extern functions are extern "C" fn
88
let _x: extern "C" fn() = f; // OK
99
is_fn(f);
10-
//~^ ERROR expected a `Fn<()>` closure, found `extern "C" fn() {f}`
10+
//~^ ERROR expected a `Fn()` closure, found `extern "C" fn() {f}`
1111
}

tests/ui/extern/extern-wrong-value-type.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0277]: expected a `Fn<()>` closure, found `extern "C" fn() {f}`
1+
error[E0277]: expected a `Fn()` closure, found `extern "C" fn() {f}`
22
--> $DIR/extern-wrong-value-type.rs:9:11
33
|
44
LL | is_fn(f);
5-
| ----- ^ expected an `Fn<()>` closure, found `extern "C" fn() {f}`
5+
| ----- ^ expected an `Fn()` closure, found `extern "C" fn() {f}`
66
| |
77
| required by a bound introduced by this call
88
|

tests/ui/fn/fn-trait-formatting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ fn main() {
1717
//~| found struct `Box<dyn FnMut() -> isize>`
1818

1919
needs_fn(1);
20-
//~^ ERROR expected a `Fn<(isize,)>` closure, found `{integer}`
20+
//~^ ERROR expected a `Fn(isize)` closure, found `{integer}`
2121
}

tests/ui/fn/fn-trait-formatting.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ LL | let _: () = Box::new(|| -> isize { unimplemented!() }) as Box<dyn FnMut
3939
= note: expected unit type `()`
4040
found struct `Box<dyn FnMut() -> isize>`
4141

42-
error[E0277]: expected a `Fn<(isize,)>` closure, found `{integer}`
42+
error[E0277]: expected a `Fn(isize)` closure, found `{integer}`
4343
--> $DIR/fn-trait-formatting.rs:19:14
4444
|
4545
LL | needs_fn(1);
46-
| -------- ^ expected an `Fn<(isize,)>` closure, found `{integer}`
46+
| -------- ^ expected an `Fn(isize)` closure, found `{integer}`
4747
| |
4848
| required by a bound introduced by this call
4949
|

0 commit comments

Comments
 (0)