diff --git a/rinja_derive/src/input.rs b/rinja_derive/src/input.rs index 497f683fe..59d6d3c56 100644 --- a/rinja_derive/src/input.rs +++ b/rinja_derive/src/input.rs @@ -302,9 +302,17 @@ pub(crate) struct TemplateArgs { impl TemplateArgs { pub(crate) fn new(ast: &syn::DeriveInput) -> Result { + // FIXME: implement once is stable + if let syn::Data::Union(data) = &ast.data { + return Err(CompileError::new_with_span( + "rinja templates are not supported for `union` types, only `struct` and `enum`", + None, + Some(data.union_token.span), + )); + } + // Check that an attribute called `template()` exists at least once and that it is // the proper type (list). - let mut templates_attrs = ast .attrs .iter() @@ -532,6 +540,7 @@ fn no_rinja_code_block(name: &syn::Ident, ast: &syn::DeriveInput) -> CompileErro let kind = match &ast.data { syn::Data::Struct(_) => "struct", syn::Data::Enum(_) => "enum", + // actually unreachable: `union`s are rejected by `TemplateArgs::new()` syn::Data::Union(_) => "union", }; CompileError::no_file_info( diff --git a/testing/tests/ui/rinja-block.stderr b/testing/tests/ui/rinja-block.stderr index 976951ab0..123737b3c 100644 --- a/testing/tests/ui/rinja-block.stderr +++ b/testing/tests/ui/rinja-block.stderr @@ -42,8 +42,8 @@ error: when using `in_doc = true`, the enum's documentation needs a `rinja` code 60 | #[template(ext = "txt", in_doc = true)] | ^^^^^^ -error: when using `in_doc = true`, the union's documentation needs a `rinja` code block - --> tests/ui/rinja-block.rs:67:25 +error: rinja templates are not supported for `union` types, only `struct` and `enum` + --> tests/ui/rinja-block.rs:68:1 | -67 | #[template(ext = "txt", in_doc = true)] - | ^^^^^^ +68 | union NoDocForUnion { + | ^^^^^ diff --git a/testing/tests/ui/union.rs b/testing/tests/ui/union.rs new file mode 100644 index 000000000..81876b253 --- /dev/null +++ b/testing/tests/ui/union.rs @@ -0,0 +1,10 @@ +use rinja::Template; + +#[derive(Template)] +#[template(source = "a={{ a }} b={{ b }}", ext = "html")] +union Tmpl { + a: i32, + b: u32, +} + +fn main() {} diff --git a/testing/tests/ui/union.stderr b/testing/tests/ui/union.stderr new file mode 100644 index 000000000..0653c7e08 --- /dev/null +++ b/testing/tests/ui/union.stderr @@ -0,0 +1,5 @@ +error: rinja templates are not supported for `union` types, only `struct` and `enum` + --> tests/ui/union.rs:5:1 + | +5 | union Tmpl { + | ^^^^^