Skip to content

Commit 2884a74

Browse files
committed
Fix non-capturing closure return type coercion
1 parent 3d0774d commit 2884a74

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

compiler/rustc_typeck/src/check/coercion.rs

+4
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
941941
exprs.len()
942942
);
943943

944+
if prev_ty == new_ty {
945+
return Ok(prev_ty);
946+
}
947+
944948
// Special-case that coercion alone cannot handle:
945949
// Function items or non-capturing closures of differing IDs or InternalSubsts.
946950
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)