Skip to content
Draft
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
38 changes: 38 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ strip-ansi-escapes = "0.2"
toml = "0.8"
serde_yaml = "0.9"
proptest = "1.11.0"
trybuild = { version = "1", features = ["diff"] }

# Target-specific dev-deps
[target.'cfg(unix)'.dev-dependencies]
Expand Down
13 changes: 13 additions & 0 deletions tests/compile_fail_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//! Compile-time UI tests for `Captured::state()` lifetime correctness.
//!
//! `MacroStateGuard` (in `src/manifest/jinja_macros/cache.rs`) relies on the
//! borrow checker rejecting any programme that lets a `&State` outlive the
//! owning `Captured`. These trybuild cases pin that guarantee: the dangling
//! case must fail to compile, and the well-scoped case must compile.

#[test]
fn captured_state_lifetime_tests() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/ui/captured_state_outlives_captured.rs");
t.pass("tests/ui/captured_state_within_scope.rs");
}
15 changes: 15 additions & 0 deletions tests/ui/captured_state_outlives_captured.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! Attempting to hold a `&State` beyond the owning `Captured`'s lifetime
//! must be rejected by the borrow checker.

use minijinja::Environment;

fn main() {
let mut env = Environment::new();
env.add_template("greeting", "{{ 1 }}").expect("template");
let template = env.get_template("greeting").expect("template");
let state_ref = {
let captured = template.render_captured(()).expect("render");
captured.state()
};
let _ = state_ref;
}
11 changes: 11 additions & 0 deletions tests/ui/captured_state_outlives_captured.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0597]: `captured` does not live long enough
--> tests/ui/captured_state_outlives_captured.rs:12:9
|
10 | let state_ref = {
| --------- borrow later stored here
11 | let captured = template.render_captured(()).expect("render");
| -------- binding `captured` declared here
12 | captured.state()
| ^^^^^^^^ borrowed value does not live long enough
13 | };
| - `captured` dropped here while still borrowed
13 changes: 13 additions & 0 deletions tests/ui/captured_state_within_scope.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//! Using `Captured::state()` strictly within the owning `Captured`'s
//! lifetime compiles cleanly.

use minijinja::Environment;

fn main() {
let mut env = Environment::new();
env.add_template("greeting", "{{ 1 }}").expect("template");
let template = env.get_template("greeting").expect("template");
let captured = template.render_captured(()).expect("render");
let state = captured.state();
let _ = state.lookup("missing");
}
Loading