aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty/src/tests/simple.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_ty/src/tests/simple.rs')
-rw-r--r--crates/ra_hir_ty/src/tests/simple.rs258
1 files changed, 250 insertions, 8 deletions
diff --git a/crates/ra_hir_ty/src/tests/simple.rs b/crates/ra_hir_ty/src/tests/simple.rs
index 56abc65b8..88309157b 100644
--- a/crates/ra_hir_ty/src/tests/simple.rs
+++ b/crates/ra_hir_ty/src/tests/simple.rs
@@ -179,7 +179,7 @@ fn test(a: u32, b: isize, c: !, d: &str) {
179 17..18 'b': isize 179 17..18 'b': isize
180 27..28 'c': ! 180 27..28 'c': !
181 33..34 'd': &str 181 33..34 'd': &str
182 42..121 '{ ...f32; }': ! 182 42..121 '{ ...f32; }': ()
183 48..49 'a': u32 183 48..49 'a': u32
184 55..56 'b': isize 184 55..56 'b': isize
185 62..63 'c': ! 185 62..63 'c': !
@@ -385,6 +385,26 @@ fn test(a: &u32, b: &mut u32, c: *const u32, d: *mut u32) {
385} 385}
386 386
387#[test] 387#[test]
388fn infer_raw_ref() {
389 assert_snapshot!(
390 infer(r#"
391fn test(a: i32) {
392 &raw mut a;
393 &raw const a;
394}
395"#),
396 @r###"
397 9..10 'a': i32
398 17..54 '{ ...t a; }': ()
399 23..33 '&raw mut a': *mut i32
400 32..33 'a': i32
401 39..51 '&raw const a': *const i32
402 50..51 'a': i32
403 "###
404 );
405}
406
407#[test]
388fn infer_literals() { 408fn infer_literals() {
389 assert_snapshot!( 409 assert_snapshot!(
390 infer(r##" 410 infer(r##"
@@ -414,7 +434,7 @@ fn test() {
414 27..31 '5f32': f32 434 27..31 '5f32': f32
415 37..41 '5f64': f64 435 37..41 '5f64': f64
416 47..54 '"hello"': &str 436 47..54 '"hello"': &str
417 60..68 'b"bytes"': &[u8] 437 60..68 'b"bytes"': &[u8; _]
418 74..77 ''c'': char 438 74..77 ''c'': char
419 83..87 'b'b'': u8 439 83..87 'b'b'': u8
420 93..97 '3.14': f64 440 93..97 '3.14': f64
@@ -422,7 +442,7 @@ fn test() {
422 113..118 'false': bool 442 113..118 'false': bool
423 124..128 'true': bool 443 124..128 'true': bool
424 134..202 'r#" ... "#': &str 444 134..202 'r#" ... "#': &str
425 208..218 'br#"yolo"#': &[u8] 445 208..218 'br#"yolo"#': &[u8; _]
426 "### 446 "###
427 ); 447 );
428} 448}
@@ -576,6 +596,50 @@ impl S {
576} 596}
577 597
578#[test] 598#[test]
599fn infer_self_as_path() {
600 assert_snapshot!(
601 infer(r#"
602struct S1;
603struct S2(isize);
604enum E {
605 V1,
606 V2(u32),
607}
608
609impl S1 {
610 fn test() {
611 Self;
612 }
613}
614impl S2 {
615 fn test() {
616 Self(1);
617 }
618}
619impl E {
620 fn test() {
621 Self::V1;
622 Self::V2(1);
623 }
624}
625"#),
626 @r###"
627 87..108 '{ ... }': ()
628 97..101 'Self': S1
629 135..159 '{ ... }': ()
630 145..149 'Self': S2(isize) -> S2
631 145..152 'Self(1)': S2
632 150..151 '1': isize
633 185..231 '{ ... }': ()
634 195..203 'Self::V1': E
635 213..221 'Self::V2': V2(u32) -> E
636 213..224 'Self::V2(1)': E
637 222..223 '1': u32
638 "###
639 );
640}
641
642#[test]
579fn infer_binary_op() { 643fn infer_binary_op() {
580 assert_snapshot!( 644 assert_snapshot!(
581 infer(r#" 645 infer(r#"
@@ -893,7 +957,7 @@ fn main(foo: Foo) {
893 51..107 'if tru... }': () 957 51..107 'if tru... }': ()
894 54..58 'true': bool 958 54..58 'true': bool
895 59..67 '{ }': () 959 59..67 '{ }': ()
896 73..107 'if fal... }': () 960 73..107 'if fal... }': i32
897 76..81 'false': bool 961 76..81 'false': bool
898 82..107 '{ ... }': i32 962 82..107 '{ ... }': i32
899 92..95 'foo': Foo 963 92..95 'foo': Foo
@@ -935,7 +999,7 @@ fn foo() {
935 29..33 'true': bool 999 29..33 'true': bool
936 34..51 '{ ... }': i32 1000 34..51 '{ ... }': i32
937 44..45 '1': i32 1001 44..45 '1': i32
938 57..80 '{ ... }': ! 1002 57..80 '{ ... }': i32
939 67..73 'return': ! 1003 67..73 'return': !
940 90..93 '_x2': i32 1004 90..93 '_x2': i32
941 96..149 'if tru... }': i32 1005 96..149 'if tru... }': i32
@@ -951,7 +1015,7 @@ fn foo() {
951 186..190 'true': bool 1015 186..190 'true': bool
952 194..195 '3': i32 1016 194..195 '3': i32
953 205..206 '_': bool 1017 205..206 '_': bool
954 210..241 '{ ... }': ! 1018 210..241 '{ ... }': i32
955 224..230 'return': ! 1019 224..230 'return': !
956 257..260 '_x4': i32 1020 257..260 '_x4': i32
957 263..320 'match ... }': i32 1021 263..320 'match ... }': i32
@@ -1687,7 +1751,7 @@ fn foo() -> u32 {
1687 17..59 '{ ...; }; }': () 1751 17..59 '{ ...; }; }': ()
1688 27..28 'x': || -> usize 1752 27..28 'x': || -> usize
1689 31..56 '|| -> ...n 1; }': || -> usize 1753 31..56 '|| -> ...n 1; }': || -> usize
1690 43..56 '{ return 1; }': ! 1754 43..56 '{ return 1; }': usize
1691 45..53 'return 1': ! 1755 45..53 'return 1': !
1692 52..53 '1': usize 1756 52..53 '1': usize
1693 "### 1757 "###
@@ -1706,7 +1770,7 @@ fn foo() -> u32 {
1706 17..48 '{ ...; }; }': () 1770 17..48 '{ ...; }; }': ()
1707 27..28 'x': || -> () 1771 27..28 'x': || -> ()
1708 31..45 '|| { return; }': || -> () 1772 31..45 '|| { return; }': || -> ()
1709 34..45 '{ return; }': ! 1773 34..45 '{ return; }': ()
1710 36..42 'return': ! 1774 36..42 'return': !
1711 "### 1775 "###
1712 ); 1776 );
@@ -1755,3 +1819,181 @@ fn main() {
1755 "### 1819 "###
1756 ); 1820 );
1757} 1821}
1822
1823#[test]
1824fn effects_smoke_test() {
1825 assert_snapshot!(
1826 infer(r#"
1827fn main() {
1828 let x = unsafe { 92 };
1829 let y = async { async { () }.await };
1830 let z = try { () };
1831 let t = 'a: { 92 };
1832}
1833"#),
1834 @r###"
1835 11..131 '{ ...2 }; }': ()
1836 21..22 'x': i32
1837 32..38 '{ 92 }': i32
1838 34..36 '92': i32
1839 48..49 'y': {unknown}
1840 58..80 '{ asyn...wait }': {unknown}
1841 60..78 'async ....await': {unknown}
1842 66..72 '{ () }': ()
1843 68..70 '()': ()
1844 90..91 'z': {unknown}
1845 94..104 'try { () }': {unknown}
1846 98..104 '{ () }': ()
1847 100..102 '()': ()
1848 114..115 't': i32
1849 122..128 '{ 92 }': i32
1850 124..126 '92': i32
1851 "###
1852 )
1853}
1854
1855#[test]
1856fn infer_generic_from_later_assignment() {
1857 assert_snapshot!(
1858 infer(r#"
1859enum Option<T> { Some(T), None }
1860use Option::*;
1861
1862fn test() {
1863 let mut end = None;
1864 loop {
1865 end = Some(true);
1866 }
1867}
1868"#),
1869 @r###"
1870 60..130 '{ ... } }': ()
1871 70..77 'mut end': Option<bool>
1872 80..84 'None': Option<bool>
1873 90..128 'loop {... }': !
1874 95..128 '{ ... }': ()
1875 105..108 'end': Option<bool>
1876 105..121 'end = ...(true)': ()
1877 111..115 'Some': Some<bool>(bool) -> Option<bool>
1878 111..121 'Some(true)': Option<bool>
1879 116..120 'true': bool
1880 "###
1881 );
1882}
1883
1884#[test]
1885fn infer_loop_break_with_val() {
1886 assert_snapshot!(
1887 infer(r#"
1888enum Option<T> { Some(T), None }
1889use Option::*;
1890
1891fn test() {
1892 let x = loop {
1893 if false {
1894 break None;
1895 }
1896
1897 break Some(true);
1898 };
1899}
1900"#),
1901 @r###"
1902 60..169 '{ ... }; }': ()
1903 70..71 'x': Option<bool>
1904 74..166 'loop {... }': Option<bool>
1905 79..166 '{ ... }': ()
1906 89..133 'if fal... }': ()
1907 92..97 'false': bool
1908 98..133 '{ ... }': ()
1909 112..122 'break None': !
1910 118..122 'None': Option<bool>
1911 143..159 'break ...(true)': !
1912 149..153 'Some': Some<bool>(bool) -> Option<bool>
1913 149..159 'Some(true)': Option<bool>
1914 154..158 'true': bool
1915 "###
1916 );
1917}
1918
1919#[test]
1920fn infer_loop_break_without_val() {
1921 assert_snapshot!(
1922 infer(r#"
1923enum Option<T> { Some(T), None }
1924use Option::*;
1925
1926fn test() {
1927 let x = loop {
1928 if false {
1929 break;
1930 }
1931 };
1932}
1933"#),
1934 @r###"
1935 60..137 '{ ... }; }': ()
1936 70..71 'x': ()
1937 74..134 'loop {... }': ()
1938 79..134 '{ ... }': ()
1939 89..128 'if fal... }': ()
1940 92..97 'false': bool
1941 98..128 '{ ... }': ()
1942 112..117 'break': !
1943 "###
1944 );
1945}
1946
1947#[test]
1948fn infer_labelled_break_with_val() {
1949 assert_snapshot!(
1950 infer(r#"
1951fn foo() {
1952 let _x = || 'outer: loop {
1953 let inner = 'inner: loop {
1954 let i = Default::default();
1955 if (break 'outer i) {
1956 loop { break 'inner 5i8; };
1957 } else if true {
1958 break 'inner 6;
1959 }
1960 break 7;
1961 };
1962 break inner < 8;
1963 };
1964}
1965"#),
1966 @r###"
1967 10..336 '{ ... }; }': ()
1968 20..22 '_x': || -> bool
1969 25..333 '|| 'ou... }': || -> bool
1970 28..333 ''outer... }': bool
1971 41..333 '{ ... }': ()
1972 55..60 'inner': i8
1973 63..301 ''inner... }': i8
1974 76..301 '{ ... }': ()
1975 94..95 'i': bool
1976 98..114 'Defaul...efault': {unknown}
1977 98..116 'Defaul...ault()': bool
1978 130..270 'if (br... }': ()
1979 134..148 'break 'outer i': !
1980 147..148 'i': bool
1981 150..209 '{ ... }': ()
1982 168..194 'loop {...5i8; }': !
1983 173..194 '{ brea...5i8; }': ()
1984 175..191 'break ...er 5i8': !
1985 188..191 '5i8': i8
1986 215..270 'if tru... }': ()
1987 218..222 'true': bool
1988 223..270 '{ ... }': ()
1989 241..255 'break 'inner 6': !
1990 254..255 '6': i8
1991 283..290 'break 7': !
1992 289..290 '7': i8
1993 311..326 'break inner < 8': !
1994 317..322 'inner': i8
1995 317..326 'inner < 8': bool
1996 325..326 '8': i8
1997 "###
1998 );
1999}