Skip to content

Commit

Permalink
Allow variables to be inherited in blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Nov 22, 2024
1 parent 0edbca2 commit b3f8110
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
14 changes: 11 additions & 3 deletions rinja_derive/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1157,11 +1157,19 @@ impl<'a> Generator<'a> {
self.input,
self.contexts,
Some(heritage),
// FIXME: Fix the broken lifetimes.

Check notice

Code scanning / devskim

A "TODO" or similar was left in source code, possibly indicating incomplete functionality Note

Suspicious comment
//
// `transmute` is here to fix the lifetime nightmare around `&self.locals` not
// living long enough. The correct solution would be to make `MapChain` take two
// lifetimes `&'a MapChain<'a, 'b, K, V>`. Except... compiler is triggered because then
// the `'b` lifetime is marked as unused.
MapChain::with_parent(unsafe { mem::transmute(&self.locals) }),
// lifetimes `&'a MapChain<'a, 'b, K, V>`, making the `parent` field take
// `<'b, 'b, ...>`. Except... it doesn't work because `self` still doesn't live long
// enough here for some reason...
MapChain::with_parent(unsafe {
mem::transmute::<
&MapChain<'_, Cow<'_, str>, LocalMeta>,
&MapChain<'_, Cow<'_, str>, LocalMeta>,
>(&self.locals)
}),
self.buf_writable.discard,
self.is_in_filter_block,
);
Expand Down
2 changes: 2 additions & 0 deletions testing/templates/base-decl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{%- let variable = 42 -%}
{%- block extended %}{% endblock -%}
1 change: 1 addition & 0 deletions testing/templates/use-var.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ variable }}
18 changes: 18 additions & 0 deletions testing/tests/block_fragments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,21 @@ fn test_fragment_include() {
let fragment_include = FragmentInclude { s: "world" };
assert_eq!(fragment_include.render().unwrap(), "\nINCLUDED: world\n");
}

// This test ensures that parent variables are inherited in the block.
// This is a regression test for <https://github.com/rinja-rs/rinja/issues/246>.
#[test]
fn test_variable_inheritance_in_block() {
#[derive(Template)]
#[template(
source = r#"{% extends "base-decl.txt" %}
{%- block extended -%}
--> {{ variable }}
{% include "use-var.txt" -%}
{% endblock %}"#,
ext = "txt",
)]
struct Y;

assert_eq!(Y.render().unwrap(), "--> 42\n42");
}

0 comments on commit b3f8110

Please sign in to comment.