mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 05:38:46 +00:00
Use core instead of std for builtin derive macros
This commit is contained in:
parent
ef67e0a497
commit
6a5014329a
2 changed files with 65 additions and 36 deletions
|
@ -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<tt::Subtree, mbe::ExpandError> {
|
||||
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<tt::Subtree, mbe::ExpandError> {
|
||||
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<tt::Subtree, mbe::ExpandError> {
|
||||
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<tt::Subtree, mbe::ExpandError> {
|
||||
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<tt::Subtree, mbe::ExpandError> {
|
||||
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<tt::Subtree, mbe::ExpandError> {
|
||||
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<tt::Subtree, mbe::ExpandError> {
|
||||
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<tt::Subtree, mbe::ExpandError> {
|
||||
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<tt::Subtree, mbe::ExpandError> {
|
||||
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,
|
||||
"impl<T0:std::marker::Copy,T1:std::marker::Copy>std::marker::CopyforFoo<T0,T1>{}"
|
||||
"impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}"
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -297,7 +326,7 @@ mod tests {
|
|||
|
||||
assert_eq!(
|
||||
expanded,
|
||||
"impl<T0:std::marker::Copy,T1:std::marker::Copy>std::marker::CopyforFoo<T0,T1>{}"
|
||||
"impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}"
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -313,7 +342,7 @@ mod tests {
|
|||
|
||||
assert_eq!(
|
||||
expanded,
|
||||
"impl<T0:std::clone::Clone,T1:std::clone::Clone>std::clone::CloneforFoo<T0,T1>{}"
|
||||
"impl<T0:core::clone::Clone,T1:core::clone::Clone>core::clone::CloneforFoo<T0,T1>{}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue