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

fix incomplete diagnostic notes when closure returns conflicting for genric type #84244

Merged
merged 1 commit into from
Apr 17, 2021
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
22 changes: 0 additions & 22 deletions compiler/rustc_typeck/src/check/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1492,28 +1492,6 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
if let (Some(sp), Some(fn_output)) = (fcx.ret_coercion_span.get(), fn_output) {
self.add_impl_trait_explanation(&mut err, cause, fcx, expected, sp, fn_output);
}

if let Some(sp) = fcx.ret_coercion_span.get() {
// If the closure has an explicit return type annotation,
// then a type error may occur at the first return expression we
// see in the closure (if it conflicts with the declared
// return type). Skip adding a note in this case, since it
// would be incorrect.
if !err.span.primary_spans().iter().any(|&span| span == sp) {
let hir = fcx.tcx.hir();
let body_owner = hir.body_owned_by(hir.enclosing_body_owner(fcx.body_id));
if fcx.tcx.is_closure(hir.body_owner_def_id(body_owner).to_def_id()) {
err.span_note(
sp,
&format!(
"return type inferred to be `{}` here",
fcx.resolve_vars_if_possible(expected)
),
);
}
}
}

err
}

Expand Down
29 changes: 29 additions & 0 deletions compiler/rustc_typeck/src/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.suggest_missing_parentheses(err, expr);
self.note_need_for_fn_pointer(err, expected, expr_ty);
self.note_internal_mutation_in_method(err, expr, expected, expr_ty);
self.report_closure_infered_return_type(err, expected)
}

// Requires that the two types unify, and prints an error message if
Expand Down Expand Up @@ -1061,4 +1062,32 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
_ => false,
}
}

// Report the type inferred by the return statement.
fn report_closure_infered_return_type(
&self,
err: &mut DiagnosticBuilder<'_>,
expected: Ty<'tcx>,
) {
if let Some(sp) = self.ret_coercion_span.get() {
// If the closure has an explicit return type annotation,
// then a type error may occur at the first return expression we
// see in the closure (if it conflicts with the declared
// return type). Skip adding a note in this case, since it
// would be incorrect.
if !err.span.primary_spans().iter().any(|&span| span == sp) {
let hir = self.tcx.hir();
let body_owner = hir.body_owned_by(hir.enclosing_body_owner(self.body_id));
if self.tcx.is_closure(hir.body_owner_def_id(body_owner).to_def_id()) {
err.span_note(
sp,
&format!(
"return type inferred to be `{}` here",
self.resolve_vars_if_possible(expected)
),
);
}
}
}
}
}
16 changes: 16 additions & 0 deletions src/test/ui/closures/issue-84128.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// test for issue 84128
// missing suggestion for similar ADT type with diffetent generic paramenter
// on closure ReturnNoExpression

struct Foo<T>(T);

fn main() {
|| {
if false {
return Foo(0);
}

Foo(())
//~^ ERROR mismatched types [E0308]
};
}
15 changes: 15 additions & 0 deletions src/test/ui/closures/issue-84128.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0308]: mismatched types
--> $DIR/issue-84128.rs:13:13
|
LL | Foo(())
| ^^ expected integer, found `()`
|
note: return type inferred to be `{integer}` here
Copy link
Member

Choose a reason for hiding this comment

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

It's not really the 'return type' that's being inferred here

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 missed that, thanks. I will fix it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't it either be "return type inferred to be Foo<{integer}> here" with span of Foo(0) indicated; or else "{integer} inferred here" (with the span of 0 indicated)? (My personal view, FWIW, is that the former is a tad more informative but either seems fine).

As it is, it's a bit confusing: essentially "{integer} inferred here" but Foo(0) indicated.

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 have some issue to solve this I am not sure how do do it. I found that the suggestions is emitted from
compiler\rustc_typeck\src\check\fn_ctxt\checks.rs: check_argument_types
compiler\rustc_typeck\src\check\demand.rs: demand_coerce
compiler\rustc_typeck\src\check\demand.rs: demand_coerce_diag
compiler\rustc_typeck\src\check\demand.rs: emit_coerce_suggestions
is there a way to get the return type from the FnCtxt ?

--> $DIR/issue-84128.rs:10:20
|
LL | return Foo(0);
| ^^^^^^

error: aborting due to previous error

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