Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit d9f4cbb

Browse files
committed
Emit function bodies in expanding builtin derives
1 parent 9811a3a commit d9f4cbb

File tree

7 files changed

+962
-38
lines changed

7 files changed

+962
-38
lines changed

crates/hir-def/src/macro_expansion_tests/builtin_derive_macro.rs

Lines changed: 288 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,33 @@ fn test_clone_expand() {
8484
r#"
8585
//- minicore: derive, clone
8686
#[derive(Clone)]
87-
struct Foo<A, B>;
87+
enum Command<A, B> {
88+
Move { x: A, y: B },
89+
Do(&'static str),
90+
Jump,
91+
}
8892
"#,
8993
expect![[r#"
9094
#[derive(Clone)]
91-
struct Foo<A, B>;
95+
enum Command<A, B> {
96+
Move { x: A, y: B },
97+
Do(&'static str),
98+
Jump,
99+
}
92100
93-
impl <A: core::clone::Clone, B: core::clone::Clone, > core::clone::Clone for Foo<A, B, > where {}"#]],
101+
impl <A: core::clone::Clone, B: core::clone::Clone, > core::clone::Clone for Command<A, B, > where {
102+
fn clone(&self ) -> Self {
103+
match self {
104+
Command::Move {
105+
x: x, y: y,
106+
}
107+
=>Command::Move {
108+
x: x.clone(), y: y.clone(),
109+
}
110+
, Command::Do(f0, )=>Command::Do(f0.clone(), ), Command::Jump=>Command::Jump,
111+
}
112+
}
113+
}"#]],
94114
);
95115
}
96116

@@ -106,6 +126,270 @@ struct Foo<const X: usize, T>(u32);
106126
#[derive(Clone)]
107127
struct Foo<const X: usize, T>(u32);
108128
109-
impl <const X: usize, T: core::clone::Clone, > core::clone::Clone for Foo<X, T, > where {}"#]],
129+
impl <const X: usize, T: core::clone::Clone, > core::clone::Clone for Foo<X, T, > where {
130+
fn clone(&self ) -> Self {
131+
match self {
132+
Foo(f0, )=>Foo(f0.clone(), ),
133+
}
134+
}
135+
}"#]],
136+
);
137+
}
138+
139+
#[test]
140+
fn test_default_expand() {
141+
check(
142+
r#"
143+
//- minicore: derive, default
144+
#[derive(Default)]
145+
struct Foo {
146+
field1: i32,
147+
field2: (),
148+
}
149+
#[derive(Default)]
150+
enum Bar {
151+
Foo(u8),
152+
#[default]
153+
Bar,
154+
}
155+
"#,
156+
expect![[r#"
157+
#[derive(Default)]
158+
struct Foo {
159+
field1: i32,
160+
field2: (),
161+
}
162+
#[derive(Default)]
163+
enum Bar {
164+
Foo(u8),
165+
#[default]
166+
Bar,
167+
}
168+
169+
impl < > core::default::Default for Foo< > where {
170+
fn default() -> Self {
171+
Foo {
172+
field1: core::default::Default::default(), field2: core::default::Default::default(),
173+
}
174+
}
175+
}
176+
impl < > core::default::Default for Bar< > where {
177+
fn default() -> Self {
178+
Bar::Bar
179+
}
180+
}"#]],
181+
);
182+
}
183+
184+
#[test]
185+
fn test_partial_eq_expand() {
186+
check(
187+
r#"
188+
//- minicore: derive, eq
189+
#[derive(PartialEq, Eq)]
190+
enum Command {
191+
Move { x: i32, y: i32 },
192+
Do(&'static str),
193+
Jump,
194+
}
195+
"#,
196+
expect![[r#"
197+
#[derive(PartialEq, Eq)]
198+
enum Command {
199+
Move { x: i32, y: i32 },
200+
Do(&'static str),
201+
Jump,
202+
}
203+
204+
impl < > core::cmp::PartialEq for Command< > where {
205+
fn eq(&self , other: &Self ) -> bool {
206+
match (self , other) {
207+
(Command::Move {
208+
x: x_self, y: y_self,
209+
}
210+
, Command::Move {
211+
x: x_other, y: y_other,
212+
}
213+
)=>x_self.eq(x_other) && y_self.eq(y_other), (Command::Do(f0_self, ), Command::Do(f0_other, ))=>f0_self.eq(f0_other), (Command::Jump, Command::Jump)=>true , _unused=>false
214+
}
215+
}
216+
}
217+
impl < > core::cmp::Eq for Command< > where {}"#]],
218+
);
219+
}
220+
221+
#[test]
222+
fn test_partial_ord_expand() {
223+
check(
224+
r#"
225+
//- minicore: derive, ord
226+
#[derive(PartialOrd, Ord)]
227+
enum Command {
228+
Move { x: i32, y: i32 },
229+
Do(&'static str),
230+
Jump,
231+
}
232+
"#,
233+
expect![[r#"
234+
#[derive(PartialOrd, Ord)]
235+
enum Command {
236+
Move { x: i32, y: i32 },
237+
Do(&'static str),
238+
Jump,
239+
}
240+
241+
impl < > core::cmp::PartialOrd for Command< > where {
242+
fn partial_cmp(&self , other: &Self ) -> core::option::Option::Option<core::cmp::Ordering> {
243+
match core::intrinsics::discriminant_value(self ).partial_cmp(&core::intrinsics::discriminant_value(other)) {
244+
core::option::Option::Some(core::cmp::Ordering::Equal)=> {
245+
match (self , other) {
246+
(Command::Move {
247+
x: x_self, y: y_self,
248+
}
249+
, Command::Move {
250+
x: x_other, y: y_other,
251+
}
252+
)=>match x_self.partial_cmp(&x_other) {
253+
core::option::Option::Some(core::cmp::Ordering::Equal)=> {
254+
match y_self.partial_cmp(&y_other) {
255+
core::option::Option::Some(core::cmp::Ordering::Equal)=> {
256+
core::option::Option::Some(core::cmp::Ordering::Equal)
257+
}
258+
c=>return c,
259+
}
260+
}
261+
c=>return c,
262+
}
263+
, (Command::Do(f0_self, ), Command::Do(f0_other, ))=>match f0_self.partial_cmp(&f0_other) {
264+
core::option::Option::Some(core::cmp::Ordering::Equal)=> {
265+
core::option::Option::Some(core::cmp::Ordering::Equal)
266+
}
267+
c=>return c,
268+
}
269+
, (Command::Jump, Command::Jump)=>core::option::Option::Some(core::cmp::Ordering::Equal), _unused=>core::option::Option::Some(core::cmp::Ordering::Equal)
270+
}
271+
}
272+
c=>return c,
273+
}
274+
}
275+
}
276+
impl < > core::cmp::Ord for Command< > where {
277+
fn cmp(&self , other: &Self ) -> core::cmp::Ordering {
278+
match core::intrinsics::discriminant_value(self ).cmp(&core::intrinsics::discriminant_value(other)) {
279+
core::cmp::Ordering::Equal=> {
280+
match (self , other) {
281+
(Command::Move {
282+
x: x_self, y: y_self,
283+
}
284+
, Command::Move {
285+
x: x_other, y: y_other,
286+
}
287+
)=>match x_self.cmp(&x_other) {
288+
core::cmp::Ordering::Equal=> {
289+
match y_self.cmp(&y_other) {
290+
core::cmp::Ordering::Equal=> {
291+
core::cmp::Ordering::Equal
292+
}
293+
c=>return c,
294+
}
295+
}
296+
c=>return c,
297+
}
298+
, (Command::Do(f0_self, ), Command::Do(f0_other, ))=>match f0_self.cmp(&f0_other) {
299+
core::cmp::Ordering::Equal=> {
300+
core::cmp::Ordering::Equal
301+
}
302+
c=>return c,
303+
}
304+
, (Command::Jump, Command::Jump)=>core::cmp::Ordering::Equal, _unused=>core::cmp::Ordering::Equal
305+
}
306+
}
307+
c=>return c,
308+
}
309+
}
310+
}"#]],
311+
);
312+
}
313+
314+
#[test]
315+
fn test_hash_expand() {
316+
check(
317+
r#"
318+
//- minicore: derive, hash
319+
use core::hash::Hash;
320+
321+
#[derive(Hash)]
322+
enum Command {
323+
Move { x: i32, y: i32 },
324+
Do(&'static str),
325+
Jump,
326+
}
327+
"#,
328+
expect![[r#"
329+
use core::hash::Hash;
330+
331+
#[derive(Hash)]
332+
enum Command {
333+
Move { x: i32, y: i32 },
334+
Do(&'static str),
335+
Jump,
336+
}
337+
338+
impl < > core::hash::Hash for Command< > where {
339+
fn hash<H: core::hash::Hasher>(&self , state: &mut H) {
340+
core::mem::discriminant(self ).hash(state);
341+
match self {
342+
Command::Move {
343+
x: x, y: y,
344+
}
345+
=> {
346+
x.hash(state);
347+
y.hash(state);
348+
}
349+
, Command::Do(f0, )=> {
350+
f0.hash(state);
351+
}
352+
, Command::Jump=> {}
353+
,
354+
}
355+
}
356+
}"#]],
357+
);
358+
}
359+
360+
#[test]
361+
fn test_debug_expand() {
362+
check(
363+
r#"
364+
//- minicore: derive, fmt
365+
use core::fmt::Debug;
366+
367+
#[derive(Debug)]
368+
enum Command {
369+
Move { x: i32, y: i32 },
370+
Do(&'static str),
371+
Jump,
372+
}
373+
"#,
374+
expect![[r#"
375+
use core::fmt::Debug;
376+
377+
#[derive(Debug)]
378+
enum Command {
379+
Move { x: i32, y: i32 },
380+
Do(&'static str),
381+
Jump,
382+
}
383+
384+
impl < > core::fmt::Debug for Command< > where {
385+
fn fmt(&self , f: &mut core::fmt::Formatter) -> core::fmt::Result {
386+
match self {
387+
Command::Move {
388+
x: x, y: y,
389+
}
390+
=>f.debug_struct("Move").field("x", x).field("y", y).finish(), Command::Do(f0, )=>f.debug_tuple("Do").field(f0).finish(), Command::Jump=>f.write_str("Jump"),
391+
}
392+
}
393+
}"#]],
110394
);
111395
}

0 commit comments

Comments
 (0)