Skip to content

Fix wrong report on closure args mismatch when a ref is expected but not found #42270

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

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions src/librustc/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2027,4 +2027,5 @@ register_diagnostics! {
E0495, // cannot infer an appropriate lifetime due to conflicting requirements
E0566, // conflicting representation hints
E0587, // conflicting packed and align representation hints
E0623, // type mismatch in closure arguments
}
63 changes: 38 additions & 25 deletions src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,32 +725,27 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
expected_found.found,
expected_trait_ty.is_closure())
} else if let &TypeError::Sorts(ref expected_found) = e {
let expected = if let ty::TyTuple(tys, _) = expected_found.expected.sty {
tys.len()
} else {
1
};
let found = if let ty::TyTuple(tys, _) = expected_found.found.sty {
tys.len()
} else {
1
};

if expected != found {
// Expected `|| { }`, found `|x, y| { }`
// Expected `fn(x) -> ()`, found `|| { }`
self.report_arg_count_mismatch(span,
found_span,
expected,
found,
expected_trait_ty.is_closure())
let mut count_mismatch = None;
if let ty::TyTuple(expected_tys, _) = expected_found.expected.sty {
if let ty::TyTuple(found_tys, _) = expected_found.found.sty {
if expected_tys.len() != found_tys.len() {
// Expected `|| { }`, found `|x, y| { }`
// Expected `fn(x) -> ()`, found `|| { }`
count_mismatch = Some(self.report_arg_count_mismatch(span,
found_span,
expected_tys.len(),
found_tys.len(),
expected_trait_ty.is_closure()));
}
}
}
if let Some(count_mismatch) = count_mismatch {
count_mismatch
} else {
self.report_type_argument_mismatch(span,
found_span,
expected_trait_ty,
expected_trait_ref,
actual_trait_ref,
e)
self.report_closure_arg_mismatch(span,
found_span,
expected_trait_ref,
actual_trait_ref)
}
} else {
self.report_type_argument_mismatch(span,
Expand Down Expand Up @@ -825,6 +820,24 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
}
err
}

fn report_closure_arg_mismatch(&self,
span: Span,
found_span: Option<Span>,
expected_ref: ty::PolyTraitRef<'tcx>,
found: ty::PolyTraitRef<'tcx>)
-> DiagnosticBuilder<'tcx>
{
let mut err = struct_span_err!(self.tcx.sess, span, E0623,
"type mismatch in closure arguments");
if let Some(sp) = found_span {
err.span_label(span, format!("expected closure that takes a `{}`", found));
Copy link
Contributor

@nikomatsakis nikomatsakis Jul 7, 2017

Choose a reason for hiding this comment

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

we should be able to extract the types of the arguments by examining the substs of the expected_ref and found trait-refs. Something like:

let expected_args = expected_ref.map_bound_ref(|trait_ref| trait_ref.substs.type_at(1));

But it might need to be type_at(0). That will give you a Binder<Ty<'tcx>>, which you should be able to use with {} I think.

err.span_label(sp, format!("takes a `{}`", expected_ref));
} else {
panic!();
}
err
}
}

impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
Expand Down
19 changes: 19 additions & 0 deletions src/test/ui/mismatched_types/closure-arg-type-mismatch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn main() {
let a = [(1u32,2u32)];
let b = a.iter().map(|x: (u32, u32)| 45);
let d1 = a.iter().map(|x: &(u16,u16)| 45);
let d2 = a.iter().map(|x: (u16,u16)| 45);
foo(|y: isize| ());
}

fn foo<F>(m: F) where F: ::std::ops::Fn(usize) {}
36 changes: 36 additions & 0 deletions src/test/ui/mismatched_types/closure-arg-type-mismatch.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
error[E0623]: type mismatch in closure arguments
--> $DIR/closure-arg-type-mismatch.rs:13:26
|
13 | let b = a.iter().map(|x: (u32, u32)| 45);
| ^^^ ------------------ takes a `std::ops::FnMut<((u32, u32),)>`
| |
| expected closure that takes a `std::ops::FnMut<(&(u32, u32),)>`

error[E0623]: type mismatch in closure arguments
--> $DIR/closure-arg-type-mismatch.rs:14:27
|
14 | let d1 = a.iter().map(|x: &(u16,u16)| 45);
| ^^^ ------------------ takes a `for<'r> std::ops::FnMut<(&'r (u16, u16),)>`
| |
| expected closure that takes a `std::ops::FnMut<(&(u32, u32),)>`

error[E0623]: type mismatch in closure arguments
--> $DIR/closure-arg-type-mismatch.rs:15:27
|
15 | let d2 = a.iter().map(|x: (u16,u16)| 45);
| ^^^ ----------------- takes a `std::ops::FnMut<((u16, u16),)>`
| |
| expected closure that takes a `std::ops::FnMut<(&(u32, u32),)>`

error[E0623]: type mismatch in closure arguments
--> $DIR/closure-arg-type-mismatch.rs:16:9
|
16 | foo(|y: isize| ());
| ^^^ ------------- takes a `std::ops::Fn<(isize,)>`
| |
| expected closure that takes a `std::ops::Fn<(usize,)>`
|
= note: required by `foo`

error: aborting due to 4 previous errors