Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shorten error message for callable with wrong return type #136414

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1518,7 +1518,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {

with_forced_trimmed_paths! {
if self.tcx.is_lang_item(projection_term.def_id, LangItem::FnOnceOutput) {
let fn_kind = self_ty.prefix_string(self.tcx);
let (span, closure_span) = if let ty::Closure(def_id, _) = self_ty.kind() {
let def_span = self.tcx.def_span(def_id);
if let Some(local_def_id) = def_id.as_local()
Expand Down Expand Up @@ -1555,8 +1554,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
_ => self_ty.to_string(),
};
Some((format!(
"expected `{item}` to be a {fn_kind} that returns `{expected_ty}`, but it \
returns `{normalized_ty}`",
"expected `{item}` to return `{expected_ty}`, but it returns `{normalized_ty}`",
), span, closure_span))
} else if self.tcx.is_lang_item(trait_def_id, LangItem::Future) {
Some((format!(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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}`
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}`
--> $DIR/is-not-fn.rs:8:14
|
LL | needs_fn(async || {});
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/async-await/async-closures/is-not-fn.next.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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}`
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}`
--> $DIR/is-not-fn.rs:8:14
|
LL | needs_fn(async || {});
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/async-await/async-closures/is-not-fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
fn main() {
fn needs_fn(x: impl FnOnce()) {}
needs_fn(async || {});
//~^ ERROR expected `{async [email protected]:8:14}` to be a closure that returns `()`
//~^ ERROR expected `{async [email protected]:8:14}` to return `()`
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ LL | true
= note: expected enum `Option<()>`
found type `bool`

error[E0271]: expected `{[email protected]:6:10}` to be a closure that returns `bool`, but it returns `Option<()>`
error[E0271]: expected `{[email protected]:6:10}` to return `bool`, but it returns `Option<()>`
--> $DIR/dont-ice-for-type-mismatch-in-closure-in-async.rs:6:16
|
LL | call(|| -> Option<()> {
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/async-await/issue-98634.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ impl Runtime {
fn main() {
Runtime.block_on(async {
StructAsync { callback }.await;
//~^ ERROR expected `callback` to be a fn item that returns `Pin<Box<dyn Future<Output = ()>>>`, but it returns `impl Future<Output = ()>`
//~| ERROR expected `callback` to be a fn item that returns `Pin<Box<dyn Future<Output = ()>>>`, but it returns `impl Future<Output = ()>`
//~| ERROR expected `callback` to be a fn item that returns `Pin<Box<dyn Future<Output = ()>>>`, but it returns `impl Future<Output = ()>`
//~^ ERROR expected `callback` to return `Pin<Box<dyn Future<Output = ()>>>`, but it returns `impl Future<Output = ()>`
//~| ERROR expected `callback` to return `Pin<Box<dyn Future<Output = ()>>>`, but it returns `impl Future<Output = ()>`
//~| ERROR expected `callback` to return `Pin<Box<dyn Future<Output = ()>>>`, but it returns `impl Future<Output = ()>`
});
}
6 changes: 3 additions & 3 deletions tests/ui/async-await/issue-98634.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0271]: expected `callback` to be a fn item that returns `Pin<Box<dyn Future<Output = ()>>>`, but it returns `impl Future<Output = ()>`
error[E0271]: expected `callback` to return `Pin<Box<dyn Future<Output = ()>>>`, but it returns `impl Future<Output = ()>`
--> $DIR/issue-98634.rs:45:23
|
LL | StructAsync { callback }.await;
Expand All @@ -10,7 +10,7 @@ note: required by a bound in `StructAsync`
LL | pub struct StructAsync<F: Fn() -> Pin<Box<dyn Future<Output = ()>>>> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `StructAsync`

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

error[E0271]: expected `callback` to be a fn item that returns `Pin<Box<dyn Future<Output = ()>>>`, but it returns `impl Future<Output = ()>`
error[E0271]: expected `callback` to return `Pin<Box<dyn Future<Output = ()>>>`, but it returns `impl Future<Output = ()>`
--> $DIR/issue-98634.rs:45:34
|
LL | StructAsync { callback }.await;
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/closures/return-type-doesnt-match-bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn foo<F>(f: F) -> ()
where
F: FnOnce() -> Result<(), Box<dyn Error>>,
{
f().or_else(|e| -> ! { //~ ERROR to be a closure that returns
f().or_else(|e| -> ! { //~ ERROR to return
eprintln!("{:?}", e);
exit(1)
});
Expand All @@ -15,7 +15,7 @@ fn bar<F>(f: F) -> ()
where
F: FnOnce() -> Result<(), Box<dyn Error>>,
{
let c = |e| -> ! { //~ ERROR to be a closure that returns
let c = |e| -> ! { //~ ERROR to return
eprintln!("{:?}", e);
exit(1)
};
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/closures/return-type-doesnt-match-bound.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0271]: expected `{[email protected]:8:17}` to be a closure that returns `Result<(), _>`, but it returns `!`
error[E0271]: expected `{[email protected]:8:17}` to return `Result<(), _>`, but it returns `!`
--> $DIR/return-type-doesnt-match-bound.rs:8:24
|
LL | f().or_else(|e| -> ! {
Expand All @@ -13,7 +13,7 @@ LL | f().or_else(|e| -> ! {
note: required by a bound in `Result::<T, E>::or_else`
--> $SRC_DIR/core/src/result.rs:LL:COL

error[E0271]: expected `{[email protected]:18:13}` to be a closure that returns `Result<(), _>`, but it returns `!`
error[E0271]: expected `{[email protected]:18:13}` to return `Result<(), _>`, but it returns `!`
--> $DIR/return-type-doesnt-match-bound.rs:18:20
|
LL | let c = |e| -> ! {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn main() {
L { //~ ERROR type mismatch
f: |x| {
drop(x);
Unit4 //~ ERROR to be a closure that returns `Unit3`, but it returns `Unit4`
Unit4 //~ ERROR to return `Unit3`, but it returns `Unit4`
},
},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ LL | where
LL | F: for<'r> T0<'r, (<Self as Ty<'r>>::V,), O = <B as Ty<'r>>::V>,
| ^^^^^^^^^^^^^^^^^^^^ required by this bound in `T1::m`

error[E0271]: expected `{[email protected]:40:16}` to be a closure that returns `Unit3`, but it returns `Unit4`
error[E0271]: expected `{[email protected]:40:16}` to return `Unit3`, but it returns `Unit4`
--> $DIR/issue-62203-hrtb-ice.rs:42:17
|
LL | let v = Unit2.m(
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/intrinsics/const-eval-select-bad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn baz(n: bool) -> i32 {

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

const fn args_ty_mismatch() {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/intrinsics/const-eval-select-bad.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ 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[E0271]: expected `bar` to be a fn item that returns `i32`, but it returns `bool`
error[E0271]: expected `bar` to return `i32`, but it returns `bool`
--> $DIR/const-eval-select-bad.rs:32:34
|
LL | const_eval_select((1,), foo, bar);
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/lint/issue-106991.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ fn foo(items: &mut Vec<u8>) {
}

fn bar() -> impl Iterator<Item = i32> {
//~^ ERROR expected `foo` to be a fn item that returns `i32`, but it returns `()` [E0271]
//~^ ERROR expected `foo` to return `i32`, but it returns `()` [E0271]
let mut x: Vec<Vec<u8>> = vec![vec![0, 2, 1], vec![5, 4, 3]];
x.iter_mut().map(foo)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/lint/issue-106991.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0271]: expected `foo` to be a fn item that returns `i32`, but it returns `()`
error[E0271]: expected `foo` to return `i32`, but it returns `()`
--> $DIR/issue-106991.rs:5:13
|
LL | fn bar() -> impl Iterator<Item = i32> {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/never_type/fallback-closure-wrap.fallback.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0271]: expected `{[email protected]:18:40}` to be a closure that returns `()`, but it returns `!`
error[E0271]: expected `{[email protected]:18:40}` to return `()`, but it returns `!`
--> $DIR/fallback-closure-wrap.rs:19:9
|
LL | let error = Closure::wrap(Box::new(move || {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/never_type/fallback-closure-wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::marker::PhantomData;
fn main() {
let error = Closure::wrap(Box::new(move || {
panic!("Can't connect to server.");
//[fallback]~^ to be a closure that returns `()`, but it returns `!`
//[fallback]~^ to return `()`, but it returns `!`
}) as Box<dyn FnMut()>);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ fn bar<X>(x: Box<dyn FnOnce() -> X>) {}

fn main() {
foo(async move || {});
//~^ ERROR expected `{async [email protected]:9:9}` to be a closure that returns `Box<_>`
//~^ ERROR expected `{async [email protected]:9:9}` to return `Box<_>`
bar(async move || {}); //~ ERROR mismatched types
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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}`
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}`
--> $DIR/dont-suggest-boxing-async-closure-body.rs:9:9
|
LL | foo(async move || {});
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/type-alias-impl-trait/issue-98604.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ async fn test() {}
#[allow(unused_must_use)]
fn main() {
Box::new(test) as AsyncFnPtr;
//~^ ERROR expected `test` to be a fn item that returns `Pin<Box<dyn Future<Output = ()>>>`, but it returns `impl Future<Output = ()>
//~^ ERROR expected `test` to return `Pin<Box<dyn Future<Output = ()>>>`, but it returns `impl Future<Output = ()>
}
2 changes: 1 addition & 1 deletion tests/ui/type-alias-impl-trait/issue-98604.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0271]: expected `test` to be a fn item that returns `Pin<Box<dyn Future<Output = ()>>>`, but it returns `impl Future<Output = ()>`
error[E0271]: expected `test` to return `Pin<Box<dyn Future<Output = ()>>>`, but it returns `impl Future<Output = ()>`
--> $DIR/issue-98604.rs:9:5
|
LL | Box::new(test) as AsyncFnPtr;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/type-alias-impl-trait/issue-98608.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fn hi() -> impl Sized {

fn main() {
let b: Box<dyn Fn() -> Box<u8>> = Box::new(hi);
//~^ ERROR expected `hi` to be a fn item that returns `Box<u8>`, but it returns `impl Sized`
//~^ ERROR expected `hi` to return `Box<u8>`, but it returns `impl Sized`
let boxed = b();
let null = *boxed;
println!("{null:?}");
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/type-alias-impl-trait/issue-98608.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0271]: expected `hi` to be a fn item that returns `Box<u8>`, but it returns `impl Sized`
error[E0271]: expected `hi` to return `Box<u8>`, but it returns `impl Sized`
--> $DIR/issue-98608.rs:6:39
|
LL | fn hi() -> impl Sized {
Expand Down
Loading