From 6a5014329aecf73da81943216729ab64fa255368 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Tue, 28 Apr 2020 01:48:55 +0800 Subject: Use core instead of std for builtin derive macros --- crates/ra_hir_expand/src/builtin_derive.rs | 93 ++++++++++++++++++++---------- crates/ra_hir_ty/src/tests/macros.rs | 8 +-- 2 files changed, 65 insertions(+), 36 deletions(-) diff --git a/crates/ra_hir_expand/src/builtin_derive.rs b/crates/ra_hir_expand/src/builtin_derive.rs index bb45b0f1d..dae623dd0 100644 --- a/crates/ra_hir_expand/src/builtin_derive.rs +++ b/crates/ra_hir_expand/src/builtin_derive.rs @@ -9,7 +9,7 @@ use ra_syntax::{ }; use crate::db::AstDatabase; -use crate::{name, quote, LazyMacroId, MacroDefId, MacroDefKind}; +use crate::{name, quote, LazyMacroId, MacroCallId, MacroDefId, MacroDefKind}; macro_rules! register_builtin { ( $($trait:ident => $expand:ident),* ) => { @@ -153,76 +153,105 @@ fn expand_simple_derive( Ok(expanded) } +fn find_builtin_crate(db: &dyn AstDatabase, id: LazyMacroId) -> tt::TokenTree { + // FIXME: make hygiene works for builtin derive macro + // such that $crate can be used here. + + let m: MacroCallId = id.into(); + let file_id = m.as_file().original_file(db); + let cg = db.crate_graph(); + let crates = db.relevant_crates(file_id); + let mut crate_names = + crates.iter().filter_map(|krate| cg[*krate].display_name.clone()).map(|it| it.to_string()); + + let tt = if crate_names.any(|name| name == "std" || name == "core") { + quote! { krate } + } else { + quote! { core } + }; + + tt.token_trees[0].clone() +} + fn copy_expand( - _db: &dyn AstDatabase, - _id: LazyMacroId, + db: &dyn AstDatabase, + id: LazyMacroId, tt: &tt::Subtree, ) -> Result { - expand_simple_derive(tt, quote! { std::marker::Copy }) + let krate = find_builtin_crate(db, id); + expand_simple_derive(tt, quote! { #krate::marker::Copy }) } fn clone_expand( - _db: &dyn AstDatabase, - _id: LazyMacroId, + db: &dyn AstDatabase, + id: LazyMacroId, tt: &tt::Subtree, ) -> Result { - expand_simple_derive(tt, quote! { std::clone::Clone }) + let krate = find_builtin_crate(db, id); + expand_simple_derive(tt, quote! { #krate::clone::Clone }) } fn default_expand( - _db: &dyn AstDatabase, - _id: LazyMacroId, + db: &dyn AstDatabase, + id: LazyMacroId, tt: &tt::Subtree, ) -> Result { - expand_simple_derive(tt, quote! { std::default::Default }) + let krate = find_builtin_crate(db, id); + expand_simple_derive(tt, quote! { #krate::default::Default }) } fn debug_expand( - _db: &dyn AstDatabase, - _id: LazyMacroId, + db: &dyn AstDatabase, + id: LazyMacroId, tt: &tt::Subtree, ) -> Result { - expand_simple_derive(tt, quote! { std::fmt::Debug }) + let krate = find_builtin_crate(db, id); + expand_simple_derive(tt, quote! { #krate::fmt::Debug }) } fn hash_expand( - _db: &dyn AstDatabase, - _id: LazyMacroId, + db: &dyn AstDatabase, + id: LazyMacroId, tt: &tt::Subtree, ) -> Result { - expand_simple_derive(tt, quote! { std::hash::Hash }) + let krate = find_builtin_crate(db, id); + expand_simple_derive(tt, quote! { #krate::hash::Hash }) } fn eq_expand( - _db: &dyn AstDatabase, - _id: LazyMacroId, + db: &dyn AstDatabase, + id: LazyMacroId, tt: &tt::Subtree, ) -> Result { - expand_simple_derive(tt, quote! { std::cmp::Eq }) + let krate = find_builtin_crate(db, id); + expand_simple_derive(tt, quote! { #krate::cmp::Eq }) } fn partial_eq_expand( - _db: &dyn AstDatabase, - _id: LazyMacroId, + db: &dyn AstDatabase, + id: LazyMacroId, tt: &tt::Subtree, ) -> Result { - expand_simple_derive(tt, quote! { std::cmp::PartialEq }) + let krate = find_builtin_crate(db, id); + expand_simple_derive(tt, quote! { #krate::cmp::PartialEq }) } fn ord_expand( - _db: &dyn AstDatabase, - _id: LazyMacroId, + db: &dyn AstDatabase, + id: LazyMacroId, tt: &tt::Subtree, ) -> Result { - expand_simple_derive(tt, quote! { std::cmp::Ord }) + let krate = find_builtin_crate(db, id); + expand_simple_derive(tt, quote! { #krate::cmp::Ord }) } fn partial_ord_expand( - _db: &dyn AstDatabase, - _id: LazyMacroId, + db: &dyn AstDatabase, + id: LazyMacroId, tt: &tt::Subtree, ) -> Result { - expand_simple_derive(tt, quote! { std::cmp::PartialOrd }) + let krate = find_builtin_crate(db, id); + expand_simple_derive(tt, quote! { #krate::cmp::PartialOrd }) } #[cfg(test)] @@ -264,7 +293,7 @@ mod tests { known::Copy, ); - assert_eq!(expanded, "impl< >std::marker::CopyforFoo< >{}"); + assert_eq!(expanded, "impl< >core::marker::CopyforFoo< >{}"); } #[test] @@ -279,7 +308,7 @@ mod tests { assert_eq!( expanded, - "implstd::marker::CopyforFoo{}" + "implcore::marker::CopyforFoo{}" ); } @@ -297,7 +326,7 @@ mod tests { assert_eq!( expanded, - "implstd::marker::CopyforFoo{}" + "implcore::marker::CopyforFoo{}" ); } @@ -313,7 +342,7 @@ mod tests { assert_eq!( expanded, - "implstd::clone::CloneforFoo{}" + "implcore::clone::CloneforFoo{}" ); } } diff --git a/crates/ra_hir_ty/src/tests/macros.rs b/crates/ra_hir_ty/src/tests/macros.rs index 6b5267232..4f82ff702 100644 --- a/crates/ra_hir_ty/src/tests/macros.rs +++ b/crates/ra_hir_ty/src/tests/macros.rs @@ -622,14 +622,14 @@ fn main() { fn infer_derive_clone_simple() { let (db, pos) = TestDB::with_position( r#" -//- /main.rs crate:main deps:std +//- /main.rs crate:main deps:core #[derive(Clone)] struct S; fn test() { S.clone()<|>; } -//- /lib.rs crate:std +//- /lib.rs crate:core #[prelude_import] use clone::*; mod clone { @@ -646,7 +646,7 @@ mod clone { fn infer_derive_clone_with_params() { let (db, pos) = TestDB::with_position( r#" -//- /main.rs crate:main deps:std +//- /main.rs crate:main deps:core #[derive(Clone)] struct S; #[derive(Clone)] @@ -656,7 +656,7 @@ fn test() { (Wrapper(S).clone(), Wrapper(NonClone).clone())<|>; } -//- /lib.rs crate:std +//- /lib.rs crate:core #[prelude_import] use clone::*; mod clone { -- cgit v1.2.3