Skip to content

Commit

Permalink
Point at tail expression on rpit E0277
Browse files Browse the repository at this point in the history
```
error[E0277]: the trait bound `{gen block@$DIR/gen_block_is_coro.rs:7:5: 7:8}: Coroutine` is not satisfied
  --> $DIR/gen_block_is_coro.rs:6:13
   |
LL | fn foo() -> impl Coroutine<Yield = u32, Return = ()> {
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Coroutine` is not implemented for `{gen block@$DIR/gen_block_is_coro.rs:7:5: 7:8}`
LL |     gen { yield 42 }
   |     ---------------- return type was inferred to be `{gen block@$DIR/gen_block_is_coro.rs:7:5: 7:8}` here
```

The secondary span label is new.
  • Loading branch information
estebank committed Oct 25, 2024
1 parent a50c8eb commit 6651238
Show file tree
Hide file tree
Showing 20 changed files with 121 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3567,17 +3567,34 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
)]);
}
ObligationCauseCode::OpaqueReturnType(expr_info) => {
if let Some((expr_ty, hir_id)) = expr_info {
let expr_ty = self.tcx.short_ty_string(expr_ty, long_ty_file);
let expr = self.infcx.tcx.hir().expect_expr(hir_id);
err.span_label(
expr.span,
with_forced_trimmed_paths!(format!(
"return type was inferred to be `{expr_ty}` here",
)),
);
suggest_remove_deref(err, &expr);
}
let (expr_ty, expr) = if let Some((expr_ty, hir_id)) = expr_info {
let expr_ty = tcx.short_ty_string(expr_ty, long_ty_file);
let expr = tcx.hir().expect_expr(hir_id);
(expr_ty, expr)
} else if let Some(body_id) = tcx.hir_node_by_def_id(body_id).body_id()
&& let body = tcx.hir().body(body_id)
&& let hir::ExprKind::Block(block, _) = body.value.kind
&& let Some(expr) = block.expr
&& let Some(expr_ty) = self
.typeck_results
.as_ref()
.and_then(|typeck| typeck.node_type_opt(expr.hir_id))
&& let Some(pred) = predicate.as_clause()
&& let ty::ClauseKind::Trait(pred) = pred.kind().skip_binder()
&& self.can_eq(param_env, pred.self_ty(), expr_ty)
{
let expr_ty = tcx.short_ty_string(expr_ty, long_ty_file);
(expr_ty, expr)
} else {
return;
};
err.span_label(
expr.span,
with_forced_trimmed_paths!(format!(
"return type was inferred to be `{expr_ty}` here",
)),
);
suggest_remove_deref(err, &expr);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/async-await/async-error-span.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ error[E0277]: `()` is not a future
|
LL | fn get_future() -> impl Future<Output = ()> {
| ^^^^^^^^^^^^^^^^^^^^^^^^ `()` is not a future
LL |
LL | panic!()
| -------- return type was inferred to be `_` here
|
= help: the trait `Future` is not implemented for `()`

Expand Down
5 changes: 4 additions & 1 deletion tests/ui/coroutine/arg-count-mismatch-on-unit-input.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ LL | fn foo() -> impl Coroutine<u8> {
| ^^^^^^^^^^^^^^^^^^ expected due to this
...
LL | |_: ()| {}
| ------- found signature defined here
| ----------
| |
| found signature defined here
| return type was inferred to be `{coroutine@$DIR/arg-count-mismatch-on-unit-input.rs:8:5: 8:12}` here
|
= note: expected coroutine signature `fn(u8) -> _`
found coroutine signature `fn(()) -> _`
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/coroutine/gen_block_is_coro.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@ error[E0277]: the trait bound `{gen block@$DIR/gen_block_is_coro.rs:7:5: 7:8}: C
|
LL | fn foo() -> impl Coroutine<Yield = u32, Return = ()> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Coroutine` is not implemented for `{gen block@$DIR/gen_block_is_coro.rs:7:5: 7:8}`
LL | gen { yield 42 }
| ---------------- return type was inferred to be `{gen block@$DIR/gen_block_is_coro.rs:7:5: 7:8}` here

error[E0277]: the trait bound `{gen block@$DIR/gen_block_is_coro.rs:11:5: 11:8}: Coroutine` is not satisfied
--> $DIR/gen_block_is_coro.rs:10:13
|
LL | fn bar() -> impl Coroutine<Yield = i64, Return = ()> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Coroutine` is not implemented for `{gen block@$DIR/gen_block_is_coro.rs:11:5: 11:8}`
LL | gen { yield 42 }
| ---------------- return type was inferred to be `{gen block@$DIR/gen_block_is_coro.rs:11:5: 11:8}` here

error[E0277]: the trait bound `{gen block@$DIR/gen_block_is_coro.rs:15:5: 15:8}: Coroutine` is not satisfied
--> $DIR/gen_block_is_coro.rs:14:13
|
LL | fn baz() -> impl Coroutine<Yield = i32, Return = ()> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Coroutine` is not implemented for `{gen block@$DIR/gen_block_is_coro.rs:15:5: 15:8}`
LL | gen { yield 42 }
| ---------------- return type was inferred to be `{gen block@$DIR/gen_block_is_coro.rs:15:5: 15:8}` here

error: aborting due to 3 previous errors

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/coroutine/gen_block_is_no_future.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ error[E0277]: `{gen block@$DIR/gen_block_is_no_future.rs:5:5: 5:8}` is not a fut
|
LL | fn foo() -> impl std::future::Future {
| ^^^^^^^^^^^^^^^^^^^^^^^^ `{gen block@$DIR/gen_block_is_no_future.rs:5:5: 5:8}` is not a future
LL | gen { yield 42 }
| ---------------- return type was inferred to be `{gen block@$DIR/gen_block_is_no_future.rs:5:5: 5:8}` here
|
= help: the trait `Future` is not implemented for `{gen block@$DIR/gen_block_is_no_future.rs:5:5: 5:8}`

Expand Down
1 change: 1 addition & 0 deletions tests/ui/coroutine/issue-88653.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ fn foo(bar: bool) -> impl Coroutine<(bool,)> {
#[coroutine]
|bar| {
//~^ NOTE: found signature defined here
//~| NOTE: return type was inferred to be
if bar {
yield bar;
}
Expand Down
18 changes: 14 additions & 4 deletions tests/ui/coroutine/issue-88653.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
error[E0631]: type mismatch in coroutine arguments
--> $DIR/issue-88653.rs:8:22
|
LL | fn foo(bar: bool) -> impl Coroutine<(bool,)> {
| ^^^^^^^^^^^^^^^^^^^^^^^ expected due to this
LL | fn foo(bar: bool) -> impl Coroutine<(bool,)> {
| ^^^^^^^^^^^^^^^^^^^^^^^ expected due to this
...
LL | |bar| {
| ----- found signature defined here
LL | |bar| {
| -----
| |
| _____found signature defined here
| |
LL | |
LL | |
LL | | if bar {
LL | | yield bar;
LL | | }
LL | | }
| |_____- return type was inferred to be `{coroutine@$DIR/issue-88653.rs:15:5: 15:10}` here
|
= note: expected coroutine signature `fn((bool,)) -> _`
found coroutine signature `fn(bool) -> _`
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions tests/ui/impl-trait/issue-55872-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ error[E0277]: the trait bound `S: Copy` is not satisfied in `(S, T)`
|
LL | fn foo<T: Default>() -> Self::E {
| ^^^^^^^ within `(S, T)`, the trait `Copy` is not implemented for `S`, which is required by `(S, T): Copy`
...
LL | (S::default(), T::default())
| ---------------------------- return type was inferred to be `(S, T)` here
|
= note: required because it appears within the type `(S, T)`
help: consider further restricting this bound
Expand All @@ -24,6 +27,9 @@ error[E0277]: the trait bound `T: Copy` is not satisfied in `(S, T)`
|
LL | fn foo<T: Default>() -> Self::E {
| ^^^^^^^ within `(S, T)`, the trait `Copy` is not implemented for `T`, which is required by `(S, T): Copy`
...
LL | (S::default(), T::default())
| ---------------------------- return type was inferred to be `(S, T)` here
|
= note: required because it appears within the type `(S, T)`
help: consider further restricting this bound
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/impl-trait/issue-55872-3.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ error[E0277]: the trait bound `{async block@$DIR/issue-55872-3.rs:15:9: 15:14}:
|
LL | fn foo<T>() -> Self::E {
| ^^^^^^^ the trait `Copy` is not implemented for `{async block@$DIR/issue-55872-3.rs:15:9: 15:14}`
LL |
LL | async {}
| -------- return type was inferred to be `{async block@$DIR/issue-55872-3.rs:15:9: 15:14}` here

error: aborting due to 1 previous error

Expand Down
8 changes: 6 additions & 2 deletions tests/ui/impl-trait/nested_impl_trait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ error[E0277]: the trait bound `impl Debug: From<impl Into<u32>>` is not satisfie
--> $DIR/nested_impl_trait.rs:6:46
|
LL | fn bad_in_ret_position(x: impl Into<u32>) -> impl Into<impl Debug> { x }
| ^^^^^^^^^^^^^^^^^^^^^ the trait `From<impl Into<u32>>` is not implemented for `impl Debug`, which is required by `impl Into<u32>: Into<impl Debug>`
| ^^^^^^^^^^^^^^^^^^^^^ - return type was inferred to be `impl Into<u32>` here
| |
| the trait `From<impl Into<u32>>` is not implemented for `impl Debug`, which is required by `impl Into<u32>: Into<impl Debug>`
|
= help: the trait `Into<U>` is implemented for `T`
= note: required for `impl Into<u32>` to implement `Into<impl Debug>`
Expand All @@ -55,7 +57,9 @@ error[E0277]: the trait bound `impl Debug: From<impl Into<u32>>` is not satisfie
--> $DIR/nested_impl_trait.rs:19:34
|
LL | fn bad(x: impl Into<u32>) -> impl Into<impl Debug> { x }
| ^^^^^^^^^^^^^^^^^^^^^ the trait `From<impl Into<u32>>` is not implemented for `impl Debug`, which is required by `impl Into<u32>: Into<impl Debug>`
| ^^^^^^^^^^^^^^^^^^^^^ - return type was inferred to be `impl Into<u32>` here
| |
| the trait `From<impl Into<u32>>` is not implemented for `impl Debug`, which is required by `impl Into<u32>: Into<impl Debug>`
|
= help: the trait `Into<U>` is implemented for `T`
= note: required for `impl Into<u32>` to implement `Into<impl Debug>`
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/impl-trait/opaque-cast-field-access-in-future.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ error[E0283]: type annotations needed
|
LL | fn run() -> Foo<impl Future<Output = ()>> {
| ^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type
LL |
LL | loop {}
| ------- return type was inferred to be `!` here
|
= note: cannot satisfy `_: Future`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ error[E0271]: type mismatch resolving `<() as Super>::Assoc == ()`
|
LL | fn test() -> impl Test {
| ^^^^^^^^^ type mismatch resolving `<() as Super>::Assoc == ()`
LL |
LL | ()
| -- return type was inferred to be `()` here
|
note: expected this to be `()`
--> $DIR/projection-mismatch-in-impl-where-clause.rs:6:18
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/lifetimes/lifetime-elision-return-type-trait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ error[E0277]: the trait bound `Result<(), _>: Future` is not satisfied
|
LL | fn foo() -> impl Future<Item=(), Error=Box<dyn Error>> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Future` is not implemented for `Result<(), _>`
LL |
LL | Ok(())
| ------ return type was inferred to be `Result<(), _>` here
|
help: this trait has no implementations, consider adding one
--> $DIR/lifetime-elision-return-type-trait.rs:1:1
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/lint/issue-106991.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ error[E0271]: expected `foo` to be a fn item that returns `i32`, but it returns
|
LL | fn bar() -> impl Iterator<Item = i32> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `i32`, found `()`
...
LL | x.iter_mut().map(foo)
| --------------------- return type was inferred to be `Map<std::slice::IterMut<'_, Vec<u8>>, for<'a> fn(&'a mut Vec<u8>) {foo}>` here
|
= note: required for `Map<std::slice::IterMut<'_, Vec<u8>>, for<'a> fn(&'a mut Vec<u8>) {foo}>` to implement `Iterator`

Expand Down
6 changes: 6 additions & 0 deletions tests/ui/never_type/impl_trait_fallback2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ error[E0277]: the trait bound `(): T` is not satisfied
|
LL | fn should_ret_unit() -> impl T {
| ^^^^^^ the trait `T` is not implemented for `()`
LL |
LL | panic!()
| -------- return type was inferred to be `_` here
|
= help: the trait `T` is implemented for `i32`

Expand All @@ -11,6 +14,9 @@ error[E0277]: the trait bound `(): T` is not satisfied
|
LL | fn a() -> Foo {
| ^^^ the trait `T` is not implemented for `()`
LL |
LL | panic!()
| -------- return type was inferred to be `_` here
|
= help: the trait `T` is implemented for `i32`

Expand Down
3 changes: 3 additions & 0 deletions tests/ui/never_type/impl_trait_fallback3.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ error[E0277]: the trait bound `(): T` is not satisfied
|
LL | fn a() -> Foo {
| ^^^ the trait `T` is not implemented for `()`
...
LL | panic!()
| -------- return type was inferred to be `_` here
|
help: this trait has no implementations, consider adding one
--> $DIR/impl_trait_fallback3.rs:5:1
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/never_type/impl_trait_fallback4.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ error[E0277]: the trait bound `(): T` is not satisfied
|
LL | fn foo() -> impl T {
| ^^^^^^ the trait `T` is not implemented for `()`
LL |
LL | panic!()
| -------- return type was inferred to be `_` here
|
help: this trait has no implementations, consider adding one
--> $DIR/impl_trait_fallback4.rs:3:1
Expand Down
5 changes: 4 additions & 1 deletion tests/ui/type-alias-impl-trait/fallback.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ error[E0283]: type annotations needed
LL | fn unconstrained_foo() -> Wrapper<Foo> {
| ------------ type must be known at this point
LL | Wrapper::Second
| ^^^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the enum `Wrapper`
| ^^^^^^^^^^^^^^^
| |
| cannot infer type of the type parameter `T` declared on the enum `Wrapper`
| return type was inferred to be `Wrapper<_>` here
|
= note: cannot satisfy `_: Copy`
help: consider specifying the generic argument
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ error[E0277]: the trait bound `{integer}: Trait<()>` is not satisfied
|
LL | fn produce() -> impl for<T> Trait<(), Assoc = impl Trait<T>> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Trait<()>` is not implemented for `{integer}`
...
LL | 16
| -- return type was inferred to be `{integer}` here
|
help: this trait has no implementations, consider adding one
--> $DIR/non-lifetime-binder-in-constraint.rs:4:1
Expand Down

0 comments on commit 6651238

Please sign in to comment.