Skip to content

Commit a78e873

Browse files
committed
fix: handle ref mut bindings in contains_explicit_ref_binding
the standalone `contains_explicit_ref_binding` function only checked for `BindingAnnotation::Ref`, missing `BindingAnnotation::RefMut`. this caused `let ref mut x = expr` to incorrectly take the coercion path instead of preserving the exact type of the rhs expression. the method version used for match arms already handles both `Ref` and `RefMut` correctly.
1 parent b7d3e7b commit a78e873

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

crates/hir-ty/src/infer/pat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ impl<'db> InferenceContext<'_, 'db> {
673673
pub(super) fn contains_explicit_ref_binding(body: &Body, pat_id: PatId) -> bool {
674674
let mut res = false;
675675
body.walk_pats(pat_id, &mut |pat| {
676-
res |= matches!(body[pat], Pat::Bind { id, .. } if body[id].mode == BindingAnnotation::Ref);
676+
res |= matches!(body[pat], Pat::Bind { id, .. } if matches!(body[id].mode, BindingAnnotation::Ref | BindingAnnotation::RefMut));
677677
});
678678
res
679679
}

crates/hir-ty/src/tests/never_type.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,32 @@ fn coerce_ref_binding() -> ! {
760760
)
761761
}
762762

763+
#[test]
764+
fn diverging_place_match_ref_mut() {
765+
check_infer_with_mismatches(
766+
r#"
767+
//- minicore: sized
768+
fn coerce_ref_mut_binding() -> ! {
769+
unsafe {
770+
let x: *mut ! = 0 as _;
771+
let ref mut _x: () = *x;
772+
}
773+
}
774+
"#,
775+
expect![[r#"
776+
33..120 '{ ... } }': !
777+
39..118 'unsafe... }': !
778+
60..61 'x': *mut !
779+
72..73 '0': i32
780+
72..78 '0 as _': *mut !
781+
92..102 'ref mut _x': &'? mut ()
782+
109..111 '*x': !
783+
110..111 'x': *mut !
784+
109..111: expected (), got !
785+
"#]],
786+
)
787+
}
788+
763789
#[test]
764790
fn never_place_isnt_diverging() {
765791
check_infer_with_mismatches(

0 commit comments

Comments
 (0)