Skip to content
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

Allow variables inheritance in block #264

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 15 additions & 3 deletions rinja_derive/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1152,16 +1152,28 @@
.or_insert_with(|| import.clone());
}

let buf_writable = mem::take(&mut self.buf_writable);
let mut child = Self::new(
self.input,
self.contexts,
Some(heritage),
// Variables are NOT inherited from the parent scope.
MapChain::default(),
// FIXME: Fix the broken lifetimes.
Fixed Show fixed Hide fixed
//
// `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>`, 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::<
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still very unhappy about this. I was able to improve the situation a lot but still get this last error:

error: lifetime may not live long enough
    --> rinja_derive/src/generator.rs:1166:13
     |
84   | impl<'a, 'b> Generator<'a, 'b> {
     |      --  -- lifetime `'b` defined here
     |      |
     |      lifetime `'a` defined here
...
1166 |             MapChain::with_parent(&self.locals),
     |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ argument requires that `'b` must outlive `'a`
     |
     = help: consider adding the following bound: `'b: 'a`

I pushed my changes here.

I'll try to continue to understand what's wrong. If anyone wants to give it a try though...

&MapChain<'_, Cow<'_, str>, LocalMeta>,
&MapChain<'_, Cow<'_, str>, LocalMeta>,
>(&self.locals)
}),
self.buf_writable.discard,
self.is_in_filter_block,
);
child.buf_writable = mem::take(&mut self.buf_writable);
child.buf_writable = buf_writable;

// Handle inner whitespace suppression spec and process block nodes
child.prepare_ws(def.ws1);
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");
}