Skip to content

Commit daaa9a4

Browse files
committed
Fix ICE for mismatched args on target without span
Commit 7ed00ca improved our error reporting by including the target function in our error messages when there is an argument count mismatch. A simple example from the UI tests is: ``` error[E0593]: function is expected to take a single 2-tuple as argument, but it takes 0 arguments --> $DIR/closure-arg-count.rs:32:53 | 32 | let _it = vec![1, 2, 3].into_iter().enumerate().map(foo); | ^^^ expected function that takes a single 2-tuple as argument ... 44 | fn foo() {} | -------- takes 0 arguments ``` However, this assumed the target span was always available. This does not hold true if the target function is in `std` or another crate. A simple example from #48046 is assigning `str::split` to a function type with a different number of arguments. Fix by removing all of the labels and suggestions related to the target span when it's not found. Fixes #48046
1 parent 4f93357 commit daaa9a4

File tree

3 files changed

+61
-47
lines changed

3 files changed

+61
-47
lines changed

src/librustc/traits/error_reporting.rs

+49-44
Original file line numberDiff line numberDiff line change
@@ -744,8 +744,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
744744
} else {
745745
let (closure_span, found) = found_did
746746
.and_then(|did| self.tcx.hir.get_if_local(did))
747-
.map(|node| self.get_fn_like_arguments(node))
748-
.unwrap_or((found_span.unwrap(), found));
747+
.map(|node| {
748+
let (found_span, found) = self.get_fn_like_arguments(node);
749+
(Some(found_span), found)
750+
}).unwrap_or((found_span, found));
749751

750752
self.report_arg_count_mismatch(span,
751753
closure_span,
@@ -855,7 +857,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
855857
fn report_arg_count_mismatch(
856858
&self,
857859
span: Span,
858-
found_span: Span,
860+
found_span: Option<Span>,
859861
expected_args: Vec<ArgKind>,
860862
found_args: Vec<ArgKind>,
861863
is_closure: bool,
@@ -893,48 +895,51 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
893895
);
894896

895897
err.span_label(span, format!( "expected {} that takes {}", kind, expected_str));
896-
err.span_label(found_span, format!("takes {}", found_str));
897-
898-
if let &[ArgKind::Tuple(_, ref fields)] = &found_args[..] {
899-
if fields.len() == expected_args.len() {
900-
let sugg = fields.iter()
901-
.map(|(name, _)| name.to_owned())
902-
.collect::<Vec<String>>().join(", ");
903-
err.span_suggestion(found_span,
904-
"change the closure to take multiple arguments instead of \
905-
a single tuple",
906-
format!("|{}|", sugg));
898+
899+
if let Some(found_span) = found_span {
900+
err.span_label(found_span, format!("takes {}", found_str));
901+
902+
if let &[ArgKind::Tuple(_, ref fields)] = &found_args[..] {
903+
if fields.len() == expected_args.len() {
904+
let sugg = fields.iter()
905+
.map(|(name, _)| name.to_owned())
906+
.collect::<Vec<String>>().join(", ");
907+
err.span_suggestion(found_span,
908+
"change the closure to take multiple arguments instead of \
909+
a single tuple",
910+
format!("|{}|", sugg));
911+
}
907912
}
908-
}
909-
if let &[ArgKind::Tuple(_, ref fields)] = &expected_args[..] {
910-
if fields.len() == found_args.len() && is_closure {
911-
let sugg = format!(
912-
"|({}){}|",
913-
found_args.iter()
914-
.map(|arg| match arg {
915-
ArgKind::Arg(name, _) => name.to_owned(),
916-
_ => "_".to_owned(),
917-
})
918-
.collect::<Vec<String>>()
919-
.join(", "),
920-
// add type annotations if available
921-
if found_args.iter().any(|arg| match arg {
922-
ArgKind::Arg(_, ty) => ty != "_",
923-
_ => false,
924-
}) {
925-
format!(": ({})",
926-
fields.iter()
927-
.map(|(_, ty)| ty.to_owned())
928-
.collect::<Vec<String>>()
929-
.join(", "))
930-
} else {
931-
"".to_owned()
932-
},
933-
);
934-
err.span_suggestion(found_span,
935-
"change the closure to accept a tuple instead of individual \
936-
arguments",
937-
sugg);
913+
if let &[ArgKind::Tuple(_, ref fields)] = &expected_args[..] {
914+
if fields.len() == found_args.len() && is_closure {
915+
let sugg = format!(
916+
"|({}){}|",
917+
found_args.iter()
918+
.map(|arg| match arg {
919+
ArgKind::Arg(name, _) => name.to_owned(),
920+
_ => "_".to_owned(),
921+
})
922+
.collect::<Vec<String>>()
923+
.join(", "),
924+
// add type annotations if available
925+
if found_args.iter().any(|arg| match arg {
926+
ArgKind::Arg(_, ty) => ty != "_",
927+
_ => false,
928+
}) {
929+
format!(": ({})",
930+
fields.iter()
931+
.map(|(_, ty)| ty.to_owned())
932+
.collect::<Vec<String>>()
933+
.join(", "))
934+
} else {
935+
"".to_owned()
936+
},
937+
);
938+
err.span_suggestion(found_span,
939+
"change the closure to accept a tuple instead of \
940+
individual arguments",
941+
sugg);
942+
}
938943
}
939944
}
940945

src/test/ui/mismatched_types/closure-arg-count.rs

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ fn main() {
3636
//~^ ERROR closure is expected to take
3737
let _it = vec![1, 2, 3].into_iter().enumerate().map(qux);
3838
//~^ ERROR function is expected to take
39+
40+
let _it = vec![1, 2, 3].into_iter().map(usize::checked_add);
41+
//~^ ERROR function is expected to take
3942
}
4043

4144
fn foo() {}

src/test/ui/mismatched_types/closure-arg-count.stderr

+9-3
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ error[E0593]: function is expected to take a single 2-tuple as argument, but it
9090
32 | let _it = vec![1, 2, 3].into_iter().enumerate().map(foo);
9191
| ^^^ expected function that takes a single 2-tuple as argument
9292
...
93-
41 | fn foo() {}
93+
44 | fn foo() {}
9494
| -------- takes 0 arguments
9595

9696
error[E0593]: closure is expected to take a single 2-tuple as argument, but it takes 3 distinct arguments
@@ -107,8 +107,14 @@ error[E0593]: function is expected to take a single 2-tuple as argument, but it
107107
37 | let _it = vec![1, 2, 3].into_iter().enumerate().map(qux);
108108
| ^^^ expected function that takes a single 2-tuple as argument
109109
...
110-
42 | fn qux(x: usize, y: usize) {}
110+
45 | fn qux(x: usize, y: usize) {}
111111
| -------------------------- takes 2 distinct arguments
112112

113-
error: aborting due to 11 previous errors
113+
error[E0593]: function is expected to take 1 argument, but it takes 2 arguments
114+
--> $DIR/closure-arg-count.rs:40:41
115+
|
116+
40 | let _it = vec![1, 2, 3].into_iter().map(usize::checked_add);
117+
| ^^^ expected function that takes 1 argument
118+
119+
error: aborting due to 12 previous errors
114120

0 commit comments

Comments
 (0)