Skip to content

Commit dac190a

Browse files
committed
Rewrite #[derive(QueryableByName)] in derives2
Since `QueryableByName` is one of the more recently written derives, it should have been a really straightforward port. Unfortunately, the tests for this derive hit multiple rustc bugs - rust-lang/rust#47983 - rust-lang/rust#47311 I love what we were able to do with the error message here. We could even go so far as to have the `help` lines point at the struct itself for the `table_name` annotation if we want to. I also much prefer the workaround for rust-lang/rust#47311 in this PR to the one I did in #1529. I'll need to update that PR if this is merged first.
1 parent 8e5e59c commit dac190a

25 files changed

+256
-238
lines changed

.travis.yml

-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ script:
2020
(cd diesel && travis-cargo test -- --no-default-features --features "extras $BACKEND")
2121
fi &&
2222
(cd diesel && travis-cargo test -- --no-default-features --features "extras with-deprecated $BACKEND") &&
23-
(cd diesel_derives && travis-cargo test -- --features "diesel/$BACKEND") &&
2423
(cd diesel_derives2 && travis-cargo test -- --features "$BACKEND") &&
2524
if [[ "$TRAVIS_RUST_VERSION" == nightly* ]]; then
2625
(cd diesel_derives2 && travis-cargo test -- --features "nightly $BACKEND")
@@ -48,7 +47,6 @@ matrix:
4847
script:
4948
- (cd diesel && cargo rustc --no-default-features --features "lint unstable sqlite postgres mysql extras" -- -Zno-trans)
5049
- (cd diesel_cli && cargo rustc --no-default-features --features "lint sqlite postgres mysql" -- -Zno-trans)
51-
- (cd diesel_derives && cargo rustc --no-default-features --features "lint dotenv sqlite postgres mysql" -- -Zno-trans)
5250
- (cd diesel_derives2 && cargo rustc --no-default-features --features "lint" -- -Zno-trans)
5351
- (cd diesel_migrations && cargo rustc --no-default-features --features "lint dotenv sqlite postgres mysql" -- -Zno-trans)
5452
- (cd diesel_infer_schema && cargo rustc --no-default-features --features "lint dotenv sqlite postgres mysql" -- -Zno-trans)

bin/check

-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ check() {
77
cargo rustc --quiet --features "lint $BACKENDS $1" -- -Zno-trans
88
}
99

10-
(cd diesel_derives &&
11-
check &&
12-
echo "✔ derives crate looks good!")
13-
1410
(cd diesel_derives2 &&
1511
check &&
1612
echo "✔ derives2 crate looks good!")

bin/test

-3
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@ else
2626
(cd diesel_infer_schema/infer_schema_macros && cargo test --features "$CLIPPY sqlite" $*)
2727
(cd diesel_infer_schema && cargo test --features "$CLIPPY sqlite" $*)
2828
(cd diesel_migrations && cargo test --features "$CLIPPY sqlite" $*)
29-
(cd diesel_derives && cargo test --features "$CLIPPY diesel/sqlite" $*)
3029
(cd diesel_derives2 && cargo test --features "$CLIPPY sqlite" $*)
3130
(cd diesel_tests && cargo test --features "$CLIPPY sqlite" --no-default-features $*)
3231

3332
(cd diesel_infer_schema/infer_schema_internals && cargo test --features "$CLIPPY postgres" $*)
3433
(cd diesel_infer_schema/infer_schema_macros && cargo test --features "$CLIPPY postgres" $*)
3534
(cd diesel_infer_schema && cargo test --features "$CLIPPY postgres" $*)
3635
(cd diesel_migrations && cargo test --features "$CLIPPY postgres" $*)
37-
(cd diesel_derives && cargo test --features "$CLIPPY diesel/postgres" $*)
3836
(cd diesel_derives2 && cargo test --features "$CLIPPY postgres" $*)
3937
(cd diesel_cli && cargo test --features "$CLIPPY postgres" --no-default-features $*)
4038
(cd diesel_tests && cargo test --features "$CLIPPY postgres" --no-default-features $*)
@@ -44,7 +42,6 @@ else
4442
(cd diesel_infer_schema/infer_schema_macros && cargo test --features "$CLIPPY mysql" $*)
4543
(cd diesel_infer_schema && cargo test --features "$CLIPPY mysql" $*)
4644
(cd diesel_migrations && cargo test --features "$CLIPPY mysql" $*)
47-
(cd diesel_derives && cargo test --features "$CLIPPY diesel/mysql" $*)
4845
(cd diesel_derives2 && cargo test --features "$CLIPPY mysql" $*)
4946
(cd diesel_cli && cargo test --features "$CLIPPY mysql" --no-default-features $*)
5047
(cd diesel_tests && cargo test --features "$CLIPPY mysql" --no-default-features $*)

diesel_compile_tests/tests/compile-fail/queryable_by_name_requires_table_name_or_sql_type_annotation.rs

-9
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#[macro_use] extern crate diesel;
2+
3+
#[derive(QueryableByName)]
4+
struct Foo {
5+
foo: i32,
6+
bar: String,
7+
}
8+
9+
#[derive(QueryableByName)]
10+
struct Bar(i32, String);
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
error: Cannot determine the SQL type of foo
2+
--> $DIR/queryable_by_name_requires_table_name_or_sql_type_annotation.rs:5:5
3+
|
4+
5 | foo: i32,
5+
| ^^^
6+
|
7+
= help: Your struct must either be annotated with `#[table_name = "foo"]` or have all of its fields annotated with `#[sql_type = "Integer"]`
8+
9+
error: Cannot determine the SQL type of bar
10+
--> $DIR/queryable_by_name_requires_table_name_or_sql_type_annotation.rs:6:5
11+
|
12+
6 | bar: String,
13+
| ^^^
14+
|
15+
= help: Your struct must either be annotated with `#[table_name = "foo"]` or have all of its fields annotated with `#[sql_type = "Integer"]`
16+
17+
error: All fields of tuple structs must be annotated with `#[column_name]`
18+
--> $DIR/queryable_by_name_requires_table_name_or_sql_type_annotation.rs:10:12
19+
|
20+
10 | struct Bar(i32, String);
21+
| ^^^
22+
23+
error: Cannot determine the SQL type of field
24+
--> $DIR/queryable_by_name_requires_table_name_or_sql_type_annotation.rs:10:12
25+
|
26+
10 | struct Bar(i32, String);
27+
| ^^^
28+
|
29+
= help: Your struct must either be annotated with `#[table_name = "foo"]` or have all of its fields annotated with `#[sql_type = "Integer"]`
30+
31+
error: All fields of tuple structs must be annotated with `#[column_name]`
32+
--> $DIR/queryable_by_name_requires_table_name_or_sql_type_annotation.rs:10:17
33+
|
34+
10 | struct Bar(i32, String);
35+
| ^^^^^^
36+
37+
error: Cannot determine the SQL type of field
38+
--> $DIR/queryable_by_name_requires_table_name_or_sql_type_annotation.rs:10:17
39+
|
40+
10 | struct Bar(i32, String);
41+
| ^^^^^^
42+
|
43+
= help: Your struct must either be annotated with `#[table_name = "foo"]` or have all of its fields annotated with `#[sql_type = "Integer"]`
44+
45+
error: aborting due to 6 previous errors
46+

diesel_derives/Cargo.toml

-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ postgres = []
2929
sqlite = []
3030
mysql = []
3131

32-
[[test]]
33-
name = "tests"
34-
3532
[badges]
3633
travis-ci = { repository = "diesel-rs/diesel" }
3734
appveyor = { repository = "diesel-rs/diesel" }

diesel_derives/src/attr.rs

-25
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,6 @@ impl Attr {
3636
}
3737
}
3838

39-
pub fn field_name(&self) -> &syn::Ident {
40-
self.field_name.as_ref().unwrap_or(&self.field_position)
41-
}
42-
43-
pub fn column_name(&self) -> &syn::Ident {
44-
self.column_name
45-
.as_ref()
46-
.or_else(|| self.field_name.as_ref())
47-
.expect(
48-
"All fields of tuple structs must be annotated with `#[column_name=\"something\"]`",
49-
)
50-
}
51-
52-
pub fn sql_type(&self) -> Option<&syn::Ty> {
53-
self.sql_type.as_ref()
54-
}
55-
56-
pub fn has_flag<T>(&self, flag: &T) -> bool
57-
where
58-
T: ?Sized,
59-
syn::Ident: PartialEq<T>,
60-
{
61-
self.flags.iter().any(|f| f == flag)
62-
}
63-
6439
fn field_kind(&self) -> &str {
6540
if is_option_ty(&self.ty) {
6641
"option"

diesel_derives/src/lib.rs

-6
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,12 @@ mod attr;
3030
mod from_sql_row;
3131
mod insertable;
3232
mod model;
33-
mod queryable_by_name;
3433
mod sql_type;
3534
mod util;
3635

3736
use proc_macro::TokenStream;
3837
use syn::parse_derive_input;
3938

40-
#[proc_macro_derive(QueryableByName, attributes(table_name, column_name, sql_type, diesel))]
41-
pub fn derive_queryable_by_name(input: TokenStream) -> TokenStream {
42-
expand_derive(input, queryable_by_name::derive)
43-
}
44-
4539
#[proc_macro_derive(Insertable, attributes(table_name, column_name))]
4640
pub fn derive_insertable(input: TokenStream) -> TokenStream {
4741
expand_derive(input, insertable::derive_insertable)

diesel_derives/src/model.rs

-5
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ impl Model {
5050
pub fn has_table_name_annotation(&self) -> bool {
5151
self.table_name_from_annotation.is_some()
5252
}
53-
54-
pub fn dummy_const_name(&self, trait_name: &str) -> syn::Ident {
55-
let model_name = self.name.as_ref().to_uppercase();
56-
format!("_IMPL_{}_FOR_{}", trait_name, model_name).into()
57-
}
5853
}
5954

6055
pub fn infer_association_name(name: &str) -> String {

diesel_derives/src/queryable_by_name.rs

-76
This file was deleted.

diesel_derives/tests/test_helpers.rs

-56
This file was deleted.

diesel_derives/tests/tests.rs

-9
This file was deleted.

diesel_derives2/src/as_changeset.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ fn field_changeset_expr(
8686
table_name: syn::Ident,
8787
treat_none_as_null: bool,
8888
) -> syn::Expr {
89-
let field_access = &field.name;
89+
let field_access = field.name.access();
9090
let column_name = field.column_name();
9191
if !treat_none_as_null && is_option_ty(&field.ty) {
9292
parse_quote!(self#field_access.as_ref().map(|x| #table_name::#column_name.eq(x)))

diesel_derives2/src/associations.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ fn derive_belongs_to(
3939

4040
let foreign_key_field = model.find_column(foreign_key)?;
4141
let struct_name = model.name;
42-
let foreign_key_access = &foreign_key_field.name;
42+
let foreign_key_access = foreign_key_field.name.access();
4343
let foreign_key_ty = inner_of_option_ty(&foreign_key_field.ty);
4444
let table_name = model.table_name();
4545

0 commit comments

Comments
 (0)