From 289993ad7f42fc05ae480a6335b52c17ef4f14b6 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 16 Feb 2023 02:05:26 +0800 Subject: [PATCH] checker: fix printing address of integer variable (#17327) --- vlib/v/checker/str.v | 5 +++-- vlib/v/tests/print_address_of_reference_int_test.v | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/print_address_of_reference_int_test.v diff --git a/vlib/v/checker/str.v b/vlib/v/checker/str.v index b0ec794702..3b5f3f9831 100644 --- a/vlib/v/checker/str.v +++ b/vlib/v/checker/str.v @@ -83,14 +83,15 @@ fn (mut c Checker) string_inter_lit(mut node ast.StringInterLiteral) ast.Type { if node.pluss[i] && !typ.is_number() { c.error('plus prefix only allowed for numbers', node.fmt_poss[i]) } - if (typ.is_unsigned() && fmt !in [`u`, `x`, `X`, `o`, `c`, `b`]) + if ((typ.is_unsigned() && fmt !in [`u`, `x`, `X`, `o`, `c`, `b`]) || (typ.is_signed() && fmt !in [`d`, `x`, `X`, `o`, `c`, `b`]) || (typ.is_int_literal() && fmt !in [`d`, `c`, `x`, `X`, `o`, `u`, `x`, `X`, `o`, `b`]) || (typ.is_float() && fmt !in [`E`, `F`, `G`, `e`, `f`, `g`]) || (typ.is_pointer() && fmt !in [`p`, `x`, `X`]) || (typ.is_string() && fmt !in [`s`, `S`]) - || (typ.idx() in [ast.i64_type_idx, ast.f64_type_idx] && fmt == `c`) { + || (typ.idx() in [ast.i64_type_idx, ast.f64_type_idx] && fmt == `c`)) + && !(typ.is_ptr() && fmt in [`p`, `x`, `X`]) { c.error('illegal format specifier `${fmt:c}` for type `${c.table.get_type_name(ftyp)}`', node.fmt_poss[i]) } diff --git a/vlib/v/tests/print_address_of_reference_int_test.v b/vlib/v/tests/print_address_of_reference_int_test.v new file mode 100644 index 0000000000..d347d37776 --- /dev/null +++ b/vlib/v/tests/print_address_of_reference_int_test.v @@ -0,0 +1,14 @@ +fn test_print_address_of_reference_base_type() { + a1 := 22 + println('${&a1:p}') + a2 := 22.22 + println('${&a2:p}') + a3 := `a` + println('${&a3:p}') + a4 := 'hello' + println('${&a4:p}') + a5 := true + println('${&a5:p}') + + assert true +}