aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/infer
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-10-15 19:02:27 +0100
committerGitHub <[email protected]>2020-10-15 19:02:27 +0100
commit0d45802d671f94cb768b93a64882733396cfbe2d (patch)
treeabb91645ee84216304065bf42a959223d040814c /crates/hir_ty/src/infer
parent1de202010948c94658235f7cfe9b25dda0c7ddf3 (diff)
parent0e9d1e17d6e45b44ec1a8f1430109cfc75e41241 (diff)
Merge #6220
6220: implement binary operator overloading type inference r=flodiebold a=ruabmbua Extend type inference of *binary operator expression*, by adding support for operator overloads. Before this merge request, the type inference of binary expressions could only resolve operations done on built-in primitive types. This merge requests adds a code path, which is executed in case the built-in inference could not get any results. It resolves the proper operator overload trait in *core::ops* via lang items, and then resolves the associated *Output* type. ```rust struct V2([f32; 2]); #[lang = "add"] pub trait Add<Rhs = Self> { /// The resulting type after applying the `+` operator. type Output; /// Performs the `+` operation. #[must_use] fn add(self, rhs: Rhs) -> Self::Output; } impl Add<V2> for V2 { type Output = V2; fn add(self, rhs: V2) -> V2 { let x = self.0[0] + rhs.0[0]; let y = self.0[1] + rhs.0[1]; V2([x, y]) } } fn test() { let va = V2([0.0, 1.0]); let vb = V2([0.0, 1.0]); let r = va + vb; // This infers to V2 now } ``` There is a problem with operator overloads, which do not explicitly set the *Rhs* type parameter in the respective impl block. **Example:** ```rust impl Add for V2 { type Output = V2; fn add(self, rhs: V2) -> V2 { let x = self.0[0] + rhs.0[0]; let y = self.0[1] + rhs.0[1]; V2([x, y]) } } ``` In this case, the trait solver does not realize, that the *Rhs* type parameter is actually self in the context of the impl block. This stops type inference in its tracks, and it can not resolve the associated *Output* type. I guess we can still merge this back, because it increases the amount of resolved types, and does not regress anything (in the tests). Somewhat blocked by https://github.com/rust-analyzer/rust-analyzer/issues/5685 Resolves https://github.com/rust-analyzer/rust-analyzer/issues/5544 Co-authored-by: Roland Ruckerbauer <[email protected]>
Diffstat (limited to 'crates/hir_ty/src/infer')
-rw-r--r--crates/hir_ty/src/infer/expr.rs18
1 files changed, 14 insertions, 4 deletions
diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs
index 0a141b9cb..8ac4cf89a 100644
--- a/crates/hir_ty/src/infer/expr.rs
+++ b/crates/hir_ty/src/infer/expr.rs
@@ -12,6 +12,7 @@ use hir_def::{
12}; 12};
13use hir_expand::name::{name, Name}; 13use hir_expand::name::{name, Name};
14use syntax::ast::RangeOp; 14use syntax::ast::RangeOp;
15use test_utils::mark;
15 16
16use crate::{ 17use crate::{
17 autoderef, method_resolution, op, 18 autoderef, method_resolution, op,
@@ -531,13 +532,22 @@ impl<'a> InferenceContext<'a> {
531 _ => Expectation::none(), 532 _ => Expectation::none(),
532 }; 533 };
533 let lhs_ty = self.infer_expr(*lhs, &lhs_expectation); 534 let lhs_ty = self.infer_expr(*lhs, &lhs_expectation);
534 // FIXME: find implementation of trait corresponding to operation
535 // symbol and resolve associated `Output` type
536 let rhs_expectation = op::binary_op_rhs_expectation(*op, lhs_ty.clone()); 535 let rhs_expectation = op::binary_op_rhs_expectation(*op, lhs_ty.clone());
537 let rhs_ty = self.infer_expr(*rhs, &Expectation::has_type(rhs_expectation)); 536 let rhs_ty = self.infer_expr(*rhs, &Expectation::has_type(rhs_expectation));
538 537
539 // FIXME: similar as above, return ty is often associated trait type 538 let ret = op::binary_op_return_ty(*op, lhs_ty.clone(), rhs_ty.clone());
540 op::binary_op_return_ty(*op, lhs_ty, rhs_ty) 539
540 if ret == Ty::Unknown {
541 mark::hit!(infer_expr_inner_binary_operator_overload);
542
543 self.resolve_associated_type_with_params(
544 lhs_ty,
545 self.resolve_binary_op_output(op),
546 &[rhs_ty],
547 )
548 } else {
549 ret
550 }
541 } 551 }
542 _ => Ty::Unknown, 552 _ => Ty::Unknown,
543 }, 553 },