diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-03-17 21:41:37 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-03-17 21:41:37 +0000 |
commit | 40c6dd1f4c57d6a8ec26c1bdef24753c884c38aa (patch) | |
tree | cf9959b252a7c9e25c4e186b57f92ff9d6bec61d /crates/ra_hir/src/ty/tests.rs | |
parent | 91e7a3b6f2a9f1a09f874e9644491898338a842f (diff) | |
parent | 6299ccd350c190003c51aa68f48b1edfb1a497b1 (diff) |
Merge #982
982: Implement BindingMode for pattern matching. r=flodiebold a=mjkillough
Implement `BindingMode` for pattern matching, so that types can be
correctly inferred using match ergonomics. The binding mode defaults to
`Move` (referred to as 'BindingMode::BindByValue` in rustc), and is
updated by automatic dereferencing of the value being matched.
Fixes #888.
- [Binding modes in The Reference](https://doc.rust-lang.org/reference/patterns.html#binding-modes)
- [`rustc` implementation](https://github.com/rust-lang/rust/blob/e17c48e2f21eefd59748e364234efc7037a3ec96/src/librustc_typeck/check/_match.rs#L77) (and [definition of `BindingMode`](https://github.com/rust-lang/rust/blob/e957ed9d10ec589bdd523b88b4b44c41b1ecf763/src/librustc/ty/binding.rs))
- [Match Ergonomics RFC](https://github.com/rust-lang/rfcs/blob/master/text/2005-match-ergonomics.md#binding-mode-rules)
Co-authored-by: Michael Killough <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/ty/tests.rs')
-rw-r--r-- | crates/ra_hir/src/ty/tests.rs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index acae71c26..0f2172ddf 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs | |||
@@ -831,6 +831,60 @@ fn test(x: &i32) { | |||
831 | } | 831 | } |
832 | 832 | ||
833 | #[test] | 833 | #[test] |
834 | fn infer_pattern_match_ergonomics() { | ||
835 | assert_snapshot_matches!( | ||
836 | infer(r#" | ||
837 | struct A<T>(T); | ||
838 | |||
839 | fn test() { | ||
840 | let A(n) = &A(1); | ||
841 | let A(n) = &mut A(1); | ||
842 | } | ||
843 | "#), | ||
844 | @r###" | ||
845 | [28; 79) '{ ...(1); }': () | ||
846 | [38; 42) 'A(n)': A<i32> | ||
847 | [40; 41) 'n': &i32 | ||
848 | [45; 50) '&A(1)': &A<i32> | ||
849 | [46; 47) 'A': A<i32>(T) -> A<T> | ||
850 | [46; 50) 'A(1)': A<i32> | ||
851 | [48; 49) '1': i32 | ||
852 | [60; 64) 'A(n)': A<i32> | ||
853 | [62; 63) 'n': &mut i32 | ||
854 | [67; 76) '&mut A(1)': &mut A<i32> | ||
855 | [72; 73) 'A': A<i32>(T) -> A<T> | ||
856 | [72; 76) 'A(1)': A<i32> | ||
857 | [74; 75) '1': i32"### | ||
858 | ); | ||
859 | } | ||
860 | |||
861 | #[test] | ||
862 | fn infer_pattern_match_ergonomics_ref() { | ||
863 | covers!(match_ergonomics_ref); | ||
864 | assert_snapshot_matches!( | ||
865 | infer(r#" | ||
866 | fn test() { | ||
867 | let v = &(1, &2); | ||
868 | let (_, &w) = v; | ||
869 | } | ||
870 | "#), | ||
871 | @r###" | ||
872 | [11; 57) '{ ...= v; }': () | ||
873 | [21; 22) 'v': &(i32, &i32) | ||
874 | [25; 33) '&(1, &2)': &(i32, &i32) | ||
875 | [26; 33) '(1, &2)': (i32, &i32) | ||
876 | [27; 28) '1': i32 | ||
877 | [30; 32) '&2': &i32 | ||
878 | [31; 32) '2': i32 | ||
879 | [43; 50) '(_, &w)': (i32, &i32) | ||
880 | [44; 45) '_': i32 | ||
881 | [47; 49) '&w': &i32 | ||
882 | [48; 49) 'w': i32 | ||
883 | [53; 54) 'v': &(i32, &i32)"### | ||
884 | ); | ||
885 | } | ||
886 | |||
887 | #[test] | ||
834 | fn infer_adt_pattern() { | 888 | fn infer_adt_pattern() { |
835 | assert_snapshot_matches!( | 889 | assert_snapshot_matches!( |
836 | infer(r#" | 890 | infer(r#" |