rust/crates
bors[bot] 0d45802d67
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 <roland.rucky@gmail.com>
2020-10-15 18:02:27 +00:00
..
arena
assists More idiomatic classification API 2020-10-15 17:38:17 +02:00
base_db ⬆️ salsa 2020-10-14 12:40:48 +02:00
cfg
flycheck Bump crossbeam-channel in crates 2020-10-13 16:57:01 +03:00
hir Merge #6130 #6135 2020-10-12 16:00:13 +00:00
hir_def Merge #6130 #6135 2020-10-12 16:00:13 +00:00
hir_expand Shorten type hints for std::iter Iterators 2020-10-06 19:20:42 +02:00
hir_ty binary operator overload type inference: add test mark 2020-10-14 19:00:04 +02:00
ide Clarify the names one more time 2020-10-15 17:38:51 +02:00
ide_db Clarify the names one more time 2020-10-15 17:38:51 +02:00
mbe Cleanup 2020-10-07 11:55:20 +02:00
parser
paths
proc_macro_api Bump crossbeam-channel in crates 2020-10-13 16:57:01 +03:00
proc_macro_srv Add track_env_var to the proc macro server 2020-10-08 17:06:20 +03:00
proc_macro_test
profile Bump rustc_lexer, cfg-if to 1.0 and add new license to check 2020-10-08 10:39:02 -04:00
project_model Log around sysroot discovery 2020-10-14 17:22:07 +02:00
rust-analyzer Bump crossbeam-channel and lsp-server in rust-analyzer 2020-10-13 18:05:06 +03:00
ssr Minor clippy performance suggestions 2020-09-30 15:22:49 -04:00
stdx Add to_upper_snake_case function to stdx 2020-10-12 11:05:00 +03:00
syntax Bump rustc_lexer 2020-10-13 18:06:23 +03:00
test_utils
text_edit
toolchain
tt
vfs
vfs-notify Bump crossbeam-channel in crates 2020-10-13 16:57:01 +03:00