]> git.lizzy.rs Git - rust.git/commitdiff
Implement all the other built-in derives
authorFlorian Diebold <flodiebold@gmail.com>
Thu, 5 Dec 2019 18:52:52 +0000 (19:52 +0100)
committerFlorian Diebold <flodiebold@gmail.com>
Thu, 5 Dec 2019 18:52:52 +0000 (19:52 +0100)
Since as long as we're not implementing the bodies, they all work the same way.

crates/ra_hir_expand/src/builtin_derive.rs
crates/ra_hir_expand/src/name.rs

index fde50f7e6d37a43a652c3c80138da0d1e5046c1e..78fa9b09a2744e1f4efc84930e8be3f415d9b03f 100644 (file)
@@ -45,7 +45,14 @@ pub fn find_builtin_derive(ident: &name::Name) -> Option<MacroDefId> {
 
 register_builtin! {
     (COPY_TRAIT, Copy) => copy_expand,
-    (CLONE_TRAIT, Clone) => clone_expand
+    (CLONE_TRAIT, Clone) => clone_expand,
+    (DEFAULT_TRAIT, Default) => default_expand,
+    (DEBUG_TRAIT, Debug) => debug_expand,
+    (HASH_TRAIT, Hash) => hash_expand,
+    (ORD_TRAIT, Ord) => ord_expand,
+    (PARTIAL_ORD_TRAIT, PartialOrd) => partial_ord_expand,
+    (EQ_TRAIT, Eq) => eq_expand,
+    (PARTIAL_EQ_TRAIT, PartialEq) => partial_eq_expand
 }
 
 struct BasicAdtInfo {
@@ -109,36 +116,93 @@ fn make_type_args(n: usize, bound: Vec<tt::TokenTree>) -> Vec<tt::TokenTree> {
     result
 }
 
-fn copy_expand(
-    _db: &dyn AstDatabase,
-    _id: MacroCallId,
+fn expand_simple_derive(
     tt: &tt::Subtree,
+    trait_path: tt::Subtree,
 ) -> Result<tt::Subtree, mbe::ExpandError> {
     let info = parse_adt(tt)?;
     let name = info.name;
-    let bound = (quote! { : std::marker::Copy }).token_trees;
+    let trait_path_clone = trait_path.token_trees.clone();
+    let bound = (quote! { : ##trait_path_clone }).token_trees;
     let type_params = make_type_args(info.type_params, bound);
     let type_args = make_type_args(info.type_params, Vec::new());
+    let trait_path = trait_path.token_trees;
     let expanded = quote! {
-        impl ##type_params std::marker::Copy for #name ##type_args {}
+        impl ##type_params ##trait_path for #name ##type_args {}
     };
     Ok(expanded)
 }
 
+fn copy_expand(
+    _db: &dyn AstDatabase,
+    _id: MacroCallId,
+    tt: &tt::Subtree,
+) -> Result<tt::Subtree, mbe::ExpandError> {
+    expand_simple_derive(tt, quote! { std::marker::Copy })
+}
+
 fn clone_expand(
     _db: &dyn AstDatabase,
     _id: MacroCallId,
     tt: &tt::Subtree,
 ) -> Result<tt::Subtree, mbe::ExpandError> {
-    let info = parse_adt(tt)?;
-    let name = info.name;
-    let bound = (quote! { : std::clone::Clone }).token_trees;
-    let type_params = make_type_args(info.type_params, bound);
-    let type_args = make_type_args(info.type_params, Vec::new());
-    let expanded = quote! {
-        impl ##type_params std::clone::Clone for #name ##type_args {}
-    };
-    Ok(expanded)
+    expand_simple_derive(tt, quote! { std::clone::Clone })
+}
+
+fn default_expand(
+    _db: &dyn AstDatabase,
+    _id: MacroCallId,
+    tt: &tt::Subtree,
+) -> Result<tt::Subtree, mbe::ExpandError> {
+    expand_simple_derive(tt, quote! { std::default::Default })
+}
+
+fn debug_expand(
+    _db: &dyn AstDatabase,
+    _id: MacroCallId,
+    tt: &tt::Subtree,
+) -> Result<tt::Subtree, mbe::ExpandError> {
+    expand_simple_derive(tt, quote! { std::fmt::Debug })
+}
+
+fn hash_expand(
+    _db: &dyn AstDatabase,
+    _id: MacroCallId,
+    tt: &tt::Subtree,
+) -> Result<tt::Subtree, mbe::ExpandError> {
+    expand_simple_derive(tt, quote! { std::hash::Hash })
+}
+
+fn eq_expand(
+    _db: &dyn AstDatabase,
+    _id: MacroCallId,
+    tt: &tt::Subtree,
+) -> Result<tt::Subtree, mbe::ExpandError> {
+    expand_simple_derive(tt, quote! { std::cmp::Eq })
+}
+
+fn partial_eq_expand(
+    _db: &dyn AstDatabase,
+    _id: MacroCallId,
+    tt: &tt::Subtree,
+) -> Result<tt::Subtree, mbe::ExpandError> {
+    expand_simple_derive(tt, quote! { std::cmp::PartialEq })
+}
+
+fn ord_expand(
+    _db: &dyn AstDatabase,
+    _id: MacroCallId,
+    tt: &tt::Subtree,
+) -> Result<tt::Subtree, mbe::ExpandError> {
+    expand_simple_derive(tt, quote! { std::cmp::Ord })
+}
+
+fn partial_ord_expand(
+    _db: &dyn AstDatabase,
+    _id: MacroCallId,
+    tt: &tt::Subtree,
+) -> Result<tt::Subtree, mbe::ExpandError> {
+    expand_simple_derive(tt, quote! { std::cmp::PartialOrd })
 }
 
 #[cfg(test)]
index 86709b5cf2628ef1c7efb624b1b23c099ec367de..c5a19116093ba7d9b520b756a8230c7f3807a5b0 100644 (file)
@@ -163,3 +163,10 @@ fn as_name(&self) -> Name {
 // Builtin derives
 pub const COPY_TRAIT: Name = Name::new_inline_ascii(4, b"Copy");
 pub const CLONE_TRAIT: Name = Name::new_inline_ascii(5, b"Clone");
+pub const DEFAULT_TRAIT: Name = Name::new_inline_ascii(7, b"Default");
+pub const DEBUG_TRAIT: Name = Name::new_inline_ascii(5, b"Debug");
+pub const HASH_TRAIT: Name = Name::new_inline_ascii(4, b"Hash");
+pub const ORD_TRAIT: Name = Name::new_inline_ascii(3, b"Ord");
+pub const PARTIAL_ORD_TRAIT: Name = Name::new_inline_ascii(10, b"PartialOrd");
+pub const EQ_TRAIT: Name = Name::new_inline_ascii(2, b"Eq");
+pub const PARTIAL_EQ_TRAIT: Name = Name::new_inline_ascii(9, b"PartialEq");