Skip to content

Commit 9c244ee

Browse files
committed
Add error message for derive(Insertable) on empty struct
This change adds a new error message when attempting to derive Insertable on a struct with no fields, and a compile-fail test to ensure it is displayed appropriately. See diesel-rs#920
1 parent 0aee391 commit 9c244ee

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

diesel_codegen/src/insertable.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ pub fn derive_insertable(item: syn::MacroInput) -> quote::Tokens {
2121
let lifetimes = model.generics.lifetimes;
2222
let fields = model.attrs.as_slice();
2323

24+
if fields.is_empty() {
25+
panic!("Failed to derive `Insertable` for `{}`: `Insertable` \
26+
cannot be used on structs with empty fields", struct_name);
27+
}
28+
2429
quote!(impl_Insertable! {
2530
(
2631
struct_name = #struct_name,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#[macro_use]
2+
extern crate diesel;
3+
#[macro_use]
4+
extern crate diesel_codegen;
5+
6+
use diesel::*;
7+
8+
table! {
9+
users {
10+
id -> Integer,
11+
}
12+
}
13+
14+
#[derive(Insertable)]
15+
//~^ ERROR proc-macro derive panicked
16+
//~| HELP Failed to derive `Insertable` for `NewUser`: `Insertable` cannot be used on structs with empty fields
17+
#[table_name="users"]
18+
pub struct NewUser {}
19+
20+
fn main() {
21+
}

0 commit comments

Comments
 (0)