Add regression test for */& operators in functions/methods arguments

This commit is contained in:
Guillaume Gomez 2024-10-02 22:11:25 +02:00
parent 0f9c5a0281
commit 8ba1e16f3e
2 changed files with 45 additions and 2 deletions

View File

@ -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 {

View File

@ -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");
}