|
1 | 1 | use expect_test::expect; |
| 2 | +use test_utils::mark; |
2 | 3 |
|
3 | 4 | use super::{check_infer, check_types}; |
4 | 5 |
|
@@ -2225,3 +2226,91 @@ fn generic_default_depending_on_other_type_arg_forward() { |
2225 | 2226 | "#]], |
2226 | 2227 | ); |
2227 | 2228 | } |
| 2229 | + |
| 2230 | +#[test] |
| 2231 | +fn infer_operator_overload() { |
| 2232 | + mark::check!(infer_expr_inner_binary_operator_overload); |
| 2233 | + |
| 2234 | + check_infer( |
| 2235 | + r#" |
| 2236 | + struct V2([f32; 2]); |
| 2237 | +
|
| 2238 | + #[lang = "add"] |
| 2239 | + pub trait Add<Rhs = Self> { |
| 2240 | + /// The resulting type after applying the `+` operator. |
| 2241 | + type Output; |
| 2242 | +
|
| 2243 | + /// Performs the `+` operation. |
| 2244 | + #[must_use] |
| 2245 | + fn add(self, rhs: Rhs) -> Self::Output; |
| 2246 | + } |
| 2247 | +
|
| 2248 | + impl Add<V2> for V2 { |
| 2249 | + type Output = V2; |
| 2250 | +
|
| 2251 | + fn add(self, rhs: V2) -> V2 { |
| 2252 | + let x = self.0[0] + rhs.0[0]; |
| 2253 | + let y = self.0[1] + rhs.0[1]; |
| 2254 | + V2([x, y]) |
| 2255 | + } |
| 2256 | + } |
| 2257 | +
|
| 2258 | + fn test() { |
| 2259 | + let va = V2([0.0, 1.0]); |
| 2260 | + let vb = V2([0.0, 1.0]); |
| 2261 | +
|
| 2262 | + let r = va + vb; |
| 2263 | + } |
| 2264 | +
|
| 2265 | + "#, |
| 2266 | + expect![[r#" |
| 2267 | + 207..211 'self': Self |
| 2268 | + 213..216 'rhs': Rhs |
| 2269 | + 299..303 'self': V2 |
| 2270 | + 305..308 'rhs': V2 |
| 2271 | + 320..422 '{ ... }': V2 |
| 2272 | + 334..335 'x': f32 |
| 2273 | + 338..342 'self': V2 |
| 2274 | + 338..344 'self.0': [f32; _] |
| 2275 | + 338..347 'self.0[0]': {unknown} |
| 2276 | + 338..358 'self.0...s.0[0]': f32 |
| 2277 | + 345..346 '0': i32 |
| 2278 | + 350..353 'rhs': V2 |
| 2279 | + 350..355 'rhs.0': [f32; _] |
| 2280 | + 350..358 'rhs.0[0]': {unknown} |
| 2281 | + 356..357 '0': i32 |
| 2282 | + 372..373 'y': f32 |
| 2283 | + 376..380 'self': V2 |
| 2284 | + 376..382 'self.0': [f32; _] |
| 2285 | + 376..385 'self.0[1]': {unknown} |
| 2286 | + 376..396 'self.0...s.0[1]': f32 |
| 2287 | + 383..384 '1': i32 |
| 2288 | + 388..391 'rhs': V2 |
| 2289 | + 388..393 'rhs.0': [f32; _] |
| 2290 | + 388..396 'rhs.0[1]': {unknown} |
| 2291 | + 394..395 '1': i32 |
| 2292 | + 406..408 'V2': V2([f32; _]) -> V2 |
| 2293 | + 406..416 'V2([x, y])': V2 |
| 2294 | + 409..415 '[x, y]': [f32; _] |
| 2295 | + 410..411 'x': f32 |
| 2296 | + 413..414 'y': f32 |
| 2297 | + 436..519 '{ ... vb; }': () |
| 2298 | + 446..448 'va': V2 |
| 2299 | + 451..453 'V2': V2([f32; _]) -> V2 |
| 2300 | + 451..465 'V2([0.0, 1.0])': V2 |
| 2301 | + 454..464 '[0.0, 1.0]': [f32; _] |
| 2302 | + 455..458 '0.0': f32 |
| 2303 | + 460..463 '1.0': f32 |
| 2304 | + 475..477 'vb': V2 |
| 2305 | + 480..482 'V2': V2([f32; _]) -> V2 |
| 2306 | + 480..494 'V2([0.0, 1.0])': V2 |
| 2307 | + 483..493 '[0.0, 1.0]': [f32; _] |
| 2308 | + 484..487 '0.0': f32 |
| 2309 | + 489..492 '1.0': f32 |
| 2310 | + 505..506 'r': V2 |
| 2311 | + 509..511 'va': V2 |
| 2312 | + 509..516 'va + vb': V2 |
| 2313 | + 514..516 'vb': V2 |
| 2314 | + "#]], |
| 2315 | + ); |
| 2316 | +} |
0 commit comments