Skip to content

Commit bc664d7

Browse files
committed
Rewrite #[derive(Associations)] in derives2
This was a pretty straightforward rewrite. Overall I feel much better about this code structure than what we had before. We're having to do a bit of funkyness to avoid rust-lang/rust#47941, but other than that everything here was pretty straightforward. I had the UI tests guide me the entire time.
1 parent 0b0b1e8 commit bc664d7

15 files changed

+408
-68
lines changed

diesel_compile_tests/tests/compile-fail/associations_errors.rs

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#[macro_use]
2+
extern crate diesel;
3+
4+
table! {
5+
foo {
6+
id -> Integer,
7+
}
8+
}
9+
10+
#[derive(Identifiable)]
11+
#[table_name = "foo"]
12+
struct Bar {
13+
id: i32,
14+
}
15+
16+
#[derive(Identifiable)]
17+
#[table_name = "foo"]
18+
struct Baz {
19+
id: i32,
20+
}
21+
22+
#[derive(Associations)]
23+
#[belongs_to]
24+
#[belongs_to = "Bar"]
25+
#[belongs_to()]
26+
#[belongs_to(foreign_key = "bar_id")]
27+
#[belongs_to(Bar, foreign_key)]
28+
#[belongs_to(Bar, foreign_key(bar_id))]
29+
#[belongs_to(Baz, foreign_key = "bar_id", random_option)]
30+
struct Foo {
31+
bar_id: i32,
32+
}
33+
34+
fn main() {}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
error: `belongs_to` must be in the form `belongs_to(...)`
2+
--> <macro expansion>:1:1
3+
|
4+
1 | #[belongs_to]
5+
| ^
6+
7+
error: `belongs_to` must be in the form `belongs_to(...)`
8+
--> <macro expansion>:1:1
9+
|
10+
1 | #[belongs_to = "Bar"]
11+
| ^
12+
13+
error: Expected a struct name
14+
--> <macro expansion>:1:1
15+
|
16+
1 | #[belongs_to()]
17+
| ^
18+
|
19+
= help: e.g. `#[belongs_to(User)]`
20+
21+
error: Expected a struct name
22+
--> <macro expansion>:1:1
23+
|
24+
1 | #[belongs_to(foreign_key = "bar_id")]
25+
| ^
26+
|
27+
= help: e.g. `#[belongs_to(User)]`
28+
29+
error: `foreign_key` must be in the form `foreign_key = "value"`
30+
--> <macro expansion>:1:1
31+
|
32+
1 | #[belongs_to(Bar, foreign_key)]
33+
| ^
34+
35+
warning: The form `foreign_key(value)` is deprecated. Use `foreign_key = "value"` instead
36+
--> <macro expansion>:1:1
37+
|
38+
1 | #[belongs_to(Bar, foreign_key(bar_id))]
39+
| ^
40+
41+
warning: Unrecognized option random_option
42+
--> <macro expansion>:1:1
43+
|
44+
1 | #[belongs_to(Baz, foreign_key = "bar_id", random_option)]
45+
| ^
46+
47+
error: aborting due to 5 previous errors
48+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#[macro_use]
2+
extern crate diesel;
3+
4+
struct Bar;
5+
6+
table! {
7+
foo {
8+
id -> Integer,
9+
}
10+
}
11+
12+
#[derive(Associations)]
13+
#[belongs_to(Bar)]
14+
#[table_name = "foo"]
15+
struct Foo1 {
16+
bar_id: i32,
17+
}
18+
19+
#[derive(Associations)]
20+
#[belongs_to(Bar, foreign_key = "bar_id")]
21+
#[table_name = "foo"]
22+
struct Foo2 {
23+
bar_id: i32,
24+
}
25+
26+
#[derive(Associations)]
27+
#[belongs_to(Bar)]
28+
#[table_name = "foo"]
29+
struct Foo3 {
30+
#[column_name = "bar_id"]
31+
baz_id: i32,
32+
}
33+
34+
#[derive(Associations)]
35+
#[belongs_to(Bar, foreign_key = "bar_id")]
36+
#[table_name = "foo"]
37+
struct Foo4 {
38+
#[column_name = "bar_id"]
39+
baz_id: i32,
40+
}
41+
42+
fn main() {}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
error[E0412]: cannot find type `bar_id` in module `foo`
2+
--> <macro expansion>:1:1
3+
|
4+
1 | #[belongs_to(Bar)]
5+
| ^ not found in `foo`
6+
7+
error[E0425]: cannot find value `bar_id` in module `foo`
8+
--> <macro expansion>:1:1
9+
|
10+
1 | #[belongs_to(Bar)]
11+
| ^ not found in `foo`
12+
13+
error[E0412]: cannot find type `bar_id` in module `foo`
14+
--> <macro expansion>:1:1
15+
|
16+
1 | #[belongs_to(Bar, foreign_key = "bar_id")]
17+
| ^ not found in `foo`
18+
19+
error[E0425]: cannot find value `bar_id` in module `foo`
20+
--> <macro expansion>:1:1
21+
|
22+
1 | #[belongs_to(Bar, foreign_key = "bar_id")]
23+
| ^ not found in `foo`
24+
25+
error[E0412]: cannot find type `bar_id` in module `foo`
26+
--> <macro expansion>:1:1
27+
|
28+
1 | #[belongs_to(Bar)]
29+
| ^ not found in `foo`
30+
31+
error[E0425]: cannot find value `bar_id` in module `foo`
32+
--> <macro expansion>:1:1
33+
|
34+
1 | #[belongs_to(Bar)]
35+
| ^ not found in `foo`
36+
37+
error[E0412]: cannot find type `bar_id` in module `foo`
38+
--> <macro expansion>:1:1
39+
|
40+
1 | #[belongs_to(Bar, foreign_key = "bar_id")]
41+
| ^ not found in `foo`
42+
43+
error[E0425]: cannot find value `bar_id` in module `foo`
44+
--> <macro expansion>:1:1
45+
|
46+
1 | #[belongs_to(Bar, foreign_key = "bar_id")]
47+
| ^ not found in `foo`
48+
49+
error: aborting due to 8 previous errors
50+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#[macro_use]
2+
extern crate diesel;
3+
4+
struct Bar;
5+
6+
#[derive(Associations)]
7+
#[belongs_to(Bar)]
8+
#[belongs_to(Bar, foreign_key = "bar_id")]
9+
struct Foo {}
10+
11+
#[derive(Associations)]
12+
#[belongs_to(Bar)]
13+
#[belongs_to(Bar, foreign_key = "bar_id")]
14+
struct Baz {
15+
#[column_name = "baz_id"]
16+
bar_id: i32,
17+
}
18+
19+
fn main() {}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: No field with column name bar_id
2+
--> <macro expansion>:1:1
3+
|
4+
1 | #[belongs_to(Bar)]
5+
| ^
6+
7+
error: No field with column name bar_id
8+
--> <macro expansion>:1:1
9+
|
10+
1 | #[belongs_to(Bar, foreign_key = "bar_id")]
11+
| ^
12+
13+
error: No field with column name bar_id
14+
--> <macro expansion>:1:1
15+
|
16+
1 | #[belongs_to(Bar)]
17+
| ^
18+
19+
error: No field with column name bar_id
20+
--> <macro expansion>:1:1
21+
|
22+
1 | #[belongs_to(Bar, foreign_key = "bar_id")]
23+
| ^
24+
25+
error: aborting due to 4 previous errors
26+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#[macro_use]
2+
extern crate diesel;
3+
4+
table! {
5+
foos {
6+
id -> Integer,
7+
bar_id -> Integer,
8+
}
9+
}
10+
11+
#[derive(Associations)]
12+
#[belongs_to(Bar)]
13+
struct Foo {
14+
bar_id: i32,
15+
}
16+
17+
fn main() {}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error[E0412]: cannot find type `Bar` in this scope
2+
--> <macro expansion>:1:1
3+
|
4+
1 | #[belongs_to(Bar)]
5+
| ^ not found in this scope
6+
7+
error: aborting due to previous error
8+

diesel_derives/src/lib.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ extern crate quote;
2525
extern crate syn;
2626

2727
mod as_expression;
28-
mod associations;
2928
mod ast_builder;
3029
mod attr;
3130
mod from_sql_row;
@@ -61,11 +60,6 @@ pub fn derive_insertable(input: TokenStream) -> TokenStream {
6160
expand_derive(input, insertable::derive_insertable)
6261
}
6362

64-
#[proc_macro_derive(Associations, attributes(table_name, belongs_to))]
65-
pub fn derive_associations(input: TokenStream) -> TokenStream {
66-
expand_derive(input, associations::derive_associations)
67-
}
68-
6963
#[proc_macro_derive(QueryId)]
7064
pub fn derive_query_id(input: TokenStream) -> TokenStream {
7165
expand_derive(input, query_id::derive)

0 commit comments

Comments
 (0)