From 4e49b2f73144460cde5ada8140964d96166f41fd Mon Sep 17 00:00:00 2001 From: Roland Ruckerbauer Date: Tue, 13 Oct 2020 20:48:08 +0200 Subject: Implement binary operator overloading type inference --- crates/hir_ty/src/tests/simple.rs | 86 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) (limited to 'crates/hir_ty/src/tests') diff --git a/crates/hir_ty/src/tests/simple.rs b/crates/hir_ty/src/tests/simple.rs index 5b07948f3..a3ae304a1 100644 --- a/crates/hir_ty/src/tests/simple.rs +++ b/crates/hir_ty/src/tests/simple.rs @@ -2225,3 +2225,89 @@ fn generic_default_depending_on_other_type_arg_forward() { "#]], ); } + +#[test] +fn infer_operator_overload() { + check_infer( + r#" + struct V2([f32; 2]); + + #[lang = "add"] + pub trait Add { + /// The resulting type after applying the `+` operator. + type Output; + + /// Performs the `+` operation. + #[must_use] + fn add(self, rhs: Rhs) -> Self::Output; + } + + 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]) + } + } + + fn test() { + let va = V2([0.0, 1.0]); + let vb = V2([0.0, 1.0]); + + let r = va + vb; + } + + "#, + expect![[r#" + 207..211 'self': Self + 213..216 'rhs': Rhs + 299..303 'self': V2 + 305..308 'rhs': V2 + 320..422 '{ ... }': V2 + 334..335 'x': f32 + 338..342 'self': V2 + 338..344 'self.0': [f32; _] + 338..347 'self.0[0]': {unknown} + 338..358 'self.0...s.0[0]': f32 + 345..346 '0': i32 + 350..353 'rhs': V2 + 350..355 'rhs.0': [f32; _] + 350..358 'rhs.0[0]': {unknown} + 356..357 '0': i32 + 372..373 'y': f32 + 376..380 'self': V2 + 376..382 'self.0': [f32; _] + 376..385 'self.0[1]': {unknown} + 376..396 'self.0...s.0[1]': f32 + 383..384 '1': i32 + 388..391 'rhs': V2 + 388..393 'rhs.0': [f32; _] + 388..396 'rhs.0[1]': {unknown} + 394..395 '1': i32 + 406..408 'V2': V2([f32; _]) -> V2 + 406..416 'V2([x, y])': V2 + 409..415 '[x, y]': [f32; _] + 410..411 'x': f32 + 413..414 'y': f32 + 436..519 '{ ... vb; }': () + 446..448 'va': V2 + 451..453 'V2': V2([f32; _]) -> V2 + 451..465 'V2([0.0, 1.0])': V2 + 454..464 '[0.0, 1.0]': [f32; _] + 455..458 '0.0': f32 + 460..463 '1.0': f32 + 475..477 'vb': V2 + 480..482 'V2': V2([f32; _]) -> V2 + 480..494 'V2([0.0, 1.0])': V2 + 483..493 '[0.0, 1.0]': [f32; _] + 484..487 '0.0': f32 + 489..492 '1.0': f32 + 505..506 'r': V2 + 509..511 'va': V2 + 509..516 'va + vb': V2 + 514..516 'vb': V2 + "#]], + ); +} -- cgit v1.2.3