diff options
Diffstat (limited to 'crates/ra_hir_ty/src/tests/simple.rs')
-rw-r--r-- | crates/ra_hir_ty/src/tests/simple.rs | 134 |
1 files changed, 73 insertions, 61 deletions
diff --git a/crates/ra_hir_ty/src/tests/simple.rs b/crates/ra_hir_ty/src/tests/simple.rs index cd919466f..de63f4cce 100644 --- a/crates/ra_hir_ty/src/tests/simple.rs +++ b/crates/ra_hir_ty/src/tests/simple.rs | |||
@@ -1,19 +1,17 @@ | |||
1 | use super::{infer, type_at, type_at_pos}; | ||
2 | use crate::test_db::TestDB; | ||
3 | use insta::assert_snapshot; | 1 | use insta::assert_snapshot; |
4 | use ra_db::fixture::WithFixture; | 2 | |
3 | use super::{check_types, infer}; | ||
5 | 4 | ||
6 | #[test] | 5 | #[test] |
7 | fn infer_box() { | 6 | fn infer_box() { |
8 | let (db, pos) = TestDB::with_position( | 7 | check_types( |
9 | r#" | 8 | r#" |
10 | //- /main.rs crate:main deps:std | 9 | //- /main.rs crate:main deps:std |
11 | |||
12 | fn test() { | 10 | fn test() { |
13 | let x = box 1; | 11 | let x = box 1; |
14 | let t = (x, box x, box &1, box [1]); | 12 | let t = (x, box x, box &1, box [1]); |
15 | t<|>; | 13 | t; |
16 | } | 14 | } //^ (Box<i32>, Box<Box<i32>>, Box<&i32>, Box<[i32; _]>) |
17 | 15 | ||
18 | //- /std.rs crate:std | 16 | //- /std.rs crate:std |
19 | #[prelude_import] use prelude::*; | 17 | #[prelude_import] use prelude::*; |
@@ -25,29 +23,24 @@ mod boxed { | |||
25 | inner: *mut T, | 23 | inner: *mut T, |
26 | } | 24 | } |
27 | } | 25 | } |
28 | |||
29 | "#, | 26 | "#, |
30 | ); | 27 | ); |
31 | assert_eq!("(Box<i32>, Box<Box<i32>>, Box<&i32>, Box<[i32; _]>)", type_at_pos(&db, pos)); | ||
32 | } | 28 | } |
33 | 29 | ||
34 | #[test] | 30 | #[test] |
35 | fn infer_adt_self() { | 31 | fn infer_adt_self() { |
36 | let (db, pos) = TestDB::with_position( | 32 | check_types( |
37 | r#" | 33 | r#" |
38 | //- /main.rs | ||
39 | enum Nat { Succ(Self), Demo(Nat), Zero } | 34 | enum Nat { Succ(Self), Demo(Nat), Zero } |
40 | 35 | ||
41 | fn test() { | 36 | fn test() { |
42 | let foo: Nat = Nat::Zero; | 37 | let foo: Nat = Nat::Zero; |
43 | if let Nat::Succ(x) = foo { | 38 | if let Nat::Succ(x) = foo { |
44 | x<|> | 39 | x |
45 | } | 40 | } //^ Nat |
46 | } | 41 | } |
47 | |||
48 | "#, | 42 | "#, |
49 | ); | 43 | ); |
50 | assert_eq!("Nat", type_at_pos(&db, pos)); | ||
51 | } | 44 | } |
52 | 45 | ||
53 | #[test] | 46 | #[test] |
@@ -93,7 +86,7 @@ fn foo() { | |||
93 | 86 | ||
94 | #[test] | 87 | #[test] |
95 | fn infer_ranges() { | 88 | fn infer_ranges() { |
96 | let (db, pos) = TestDB::with_position( | 89 | check_types( |
97 | r#" | 90 | r#" |
98 | //- /main.rs crate:main deps:core | 91 | //- /main.rs crate:main deps:core |
99 | fn test() { | 92 | fn test() { |
@@ -105,8 +98,8 @@ fn test() { | |||
105 | let f = 'a'..='z'; | 98 | let f = 'a'..='z'; |
106 | 99 | ||
107 | let t = (a, b, c, d, e, f); | 100 | let t = (a, b, c, d, e, f); |
108 | t<|>; | 101 | t; |
109 | } | 102 | } //^ (RangeFull, RangeFrom<i32>, RangeTo<u32>, Range<usize>, RangeToInclusive<i32>, RangeInclusive<char>) |
110 | 103 | ||
111 | //- /core.rs crate:core | 104 | //- /core.rs crate:core |
112 | #[prelude_import] use prelude::*; | 105 | #[prelude_import] use prelude::*; |
@@ -135,29 +128,22 @@ pub mod ops { | |||
135 | } | 128 | } |
136 | "#, | 129 | "#, |
137 | ); | 130 | ); |
138 | assert_eq!( | ||
139 | "(RangeFull, RangeFrom<i32>, RangeTo<u32>, Range<usize>, RangeToInclusive<i32>, RangeInclusive<char>)", | ||
140 | type_at_pos(&db, pos), | ||
141 | ); | ||
142 | } | 131 | } |
143 | 132 | ||
144 | #[test] | 133 | #[test] |
145 | fn infer_while_let() { | 134 | fn infer_while_let() { |
146 | let (db, pos) = TestDB::with_position( | 135 | check_types( |
147 | r#" | 136 | r#" |
148 | //- /main.rs | ||
149 | enum Option<T> { Some(T), None } | 137 | enum Option<T> { Some(T), None } |
150 | 138 | ||
151 | fn test() { | 139 | fn test() { |
152 | let foo: Option<f32> = None; | 140 | let foo: Option<f32> = None; |
153 | while let Option::Some(x) = foo { | 141 | while let Option::Some(x) = foo { |
154 | <|>x | 142 | x |
155 | } | 143 | } //^ f32 |
156 | } | 144 | } |
157 | |||
158 | "#, | 145 | "#, |
159 | ); | 146 | ); |
160 | assert_eq!("f32", type_at_pos(&db, pos)); | ||
161 | } | 147 | } |
162 | 148 | ||
163 | #[test] | 149 | #[test] |
@@ -1687,9 +1673,8 @@ fn test() { | |||
1687 | 1673 | ||
1688 | #[test] | 1674 | #[test] |
1689 | fn shadowing_primitive() { | 1675 | fn shadowing_primitive() { |
1690 | let t = type_at( | 1676 | check_types( |
1691 | r#" | 1677 | r#" |
1692 | //- /main.rs | ||
1693 | struct i32; | 1678 | struct i32; |
1694 | struct Foo; | 1679 | struct Foo; |
1695 | 1680 | ||
@@ -1697,15 +1682,15 @@ impl i32 { fn foo(&self) -> Foo { Foo } } | |||
1697 | 1682 | ||
1698 | fn main() { | 1683 | fn main() { |
1699 | let x: i32 = i32; | 1684 | let x: i32 = i32; |
1700 | x.foo()<|>; | 1685 | x.foo(); |
1686 | //^ Foo | ||
1701 | }"#, | 1687 | }"#, |
1702 | ); | 1688 | ); |
1703 | assert_eq!(t, "Foo"); | ||
1704 | } | 1689 | } |
1705 | 1690 | ||
1706 | #[test] | 1691 | #[test] |
1707 | fn not_shadowing_primitive_by_module() { | 1692 | fn not_shadowing_primitive_by_module() { |
1708 | let t = type_at( | 1693 | check_types( |
1709 | r#" | 1694 | r#" |
1710 | //- /str.rs | 1695 | //- /str.rs |
1711 | fn foo() {} | 1696 | fn foo() {} |
@@ -1715,15 +1700,15 @@ mod str; | |||
1715 | fn foo() -> &'static str { "" } | 1700 | fn foo() -> &'static str { "" } |
1716 | 1701 | ||
1717 | fn main() { | 1702 | fn main() { |
1718 | foo()<|>; | 1703 | foo(); |
1704 | //^ &str | ||
1719 | }"#, | 1705 | }"#, |
1720 | ); | 1706 | ); |
1721 | assert_eq!(t, "&str"); | ||
1722 | } | 1707 | } |
1723 | 1708 | ||
1724 | #[test] | 1709 | #[test] |
1725 | fn not_shadowing_module_by_primitive() { | 1710 | fn not_shadowing_module_by_primitive() { |
1726 | let t = type_at( | 1711 | check_types( |
1727 | r#" | 1712 | r#" |
1728 | //- /str.rs | 1713 | //- /str.rs |
1729 | fn foo() -> u32 {0} | 1714 | fn foo() -> u32 {0} |
@@ -1733,10 +1718,10 @@ mod str; | |||
1733 | fn foo() -> &'static str { "" } | 1718 | fn foo() -> &'static str { "" } |
1734 | 1719 | ||
1735 | fn main() { | 1720 | fn main() { |
1736 | str::foo()<|>; | 1721 | str::foo(); |
1722 | //^ u32 | ||
1737 | }"#, | 1723 | }"#, |
1738 | ); | 1724 | ); |
1739 | assert_eq!(t, "u32"); | ||
1740 | } | 1725 | } |
1741 | 1726 | ||
1742 | // This test is actually testing the shadowing behavior within ra_hir_def. It | 1727 | // This test is actually testing the shadowing behavior within ra_hir_def. It |
@@ -1744,27 +1729,7 @@ fn main() { | |||
1744 | // capable of asserting the necessary conditions. | 1729 | // capable of asserting the necessary conditions. |
1745 | #[test] | 1730 | #[test] |
1746 | fn should_be_shadowing_imports() { | 1731 | fn should_be_shadowing_imports() { |
1747 | let t = type_at( | 1732 | check_types( |
1748 | r#" | ||
1749 | mod a { | ||
1750 | pub fn foo() -> i8 {0} | ||
1751 | pub struct foo { a: i8 } | ||
1752 | } | ||
1753 | mod b { pub fn foo () -> u8 {0} } | ||
1754 | mod c { pub struct foo { a: u8 } } | ||
1755 | mod d { | ||
1756 | pub use super::a::*; | ||
1757 | pub use super::c::foo; | ||
1758 | pub use super::b::foo; | ||
1759 | } | ||
1760 | |||
1761 | fn main() { | ||
1762 | d::foo()<|>; | ||
1763 | }"#, | ||
1764 | ); | ||
1765 | assert_eq!(t, "u8"); | ||
1766 | |||
1767 | let t = type_at( | ||
1768 | r#" | 1733 | r#" |
1769 | mod a { | 1734 | mod a { |
1770 | pub fn foo() -> i8 {0} | 1735 | pub fn foo() -> i8 {0} |
@@ -1779,10 +1744,12 @@ mod d { | |||
1779 | } | 1744 | } |
1780 | 1745 | ||
1781 | fn main() { | 1746 | fn main() { |
1782 | d::foo{a:0<|>}; | 1747 | d::foo(); |
1748 | //^ u8 | ||
1749 | d::foo{a:0}; | ||
1750 | //^ u8 | ||
1783 | }"#, | 1751 | }"#, |
1784 | ); | 1752 | ); |
1785 | assert_eq!(t, "u8"); | ||
1786 | } | 1753 | } |
1787 | 1754 | ||
1788 | #[test] | 1755 | #[test] |
@@ -2152,3 +2119,48 @@ fn test() { | |||
2152 | "### | 2119 | "### |
2153 | ); | 2120 | ); |
2154 | } | 2121 | } |
2122 | |||
2123 | #[test] | ||
2124 | fn generic_default_depending_on_other_type_arg() { | ||
2125 | assert_snapshot!( | ||
2126 | infer(r#" | ||
2127 | struct Thing<T = u128, F = fn() -> T> { t: T } | ||
2128 | |||
2129 | fn test(t1: Thing<u32>, t2: Thing) { | ||
2130 | t1; | ||
2131 | t2; | ||
2132 | Thing::<_> { t: 1u32 }; | ||
2133 | } | ||
2134 | "#), | ||
2135 | // FIXME: the {unknown} is a bug | ||
2136 | @r###" | ||
2137 | 56..58 't1': Thing<u32, fn() -> u32> | ||
2138 | 72..74 't2': Thing<u128, fn() -> u128> | ||
2139 | 83..130 '{ ...2 }; }': () | ||
2140 | 89..91 't1': Thing<u32, fn() -> u32> | ||
2141 | 97..99 't2': Thing<u128, fn() -> u128> | ||
2142 | 105..127 'Thing:...1u32 }': Thing<u32, fn() -> {unknown}> | ||
2143 | 121..125 '1u32': u32 | ||
2144 | "### | ||
2145 | ); | ||
2146 | } | ||
2147 | |||
2148 | #[test] | ||
2149 | fn generic_default_depending_on_other_type_arg_forward() { | ||
2150 | assert_snapshot!( | ||
2151 | infer(r#" | ||
2152 | struct Thing<F = fn() -> T, T = u128> { t: T } | ||
2153 | |||
2154 | fn test(t1: Thing) { | ||
2155 | t1; | ||
2156 | } | ||
2157 | "#), | ||
2158 | // the {unknown} here is intentional, as defaults are not allowed to | ||
2159 | // refer to type parameters coming later | ||
2160 | @r###" | ||
2161 | 56..58 't1': Thing<fn() -> {unknown}, u128> | ||
2162 | 67..78 '{ t1; }': () | ||
2163 | 73..75 't1': Thing<fn() -> {unknown}, u128> | ||
2164 | "### | ||
2165 | ); | ||
2166 | } | ||