Skip to content

Explain type mismatch cause pointing to return type when it is impl Trait #57793

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

Merged
merged 2 commits into from
Jan 24, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 24 additions & 5 deletions src/librustc_typeck/check/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1199,7 +1199,6 @@ impl<'gcx, 'tcx, 'exprs, E> CoerceMany<'gcx, 'tcx, 'exprs, E>
(self.final_ty.unwrap_or(self.expected_ty), expression_ty)
};

let reason_label = "expected because of this statement";
let mut db;
match cause.code {
ObligationCauseCode::ReturnNoExpression => {
Expand Down Expand Up @@ -1244,26 +1243,46 @@ impl<'gcx, 'tcx, 'exprs, E> CoerceMany<'gcx, 'tcx, 'exprs, E>
// as prior return coercions would not be relevant (#57664).
let parent_id = fcx.tcx.hir().get_parent_node(blk_id);
let parent = fcx.tcx.hir().get(fcx.tcx.hir().get_parent_node(parent_id));
if fcx.get_node_fn_decl(parent).is_some() && !pointing_at_return_type {
if let (Some((fn_decl, _, _)), false) = (
fcx.get_node_fn_decl(parent),
pointing_at_return_type,
) {
if let Some(sp) = fcx.ret_coercion_span.borrow().as_ref() {
db.span_label(*sp, reason_label);
db.span_label(
fn_decl.output.span(),
"expected because this return type...",
);
db.span_label(*sp, format!(
"...is found to be `{}` here",
fcx.resolve_type_vars_with_obligations(expected),
));
}
}
}
ObligationCauseCode::ReturnType(_id) => {
db = fcx.report_mismatched_types(cause, expected, found, err);
let _id = fcx.tcx.hir().get_parent_node(_id);
let mut pointing_at_return_type = false;
let mut return_sp = None;
if let Some((fn_decl, can_suggest)) = fcx.get_fn_decl(_id) {
pointing_at_return_type = fcx.suggest_missing_return_type(
&mut db, &fn_decl, expected, found, can_suggest);
if !pointing_at_return_type {
return_sp = Some(fn_decl.output.span()); // `impl Trait` return type
}
}
if let (Some(sp), false) = (
fcx.ret_coercion_span.borrow().as_ref(),
pointing_at_return_type,
) {
if !sp.overlaps(cause.span) {
db.span_label(*sp, reason_label);
if let Some(return_sp) = return_sp {
db.span_label(return_sp, "expected because this return type...");
db.span_label( *sp, format!(
"...is found to be `{}` here",
fcx.resolve_type_vars_with_obligations(expected),
));
} else if !sp.overlaps(cause.span) {
db.span_label(*sp, "expected because of this statement");
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/test/ui/impl-trait/equality.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
error[E0308]: mismatched types
--> $DIR/equality.rs:15:5
|
LL | fn two(x: bool) -> impl Foo {
| -------- expected because this return type...
LL | if x {
LL | return 1_i32;
| ----- expected because of this statement
| ----- ...is found to be `i32` here
LL | }
LL | 0_u32
| ^^^^^ expected i32, found u32
Expand Down
14 changes: 11 additions & 3 deletions src/test/ui/point-to-type-err-cause-on-impl-trait-return.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
error[E0308]: mismatched types
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:5:5
|
LL | fn foo() -> impl std::fmt::Display {
| ---------------------- expected because this return type...
LL | if false {
LL | return 0i32;
| ---- expected because of this statement
| ---- ...is found to be `i32` here
LL | }
LL | 1u32
| ^^^^ expected i32, found u32
Expand All @@ -13,8 +16,11 @@ LL | 1u32
error[E0308]: mismatched types
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:13:16
|
LL | fn bar() -> impl std::fmt::Display {
| ---------------------- expected because this return type...
LL | if false {
LL | return 0i32;
| ---- expected because of this statement
| ---- ...is found to be `i32` here
LL | } else {
LL | return 1u32;
| ^^^^ expected i32, found u32
Expand All @@ -25,10 +31,12 @@ LL | return 1u32;
error[E0308]: mismatched types
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:19:5
|
LL | fn baz() -> impl std::fmt::Display {
| ---------------------- expected because this return type...
LL | / if false {
LL | | //~^ ERROR mismatched types
LL | | return 0i32;
| | ---- expected because of this statement
| | ---- ...is found to be `i32` here
LL | | } else {
LL | | 1u32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly unrelated to this PR, but shouldn't the type mismatch span point to this 1u32 instead of the entire if expression?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made a few changes recently to point at the tail expression in blocks instead of entire blocks, but haven't finished yet. This change is already in my plans :)

LL | | }
Expand Down