aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/infer.rs
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.rs
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.rs')
-rw-r--r--crates/hir_ty/src/infer.rs24
1 files changed, 23 insertions, 1 deletions
diff --git a/crates/hir_ty/src/infer.rs b/crates/hir_ty/src/infer.rs
index 9a7785c76..644ebd42d 100644
--- a/crates/hir_ty/src/infer.rs
+++ b/crates/hir_ty/src/infer.rs
@@ -22,7 +22,7 @@ use arena::map::ArenaMap;
22use hir_def::{ 22use hir_def::{
23 body::Body, 23 body::Body,
24 data::{ConstData, FunctionData, StaticData}, 24 data::{ConstData, FunctionData, StaticData},
25 expr::{BindingAnnotation, ExprId, PatId}, 25 expr::{ArithOp, BinaryOp, BindingAnnotation, ExprId, PatId},
26 lang_item::LangItemTarget, 26 lang_item::LangItemTarget,
27 path::{path, Path}, 27 path::{path, Path},
28 resolver::{HasResolver, Resolver, TypeNs}, 28 resolver::{HasResolver, Resolver, TypeNs},
@@ -586,6 +586,28 @@ impl<'a> InferenceContext<'a> {
586 self.db.trait_data(trait_).associated_type_by_name(&name![Output]) 586 self.db.trait_data(trait_).associated_type_by_name(&name![Output])
587 } 587 }
588 588
589 fn resolve_binary_op_output(&self, bop: &BinaryOp) -> Option<TypeAliasId> {
590 let lang_item = match bop {
591 BinaryOp::ArithOp(aop) => match aop {
592 ArithOp::Add => "add",
593 ArithOp::Sub => "sub",
594 ArithOp::Mul => "mul",
595 ArithOp::Div => "div",
596 ArithOp::Shl => "shl",
597 ArithOp::Shr => "shr",
598 ArithOp::Rem => "rem",
599 ArithOp::BitXor => "bitxor",
600 ArithOp::BitOr => "bitor",
601 ArithOp::BitAnd => "bitand",
602 },
603 _ => return None,
604 };
605
606 let trait_ = self.resolve_lang_item(lang_item)?.as_trait();
607
608 self.db.trait_data(trait_?).associated_type_by_name(&name![Output])
609 }
610
589 fn resolve_boxed_box(&self) -> Option<AdtId> { 611 fn resolve_boxed_box(&self) -> Option<AdtId> {
590 let struct_ = self.resolve_lang_item("owned_box")?.as_struct()?; 612 let struct_ = self.resolve_lang_item("owned_box")?.as_struct()?;
591 Some(struct_.into()) 613 Some(struct_.into())