aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_expand
diff options
context:
space:
mode:
authorEdwin Cheng <[email protected]>2020-04-27 20:32:47 +0100
committerEdwin Cheng <[email protected]>2020-04-27 20:32:47 +0100
commit6d3b0af900e6cefb507939b311672b7795703461 (patch)
treeb3845795f3819119e816ec241d27d0fe5f6b2b02 /crates/ra_hir_expand
parent213d208e2d61576e104a9d5e003efc4ad1abc469 (diff)
Use empty-deps tricks to detect it is core
Diffstat (limited to 'crates/ra_hir_expand')
-rw-r--r--crates/ra_hir_expand/src/builtin_derive.rs26
1 files changed, 21 insertions, 5 deletions
diff --git a/crates/ra_hir_expand/src/builtin_derive.rs b/crates/ra_hir_expand/src/builtin_derive.rs
index cc8c45347..3bd3f7041 100644
--- a/crates/ra_hir_expand/src/builtin_derive.rs
+++ b/crates/ra_hir_expand/src/builtin_derive.rs
@@ -160,11 +160,17 @@ fn find_builtin_crate(db: &dyn AstDatabase, id: LazyMacroId) -> tt::TokenTree {
160 let m: MacroCallId = id.into(); 160 let m: MacroCallId = id.into();
161 let file_id = m.as_file().original_file(db); 161 let file_id = m.as_file().original_file(db);
162 let cg = db.crate_graph(); 162 let cg = db.crate_graph();
163 let crates = db.relevant_crates(file_id); 163 let krates = db.relevant_crates(file_id);
164 let mut crate_names = 164 let krate = match krates.get(0) {
165 crates.iter().filter_map(|krate| cg[*krate].display_name.clone()).map(|it| it.to_string()); 165 Some(krate) => krate,
166 None => {
167 let tt = quote! { core };
168 return tt.token_trees[0].clone();
169 }
170 };
166 171
167 let tt = if crate_names.any(|name| name == "std" || name == "core") { 172 // Check whether it has any deps, if not, it should be core:
173 let tt = if cg[*krate].dependencies.is_empty() {
168 quote! { crate } 174 quote! { crate }
169 } else { 175 } else {
170 quote! { core } 176 quote! { core }
@@ -263,8 +269,18 @@ mod tests {
263 269
264 fn expand_builtin_derive(s: &str, name: Name) -> String { 270 fn expand_builtin_derive(s: &str, name: Name) -> String {
265 let def = find_builtin_derive(&name).unwrap(); 271 let def = find_builtin_derive(&name).unwrap();
272 let fixture = format!(
273 r#"//- /main.rs crate:main deps:core
274<|>
275{}
276//- /lib.rs crate:core
277// empty
278"#,
279 s
280 );
266 281
267 let (db, file_id) = TestDB::with_single_file(&s); 282 let (db, file_pos) = TestDB::with_position(&fixture);
283 let file_id = file_pos.file_id;
268 let parsed = db.parse(file_id); 284 let parsed = db.parse(file_id);
269 let items: Vec<_> = 285 let items: Vec<_> =
270 parsed.syntax_node().descendants().filter_map(ast::ModuleItem::cast).collect(); 286 parsed.syntax_node().descendants().filter_map(ast::ModuleItem::cast).collect();