aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/ty/tests.rs
diff options
context:
space:
mode:
authorVille Penttinen <[email protected]>2019-02-21 10:04:14 +0000
committerVille Penttinen <[email protected]>2019-02-21 10:25:55 +0000
commit816971ebc9207c5fb5779d448613dd171c27f398 (patch)
treefe49d209a852bb1b4a51eea9d40449dcf2845209 /crates/ra_hir/src/ty/tests.rs
parentc84561bb624280b84eb2fe6c6b2a6b9fe3f1dbf7 (diff)
Implement basic support for Associated Methods and Constants
This is done in `infer_path_expr`. When `Resolver::resolve_path` returns `PartiallyResolved`, we use the returned `Resolution` together with the given `segment_index` to check if we can find something matching the segment at segment_index in the impls for that particular type.
Diffstat (limited to 'crates/ra_hir/src/ty/tests.rs')
-rw-r--r--crates/ra_hir/src/ty/tests.rs133
1 files changed, 133 insertions, 0 deletions
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs
index 4ab442b8a..c4b452ba4 100644
--- a/crates/ra_hir/src/ty/tests.rs
+++ b/crates/ra_hir/src/ty/tests.rs
@@ -587,6 +587,139 @@ fn test() -> i128 {
587} 587}
588 588
589#[test] 589#[test]
590fn infer_associated_const() {
591 check_inference(
592 "infer_associated_const",
593 r#"
594struct Struct;
595
596impl Struct {
597 const FOO: u32 = 1;
598}
599
600enum Enum;
601
602impl Enum {
603 const BAR: u32 = 2;
604}
605
606trait Trait {
607 const ID: u32;
608}
609
610struct TraitTest;
611
612impl Trait for TraitTest {
613 const ID: u32 = 5;
614}
615
616fn test() {
617 let x = Struct::FOO;
618 let y = Enum::BAR;
619 let z = TraitTest::ID;
620}
621"#,
622 );
623}
624
625#[test]
626fn infer_associated_method_struct() {
627 check_inference(
628 "infer_associated_method_struct",
629 r#"
630struct A { x: u32 };
631
632impl A {
633 fn new() -> A {
634 A { x: 0 }
635 }
636}
637fn test() {
638 let a = A::new();
639 a.x;
640}
641"#,
642 );
643}
644
645#[test]
646fn infer_associated_method_enum() {
647 check_inference(
648 "infer_associated_method_enum",
649 r#"
650enum A { B, C };
651
652impl A {
653 pub fn b() -> A {
654 A::B
655 }
656 pub fn c() -> A {
657 A::C
658 }
659}
660fn test() {
661 let a = A::b();
662 a;
663 let c = A::c();
664 c;
665}
666"#,
667 );
668}
669
670#[test]
671fn infer_associated_method_with_modules() {
672 check_inference(
673 "infer_associated_method_with_modules",
674 r#"
675mod a {
676 struct A;
677 impl A { pub fn thing() -> A { A {} }}
678}
679
680mod b {
681 struct B;
682 impl B { pub fn thing() -> u32 { 99 }}
683
684 mod c {
685 struct C;
686 impl C { pub fn thing() -> C { C {} }}
687 }
688}
689use b::c;
690
691fn test() {
692 let x = a::A::thing();
693 let y = b::B::thing();
694 let z = c::C::thing();
695}
696"#,
697 );
698}
699
700#[test]
701fn infer_associated_method_generics() {
702 check_inference(
703 "infer_associated_method_generics",
704 r#"
705struct Gen<T> {
706 val: T
707}
708
709impl<T> Gen<T> {
710 pub fn make(val: T) -> Gen<T> {
711 Gen { val }
712 }
713}
714
715fn test() {
716 let a = Gen::make(0u32);
717}
718"#,
719 );
720}
721
722#[test]
590fn no_panic_on_field_of_enum() { 723fn no_panic_on_field_of_enum() {
591 check_inference( 724 check_inference(
592 "no_panic_on_field_of_enum", 725 "no_panic_on_field_of_enum",