aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_expand/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_expand/src')
-rw-r--r--crates/ra_hir_expand/src/builtin_derive.rs94
-rw-r--r--crates/ra_hir_expand/src/name.rs7
2 files changed, 86 insertions, 15 deletions
diff --git a/crates/ra_hir_expand/src/builtin_derive.rs b/crates/ra_hir_expand/src/builtin_derive.rs
index fde50f7e6..78fa9b09a 100644
--- a/crates/ra_hir_expand/src/builtin_derive.rs
+++ b/crates/ra_hir_expand/src/builtin_derive.rs
@@ -45,7 +45,14 @@ macro_rules! register_builtin {
45 45
46register_builtin! { 46register_builtin! {
47 (COPY_TRAIT, Copy) => copy_expand, 47 (COPY_TRAIT, Copy) => copy_expand,
48 (CLONE_TRAIT, Clone) => clone_expand 48 (CLONE_TRAIT, Clone) => clone_expand,
49 (DEFAULT_TRAIT, Default) => default_expand,
50 (DEBUG_TRAIT, Debug) => debug_expand,
51 (HASH_TRAIT, Hash) => hash_expand,
52 (ORD_TRAIT, Ord) => ord_expand,
53 (PARTIAL_ORD_TRAIT, PartialOrd) => partial_ord_expand,
54 (EQ_TRAIT, Eq) => eq_expand,
55 (PARTIAL_EQ_TRAIT, PartialEq) => partial_eq_expand
49} 56}
50 57
51struct BasicAdtInfo { 58struct BasicAdtInfo {
@@ -109,36 +116,93 @@ fn make_type_args(n: usize, bound: Vec<tt::TokenTree>) -> Vec<tt::TokenTree> {
109 result 116 result
110} 117}
111 118
112fn copy_expand( 119fn expand_simple_derive(
113 _db: &dyn AstDatabase,
114 _id: MacroCallId,
115 tt: &tt::Subtree, 120 tt: &tt::Subtree,
121 trait_path: tt::Subtree,
116) -> Result<tt::Subtree, mbe::ExpandError> { 122) -> Result<tt::Subtree, mbe::ExpandError> {
117 let info = parse_adt(tt)?; 123 let info = parse_adt(tt)?;
118 let name = info.name; 124 let name = info.name;
119 let bound = (quote! { : std::marker::Copy }).token_trees; 125 let trait_path_clone = trait_path.token_trees.clone();
126 let bound = (quote! { : ##trait_path_clone }).token_trees;
120 let type_params = make_type_args(info.type_params, bound); 127 let type_params = make_type_args(info.type_params, bound);
121 let type_args = make_type_args(info.type_params, Vec::new()); 128 let type_args = make_type_args(info.type_params, Vec::new());
129 let trait_path = trait_path.token_trees;
122 let expanded = quote! { 130 let expanded = quote! {
123 impl ##type_params std::marker::Copy for #name ##type_args {} 131 impl ##type_params ##trait_path for #name ##type_args {}
124 }; 132 };
125 Ok(expanded) 133 Ok(expanded)
126} 134}
127 135
136fn copy_expand(
137 _db: &dyn AstDatabase,
138 _id: MacroCallId,
139 tt: &tt::Subtree,
140) -> Result<tt::Subtree, mbe::ExpandError> {
141 expand_simple_derive(tt, quote! { std::marker::Copy })
142}
143
128fn clone_expand( 144fn clone_expand(
129 _db: &dyn AstDatabase, 145 _db: &dyn AstDatabase,
130 _id: MacroCallId, 146 _id: MacroCallId,
131 tt: &tt::Subtree, 147 tt: &tt::Subtree,
132) -> Result<tt::Subtree, mbe::ExpandError> { 148) -> Result<tt::Subtree, mbe::ExpandError> {
133 let info = parse_adt(tt)?; 149 expand_simple_derive(tt, quote! { std::clone::Clone })
134 let name = info.name; 150}
135 let bound = (quote! { : std::clone::Clone }).token_trees; 151
136 let type_params = make_type_args(info.type_params, bound); 152fn default_expand(
137 let type_args = make_type_args(info.type_params, Vec::new()); 153 _db: &dyn AstDatabase,
138 let expanded = quote! { 154 _id: MacroCallId,
139 impl ##type_params std::clone::Clone for #name ##type_args {} 155 tt: &tt::Subtree,
140 }; 156) -> Result<tt::Subtree, mbe::ExpandError> {
141 Ok(expanded) 157 expand_simple_derive(tt, quote! { std::default::Default })
158}
159
160fn debug_expand(
161 _db: &dyn AstDatabase,
162 _id: MacroCallId,
163 tt: &tt::Subtree,
164) -> Result<tt::Subtree, mbe::ExpandError> {
165 expand_simple_derive(tt, quote! { std::fmt::Debug })
166}
167
168fn hash_expand(
169 _db: &dyn AstDatabase,
170 _id: MacroCallId,
171 tt: &tt::Subtree,
172) -> Result<tt::Subtree, mbe::ExpandError> {
173 expand_simple_derive(tt, quote! { std::hash::Hash })
174}
175
176fn eq_expand(
177 _db: &dyn AstDatabase,
178 _id: MacroCallId,
179 tt: &tt::Subtree,
180) -> Result<tt::Subtree, mbe::ExpandError> {
181 expand_simple_derive(tt, quote! { std::cmp::Eq })
182}
183
184fn partial_eq_expand(
185 _db: &dyn AstDatabase,
186 _id: MacroCallId,
187 tt: &tt::Subtree,
188) -> Result<tt::Subtree, mbe::ExpandError> {
189 expand_simple_derive(tt, quote! { std::cmp::PartialEq })
190}
191
192fn ord_expand(
193 _db: &dyn AstDatabase,
194 _id: MacroCallId,
195 tt: &tt::Subtree,
196) -> Result<tt::Subtree, mbe::ExpandError> {
197 expand_simple_derive(tt, quote! { std::cmp::Ord })
198}
199
200fn partial_ord_expand(
201 _db: &dyn AstDatabase,
202 _id: MacroCallId,
203 tt: &tt::Subtree,
204) -> Result<tt::Subtree, mbe::ExpandError> {
205 expand_simple_derive(tt, quote! { std::cmp::PartialOrd })
142} 206}
143 207
144#[cfg(test)] 208#[cfg(test)]
diff --git a/crates/ra_hir_expand/src/name.rs b/crates/ra_hir_expand/src/name.rs
index 86709b5cf..c5a191160 100644
--- a/crates/ra_hir_expand/src/name.rs
+++ b/crates/ra_hir_expand/src/name.rs
@@ -163,3 +163,10 @@ pub const STRINGIFY_MACRO: Name = Name::new_inline_ascii(9, b"stringify");
163// Builtin derives 163// Builtin derives
164pub const COPY_TRAIT: Name = Name::new_inline_ascii(4, b"Copy"); 164pub const COPY_TRAIT: Name = Name::new_inline_ascii(4, b"Copy");
165pub const CLONE_TRAIT: Name = Name::new_inline_ascii(5, b"Clone"); 165pub const CLONE_TRAIT: Name = Name::new_inline_ascii(5, b"Clone");
166pub const DEFAULT_TRAIT: Name = Name::new_inline_ascii(7, b"Default");
167pub const DEBUG_TRAIT: Name = Name::new_inline_ascii(5, b"Debug");
168pub const HASH_TRAIT: Name = Name::new_inline_ascii(4, b"Hash");
169pub const ORD_TRAIT: Name = Name::new_inline_ascii(3, b"Ord");
170pub const PARTIAL_ORD_TRAIT: Name = Name::new_inline_ascii(10, b"PartialOrd");
171pub const EQ_TRAIT: Name = Name::new_inline_ascii(2, b"Eq");
172pub const PARTIAL_EQ_TRAIT: Name = Name::new_inline_ascii(9, b"PartialEq");