aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_hir/src/marks.rs1
-rw-r--r--crates/ra_hir/src/mock.rs68
-rw-r--r--crates/ra_hir/src/ty/tests.rs84
3 files changed, 25 insertions, 128 deletions
diff --git a/crates/ra_hir/src/marks.rs b/crates/ra_hir/src/marks.rs
index b423489a1..c086e5bed 100644
--- a/crates/ra_hir/src/marks.rs
+++ b/crates/ra_hir/src/marks.rs
@@ -17,5 +17,4 @@ test_utils::marks!(
17 prelude_is_macro_use 17 prelude_is_macro_use
18 coerce_merge_fail_fallback 18 coerce_merge_fail_fallback
19 macro_dollar_crate_self 19 macro_dollar_crate_self
20 macro_dollar_crate_other
21); 20);
diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs
index 8d98f88ce..ab97a09b9 100644
--- a/crates/ra_hir/src/mock.rs
+++ b/crates/ra_hir/src/mock.rs
@@ -77,12 +77,6 @@ impl MockDatabase {
77 (db, source_root, file_id) 77 (db, source_root, file_id)
78 } 78 }
79 79
80 pub fn with_position(fixture: &str) -> (MockDatabase, FilePosition) {
81 let (db, position) = MockDatabase::from_fixture(fixture);
82 let position = position.expect("expected a marker ( <|> )");
83 (db, position)
84 }
85
86 pub fn file_id_of(&self, path: &str) -> FileId { 80 pub fn file_id_of(&self, path: &str) -> FileId {
87 match self.files.get(path) { 81 match self.files.get(path) {
88 Some(it) => *it, 82 Some(it) => *it,
@@ -90,25 +84,6 @@ impl MockDatabase {
90 } 84 }
91 } 85 }
92 86
93 pub fn set_crate_graph_from_fixture(&mut self, graph: CrateGraphFixture) {
94 let mut ids = FxHashMap::default();
95 let mut crate_graph = CrateGraph::default();
96 for (crate_name, (crate_root, edition, cfg_options, _)) in graph.0.iter() {
97 let crate_root = self.file_id_of(&crate_root);
98 let crate_id = crate_graph.add_crate_root(crate_root, *edition, cfg_options.clone());
99 Arc::make_mut(&mut self.crate_names).insert(crate_id, crate_name.clone());
100 ids.insert(crate_name, crate_id);
101 }
102 for (crate_name, (_, _, _, deps)) in graph.0.iter() {
103 let from = ids[crate_name];
104 for dep in deps {
105 let to = ids[dep];
106 crate_graph.add_dep(from, dep.as_str().into(), to).unwrap();
107 }
108 }
109 self.set_crate_graph(Arc::new(crate_graph))
110 }
111
112 pub fn diagnostics(&self) -> String { 87 pub fn diagnostics(&self) -> String {
113 let mut buf = String::new(); 88 let mut buf = String::new();
114 let mut files: Vec<FileId> = self.files.values().copied().collect(); 89 let mut files: Vec<FileId> = self.files.values().copied().collect();
@@ -285,46 +260,3 @@ impl MockDatabase {
285 .collect() 260 .collect()
286 } 261 }
287} 262}
288
289#[derive(Default)]
290pub struct CrateGraphFixture(pub Vec<(String, (String, Edition, CfgOptions, Vec<String>))>);
291
292#[macro_export]
293macro_rules! crate_graph {
294 ($(
295 $crate_name:literal: (
296 $crate_path:literal,
297 $($edition:literal,)?
298 [$($dep:literal),*]
299 $(, cfg = {
300 $($key:literal $(= $value:literal)?),*
301 $(,)?
302 })?
303 ),
304 )*) => {{
305 let mut res = $crate::mock::CrateGraphFixture::default();
306 $(
307 #[allow(unused_mut, unused_assignments)]
308 let mut edition = ra_db::Edition::Edition2018;
309 $(edition = ra_db::Edition::from_string($edition);)?
310 let cfg_options = {
311 #[allow(unused_mut)]
312 let mut cfg = ::ra_cfg::CfgOptions::default();
313 $(
314 $(
315 if 0 == 0 $(+ { drop($value); 1})? {
316 cfg.insert_atom($key.into());
317 }
318 $(cfg.insert_key_value($key.into(), $value.into());)?
319 )*
320 )?
321 cfg
322 };
323 res.0.push((
324 $crate_name.to_string(),
325 ($crate_path.to_string(), edition, cfg_options, vec![$($dep.to_string()),*])
326 ));
327 )*
328 res
329 }}
330}
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs
index f27155737..4b7e34878 100644
--- a/crates/ra_hir/src/ty/tests.rs
+++ b/crates/ra_hir/src/ty/tests.rs
@@ -2,8 +2,7 @@ use std::fmt::Write;
2use std::sync::Arc; 2use std::sync::Arc;
3 3
4use insta::assert_snapshot; 4use insta::assert_snapshot;
5 5use ra_db::{fixture::WithFixture, salsa::Database, FilePosition, SourceDatabase};
6use ra_db::{salsa::Database, FilePosition, SourceDatabase};
7use ra_syntax::{ 6use ra_syntax::{
8 algo, 7 algo,
9 ast::{self, AstNode}, 8 ast::{self, AstNode},
@@ -25,9 +24,9 @@ mod coercion;
25 24
26#[test] 25#[test]
27fn cfg_impl_block() { 26fn cfg_impl_block() {
28 let (mut db, pos) = MockDatabase::with_position( 27 let (db, pos) = MockDatabase::with_position(
29 r#" 28 r#"
30//- /main.rs 29//- /main.rs crate:main deps:foo cfg:test
31use foo::S as T; 30use foo::S as T;
32struct S; 31struct S;
33 32
@@ -46,7 +45,7 @@ fn test() {
46 t<|>; 45 t<|>;
47} 46}
48 47
49//- /foo.rs 48//- /foo.rs crate:foo
50struct S; 49struct S;
51 50
52#[cfg(not(test))] 51#[cfg(not(test))]
@@ -60,18 +59,14 @@ impl S {
60} 59}
61"#, 60"#,
62 ); 61 );
63 db.set_crate_graph_from_fixture(crate_graph! {
64 "main": ("/main.rs", ["foo"], cfg = { "test" }),
65 "foo": ("/foo.rs", []),
66 });
67 assert_eq!("(i32, {unknown}, i32, {unknown})", type_at_pos(&db, pos)); 62 assert_eq!("(i32, {unknown}, i32, {unknown})", type_at_pos(&db, pos));
68} 63}
69 64
70#[test] 65#[test]
71fn infer_await() { 66fn infer_await() {
72 let (mut db, pos) = MockDatabase::with_position( 67 let (db, pos) = MockDatabase::with_position(
73 r#" 68 r#"
74//- /main.rs 69//- /main.rs crate:main deps:std
75 70
76struct IntFuture; 71struct IntFuture;
77 72
@@ -85,7 +80,7 @@ fn test() {
85 v<|>; 80 v<|>;
86} 81}
87 82
88//- /std.rs 83//- /std.rs crate:std
89#[prelude_import] use future::*; 84#[prelude_import] use future::*;
90mod future { 85mod future {
91 trait Future { 86 trait Future {
@@ -95,18 +90,14 @@ mod future {
95 90
96"#, 91"#,
97 ); 92 );
98 db.set_crate_graph_from_fixture(crate_graph! {
99 "main": ("/main.rs", ["std"]),
100 "std": ("/std.rs", []),
101 });
102 assert_eq!("u64", type_at_pos(&db, pos)); 93 assert_eq!("u64", type_at_pos(&db, pos));
103} 94}
104 95
105#[test] 96#[test]
106fn infer_box() { 97fn infer_box() {
107 let (mut db, pos) = MockDatabase::with_position( 98 let (db, pos) = MockDatabase::with_position(
108 r#" 99 r#"
109//- /main.rs 100//- /main.rs crate:main deps:std
110 101
111fn test() { 102fn test() {
112 let x = box 1; 103 let x = box 1;
@@ -114,7 +105,7 @@ fn test() {
114 t<|>; 105 t<|>;
115} 106}
116 107
117//- /std.rs 108//- /std.rs crate:std
118#[prelude_import] use prelude::*; 109#[prelude_import] use prelude::*;
119mod prelude {} 110mod prelude {}
120 111
@@ -126,10 +117,6 @@ mod boxed {
126 117
127"#, 118"#,
128 ); 119 );
129 db.set_crate_graph_from_fixture(crate_graph! {
130 "main": ("/main.rs", ["std"]),
131 "std": ("/std.rs", []),
132 });
133 assert_eq!("(Box<i32>, Box<Box<i32>>, Box<&i32>, Box<[i32;_]>)", type_at_pos(&db, pos)); 120 assert_eq!("(Box<i32>, Box<Box<i32>>, Box<&i32>, Box<[i32;_]>)", type_at_pos(&db, pos));
134} 121}
135 122
@@ -154,9 +141,9 @@ fn test() {
154 141
155#[test] 142#[test]
156fn infer_try() { 143fn infer_try() {
157 let (mut db, pos) = MockDatabase::with_position( 144 let (db, pos) = MockDatabase::with_position(
158 r#" 145 r#"
159//- /main.rs 146//- /main.rs crate:main deps:std
160 147
161fn test() { 148fn test() {
162 let r: Result<i32, u64> = Result::Ok(1); 149 let r: Result<i32, u64> = Result::Ok(1);
@@ -164,7 +151,7 @@ fn test() {
164 v<|>; 151 v<|>;
165} 152}
166 153
167//- /std.rs 154//- /std.rs crate:std
168 155
169#[prelude_import] use ops::*; 156#[prelude_import] use ops::*;
170mod ops { 157mod ops {
@@ -189,18 +176,14 @@ mod result {
189 176
190"#, 177"#,
191 ); 178 );
192 db.set_crate_graph_from_fixture(crate_graph! {
193 "main": ("/main.rs", ["std"]),
194 "std": ("/std.rs", []),
195 });
196 assert_eq!("i32", type_at_pos(&db, pos)); 179 assert_eq!("i32", type_at_pos(&db, pos));
197} 180}
198 181
199#[test] 182#[test]
200fn infer_for_loop() { 183fn infer_for_loop() {
201 let (mut db, pos) = MockDatabase::with_position( 184 let (db, pos) = MockDatabase::with_position(
202 r#" 185 r#"
203//- /main.rs 186//- /main.rs crate:main deps:std
204 187
205use std::collections::Vec; 188use std::collections::Vec;
206 189
@@ -212,7 +195,7 @@ fn test() {
212 } 195 }
213} 196}
214 197
215//- /std.rs 198//- /std.rs crate:std
216 199
217#[prelude_import] use iter::*; 200#[prelude_import] use iter::*;
218mod iter { 201mod iter {
@@ -234,10 +217,6 @@ mod collections {
234} 217}
235"#, 218"#,
236 ); 219 );
237 db.set_crate_graph_from_fixture(crate_graph! {
238 "main": ("/main.rs", ["std"]),
239 "std": ("/std.rs", []),
240 });
241 assert_eq!("&str", type_at_pos(&db, pos)); 220 assert_eq!("&str", type_at_pos(&db, pos));
242} 221}
243 222
@@ -2505,15 +2484,15 @@ pub fn main_loop() {
2505 2484
2506#[test] 2485#[test]
2507fn cross_crate_associated_method_call() { 2486fn cross_crate_associated_method_call() {
2508 let (mut db, pos) = MockDatabase::with_position( 2487 let (db, pos) = MockDatabase::with_position(
2509 r#" 2488 r#"
2510//- /main.rs 2489//- /main.rs crate:main deps:other_crate
2511fn test() { 2490fn test() {
2512 let x = other_crate::foo::S::thing(); 2491 let x = other_crate::foo::S::thing();
2513 x<|>; 2492 x<|>;
2514} 2493}
2515 2494
2516//- /lib.rs 2495//- /lib.rs crate:other_crate
2517mod foo { 2496mod foo {
2518 struct S; 2497 struct S;
2519 impl S { 2498 impl S {
@@ -2522,10 +2501,6 @@ mod foo {
2522} 2501}
2523"#, 2502"#,
2524 ); 2503 );
2525 db.set_crate_graph_from_fixture(crate_graph! {
2526 "main": ("/main.rs", ["other_crate"]),
2527 "other_crate": ("/lib.rs", []),
2528 });
2529 assert_eq!("i128", type_at_pos(&db, pos)); 2504 assert_eq!("i128", type_at_pos(&db, pos));
2530} 2505}
2531 2506
@@ -3403,16 +3378,15 @@ fn test() { S.foo()<|>; }
3403 3378
3404#[test] 3379#[test]
3405fn infer_macro_with_dollar_crate_is_correct_in_expr() { 3380fn infer_macro_with_dollar_crate_is_correct_in_expr() {
3406 // covers!(macro_dollar_crate_other); 3381 let (db, pos) = MockDatabase::with_position(
3407 let (mut db, pos) = MockDatabase::with_position(
3408 r#" 3382 r#"
3409//- /main.rs 3383//- /main.rs crate:main deps:foo
3410fn test() { 3384fn test() {
3411 let x = (foo::foo!(1), foo::foo!(2)); 3385 let x = (foo::foo!(1), foo::foo!(2));
3412 x<|>; 3386 x<|>;
3413} 3387}
3414 3388
3415//- /lib.rs 3389//- /lib.rs crate:foo
3416#[macro_export] 3390#[macro_export]
3417macro_rules! foo { 3391macro_rules! foo {
3418 (1) => { $crate::bar!() }; 3392 (1) => { $crate::bar!() };
@@ -3427,10 +3401,6 @@ macro_rules! bar {
3427pub fn baz() -> usize { 31usize } 3401pub fn baz() -> usize { 31usize }
3428"#, 3402"#,
3429 ); 3403 );
3430 db.set_crate_graph_from_fixture(crate_graph! {
3431 "main": ("/main.rs", ["foo"]),
3432 "foo": ("/lib.rs", []),
3433 });
3434 assert_eq!("(i32, usize)", type_at_pos(&db, pos)); 3404 assert_eq!("(i32, usize)", type_at_pos(&db, pos));
3435} 3405}
3436 3406
@@ -3512,9 +3482,9 @@ fn test() { (&S).foo()<|>; }
3512 3482
3513#[test] 3483#[test]
3514fn method_resolution_trait_from_prelude() { 3484fn method_resolution_trait_from_prelude() {
3515 let (mut db, pos) = MockDatabase::with_position( 3485 let (db, pos) = MockDatabase::with_position(
3516 r#" 3486 r#"
3517//- /main.rs 3487//- /main.rs crate:main deps:other_crate
3518struct S; 3488struct S;
3519impl Clone for S {} 3489impl Clone for S {}
3520 3490
@@ -3522,7 +3492,7 @@ fn test() {
3522 S.clone()<|>; 3492 S.clone()<|>;
3523} 3493}
3524 3494
3525//- /lib.rs 3495//- /lib.rs crate:other_crate
3526#[prelude_import] use foo::*; 3496#[prelude_import] use foo::*;
3527 3497
3528mod foo { 3498mod foo {
@@ -3532,10 +3502,6 @@ mod foo {
3532} 3502}
3533"#, 3503"#,
3534 ); 3504 );
3535 db.set_crate_graph_from_fixture(crate_graph! {
3536 "main": ("/main.rs", ["other_crate"]),
3537 "other_crate": ("/lib.rs", []),
3538 });
3539 assert_eq!("S", type_at_pos(&db, pos)); 3505 assert_eq!("S", type_at_pos(&db, pos));
3540} 3506}
3541 3507