Skip to content

Commit a96b6a9

Browse files
Merge pull request #21654 from Albab-Hasan/fix/binop-rhs-never-coercion
fix: use `ExprIsRead::Yes` for rhs of binary operators
2 parents d5d00a3 + ab44d82 commit a96b6a9

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ impl<'a, 'db> InferenceContext<'a, 'db> {
178178
// trait matching creating lifetime constraints that are too strict.
179179
// e.g., adding `&'a T` and `&'b T`, given `&'x T: Add<&'x T>`, will result
180180
// in `&'a T <: &'x T` and `&'b T <: &'x T`, instead of `'a = 'b = 'x`.
181-
let lhs_ty = self.infer_expr_no_expect(lhs_expr, ExprIsRead::No);
181+
let lhs_ty = self.infer_expr_no_expect(lhs_expr, ExprIsRead::Yes);
182182
let fresh_var = self.table.next_ty_var();
183-
self.demand_coerce(lhs_expr, lhs_ty, fresh_var, AllowTwoPhase::No, ExprIsRead::No)
183+
self.demand_coerce(lhs_expr, lhs_ty, fresh_var, AllowTwoPhase::No, ExprIsRead::Yes)
184184
}
185185
};
186186
let lhs_ty = self.table.resolve_vars_with_obligations(lhs_ty);
@@ -200,7 +200,7 @@ impl<'a, 'db> InferenceContext<'a, 'db> {
200200

201201
// see `NB` above
202202
let rhs_ty =
203-
self.infer_expr_coerce(rhs_expr, &Expectation::HasType(rhs_ty_var), ExprIsRead::No);
203+
self.infer_expr_coerce(rhs_expr, &Expectation::HasType(rhs_ty_var), ExprIsRead::Yes);
204204
let rhs_ty = self.table.resolve_vars_with_obligations(rhs_ty);
205205

206206
let return_ty = match result {
@@ -320,7 +320,11 @@ impl<'a, 'db> InferenceContext<'a, 'db> {
320320
if let Some((rhs_expr, rhs_ty)) = opt_rhs
321321
&& rhs_ty.is_ty_var()
322322
{
323-
self.infer_expr_coerce(rhs_expr, &Expectation::HasType(rhs_ty), ExprIsRead::No);
323+
self.infer_expr_coerce(
324+
rhs_expr,
325+
&Expectation::HasType(rhs_ty),
326+
ExprIsRead::Yes,
327+
);
324328
}
325329

326330
// Construct an obligation `self_ty : Trait<input_tys>`

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,37 @@ fn foo() {
802802
);
803803
}
804804

805+
#[test]
806+
fn binop_rhs_never_place_diverges() {
807+
check_no_mismatches(
808+
r#"
809+
//- minicore: sized, add
810+
fn foo() -> i32 {
811+
unsafe {
812+
let p: *mut ! = 0 as _;
813+
let mut x: i32 = 0;
814+
x += *p;
815+
}
816+
}
817+
"#,
818+
);
819+
}
820+
821+
#[test]
822+
fn binop_lhs_never_place_diverges() {
823+
check_no_mismatches(
824+
r#"
825+
//- minicore: sized, add
826+
fn foo() {
827+
unsafe {
828+
let p: *mut ! = 0 as _;
829+
*p += 1;
830+
}
831+
}
832+
"#,
833+
);
834+
}
835+
805836
#[test]
806837
fn never_place_isnt_diverging() {
807838
check_infer_with_mismatches(

0 commit comments

Comments
 (0)