From e69f1583c8a866e57f3c9147675f2f80c8c7d215 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Thu, 10 Oct 2019 15:21:45 -0700 Subject: [PATCH] cfail tests for derive --- postgres-derive-test/Cargo.toml | 4 ++- .../src/compile-fail/invalid-types.rs | 25 +++++++++++++ .../src/compile-fail/invalid-types.stderr | 35 +++++++++++++++++++ .../src/compile-fail/unknown-override.rs | 15 ++++++++ .../src/compile-fail/unknown-override.stderr | 11 ++++++ postgres-derive-test/src/lib.rs | 5 +++ postgres-derive/src/fromsql.rs | 2 +- postgres-derive/src/lib.rs | 8 +++-- 8 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 postgres-derive-test/src/compile-fail/invalid-types.rs create mode 100644 postgres-derive-test/src/compile-fail/invalid-types.stderr create mode 100644 postgres-derive-test/src/compile-fail/unknown-override.rs create mode 100644 postgres-derive-test/src/compile-fail/unknown-override.stderr diff --git a/postgres-derive-test/Cargo.toml b/postgres-derive-test/Cargo.toml index 1632660e..24fd1614 100644 --- a/postgres-derive-test/Cargo.toml +++ b/postgres-derive-test/Cargo.toml @@ -4,6 +4,8 @@ version = "0.1.0" authors = ["Steven Fackler "] edition = "2018" -[dependencies] +[dev-dependencies] +trybuild = "1.0" + postgres-types = { path = "../postgres-types", features = ["derive"] } postgres = { path = "../postgres" } diff --git a/postgres-derive-test/src/compile-fail/invalid-types.rs b/postgres-derive-test/src/compile-fail/invalid-types.rs new file mode 100644 index 00000000..ef41ac82 --- /dev/null +++ b/postgres-derive-test/src/compile-fail/invalid-types.rs @@ -0,0 +1,25 @@ +use postgres_types::{FromSql, ToSql}; + +#[derive(ToSql)] +struct ToSqlUnit; + +#[derive(FromSql)] +struct FromSqlUnit; + +#[derive(ToSql)] +struct ToSqlTuple(i32, i32); + +#[derive(FromSql)] +struct FromSqlTuple(i32, i32); + +#[derive(ToSql)] +enum ToSqlEnum { + Foo(i32), +} + +#[derive(FromSql)] +enum FromSqlEnum { + Foo(i32), +} + +fn main() {} diff --git a/postgres-derive-test/src/compile-fail/invalid-types.stderr b/postgres-derive-test/src/compile-fail/invalid-types.stderr new file mode 100644 index 00000000..9b563d58 --- /dev/null +++ b/postgres-derive-test/src/compile-fail/invalid-types.stderr @@ -0,0 +1,35 @@ +error: #[derive(ToSql)] may only be applied to structs, single field tuple structs, and enums + --> $DIR/invalid-types.rs:4:1 + | +4 | struct ToSqlUnit; + | ^^^^^^^^^^^^^^^^^ + +error: #[derive(FromSql)] may only be applied to structs, single field tuple structs, and enums + --> $DIR/invalid-types.rs:7:1 + | +7 | struct FromSqlUnit; + | ^^^^^^^^^^^^^^^^^^^ + +error: #[derive(ToSql)] may only be applied to structs, single field tuple structs, and enums + --> $DIR/invalid-types.rs:10:1 + | +10 | struct ToSqlTuple(i32, i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: #[derive(FromSql)] may only be applied to structs, single field tuple structs, and enums + --> $DIR/invalid-types.rs:13:1 + | +13 | struct FromSqlTuple(i32, i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: non-C-like enums are not supported + --> $DIR/invalid-types.rs:17:5 + | +17 | Foo(i32), + | ^^^^^^^^ + +error: non-C-like enums are not supported + --> $DIR/invalid-types.rs:22:5 + | +22 | Foo(i32), + | ^^^^^^^^ diff --git a/postgres-derive-test/src/compile-fail/unknown-override.rs b/postgres-derive-test/src/compile-fail/unknown-override.rs new file mode 100644 index 00000000..e4fffd54 --- /dev/null +++ b/postgres-derive-test/src/compile-fail/unknown-override.rs @@ -0,0 +1,15 @@ +use postgres_types::{FromSql, ToSql}; + +#[derive(FromSql)] +#[postgres(foo = "bar")] +struct Foo { + a: i32, +} + +#[derive(ToSql)] +#[postgres(foo = "bar")] +struct Bar { + a: i32, +} + +fn main() {} diff --git a/postgres-derive-test/src/compile-fail/unknown-override.stderr b/postgres-derive-test/src/compile-fail/unknown-override.stderr new file mode 100644 index 00000000..b7719e3c --- /dev/null +++ b/postgres-derive-test/src/compile-fail/unknown-override.stderr @@ -0,0 +1,11 @@ +error: unknown override + --> $DIR/unknown-override.rs:4:12 + | +4 | #[postgres(foo = "bar")] + | ^^^ + +error: unknown override + --> $DIR/unknown-override.rs:10:12 + | +10 | #[postgres(foo = "bar")] + | ^^^ diff --git a/postgres-derive-test/src/lib.rs b/postgres-derive-test/src/lib.rs index 0bf429e3..7da75af8 100644 --- a/postgres-derive-test/src/lib.rs +++ b/postgres-derive-test/src/lib.rs @@ -25,3 +25,8 @@ where assert_eq!(val, &result); } } + +#[test] +fn compile_fail() { + trybuild::TestCases::new().compile_fail("src/compile-fail/*.rs"); +} diff --git a/postgres-derive/src/fromsql.rs b/postgres-derive/src/fromsql.rs index 07359b39..c4a2447f 100644 --- a/postgres-derive/src/fromsql.rs +++ b/postgres-derive/src/fromsql.rs @@ -51,7 +51,7 @@ pub fn expand_derive_fromsql(input: DeriveInput) -> Result { _ => { return Err(Error::new_spanned( input, - "#[derive(ToSql)] may only be applied to structs, single field tuple structs, and enums", + "#[derive(FromSql)] may only be applied to structs, single field tuple structs, and enums", )) } }; diff --git a/postgres-derive/src/lib.rs b/postgres-derive/src/lib.rs index ff29e7cc..25252638 100644 --- a/postgres-derive/src/lib.rs +++ b/postgres-derive/src/lib.rs @@ -18,11 +18,15 @@ mod tosql; #[proc_macro_derive(ToSql, attributes(postgres))] pub fn derive_tosql(input: TokenStream) -> TokenStream { let input = syn::parse(input).unwrap(); - tosql::expand_derive_tosql(input).unwrap().into() + tosql::expand_derive_tosql(input) + .unwrap_or_else(|e| e.to_compile_error()) + .into() } #[proc_macro_derive(FromSql, attributes(postgres))] pub fn derive_fromsql(input: TokenStream) -> TokenStream { let input = syn::parse(input).unwrap(); - fromsql::expand_derive_fromsql(input).unwrap().into() + fromsql::expand_derive_fromsql(input) + .unwrap_or_else(|e| e.to_compile_error()) + .into() }