diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-19 18:03:36 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-19 18:03:36 +0000 |
commit | 1c296d54e3dcc36c1a778873f26035000a352ba2 (patch) | |
tree | 0a6ce660ee32080287284c93bffaaaada91f3584 /crates/ra_hir/src/ty/tests.rs | |
parent | bade91db081a3465dea3547ab8ab669f78fde9dc (diff) | |
parent | 5f3509e140d19b989db418a00ac6778c622cde5d (diff) |
Merge #576
576: Beginnings of generics r=matklad a=flodiebold
This implements the beginnings of the generics infrastructure; generic parameters for structs work and are correctly substituted in fields. Functions and methods aren't handled at all yet (as the tests show).
The name resolution in `ty` really needs refactoring now, I hope to do that next ;)
Co-authored-by: Florian Diebold <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/ty/tests.rs')
-rw-r--r-- | crates/ra_hir/src/ty/tests.rs | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index fc4054159..06e32df59 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs | |||
@@ -418,6 +418,95 @@ fn test() { | |||
418 | ); | 418 | ); |
419 | } | 419 | } |
420 | 420 | ||
421 | #[test] | ||
422 | fn infer_struct_generics() { | ||
423 | check_inference( | ||
424 | r#" | ||
425 | struct A<T> { | ||
426 | x: T, | ||
427 | } | ||
428 | |||
429 | fn test(a1: A<u32>, i: i32) { | ||
430 | a1.x; | ||
431 | let a2 = A { x: i }; | ||
432 | a2.x; | ||
433 | let a3 = A::<i128> { x: 1 }; | ||
434 | a3.x; | ||
435 | } | ||
436 | "#, | ||
437 | "struct_generics.txt", | ||
438 | ); | ||
439 | } | ||
440 | |||
441 | #[test] | ||
442 | fn infer_generics_in_patterns() { | ||
443 | check_inference( | ||
444 | r#" | ||
445 | struct A<T> { | ||
446 | x: T, | ||
447 | } | ||
448 | |||
449 | enum Option<T> { | ||
450 | Some(T), | ||
451 | None, | ||
452 | } | ||
453 | |||
454 | fn test(a1: A<u32>, o: Option<u64>) { | ||
455 | let A { x: x2 } = a1; | ||
456 | let A::<i64> { x: x3 } = A { x: 1 }; | ||
457 | match o { | ||
458 | Option::Some(t) => t, | ||
459 | _ => 1, | ||
460 | }; | ||
461 | } | ||
462 | "#, | ||
463 | "generics_in_patterns.txt", | ||
464 | ); | ||
465 | } | ||
466 | |||
467 | #[test] | ||
468 | fn infer_function_generics() { | ||
469 | check_inference( | ||
470 | r#" | ||
471 | fn id<T>(t: T) -> T { t } | ||
472 | |||
473 | fn test() { | ||
474 | id(1u32); | ||
475 | id::<i128>(1); | ||
476 | let x: u64 = id(1); | ||
477 | } | ||
478 | "#, | ||
479 | "function_generics.txt", | ||
480 | ); | ||
481 | } | ||
482 | |||
483 | #[test] | ||
484 | fn infer_generic_chain() { | ||
485 | check_inference( | ||
486 | r#" | ||
487 | struct A<T> { | ||
488 | x: T, | ||
489 | } | ||
490 | impl<T2> A<T2> { | ||
491 | fn x(self) -> T2 { | ||
492 | self.x | ||
493 | } | ||
494 | } | ||
495 | fn id<T>(t: T) -> T { t } | ||
496 | |||
497 | fn test() -> i128 { | ||
498 | let x = 1; | ||
499 | let y = id(x); | ||
500 | let a = A { x: id(y) }; | ||
501 | let z = id(a.x); | ||
502 | let b = A { x: z }; | ||
503 | b.x() | ||
504 | } | ||
505 | "#, | ||
506 | "generic_chain.txt", | ||
507 | ); | ||
508 | } | ||
509 | |||
421 | fn infer(content: &str) -> String { | 510 | fn infer(content: &str) -> String { |
422 | let (db, _, file_id) = MockDatabase::with_single_file(content); | 511 | let (db, _, file_id) = MockDatabase::with_single_file(content); |
423 | let source_file = db.source_file(file_id); | 512 | let source_file = db.source_file(file_id); |