diff options
Diffstat (limited to 'crates/ra_hir/src/ty/tests.rs')
-rw-r--r-- | crates/ra_hir/src/ty/tests.rs | 142 |
1 files changed, 140 insertions, 2 deletions
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 4362bb27a..25dad81eb 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs | |||
@@ -3,6 +3,7 @@ use std::sync::Arc; | |||
3 | 3 | ||
4 | use insta::assert_snapshot; | 4 | use insta::assert_snapshot; |
5 | 5 | ||
6 | use ra_cfg::CfgOptions; | ||
6 | use ra_db::{salsa::Database, FilePosition, SourceDatabase}; | 7 | use ra_db::{salsa::Database, FilePosition, SourceDatabase}; |
7 | use ra_syntax::{ | 8 | use ra_syntax::{ |
8 | algo, | 9 | algo, |
@@ -24,6 +25,50 @@ mod never_type; | |||
24 | mod coercion; | 25 | mod coercion; |
25 | 26 | ||
26 | #[test] | 27 | #[test] |
28 | fn cfg_impl_block() { | ||
29 | let (mut db, pos) = MockDatabase::with_position( | ||
30 | r#" | ||
31 | //- /main.rs | ||
32 | use foo::S as T; | ||
33 | struct S; | ||
34 | |||
35 | #[cfg(test)] | ||
36 | impl S { | ||
37 | fn foo1(&self) -> i32 { 0 } | ||
38 | } | ||
39 | |||
40 | #[cfg(not(test))] | ||
41 | impl S { | ||
42 | fn foo2(&self) -> i32 { 0 } | ||
43 | } | ||
44 | |||
45 | fn test() { | ||
46 | let t = (S.foo1(), S.foo2(), T.foo3(), T.foo4()); | ||
47 | t<|>; | ||
48 | } | ||
49 | |||
50 | //- /foo.rs | ||
51 | struct S; | ||
52 | |||
53 | #[cfg(not(test))] | ||
54 | impl S { | ||
55 | fn foo3(&self) -> i32 { 0 } | ||
56 | } | ||
57 | |||
58 | #[cfg(test)] | ||
59 | impl S { | ||
60 | fn foo4(&self) -> i32 { 0 } | ||
61 | } | ||
62 | "#, | ||
63 | ); | ||
64 | db.set_crate_graph_from_fixture(crate_graph! { | ||
65 | "main": ("/main.rs", ["foo"], CfgOptions::default().atom("test".into())), | ||
66 | "foo": ("/foo.rs", []), | ||
67 | }); | ||
68 | assert_eq!("(i32, {unknown}, i32, {unknown})", type_at_pos(&db, pos)); | ||
69 | } | ||
70 | |||
71 | #[test] | ||
27 | fn infer_await() { | 72 | fn infer_await() { |
28 | let (mut db, pos) = MockDatabase::with_position( | 73 | let (mut db, pos) = MockDatabase::with_position( |
29 | r#" | 74 | r#" |
@@ -218,7 +263,7 @@ fn test(a: u32, b: isize, c: !, d: &str) { | |||
218 | [17; 18) 'b': isize | 263 | [17; 18) 'b': isize |
219 | [27; 28) 'c': ! | 264 | [27; 28) 'c': ! |
220 | [33; 34) 'd': &str | 265 | [33; 34) 'd': &str |
221 | [42; 121) '{ ...f32; }': () | 266 | [42; 121) '{ ...f32; }': ! |
222 | [48; 49) 'a': u32 | 267 | [48; 49) 'a': u32 |
223 | [55; 56) 'b': isize | 268 | [55; 56) 'b': isize |
224 | [62; 63) 'c': ! | 269 | [62; 63) 'c': ! |
@@ -981,6 +1026,67 @@ fn main(foo: Foo) { | |||
981 | } | 1026 | } |
982 | 1027 | ||
983 | #[test] | 1028 | #[test] |
1029 | fn infer_if_match_with_return() { | ||
1030 | assert_snapshot!( | ||
1031 | infer(r#" | ||
1032 | fn foo() { | ||
1033 | let _x1 = if true { | ||
1034 | 1 | ||
1035 | } else { | ||
1036 | return; | ||
1037 | }; | ||
1038 | let _x2 = if true { | ||
1039 | 2 | ||
1040 | } else { | ||
1041 | return | ||
1042 | }; | ||
1043 | let _x3 = match true { | ||
1044 | true => 3, | ||
1045 | _ => { | ||
1046 | return; | ||
1047 | } | ||
1048 | }; | ||
1049 | let _x4 = match true { | ||
1050 | true => 4, | ||
1051 | _ => return | ||
1052 | }; | ||
1053 | }"#), | ||
1054 | @r###" | ||
1055 | [10; 323) '{ ... }; }': () | ||
1056 | [20; 23) '_x1': i32 | ||
1057 | [26; 80) 'if tru... }': i32 | ||
1058 | [29; 33) 'true': bool | ||
1059 | [34; 51) '{ ... }': i32 | ||
1060 | [44; 45) '1': i32 | ||
1061 | [57; 80) '{ ... }': ! | ||
1062 | [67; 73) 'return': ! | ||
1063 | [90; 93) '_x2': i32 | ||
1064 | [96; 149) 'if tru... }': i32 | ||
1065 | [99; 103) 'true': bool | ||
1066 | [104; 121) '{ ... }': i32 | ||
1067 | [114; 115) '2': i32 | ||
1068 | [127; 149) '{ ... }': ! | ||
1069 | [137; 143) 'return': ! | ||
1070 | [159; 162) '_x3': i32 | ||
1071 | [165; 247) 'match ... }': i32 | ||
1072 | [171; 175) 'true': bool | ||
1073 | [186; 190) 'true': bool | ||
1074 | [194; 195) '3': i32 | ||
1075 | [205; 206) '_': bool | ||
1076 | [210; 241) '{ ... }': ! | ||
1077 | [224; 230) 'return': ! | ||
1078 | [257; 260) '_x4': i32 | ||
1079 | [263; 320) 'match ... }': i32 | ||
1080 | [269; 273) 'true': bool | ||
1081 | [284; 288) 'true': bool | ||
1082 | [292; 293) '4': i32 | ||
1083 | [303; 304) '_': bool | ||
1084 | [308; 314) 'return': ! | ||
1085 | "### | ||
1086 | ) | ||
1087 | } | ||
1088 | |||
1089 | #[test] | ||
984 | fn infer_inherent_method() { | 1090 | fn infer_inherent_method() { |
985 | assert_snapshot!( | 1091 | assert_snapshot!( |
986 | infer(r#" | 1092 | infer(r#" |
@@ -3130,6 +3236,39 @@ fn test() { S.foo()<|>; } | |||
3130 | assert_eq!(t, "u128"); | 3236 | assert_eq!(t, "u128"); |
3131 | } | 3237 | } |
3132 | 3238 | ||
3239 | #[test] | ||
3240 | fn infer_macro_with_dollar_crate_is_correct_in_expr() { | ||
3241 | covers!(macro_dollar_crate_other); | ||
3242 | let (mut db, pos) = MockDatabase::with_position( | ||
3243 | r#" | ||
3244 | //- /main.rs | ||
3245 | fn test() { | ||
3246 | let x = (foo::foo!(1), foo::foo!(2)); | ||
3247 | x<|>; | ||
3248 | } | ||
3249 | |||
3250 | //- /lib.rs | ||
3251 | #[macro_export] | ||
3252 | macro_rules! foo { | ||
3253 | (1) => { $crate::bar!() }; | ||
3254 | (2) => { 1 + $crate::baz() }; | ||
3255 | } | ||
3256 | |||
3257 | #[macro_export] | ||
3258 | macro_rules! bar { | ||
3259 | () => { 42 } | ||
3260 | } | ||
3261 | |||
3262 | pub fn baz() -> usize { 31usize } | ||
3263 | "#, | ||
3264 | ); | ||
3265 | db.set_crate_graph_from_fixture(crate_graph! { | ||
3266 | "main": ("/main.rs", ["foo"]), | ||
3267 | "foo": ("/lib.rs", []), | ||
3268 | }); | ||
3269 | assert_eq!("(i32, usize)", type_at_pos(&db, pos)); | ||
3270 | } | ||
3271 | |||
3133 | #[ignore] | 3272 | #[ignore] |
3134 | #[test] | 3273 | #[test] |
3135 | fn method_resolution_trait_before_autoref() { | 3274 | fn method_resolution_trait_before_autoref() { |
@@ -3321,7 +3460,6 @@ fn test() { S2.into()<|>; } | |||
3321 | 3460 | ||
3322 | #[test] | 3461 | #[test] |
3323 | fn method_resolution_encountering_fn_type() { | 3462 | fn method_resolution_encountering_fn_type() { |
3324 | covers!(trait_resolution_on_fn_type); | ||
3325 | type_at( | 3463 | type_at( |
3326 | r#" | 3464 | r#" |
3327 | //- /main.rs | 3465 | //- /main.rs |