Skip to content

Commit 80bee14

Browse files
bors[bot]komonad
andauthored
Merge #8687
8687: fix: closure unify without check ClosureId r=lnicola a=komonad Previously, the unification of closure types is blocked by `Ty.equals_ctor` which compares the ClosureId of the closures. Here is a workaround to allow closures to unify their substitutions. Fixes #8604. Co-authored-by: Comonad <[email protected]>
2 parents dce0d71 + 78f1583 commit 80bee14

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

crates/hir_ty/src/infer/unify.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,10 @@ impl InferenceTable {
332332
| (TyKind::Slice(ty1), TyKind::Slice(ty2)) => self.unify_inner(ty1, ty2, depth + 1),
333333
_ => true, /* we checked equals_ctor already */
334334
}
335+
} else if let (TyKind::Closure(.., substs1), TyKind::Closure(.., substs2)) =
336+
(ty1.kind(&Interner), ty2.kind(&Interner))
337+
{
338+
self.unify_substs(substs1, substs2, depth + 1)
335339
} else {
336340
self.unify_inner_trivial(&ty1, &ty2, depth)
337341
}

crates/hir_ty/src/tests/simple.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,42 @@ fn infer_in_elseif() {
10281028
)
10291029
}
10301030

1031+
#[test]
1032+
fn infer_closure_unify() {
1033+
check_infer(
1034+
r#"
1035+
fn foo(f: bool) {
1036+
let a = |x| x;
1037+
let b = |x| x;
1038+
let id = if f { a } else { b };
1039+
id(123);
1040+
}
1041+
"#,
1042+
expect![[r#"
1043+
7..8 'f': bool
1044+
16..106 '{ ...23); }': ()
1045+
26..27 'a': |i32| -> i32
1046+
30..35 '|x| x': |i32| -> i32
1047+
31..32 'x': i32
1048+
34..35 'x': i32
1049+
45..46 'b': |i32| -> i32
1050+
49..54 '|x| x': |i32| -> i32
1051+
50..51 'x': i32
1052+
53..54 'x': i32
1053+
64..66 'id': |i32| -> i32
1054+
69..90 'if f {... { b }': |i32| -> i32
1055+
72..73 'f': bool
1056+
74..79 '{ a }': |i32| -> i32
1057+
76..77 'a': |i32| -> i32
1058+
85..90 '{ b }': |i32| -> i32
1059+
87..88 'b': |i32| -> i32
1060+
96..98 'id': |i32| -> i32
1061+
96..103 'id(123)': i32
1062+
99..102 '123': i32
1063+
"#]],
1064+
)
1065+
}
1066+
10311067
#[test]
10321068
fn infer_if_match_with_return() {
10331069
check_infer(

0 commit comments

Comments
 (0)