diff options
Diffstat (limited to 'crates/ra_hir_expand/src')
-rw-r--r-- | crates/ra_hir_expand/src/builtin_derive.rs | 94 | ||||
-rw-r--r-- | crates/ra_hir_expand/src/name.rs | 7 |
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 | ||
46 | register_builtin! { | 46 | register_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 | ||
51 | struct BasicAdtInfo { | 58 | struct 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 | ||
112 | fn copy_expand( | 119 | fn 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 | ||
136 | fn 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 | |||
128 | fn clone_expand( | 144 | fn 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); | 152 | fn 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 | |||
160 | fn 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 | |||
168 | fn 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 | |||
176 | fn 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 | |||
184 | fn 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 | |||
192 | fn 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 | |||
200 | fn 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 |
164 | pub const COPY_TRAIT: Name = Name::new_inline_ascii(4, b"Copy"); | 164 | pub const COPY_TRAIT: Name = Name::new_inline_ascii(4, b"Copy"); |
165 | pub const CLONE_TRAIT: Name = Name::new_inline_ascii(5, b"Clone"); | 165 | pub const CLONE_TRAIT: Name = Name::new_inline_ascii(5, b"Clone"); |
166 | pub const DEFAULT_TRAIT: Name = Name::new_inline_ascii(7, b"Default"); | ||
167 | pub const DEBUG_TRAIT: Name = Name::new_inline_ascii(5, b"Debug"); | ||
168 | pub const HASH_TRAIT: Name = Name::new_inline_ascii(4, b"Hash"); | ||
169 | pub const ORD_TRAIT: Name = Name::new_inline_ascii(3, b"Ord"); | ||
170 | pub const PARTIAL_ORD_TRAIT: Name = Name::new_inline_ascii(10, b"PartialOrd"); | ||
171 | pub const EQ_TRAIT: Name = Name::new_inline_ascii(2, b"Eq"); | ||
172 | pub const PARTIAL_EQ_TRAIT: Name = Name::new_inline_ascii(9, b"PartialEq"); | ||