From 8ba1e16f3eac5e83cd5a0b81a94c7a1097345aae Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 2 Oct 2024 22:11:25 +0200 Subject: [PATCH] Add regression test for `*`/`&` operators in functions/methods arguments --- rinja_derive/src/generator.rs | 4 ++-- testing/tests/calls.rs | 43 +++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/rinja_derive/src/generator.rs b/rinja_derive/src/generator.rs index f77466ce..6fac97e5 100644 --- a/rinja_derive/src/generator.rs +++ b/rinja_derive/src/generator.rs @@ -1965,8 +1965,8 @@ impl<'a> Generator<'a> { ctx: &Context<'_>, buf: &mut Buffer, arg: &WithSpan<'_, Expr<'_>>, - // This variable is needed because `Expr::Unary` is not copyable but since we might - // skip a few levels. + // This parameter is needed because even though Expr::Unary is not copyable, we might still + // be able to skip a few levels. need_borrow: bool, ) -> Result<(), CompileError> { if let Expr::Unary(expr @ ("*" | "&"), ref arg) = **arg { diff --git a/testing/tests/calls.rs b/testing/tests/calls.rs index 3dea2fed..e18f1e40 100644 --- a/testing/tests/calls.rs +++ b/testing/tests/calls.rs @@ -170,3 +170,46 @@ struct NotMethod; fn test_not_method() { assert_eq!(NotMethod.render().unwrap(), "a6"); } + +#[derive(Template)] +#[template( + source = " +{{- bar(x) -}} +{{- bar(&*x) -}} +{{- foo(*x) -}} +{{- foo(*&*x) -}} +{# #} {{+ extra_fn::bar(x) -}} +{{- extra_fn::bar(&*x) -}} +{{- extra_fn::foo(*x) -}} +{{- extra_fn::foo(*&*x) -}} +", + ext = "txt" +)] +struct DerefMethodArg { + x: u32, +} + +mod extra_fn { + pub fn bar(x: &u32) -> u32 { + *x + 4 + } + pub fn foo(x: u32) -> u32 { + x + 5 + } +} + +impl DerefMethodArg { + fn bar(&self, x: &u32) -> u32 { + *x + 1 + } + fn foo(&self, x: u32) -> u32 { + x + } +} + +// This test ensures that the `*`/`&` operators are correctly placed on method/function arguments. +#[test] +fn test_deref_method_arg() { + let x = DerefMethodArg { x: 2 }; + assert_eq!(x.render().unwrap(), "3322 6677"); +}