diff --git a/tests/test_compile_error.rs b/tests/test_compile_error.rs index b1fcdc09fb7..d15cc4bdec7 100644 --- a/tests/test_compile_error.rs +++ b/tests/test_compile_error.rs @@ -25,6 +25,7 @@ fn test_compile_errors() { t.compile_fail("tests/ui/invalid_closure.rs"); t.compile_fail("tests/ui/pyclass_send.rs"); t.compile_fail("tests/ui/invalid_argument_attributes.rs"); + t.compile_fail("tests/ui/invalid_intopy_derive.rs"); t.compile_fail("tests/ui/invalid_frompy_derive.rs"); t.compile_fail("tests/ui/static_ref.rs"); t.compile_fail("tests/ui/wrong_aspyref_lifetimes.rs"); diff --git a/tests/ui/invalid_intopy_derive.rs b/tests/ui/invalid_intopy_derive.rs new file mode 100644 index 00000000000..9bbfc9b10cc --- /dev/null +++ b/tests/ui/invalid_intopy_derive.rs @@ -0,0 +1,90 @@ +use pyo3::IntoPyObject; + +#[derive(IntoPyObject)] +struct Foo(); + +#[derive(IntoPyObject)] +struct Foo2 {} + +#[derive(IntoPyObject)] +enum EmptyEnum {} + +#[derive(IntoPyObject)] +enum EnumWithEmptyTupleVar { + EmptyTuple(), + Valid(String), +} + +#[derive(IntoPyObject)] +enum EnumWithEmptyStructVar { + EmptyStruct {}, + Valid(String), +} + +#[derive(IntoPyObject)] +#[pyo3(transparent)] +struct EmptyTransparentTup(); + +#[derive(IntoPyObject)] +#[pyo3(transparent)] +struct EmptyTransparentStruct {} + +#[derive(IntoPyObject)] +enum EnumWithTransparentEmptyTupleVar { + #[pyo3(transparent)] + EmptyTuple(), + Valid(String), +} + +#[derive(IntoPyObject)] +enum EnumWithTransparentEmptyStructVar { + #[pyo3(transparent)] + EmptyStruct {}, + Valid(String), +} + +#[derive(IntoPyObject)] +#[pyo3(transparent)] +struct TransparentTupTooManyFields(String, String); + +#[derive(IntoPyObject)] +#[pyo3(transparent)] +struct TransparentStructTooManyFields { + foo: String, + bar: String, +} + +#[derive(IntoPyObject)] +enum EnumWithTransparentTupleTooMany { + #[pyo3(transparent)] + EmptyTuple(String, String), + Valid(String), +} + +#[derive(IntoPyObject)] +enum EnumWithTransparentStructTooMany { + #[pyo3(transparent)] + EmptyStruct { + foo: String, + bar: String, + }, + Valid(String), +} + +#[derive(IntoPyObject)] +#[pyo3(unknown = "should not work")] +struct UnknownContainerAttr { + a: String, +} + +#[derive(IntoPyObject)] +union Union { + a: usize, +} + +#[derive(IntoPyObject)] +enum UnitEnum { + Unit, +} + +fn main() {} diff --git a/tests/ui/invalid_intopy_derive.stderr b/tests/ui/invalid_intopy_derive.stderr new file mode 100644 index 00000000000..42cb15de0f4 --- /dev/null +++ b/tests/ui/invalid_intopy_derive.stderr @@ -0,0 +1,105 @@ +error: cannot derive `IntoPyObject` for empty structs and variants + --> tests/ui/invalid_intopy_derive.rs:4:11 + | +4 | struct Foo(); + | ^^ + +error: cannot derive `IntoPyObject` for empty structs and variants + --> tests/ui/invalid_intopy_derive.rs:7:13 + | +7 | struct Foo2 {} + | ^^ + +error: cannot derive `IntoPyObject` for empty enum + --> tests/ui/invalid_intopy_derive.rs:10:6 + | +10 | enum EmptyEnum {} + | ^^^^^^^^^ + +error: cannot derive `IntoPyObject` for empty structs and variants + --> tests/ui/invalid_intopy_derive.rs:14:15 + | +14 | EmptyTuple(), + | ^^ + +error: cannot derive `IntoPyObject` for empty structs and variants + --> tests/ui/invalid_intopy_derive.rs:20:17 + | +20 | EmptyStruct {}, + | ^^ + +error: cannot derive `IntoPyObject` for empty structs and variants + --> tests/ui/invalid_intopy_derive.rs:26:27 + | +26 | struct EmptyTransparentTup(); + | ^^ + +error: cannot derive `IntoPyObject` for empty structs and variants + --> tests/ui/invalid_intopy_derive.rs:30:31 + | +30 | struct EmptyTransparentStruct {} + | ^^ + +error: cannot derive `IntoPyObject` for empty structs and variants + --> tests/ui/invalid_intopy_derive.rs:35:15 + | +35 | EmptyTuple(), + | ^^ + +error: cannot derive `IntoPyObject` for empty structs and variants + --> tests/ui/invalid_intopy_derive.rs:42:17 + | +42 | EmptyStruct {}, + | ^^ + +error: transparent structs and variants can only have 1 field + --> tests/ui/invalid_intopy_derive.rs:48:35 + | +48 | struct TransparentTupTooManyFields(String, String); + | ^^^^^^^^^^^^^^^^ + +error: transparent structs and variants can only have 1 field + --> tests/ui/invalid_intopy_derive.rs:52:39 + | +52 | struct TransparentStructTooManyFields { + | _______________________________________^ +53 | | foo: String, +54 | | bar: String, +55 | | } + | |_^ + +error: transparent structs and variants can only have 1 field + --> tests/ui/invalid_intopy_derive.rs:60:15 + | +60 | EmptyTuple(String, String), + | ^^^^^^^^^^^^^^^^ + +error: transparent structs and variants can only have 1 field + --> tests/ui/invalid_intopy_derive.rs:67:17 + | +67 | EmptyStruct { + | _________________^ +68 | | foo: String, +69 | | bar: String, +70 | | }, + | |_____^ + +error: expected `transparent` or `crate` + --> tests/ui/invalid_intopy_derive.rs:75:8 + | +75 | #[pyo3(unknown = "should not work")] + | ^^^^^^^ + +error: #[derive(`IntoPyObject`)] is not supported for unions + --> tests/ui/invalid_intopy_derive.rs:81:1 + | +81 | union Union { + | ^^^^^ + +error: cannot derive `IntoPyObject` for empty structs and variants + --> tests/ui/invalid_intopy_derive.rs:85:10 + | +85 | #[derive(IntoPyObject)] + | ^^^^^^^^^^^^ + | + = note: this error originates in the derive macro `IntoPyObject` (in Nightly builds, run with -Z macro-backtrace for more info)