From fe551596f30685ccd716520ca286ab0bd876c9a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Kijewski?= Date: Mon, 11 Nov 2024 00:42:08 +0100 Subject: [PATCH 1/2] `union`s generate invalid code --- testing/tests/ui/union.rs | 10 ++++++++++ testing/tests/ui/union.stderr | 8 ++++++++ 2 files changed, 18 insertions(+) create mode 100644 testing/tests/ui/union.rs create mode 100644 testing/tests/ui/union.stderr 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..004d07b9a --- /dev/null +++ b/testing/tests/ui/union.stderr @@ -0,0 +1,8 @@ +error[E0133]: access to union field is unsafe and requires unsafe function or block + --> tests/ui/union.rs:3:10 + | +3 | #[derive(Template)] + | ^^^^^^^^ access to union field + | + = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior + = note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info) From 3e07719ad9665e2907994c2cff57ca4663d421f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Kijewski?= Date: Mon, 11 Nov 2024 00:49:06 +0100 Subject: [PATCH 2/2] derive: reject `unions` as they require `unsafe` code --- rinja_derive/src/input.rs | 11 ++++++++++- testing/tests/ui/rinja-block.stderr | 8 ++++---- testing/tests/ui/union.stderr | 11 ++++------- 3 files changed, 18 insertions(+), 12 deletions(-) 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.stderr b/testing/tests/ui/union.stderr index 004d07b9a..0653c7e08 100644 --- a/testing/tests/ui/union.stderr +++ b/testing/tests/ui/union.stderr @@ -1,8 +1,5 @@ -error[E0133]: access to union field is unsafe and requires unsafe function or block - --> tests/ui/union.rs:3:10 +error: rinja templates are not supported for `union` types, only `struct` and `enum` + --> tests/ui/union.rs:5:1 | -3 | #[derive(Template)] - | ^^^^^^^^ access to union field - | - = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior - = note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info) +5 | union Tmpl { + | ^^^^^