From 5a1385007cb7509d50a77afa5a47ed0685f7f2f9 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Fri, 25 Jan 2019 21:32:49 +0100 Subject: Fix type inference of binops on int/float variables Fixes #651. --- crates/ra_hir/src/ty.rs | 5 ++++- crates/ra_hir/src/ty/snapshots/tests__bug_651.snap | 13 +++++++++++++ crates/ra_hir/src/ty/tests.rs | 13 +++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 crates/ra_hir/src/ty/snapshots/tests__bug_651.snap (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 714eaaae5..179ebddee 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -790,7 +790,10 @@ fn binary_op_return_ty(op: BinaryOp, rhs_ty: Ty) -> Ty { | BinaryOp::BitwiseAnd | BinaryOp::BitwiseOr | BinaryOp::BitwiseXor => match rhs_ty { - Ty::Int(..) | Ty::Float(..) => rhs_ty, + Ty::Int(..) + | Ty::Float(..) + | Ty::Infer(InferTy::IntVar(..)) + | Ty::Infer(InferTy::FloatVar(..)) => rhs_ty, _ => Ty::Unknown, }, BinaryOp::RangeRightOpen | BinaryOp::RangeRightClosed => Ty::Unknown, diff --git a/crates/ra_hir/src/ty/snapshots/tests__bug_651.snap b/crates/ra_hir/src/ty/snapshots/tests__bug_651.snap new file mode 100644 index 000000000..d23d3f139 --- /dev/null +++ b/crates/ra_hir/src/ty/snapshots/tests__bug_651.snap @@ -0,0 +1,13 @@ +--- +created: "2019-01-25T20:31:47.275112244+00:00" +creator: insta@0.5.2 +expression: "&result" +source: crates/ra_hir/src/ty/tests.rs +--- +[11; 41) '{ ...+ y; }': () +[21; 22) 'y': i32 +[25; 27) '92': i32 +[33; 34) '1': i32 +[33; 38) '1 + y': i32 +[37; 38) 'y': i32 + diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 92c74cf00..389bdaf23 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -532,6 +532,19 @@ fn test() { ); } +#[test] +fn bug_651() { + check_inference( + "bug_651", + r#" +fn quux() { + let y = 92; + 1 + y; +} +"#, + ); +} + fn infer(content: &str) -> String { let (db, _, file_id) = MockDatabase::with_single_file(content); let source_file = db.source_file(file_id); -- cgit v1.2.3