Skip to content

derive: reject unions as they require unsafe code #231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion rinja_derive/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,17 @@

impl TemplateArgs {
pub(crate) fn new(ast: &syn::DeriveInput) -> Result<Self, CompileError> {
// FIXME: implement once <https://github.com/rust-lang/rfcs/pull/3715> 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()
Expand Down Expand Up @@ -532,6 +540,7 @@
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(
Expand Down
8 changes: 4 additions & 4 deletions testing/tests/ui/rinja-block.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
| ^^^^^
10 changes: 10 additions & 0 deletions testing/tests/ui/union.rs
Original file line number Diff line number Diff line change
@@ -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() {}
5 changes: 5 additions & 0 deletions testing/tests/ui/union.stderr
Original file line number Diff line number Diff line change
@@ -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 {
| ^^^^^