diff options
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r-- | crates/ra_hir/src/marks.rs | 1 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/infer.rs | 3 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests.rs | 175 |
3 files changed, 103 insertions, 76 deletions
diff --git a/crates/ra_hir/src/marks.rs b/crates/ra_hir/src/marks.rs index 16852a6a1..6f3e5f09d 100644 --- a/crates/ra_hir/src/marks.rs +++ b/crates/ra_hir/src/marks.rs | |||
@@ -7,4 +7,5 @@ test_utils::marks!( | |||
7 | glob_enum | 7 | glob_enum |
8 | glob_across_crates | 8 | glob_across_crates |
9 | std_prelude | 9 | std_prelude |
10 | match_ergonomics_ref | ||
10 | ); | 11 | ); |
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 3d0b7a827..0a698988c 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs | |||
@@ -619,6 +619,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
619 | } | 619 | } |
620 | } | 620 | } |
621 | } else if let Pat::Ref { .. } = &body[pat] { | 621 | } else if let Pat::Ref { .. } = &body[pat] { |
622 | tested_by!(match_ergonomics_ref); | ||
623 | // When you encounter a `&pat` pattern, reset to Move. | ||
624 | // This is so that `w` is by value: `let (_, &w) = &(1, &2);` | ||
622 | default_bm = BindingMode::Move; | 625 | default_bm = BindingMode::Move; |
623 | } | 626 | } |
624 | 627 | ||
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index b12ea4334..0f8551a9d 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs | |||
@@ -756,8 +756,6 @@ fn test(x: &str, y: isize) { | |||
756 | fn infer_pattern() { | 756 | fn infer_pattern() { |
757 | assert_snapshot_matches!( | 757 | assert_snapshot_matches!( |
758 | infer(r#" | 758 | infer(r#" |
759 | struct A<T>(T); | ||
760 | |||
761 | fn test(x: &i32) { | 759 | fn test(x: &i32) { |
762 | let y = x; | 760 | let y = x; |
763 | let &z = x; | 761 | let &z = x; |
@@ -774,12 +772,6 @@ fn test(x: &i32) { | |||
774 | 772 | ||
775 | let lambda = |a: u64, b, c: i32| { a + b; c }; | 773 | let lambda = |a: u64, b, c: i32| { a + b; c }; |
776 | 774 | ||
777 | let A(n) = &A(1); | ||
778 | let A(n) = &mut A(1); | ||
779 | |||
780 | let v = &(1, &2); | ||
781 | let (_, &w) = v; | ||
782 | |||
783 | let ref ref_to_x = x; | 775 | let ref ref_to_x = x; |
784 | let mut mut_x = x; | 776 | let mut mut_x = x; |
785 | let ref mut mut_ref_to_x = x; | 777 | let ref mut mut_ref_to_x = x; |
@@ -787,76 +779,107 @@ fn test(x: &i32) { | |||
787 | } | 779 | } |
788 | "#), | 780 | "#), |
789 | @r###" | 781 | @r###" |
790 | [26; 27) 'x': &i32 | 782 | [9; 10) 'x': &i32 |
791 | [35; 479) '{ ...o_x; }': () | 783 | [18; 369) '{ ...o_x; }': () |
792 | [45; 46) 'y': &i32 | 784 | [28; 29) 'y': &i32 |
793 | [49; 50) 'x': &i32 | 785 | [32; 33) 'x': &i32 |
794 | [60; 62) '&z': &i32 | 786 | [43; 45) '&z': &i32 |
795 | [61; 62) 'z': i32 | 787 | [44; 45) 'z': i32 |
796 | [65; 66) 'x': &i32 | 788 | [48; 49) 'x': &i32 |
797 | [76; 77) 'a': i32 | 789 | [59; 60) 'a': i32 |
798 | [80; 81) 'z': i32 | 790 | [63; 64) 'z': i32 |
799 | [91; 97) '(c, d)': (i32, &str) | 791 | [74; 80) '(c, d)': (i32, &str) |
800 | [92; 93) 'c': i32 | 792 | [75; 76) 'c': i32 |
801 | [95; 96) 'd': &str | 793 | [78; 79) 'd': &str |
802 | [100; 112) '(1, "hello")': (i32, &str) | 794 | [83; 95) '(1, "hello")': (i32, &str) |
803 | [101; 102) '1': i32 | 795 | [84; 85) '1': i32 |
804 | [104; 111) '"hello"': &str | 796 | [87; 94) '"hello"': &str |
805 | [119; 169) 'for (e... }': () | 797 | [102; 152) 'for (e... }': () |
806 | [123; 129) '(e, f)': ({unknown}, {unknown}) | 798 | [106; 112) '(e, f)': ({unknown}, {unknown}) |
807 | [124; 125) 'e': {unknown} | 799 | [107; 108) 'e': {unknown} |
808 | [127; 128) 'f': {unknown} | 800 | [110; 111) 'f': {unknown} |
809 | [133; 142) 'some_iter': {unknown} | 801 | [116; 125) 'some_iter': {unknown} |
810 | [143; 169) '{ ... }': () | 802 | [126; 152) '{ ... }': () |
811 | [157; 158) 'g': {unknown} | 803 | [140; 141) 'g': {unknown} |
812 | [161; 162) 'e': {unknown} | 804 | [144; 145) 'e': {unknown} |
813 | [175; 222) 'if let... }': () | 805 | [158; 205) 'if let... }': () |
814 | [182; 187) '[val]': {unknown} | 806 | [165; 170) '[val]': {unknown} |
815 | [190; 193) 'opt': {unknown} | 807 | [173; 176) 'opt': {unknown} |
816 | [194; 222) '{ ... }': () | 808 | [177; 205) '{ ... }': () |
817 | [208; 209) 'h': {unknown} | 809 | [191; 192) 'h': {unknown} |
818 | [212; 215) 'val': {unknown} | 810 | [195; 198) 'val': {unknown} |
819 | [232; 238) 'lambda': {unknown} | 811 | [215; 221) 'lambda': {unknown} |
820 | [241; 273) '|a: u6...b; c }': {unknown} | 812 | [224; 256) '|a: u6...b; c }': {unknown} |
821 | [242; 243) 'a': u64 | 813 | [225; 226) 'a': u64 |
814 | [233; 234) 'b': u64 | ||
815 | [236; 237) 'c': i32 | ||
816 | [244; 256) '{ a + b; c }': i32 | ||
817 | [246; 247) 'a': u64 | ||
818 | [246; 251) 'a + b': u64 | ||
822 | [250; 251) 'b': u64 | 819 | [250; 251) 'b': u64 |
823 | [253; 254) 'c': i32 | 820 | [253; 254) 'c': i32 |
824 | [261; 273) '{ a + b; c }': i32 | 821 | [267; 279) 'ref ref_to_x': &&i32 |
825 | [263; 264) 'a': u64 | 822 | [282; 283) 'x': &i32 |
826 | [263; 268) 'a + b': u64 | 823 | [293; 302) 'mut mut_x': &i32 |
827 | [267; 268) 'b': u64 | 824 | [305; 306) 'x': &i32 |
828 | [270; 271) 'c': i32 | 825 | [316; 336) 'ref mu...f_to_x': &mut &i32 |
829 | [284; 288) 'A(n)': A<i32> | 826 | [339; 340) 'x': &i32 |
830 | [286; 287) 'n': &i32 | 827 | [350; 351) 'k': &mut &i32 |
831 | [291; 296) '&A(1)': &A<i32> | 828 | [354; 366) 'mut_ref_to_x': &mut &i32"### |
832 | [292; 293) 'A': A<i32>(T) -> A<T> | 829 | ); |
833 | [292; 296) 'A(1)': A<i32> | 830 | } |
834 | [294; 295) '1': i32 | 831 | |
835 | [306; 310) 'A(n)': A<i32> | 832 | #[test] |
836 | [308; 309) 'n': &mut i32 | 833 | fn infer_pattern_match_ergonomics() { |
837 | [313; 322) '&mut A(1)': &mut A<i32> | 834 | assert_snapshot_matches!( |
838 | [318; 319) 'A': A<i32>(T) -> A<T> | 835 | infer(r#" |
839 | [318; 322) 'A(1)': A<i32> | 836 | struct A<T>(T); |
840 | [320; 321) '1': i32 | 837 | |
841 | [333; 334) 'v': &(i32, &i32) | 838 | fn test() { |
842 | [337; 345) '&(1, &2)': &(i32, &i32) | 839 | let A(n) = &A(1); |
843 | [338; 345) '(1, &2)': (i32, &i32) | 840 | let A(n) = &mut A(1); |
844 | [339; 340) '1': i32 | 841 | } |
845 | [342; 344) '&2': &i32 | 842 | "#), |
846 | [343; 344) '2': i32 | 843 | @r###" |
847 | [355; 362) '(_, &w)': (i32, &i32) | 844 | [28; 79) '{ ...(1); }': () |
848 | [356; 357) '_': i32 | 845 | [38; 42) 'A(n)': A<i32> |
849 | [359; 361) '&w': &i32 | 846 | [40; 41) 'n': &i32 |
850 | [360; 361) 'w': i32 | 847 | [45; 50) '&A(1)': &A<i32> |
851 | [365; 366) 'v': &(i32, &i32) | 848 | [46; 47) 'A': A<i32>(T) -> A<T> |
852 | [377; 389) 'ref ref_to_x': &&i32 | 849 | [46; 50) 'A(1)': A<i32> |
853 | [392; 393) 'x': &i32 | 850 | [48; 49) '1': i32 |
854 | [403; 412) 'mut mut_x': &i32 | 851 | [60; 64) 'A(n)': A<i32> |
855 | [415; 416) 'x': &i32 | 852 | [62; 63) 'n': &mut i32 |
856 | [426; 446) 'ref mu...f_to_x': &mut &i32 | 853 | [67; 76) '&mut A(1)': &mut A<i32> |
857 | [449; 450) 'x': &i32 | 854 | [72; 73) 'A': A<i32>(T) -> A<T> |
858 | [460; 461) 'k': &mut &i32 | 855 | [72; 76) 'A(1)': A<i32> |
859 | [464; 476) 'mut_ref_to_x': &mut &i32"### | 856 | [74; 75) '1': i32"### |
857 | ); | ||
858 | } | ||
859 | |||
860 | #[test] | ||
861 | fn infer_pattern_match_ergonomics_ref() { | ||
862 | covers!(match_ergonomics_ref); | ||
863 | assert_snapshot_matches!( | ||
864 | infer(r#" | ||
865 | fn test() { | ||
866 | let v = &(1, &2); | ||
867 | let (_, &w) = v; | ||
868 | } | ||
869 | "#), | ||
870 | @r###" | ||
871 | [11; 57) '{ ...= v; }': () | ||
872 | [21; 22) 'v': &(i32, &i32) | ||
873 | [25; 33) '&(1, &2)': &(i32, &i32) | ||
874 | [26; 33) '(1, &2)': (i32, &i32) | ||
875 | [27; 28) '1': i32 | ||
876 | [30; 32) '&2': &i32 | ||
877 | [31; 32) '2': i32 | ||
878 | [43; 50) '(_, &w)': (i32, &i32) | ||
879 | [44; 45) '_': i32 | ||
880 | [47; 49) '&w': &i32 | ||
881 | [48; 49) 'w': i32 | ||
882 | [53; 54) 'v': &(i32, &i32)"### | ||
860 | ); | 883 | ); |
861 | } | 884 | } |
862 | 885 | ||