Skip to content

Commit 1069704

Browse files
committed
Implement all the other built-in derives
Since as long as we're not implementing the bodies, they all work the same way.
1 parent db8a00b commit 1069704

File tree

2 files changed

+86
-15
lines changed

2 files changed

+86
-15
lines changed

crates/ra_hir_expand/src/builtin_derive.rs

+79-15
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,14 @@ macro_rules! register_builtin {
4545

4646
register_builtin! {
4747
(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
4956
}
5057

5158
struct BasicAdtInfo {
@@ -109,36 +116,93 @@ fn make_type_args(n: usize, bound: Vec<tt::TokenTree>) -> Vec<tt::TokenTree> {
109116
result
110117
}
111118

112-
fn copy_expand(
113-
_db: &dyn AstDatabase,
114-
_id: MacroCallId,
119+
fn expand_simple_derive(
115120
tt: &tt::Subtree,
121+
trait_path: tt::Subtree,
116122
) -> Result<tt::Subtree, mbe::ExpandError> {
117123
let info = parse_adt(tt)?;
118124
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;
120127
let type_params = make_type_args(info.type_params, bound);
121128
let type_args = make_type_args(info.type_params, Vec::new());
129+
let trait_path = trait_path.token_trees;
122130
let expanded = quote! {
123-
impl ##type_params std::marker::Copy for #name ##type_args {}
131+
impl ##type_params ##trait_path for #name ##type_args {}
124132
};
125133
Ok(expanded)
126134
}
127135

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+
128144
fn clone_expand(
129145
_db: &dyn AstDatabase,
130146
_id: MacroCallId,
131147
tt: &tt::Subtree,
132148
) -> Result<tt::Subtree, mbe::ExpandError> {
133-
let info = parse_adt(tt)?;
134-
let name = info.name;
135-
let bound = (quote! { : std::clone::Clone }).token_trees;
136-
let type_params = make_type_args(info.type_params, bound);
137-
let type_args = make_type_args(info.type_params, Vec::new());
138-
let expanded = quote! {
139-
impl ##type_params std::clone::Clone for #name ##type_args {}
140-
};
141-
Ok(expanded)
149+
expand_simple_derive(tt, quote! { std::clone::Clone })
150+
}
151+
152+
fn default_expand(
153+
_db: &dyn AstDatabase,
154+
_id: MacroCallId,
155+
tt: &tt::Subtree,
156+
) -> Result<tt::Subtree, mbe::ExpandError> {
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 })
142206
}
143207

144208
#[cfg(test)]

crates/ra_hir_expand/src/name.rs

+7
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,10 @@ pub const STRINGIFY_MACRO: Name = Name::new_inline_ascii(9, b"stringify");
163163
// Builtin derives
164164
pub const COPY_TRAIT: Name = Name::new_inline_ascii(4, b"Copy");
165165
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");

0 commit comments

Comments
 (0)