aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/ty
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
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')
-rw-r--r--crates/ra_hir/src/ty/snapshots/tests__infer_associated_const.snap14
-rw-r--r--crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_enum.snap20
-rw-r--r--crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_generics.snap16
-rw-r--r--crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_struct.snap16
-rw-r--r--crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_with_modules.snap23
-rw-r--r--crates/ra_hir/src/ty/tests.rs133
6 files changed, 222 insertions, 0 deletions
diff --git a/crates/ra_hir/src/ty/snapshots/tests__infer_associated_const.snap b/crates/ra_hir/src/ty/snapshots/tests__infer_associated_const.snap
new file mode 100644
index 000000000..14ab8ba96
--- /dev/null
+++ b/crates/ra_hir/src/ty/snapshots/tests__infer_associated_const.snap
@@ -0,0 +1,14 @@
1---
2created: "2019-02-20T11:04:56.553382800Z"
3creator: [email protected]
4source: crates/ra_hir/src/ty/tests.rs
5expression: "&result"
6---
7[227; 305) '{ ...:ID; }': ()
8[237; 238) 'x': u32
9[241; 252) 'Struct::FOO': u32
10[262; 263) 'y': u32
11[266; 275) 'Enum::BAR': u32
12[285; 286) 'z': u32
13[289; 302) 'TraitTest::ID': u32
14
diff --git a/crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_enum.snap b/crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_enum.snap
new file mode 100644
index 000000000..8f27af88c
--- /dev/null
+++ b/crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_enum.snap
@@ -0,0 +1,20 @@
1---
2created: "2019-02-20T11:04:56.553382800Z"
3creator: [email protected]
4source: crates/ra_hir/src/ty/tests.rs
5expression: "&result"
6---
7[48; 68) '{ ... }': A
8[58; 62) 'A::B': A
9[89; 109) '{ ... }': A
10[99; 103) 'A::C': A
11[122; 179) '{ ... c; }': ()
12[132; 133) 'a': A
13[136; 140) 'A::b': fn b() -> A
14[136; 142) 'A::b()': A
15[148; 149) 'a': A
16[159; 160) 'c': A
17[163; 167) 'A::c': fn c() -> A
18[163; 169) 'A::c()': A
19[175; 176) 'c': A
20
diff --git a/crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_generics.snap b/crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_generics.snap
new file mode 100644
index 000000000..fe5d6590e
--- /dev/null
+++ b/crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_generics.snap
@@ -0,0 +1,16 @@
1---
2created: "2019-02-21T10:25:18.568887300Z"
3creator: [email protected]
4source: crates/ra_hir/src/ty/tests.rs
5expression: "&result"
6---
7[64; 67) 'val': T
8[82; 109) '{ ... }': Gen<T>
9[92; 103) 'Gen { val }': Gen<T>
10[98; 101) 'val': T
11[123; 155) '{ ...32); }': ()
12[133; 134) 'a': Gen<[unknown]>
13[137; 146) 'Gen::make': fn make<[unknown]>(T) -> Gen<T>
14[137; 152) 'Gen::make(0u32)': Gen<[unknown]>
15[147; 151) '0u32': u32
16
diff --git a/crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_struct.snap b/crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_struct.snap
new file mode 100644
index 000000000..29ca9b0ea
--- /dev/null
+++ b/crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_struct.snap
@@ -0,0 +1,16 @@
1---
2created: "2019-02-20T11:04:56.553382800Z"
3creator: [email protected]
4source: crates/ra_hir/src/ty/tests.rs
5expression: "&result"
6---
7[50; 76) '{ ... }': A
8[60; 70) 'A { x: 0 }': A
9[67; 68) '0': u32
10[89; 123) '{ ...a.x; }': ()
11[99; 100) 'a': A
12[103; 109) 'A::new': fn new() -> A
13[103; 111) 'A::new()': A
14[117; 118) 'a': A
15[117; 120) 'a.x': u32
16
diff --git a/crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_with_modules.snap b/crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_with_modules.snap
new file mode 100644
index 000000000..6584bef39
--- /dev/null
+++ b/crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_with_modules.snap
@@ -0,0 +1,23 @@
1---
2created: "2019-02-21T08:55:53.926725400Z"
3creator: [email protected]
4source: crates/ra_hir/src/ty/tests.rs
5expression: "&result"
6---
7[56; 64) '{ A {} }': A
8[58; 62) 'A {}': A
9[126; 132) '{ 99 }': u32
10[128; 130) '99': u32
11[202; 210) '{ C {} }': C
12[204; 208) 'C {}': C
13[241; 325) '{ ...g(); }': ()
14[251; 252) 'x': A
15[255; 266) 'a::A::thing': fn thing() -> A
16[255; 268) 'a::A::thing()': A
17[278; 279) 'y': u32
18[282; 293) 'b::B::thing': fn thing() -> u32
19[282; 295) 'b::B::thing()': u32
20[305; 306) 'z': C
21[309; 320) 'c::C::thing': fn thing() -> C
22[309; 322) 'c::C::thing()': C
23
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",