Skip to content

Commit a2171fe

Browse files
committed
Fix ICE when suggesting closures for non-fn-like defs
1 parent 64d7e0d commit a2171fe

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -2112,7 +2112,8 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
21122112
&& !expected_inputs.is_empty()
21132113
&& expected_inputs.len() == found_inputs.len()
21142114
&& let Some(typeck) = &self.typeck_results
2115-
&& let Res::Def(_, fn_def_id) = typeck.qpath_res(&path, *arg_hir_id)
2115+
&& let Res::Def(res_kind, fn_def_id) = typeck.qpath_res(&path, *arg_hir_id)
2116+
&& res_kind.is_fn_like()
21162117
{
21172118
let closure: Vec<_> = self
21182119
.tcx
@@ -2155,7 +2156,13 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
21552156
.map(|(name, ty)| {
21562157
format!(
21572158
"{name}{}",
2158-
if ty.has_infer_types() { String::new() } else { format!(": {ty}") }
2159+
if ty.has_infer_types() {
2160+
String::new()
2161+
} else if ty.references_error() {
2162+
": /* type */".to_string()
2163+
} else {
2164+
format!(": {ty}")
2165+
}
21592166
)
21602167
})
21612168
.collect();
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
pub enum Sexpr<'a, S> {
2+
Ident(&'a mut S),
3+
}
4+
5+
fn map<Foo, T, F: FnOnce(&Foo) -> T>(f: F) {}
6+
7+
fn main() {
8+
map(Sexpr::Ident);
9+
//~^ ERROR type mismatch in function arguments
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error[E0631]: type mismatch in function arguments
2+
--> $DIR/issue-118510.rs:8:9
3+
|
4+
LL | Ident(&'a mut S),
5+
| ----- found signature defined here
6+
...
7+
LL | map(Sexpr::Ident);
8+
| --- ^^^^^^^^^^^^ expected due to this
9+
| |
10+
| required by a bound introduced by this call
11+
|
12+
= note: expected function signature `for<'a> fn(&'a _) -> _`
13+
found function signature `fn(&mut _) -> _`
14+
note: required by a bound in `map`
15+
--> $DIR/issue-118510.rs:5:19
16+
|
17+
LL | fn map<Foo, T, F: FnOnce(&Foo) -> T>(f: F) {}
18+
| ^^^^^^^^^^^^^^^^^ required by this bound in `map`
19+
help: consider wrapping the function in a closure
20+
|
21+
LL | map(|arg0| Sexpr::Ident(&mut *arg0));
22+
| ++++++ ++++++++++++
23+
24+
error: aborting due to 1 previous error
25+
26+
For more information about this error, try `rustc --explain E0631`.

0 commit comments

Comments
 (0)