Use empty-deps tricks to detect it is core

This commit is contained in:
Edwin Cheng 2020-04-28 03:32:47 +08:00
parent 213d208e2d
commit 6d3b0af900
2 changed files with 26 additions and 11 deletions

View file

@ -160,11 +160,17 @@ fn find_builtin_crate(db: &dyn AstDatabase, id: LazyMacroId) -> tt::TokenTree {
let m: MacroCallId = id.into(); let m: MacroCallId = id.into();
let file_id = m.as_file().original_file(db); let file_id = m.as_file().original_file(db);
let cg = db.crate_graph(); let cg = db.crate_graph();
let crates = db.relevant_crates(file_id); let krates = db.relevant_crates(file_id);
let mut crate_names = let krate = match krates.get(0) {
crates.iter().filter_map(|krate| cg[*krate].display_name.clone()).map(|it| it.to_string()); Some(krate) => krate,
None => {
let tt = quote! { core };
return tt.token_trees[0].clone();
}
};
let tt = if crate_names.any(|name| name == "std" || name == "core") { // Check whether it has any deps, if not, it should be core:
let tt = if cg[*krate].dependencies.is_empty() {
quote! { crate } quote! { crate }
} else { } else {
quote! { core } quote! { core }
@ -263,8 +269,18 @@ mod tests {
fn expand_builtin_derive(s: &str, name: Name) -> String { fn expand_builtin_derive(s: &str, name: Name) -> String {
let def = find_builtin_derive(&name).unwrap(); let def = find_builtin_derive(&name).unwrap();
let fixture = format!(
r#"//- /main.rs crate:main deps:core
<|>
{}
//- /lib.rs crate:core
// empty
"#,
s
);
let (db, file_id) = TestDB::with_single_file(&s); let (db, file_pos) = TestDB::with_position(&fixture);
let file_id = file_pos.file_id;
let parsed = db.parse(file_id); let parsed = db.parse(file_id);
let items: Vec<_> = let items: Vec<_> =
parsed.syntax_node().descendants().filter_map(ast::ModuleItem::cast).collect(); parsed.syntax_node().descendants().filter_map(ast::ModuleItem::cast).collect();

View file

@ -646,12 +646,6 @@ mod clone {
fn infer_derive_clone_in_core() { fn infer_derive_clone_in_core() {
let (db, pos) = TestDB::with_position( let (db, pos) = TestDB::with_position(
r#" r#"
//- /main.rs crate:main deps:core
use core::S;
fn test() {
S.clone()<|>;
}
//- /lib.rs crate:core //- /lib.rs crate:core
#[prelude_import] #[prelude_import]
use clone::*; use clone::*;
@ -663,6 +657,11 @@ mod clone {
#[derive(Clone)] #[derive(Clone)]
pub struct S; pub struct S;
//- /main.rs crate:main deps:core
use core::S;
fn test() {
S.clone()<|>;
}
"#, "#,
); );
assert_eq!("S", type_at_pos(&db, pos)); assert_eq!("S", type_at_pos(&db, pos));