Skip to content

Commit dc4d387

Browse files
authored
Rollup merge of #136414 - estebank:expected-return-type, r=oli-obk
Shorten error message for callable with wrong return type ``` error: expected `{closure@...}` to return `Ret`, but it returns `Other` ``` instead of ``` error: expected `{closure@...}` to be a closure that returns `Ret`, but it returns `Other` ```
2 parents 5ab356f + 9ebbba4 commit dc4d387

23 files changed

+29
-31
lines changed

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -1504,7 +1504,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
15041504

15051505
with_forced_trimmed_paths! {
15061506
if self.tcx.is_lang_item(projection_term.def_id, LangItem::FnOnceOutput) {
1507-
let fn_kind = self_ty.prefix_string(self.tcx);
15081507
let (span, closure_span) = if let ty::Closure(def_id, _) = self_ty.kind() {
15091508
let def_span = self.tcx.def_span(def_id);
15101509
if let Some(local_def_id) = def_id.as_local()
@@ -1541,8 +1540,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
15411540
_ => self.tcx.short_string(self_ty, file),
15421541
};
15431542
Some((format!(
1544-
"expected `{item}` to be a {fn_kind} that returns `{expected_ty}`, but it \
1545-
returns `{normalized_ty}`",
1543+
"expected `{item}` to return `{expected_ty}`, but it returns `{normalized_ty}`",
15461544
), span, closure_span))
15471545
} else if self.tcx.is_lang_item(trait_def_id, LangItem::Future) {
15481546
Some((format!(

tests/ui/async-await/async-closures/is-not-fn.current.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0271]: expected `{async [email protected]:8:14}` to be a closure that returns `()`, but it returns `{async closure body@$DIR/is-not-fn.rs:8:23: 8:25}`
1+
error[E0271]: expected `{async [email protected]:8:14}` to return `()`, but it returns `{async closure body@$DIR/is-not-fn.rs:8:23: 8:25}`
22
--> $DIR/is-not-fn.rs:8:14
33
|
44
LL | needs_fn(async || {});

tests/ui/async-await/async-closures/is-not-fn.next.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0271]: expected `{async [email protected]:8:14}` to be a closure that returns `()`, but it returns `{async closure body@$DIR/is-not-fn.rs:8:23: 8:25}`
1+
error[E0271]: expected `{async [email protected]:8:14}` to return `()`, but it returns `{async closure body@$DIR/is-not-fn.rs:8:23: 8:25}`
22
--> $DIR/is-not-fn.rs:8:14
33
|
44
LL | needs_fn(async || {});

tests/ui/async-await/async-closures/is-not-fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
fn main() {
77
fn needs_fn(x: impl FnOnce()) {}
88
needs_fn(async || {});
9-
//~^ ERROR expected `{async [email protected]:8:14}` to be a closure that returns `()`
9+
//~^ ERROR expected `{async [email protected]:8:14}` to return `()`
1010
}

tests/ui/async-await/dont-ice-for-type-mismatch-in-closure-in-async.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ LL | true
2020
= note: expected enum `Option<()>`
2121
found type `bool`
2222

23-
error[E0271]: expected `{[email protected]:6:10}` to be a closure that returns `bool`, but it returns `Option<()>`
23+
error[E0271]: expected `{[email protected]:6:10}` to return `bool`, but it returns `Option<()>`
2424
--> $DIR/dont-ice-for-type-mismatch-in-closure-in-async.rs:6:16
2525
|
2626
LL | call(|| -> Option<()> {

tests/ui/async-await/issue-98634.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ impl Runtime {
4343
fn main() {
4444
Runtime.block_on(async {
4545
StructAsync { callback }.await;
46-
//~^ ERROR expected `callback` to be a fn item that returns `Pin<Box<dyn Future<Output = ()>>>`, but it returns `impl Future<Output = ()>`
47-
//~| ERROR expected `callback` to be a fn item that returns `Pin<Box<dyn Future<Output = ()>>>`, but it returns `impl Future<Output = ()>`
48-
//~| ERROR expected `callback` to be a fn item that returns `Pin<Box<dyn Future<Output = ()>>>`, but it returns `impl Future<Output = ()>`
46+
//~^ ERROR expected `callback` to return `Pin<Box<dyn Future<Output = ()>>>`, but it returns `impl Future<Output = ()>`
47+
//~| ERROR expected `callback` to return `Pin<Box<dyn Future<Output = ()>>>`, but it returns `impl Future<Output = ()>`
48+
//~| ERROR expected `callback` to return `Pin<Box<dyn Future<Output = ()>>>`, but it returns `impl Future<Output = ()>`
4949
});
5050
}

tests/ui/async-await/issue-98634.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0271]: expected `callback` to be a fn item that returns `Pin<Box<dyn Future<Output = ()>>>`, but it returns `impl Future<Output = ()>`
1+
error[E0271]: expected `callback` to return `Pin<Box<dyn Future<Output = ()>>>`, but it returns `impl Future<Output = ()>`
22
--> $DIR/issue-98634.rs:45:23
33
|
44
LL | StructAsync { callback }.await;
@@ -10,7 +10,7 @@ note: required by a bound in `StructAsync`
1010
LL | pub struct StructAsync<F: Fn() -> Pin<Box<dyn Future<Output = ()>>>> {
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `StructAsync`
1212

13-
error[E0271]: expected `callback` to be a fn item that returns `Pin<Box<dyn Future<Output = ()>>>`, but it returns `impl Future<Output = ()>`
13+
error[E0271]: expected `callback` to return `Pin<Box<dyn Future<Output = ()>>>`, but it returns `impl Future<Output = ()>`
1414
--> $DIR/issue-98634.rs:45:9
1515
|
1616
LL | StructAsync { callback }.await;
@@ -22,7 +22,7 @@ note: required by a bound in `StructAsync`
2222
LL | pub struct StructAsync<F: Fn() -> Pin<Box<dyn Future<Output = ()>>>> {
2323
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `StructAsync`
2424

25-
error[E0271]: expected `callback` to be a fn item that returns `Pin<Box<dyn Future<Output = ()>>>`, but it returns `impl Future<Output = ()>`
25+
error[E0271]: expected `callback` to return `Pin<Box<dyn Future<Output = ()>>>`, but it returns `impl Future<Output = ()>`
2626
--> $DIR/issue-98634.rs:45:34
2727
|
2828
LL | StructAsync { callback }.await;

tests/ui/closures/return-type-doesnt-match-bound.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fn foo<F>(f: F) -> ()
55
where
66
F: FnOnce() -> Result<(), Box<dyn Error>>,
77
{
8-
f().or_else(|e| -> ! { //~ ERROR to be a closure that returns
8+
f().or_else(|e| -> ! { //~ ERROR to return
99
eprintln!("{:?}", e);
1010
exit(1)
1111
});
@@ -15,7 +15,7 @@ fn bar<F>(f: F) -> ()
1515
where
1616
F: FnOnce() -> Result<(), Box<dyn Error>>,
1717
{
18-
let c = |e| -> ! { //~ ERROR to be a closure that returns
18+
let c = |e| -> ! { //~ ERROR to return
1919
eprintln!("{:?}", e);
2020
exit(1)
2121
};

tests/ui/closures/return-type-doesnt-match-bound.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0271]: expected `{[email protected]:8:17}` to be a closure that returns `Result<(), _>`, but it returns `!`
1+
error[E0271]: expected `{[email protected]:8:17}` to return `Result<(), _>`, but it returns `!`
22
--> $DIR/return-type-doesnt-match-bound.rs:8:24
33
|
44
LL | f().or_else(|e| -> ! {
@@ -13,7 +13,7 @@ LL | f().or_else(|e| -> ! {
1313
note: required by a bound in `Result::<T, E>::or_else`
1414
--> $SRC_DIR/core/src/result.rs:LL:COL
1515

16-
error[E0271]: expected `{[email protected]:18:13}` to be a closure that returns `Result<(), _>`, but it returns `!`
16+
error[E0271]: expected `{[email protected]:18:13}` to return `Result<(), _>`, but it returns `!`
1717
--> $DIR/return-type-doesnt-match-bound.rs:18:20
1818
|
1919
LL | let c = |e| -> ! {

tests/ui/higher-ranked/trait-bounds/issue-62203-hrtb-ice.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ fn main() {
3939
L { //~ ERROR type mismatch
4040
f: |x| {
4141
drop(x);
42-
Unit4 //~ ERROR to be a closure that returns `Unit3`, but it returns `Unit4`
42+
Unit4 //~ ERROR to return `Unit3`, but it returns `Unit4`
4343
},
4444
},
4545
);

tests/ui/higher-ranked/trait-bounds/issue-62203-hrtb-ice.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ LL | where
2929
LL | F: for<'r> T0<'r, (<Self as Ty<'r>>::V,), O = <B as Ty<'r>>::V>,
3030
| ^^^^^^^^^^^^^^^^^^^^ required by this bound in `T1::m`
3131

32-
error[E0271]: expected `{[email protected]:40:16}` to be a closure that returns `Unit3`, but it returns `Unit4`
32+
error[E0271]: expected `{[email protected]:40:16}` to return `Unit3`, but it returns `Unit4`
3333
--> $DIR/issue-62203-hrtb-ice.rs:42:17
3434
|
3535
LL | let v = Unit2.m(

tests/ui/intrinsics/const-eval-select-bad.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn baz(n: bool) -> i32 {
3030

3131
const fn return_ty_mismatch() {
3232
const_eval_select((1,), foo, bar);
33-
//~^ ERROR expected `bar` to be a fn item that returns `i32`, but it returns `bool`
33+
//~^ ERROR expected `bar` to return `i32`, but it returns `bool`
3434
}
3535

3636
const fn args_ty_mismatch() {

tests/ui/intrinsics/const-eval-select-bad.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ LL | const_eval_select((), 42, 0xDEADBEEF);
6060
= note: expected a function item, found {integer}
6161
= help: consult the documentation on `const_eval_select` for more information
6262

63-
error[E0271]: expected `bar` to be a fn item that returns `i32`, but it returns `bool`
63+
error[E0271]: expected `bar` to return `i32`, but it returns `bool`
6464
--> $DIR/const-eval-select-bad.rs:32:34
6565
|
6666
LL | const_eval_select((1,), foo, bar);

tests/ui/lint/issue-106991.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ fn foo(items: &mut Vec<u8>) {
33
}
44

55
fn bar() -> impl Iterator<Item = i32> {
6-
//~^ ERROR expected `foo` to be a fn item that returns `i32`, but it returns `()` [E0271]
6+
//~^ ERROR expected `foo` to return `i32`, but it returns `()` [E0271]
77
let mut x: Vec<Vec<u8>> = vec![vec![0, 2, 1], vec![5, 4, 3]];
88
x.iter_mut().map(foo)
99
}

tests/ui/lint/issue-106991.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0271]: expected `foo` to be a fn item that returns `i32`, but it returns `()`
1+
error[E0271]: expected `foo` to return `i32`, but it returns `()`
22
--> $DIR/issue-106991.rs:5:13
33
|
44
LL | fn bar() -> impl Iterator<Item = i32> {

tests/ui/never_type/fallback-closure-wrap.fallback.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0271]: expected `{[email protected]:18:40}` to be a closure that returns `()`, but it returns `!`
1+
error[E0271]: expected `{[email protected]:18:40}` to return `()`, but it returns `!`
22
--> $DIR/fallback-closure-wrap.rs:19:9
33
|
44
LL | let error = Closure::wrap(Box::new(move || {

tests/ui/never_type/fallback-closure-wrap.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::marker::PhantomData;
1717
fn main() {
1818
let error = Closure::wrap(Box::new(move || {
1919
panic!("Can't connect to server.");
20-
//[fallback]~^ to be a closure that returns `()`, but it returns `!`
20+
//[fallback]~^ to return `()`, but it returns `!`
2121
}) as Box<dyn FnMut()>);
2222
}
2323

tests/ui/suggestions/dont-suggest-boxing-async-closure-body.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ fn bar<X>(x: Box<dyn FnOnce() -> X>) {}
77

88
fn main() {
99
foo(async move || {});
10-
//~^ ERROR expected `{async [email protected]:9:9}` to be a closure that returns `Box<_>`
10+
//~^ ERROR expected `{async [email protected]:9:9}` to return `Box<_>`
1111
bar(async move || {}); //~ ERROR mismatched types
1212
}

tests/ui/suggestions/dont-suggest-boxing-async-closure-body.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0271]: expected `{async [email protected]:9:9}` to be a closure that returns `Box<_>`, but it returns `{async closure body@$DIR/dont-suggest-boxing-async-closure-body.rs:9:23: 9:25}`
1+
error[E0271]: expected `{async [email protected]:9:9}` to return `Box<_>`, but it returns `{async closure body@$DIR/dont-suggest-boxing-async-closure-body.rs:9:23: 9:25}`
22
--> $DIR/dont-suggest-boxing-async-closure-body.rs:9:9
33
|
44
LL | foo(async move || {});

tests/ui/type-alias-impl-trait/issue-98604.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ async fn test() {}
77
#[allow(unused_must_use)]
88
fn main() {
99
Box::new(test) as AsyncFnPtr;
10-
//~^ ERROR expected `test` to be a fn item that returns `Pin<Box<dyn Future<Output = ()>>>`, but it returns `impl Future<Output = ()>
10+
//~^ ERROR expected `test` to return `Pin<Box<dyn Future<Output = ()>>>`, but it returns `impl Future<Output = ()>
1111
}

tests/ui/type-alias-impl-trait/issue-98604.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0271]: expected `test` to be a fn item that returns `Pin<Box<dyn Future<Output = ()>>>`, but it returns `impl Future<Output = ()>`
1+
error[E0271]: expected `test` to return `Pin<Box<dyn Future<Output = ()>>>`, but it returns `impl Future<Output = ()>`
22
--> $DIR/issue-98604.rs:9:5
33
|
44
LL | Box::new(test) as AsyncFnPtr;

tests/ui/type-alias-impl-trait/issue-98608.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ fn hi() -> impl Sized {
44

55
fn main() {
66
let b: Box<dyn Fn() -> Box<u8>> = Box::new(hi);
7-
//~^ ERROR expected `hi` to be a fn item that returns `Box<u8>`, but it returns `impl Sized`
7+
//~^ ERROR expected `hi` to return `Box<u8>`, but it returns `impl Sized`
88
let boxed = b();
99
let null = *boxed;
1010
println!("{null:?}");

tests/ui/type-alias-impl-trait/issue-98608.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0271]: expected `hi` to be a fn item that returns `Box<u8>`, but it returns `impl Sized`
1+
error[E0271]: expected `hi` to return `Box<u8>`, but it returns `impl Sized`
22
--> $DIR/issue-98608.rs:6:39
33
|
44
LL | fn hi() -> impl Sized {

0 commit comments

Comments
 (0)