Skip to content

Commit 225f353

Browse files
bors[bot]kiljacken
andauthored
Merge #4412
4412: infer: Make expected rhs type for plain assign the lhs type r=flodiebold a=kiljacken This fixes an issue where the following code sample would fail to infer the type contained in the option: ```rust fn main() { let mut end = None; // Was Option<{unknown}>, is now Option<bool> loop { end = Some(true); } } ``` Co-authored-by: Emil Lauridsen <mine809@gmail.com>
2 parents 4578154 + 85d44ca commit 225f353

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

crates/ra_hir_ty/src/op.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ pub(super) fn binary_op_return_ty(op: BinaryOp, lhs_ty: Ty, rhs_ty: Ty) -> Ty {
3030
pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty {
3131
match op {
3232
BinaryOp::LogicOp(..) => Ty::simple(TypeCtor::Bool),
33-
BinaryOp::Assignment { op: None } | BinaryOp::CmpOp(CmpOp::Eq { .. }) => match lhs_ty {
33+
BinaryOp::Assignment { op: None } => lhs_ty,
34+
BinaryOp::CmpOp(CmpOp::Eq { .. }) => match lhs_ty {
3435
Ty::Apply(ApplicationTy { ctor, .. }) => match ctor {
3536
TypeCtor::Int(..)
3637
| TypeCtor::Float(..)

crates/ra_hir_ty/src/tests/simple.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,3 +1787,32 @@ fn main() {
17871787
"###
17881788
)
17891789
}
1790+
1791+
#[test]
1792+
fn infer_generic_from_later_assignment() {
1793+
assert_snapshot!(
1794+
infer(r#"
1795+
enum Option<T> { Some(T), None }
1796+
use Option::*;
1797+
1798+
fn test() {
1799+
let mut end = None;
1800+
loop {
1801+
end = Some(true);
1802+
}
1803+
}
1804+
"#),
1805+
@r###"
1806+
60..130 '{ ... } }': ()
1807+
70..77 'mut end': Option<bool>
1808+
80..84 'None': Option<bool>
1809+
90..128 'loop {... }': !
1810+
95..128 '{ ... }': ()
1811+
105..108 'end': Option<bool>
1812+
105..121 'end = ...(true)': ()
1813+
111..115 'Some': Some<bool>(bool) -> Option<bool>
1814+
111..121 'Some(true)': Option<bool>
1815+
116..120 'true': bool
1816+
"###
1817+
);
1818+
}

0 commit comments

Comments
 (0)