Skip to content
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 @@ -4479,7 +4479,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
}
ObligationCauseCode::OpaqueReturnType(expr_info) => {
let (expr_ty, expr) = if let Some((expr_ty, hir_id)) = expr_info {
let expr_ty = tcx.short_string(expr_ty, err.long_ty_path());
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()
Expand All @@ -4494,15 +4493,27 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
&& let ty::ClauseKind::Trait(pred) = pred.kind().skip_binder()
&& self.can_eq(param_env, pred.self_ty(), expr_ty)
{
let expr_ty = tcx.short_string(expr_ty, err.long_ty_path());
(expr_ty, expr)
} else {
return;
};
let expr_ty_string = tcx.short_string(expr_ty, err.long_ty_path());
if expr_ty.is_never()
&& let span = expr.span.source_callsite()
&& let Ok(snippet) = tcx.sess.source_map().span_to_snippet(span)
&& span != expr.span
{
err.span_suggestion(
span,
"`!` can be coerced to any type; consider casting it to a concrete type that implements the trait",
format!("{snippet} as /* Type */"),
Applicability::HasPlaceholders,
);
}
err.span_label(
expr.span,
with_forced_trimmed_paths!(format!(
"return type was inferred to be `{expr_ty}` here",
"return type was inferred to be `{expr_ty_string}` here",
)),
);
suggest_remove_deref(err, &expr);
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/impl-trait/return-never-type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//@ edition:2024

#![feature(never_type)]

use std::ops::Add;

fn foo() -> impl Add<u32> {
//~^ ERROR cannot add `u32` to `!`
//~| HELP the trait `Add<u32>` is not implemented for `!`
unimplemented!()
//~^ HELP `!` can be coerced to any type; consider casting it to a concrete type that implements the trait
}

fn main() {}
18 changes: 18 additions & 0 deletions tests/ui/impl-trait/return-never-type.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error[E0277]: cannot add `u32` to `!`
--> $DIR/return-never-type.rs:7:13
|
LL | fn foo() -> impl Add<u32> {
| ^^^^^^^^^^^^^ no implementation for `! + u32`
...
LL | unimplemented!()
| ---------------- return type was inferred to be `!` here
|
= help: the trait `Add<u32>` is not implemented for `!`
help: `!` can be coerced to any type; consider casting it to a concrete type that implements the trait
|
LL | unimplemented!() as /* Type */
| +++++++++++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.
Loading