Skip to content

Commit 94cbefb

Browse files
Rollup merge of #88147 - FabianWolff:issue-88097, r=jackh726
Fix non-capturing closure return type coercion Fixes #88097. For the example given there: ```rust fn peculiar() -> impl Fn(u8) -> u8 { return |x| x + 1 } ``` which incorrectly reports an error, I noticed something weird in the debug log: ``` DEBUG rustc_typeck::check::coercion coercion::try_find_coercion_lub([[email protected]:2:12: 2:21], [[email protected]:2:12: 2:21], exprs=1 exprs) ``` Apparently, `try_find_coercion_lub()` thinks that the LUB for two closure types always has to be a function pointer (which explains the `expected closure, found fn pointer` error in #88097). There is one corner case where that isn't true, though — namely, when the two closure types are equal, in which case the trivial LUB is the type itself. This PR fixes this by inserting an explicit check for type equality in `try_find_coercion_lub()`.
2 parents 95b50eb + bbe3be9 commit 94cbefb

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

compiler/rustc_typeck/src/check/coercion.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10031003
exprs.len()
10041004
);
10051005

1006+
// The following check fixes #88097, where the compiler erroneously
1007+
// attempted to coerce a closure type to itself via a function pointer.
1008+
if prev_ty == new_ty {
1009+
return Ok(prev_ty);
1010+
}
1011+
10061012
// Special-case that coercion alone cannot handle:
10071013
// Function items or non-capturing closures of differing IDs or InternalSubsts.
10081014
let (a_sig, b_sig) = {

src/test/ui/coercion/issue-88097.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// In #88097, the compiler attempted to coerce a closure type to itself via
2+
// a function pointer, which caused an unnecessary error. Check that this
3+
// behavior has been fixed.
4+
5+
// check-pass
6+
7+
fn peculiar() -> impl Fn(u8) -> u8 {
8+
return |x| x + 1
9+
}
10+
11+
fn peculiar2() -> impl Fn(u8) -> u8 {
12+
return |x| x + 1;
13+
}
14+
15+
fn peculiar3() -> impl Fn(u8) -> u8 {
16+
let f = |x| x + 1;
17+
return f
18+
}
19+
20+
fn peculiar4() -> impl Fn(u8) -> u8 {
21+
let f = |x| x + 1;
22+
f
23+
}
24+
25+
fn peculiar5() -> impl Fn(u8) -> u8 {
26+
let f = |x| x + 1;
27+
let g = |x| x + 2;
28+
return if true { f } else { g }
29+
}
30+
31+
fn main() {}

0 commit comments

Comments
 (0)