diff --git a/rinja_derive/src/lib.rs b/rinja_derive/src/lib.rs index 0d6a68f5..14b510ba 100644 --- a/rinja_derive/src/lib.rs +++ b/rinja_derive/src/lib.rs @@ -284,22 +284,24 @@ fn build_template_item( let ctx = &contexts[&input.path]; let heritage = if !ctx.blocks.is_empty() || ctx.extends.is_some() { - let heritage = Heritage::new(ctx, &contexts); - - if let Some((block_name, block_span)) = input.block { - if !heritage.blocks.contains_key(&block_name) { - return Err(CompileError::no_file_info( - format_args!("cannot find block `{block_name}`"), - Some(block_span), - )); - } - } - - Some(heritage) + Some(Heritage::new(ctx, &contexts)) } else { None }; + if let Some((block_name, block_span)) = input.block { + let has_block = match &heritage { + Some(heritage) => heritage.blocks.contains_key(block_name), + None => ctx.blocks.contains_key(block_name), + }; + if !has_block { + return Err(CompileError::no_file_info( + format_args!("cannot find block `{block_name}`"), + Some(block_span), + )); + } + } + if input.print == Print::Ast || input.print == Print::All { eprintln!("{:?}", templates[&input.path].nodes()); } diff --git a/testing/templates/no-block-with-base-template.txt b/testing/templates/no-block-with-base-template.txt new file mode 100644 index 00000000..ee28da6e --- /dev/null +++ b/testing/templates/no-block-with-base-template.txt @@ -0,0 +1 @@ +{% extends "no-block-with-include-times-2.txt" %} diff --git a/testing/templates/no-block-with-include-times-2.txt b/testing/templates/no-block-with-include-times-2.txt new file mode 100644 index 00000000..d164ee5f --- /dev/null +++ b/testing/templates/no-block-with-include-times-2.txt @@ -0,0 +1 @@ +{% include "no-block-with-include.txt" %} diff --git a/testing/templates/no-block-with-include.txt b/testing/templates/no-block-with-include.txt new file mode 100644 index 00000000..1094814b --- /dev/null +++ b/testing/templates/no-block-with-include.txt @@ -0,0 +1 @@ +{% include "no-block.txt" %} diff --git a/testing/templates/no-block.txt b/testing/templates/no-block.txt new file mode 100644 index 00000000..e69de29b diff --git a/testing/tests/ui/no-block.rs b/testing/tests/ui/no-block.rs new file mode 100644 index 00000000..eba12126 --- /dev/null +++ b/testing/tests/ui/no-block.rs @@ -0,0 +1,28 @@ +use rinja::Template; + +#[derive(Template)] +#[template( + ext = "txt", + source = "{% block not_a %}{% endblock %}", + block = "a" +)] +struct SourceTemplate; + +#[derive(Template)] +#[template(path = "no-block.txt", block = "a")] +struct PathTemplate; + +#[derive(Template)] +#[template(path = "no-block-with-include.txt", block = "a")] +struct NoBlockWithInclude; + +#[derive(Template)] +#[template(path = "no-block-with-include-times-2.txt", block = "a")] +struct NoBlockWithIncludeTimes2; + +#[derive(Template)] +#[template(path = "no-block-with-base-template.txt", block = "a")] +struct NoBlockWithBaseTemplate; + +fn main() { +} diff --git a/testing/tests/ui/no-block.stderr b/testing/tests/ui/no-block.stderr new file mode 100644 index 00000000..8bc63d2c --- /dev/null +++ b/testing/tests/ui/no-block.stderr @@ -0,0 +1,29 @@ +error: cannot find block `a` + --> tests/ui/no-block.rs:7:13 + | +7 | block = "a" + | ^^^ + +error: cannot find block `a` + --> tests/ui/no-block.rs:12:43 + | +12 | #[template(path = "no-block.txt", block = "a")] + | ^^^ + +error: cannot find block `a` + --> tests/ui/no-block.rs:16:56 + | +16 | #[template(path = "no-block-with-include.txt", block = "a")] + | ^^^ + +error: cannot find block `a` + --> tests/ui/no-block.rs:20:64 + | +20 | #[template(path = "no-block-with-include-times-2.txt", block = "a")] + | ^^^ + +error: cannot find block `a` + --> tests/ui/no-block.rs:24:62 + | +24 | #[template(path = "no-block-with-base-template.txt", block = "a")] + | ^^^