From cf0100f140dc94c8b09685e48039c86adb2319af Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 19 Jan 2025 11:36:38 -0300 Subject: [PATCH] checker: fix missing check for invalid argument for builtin (fix #23511) (#23515) --- vlib/v/checker/str.v | 5 +- .../tests/invalid_generic_field_err.out | 49 +++++++++++++++++++ .../tests/invalid_generic_field_err.vv | 20 ++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 vlib/v/checker/tests/invalid_generic_field_err.out create mode 100644 vlib/v/checker/tests/invalid_generic_field_err.vv diff --git a/vlib/v/checker/str.v b/vlib/v/checker/str.v index a7ed0b1aeb89c0..1d84f234d65307 100644 --- a/vlib/v/checker/str.v +++ b/vlib/v/checker/str.v @@ -47,12 +47,15 @@ fn (mut c Checker) string_inter_lit(mut node ast.StringInterLiteral) ast.Type { mut ftyp := c.expr(mut expr) ftyp = c.type_resolver.get_type_or_default(expr, c.check_expr_option_or_result_call(expr, ftyp)) - if ftyp == ast.void_type { + if ftyp == ast.void_type || ftyp == 0 { c.error('expression does not return a value', expr.pos()) } else if ftyp == ast.char_type && ftyp.nr_muls() == 0 { c.error('expression returning type `char` cannot be used in string interpolation directly, print its address or cast it to an integer instead', expr.pos()) } + if ftyp == 0 { + return ast.void_type + } c.markused_string_inter_lit(mut node, ftyp) c.fail_if_unreadable(expr, ftyp, 'interpolation object') node.expr_types << ftyp diff --git a/vlib/v/checker/tests/invalid_generic_field_err.out b/vlib/v/checker/tests/invalid_generic_field_err.out new file mode 100644 index 00000000000000..2994c8a9636c5d --- /dev/null +++ b/vlib/v/checker/tests/invalid_generic_field_err.out @@ -0,0 +1,49 @@ +vlib/v/checker/tests/invalid_generic_field_err.vv:4:15: error: type `User` has no field named `typo` + 2 | + 3 | fn (u &User) debug_1() { + 4 | println('${u.typo}') + | ~~~~ + 5 | } + 6 | +vlib/v/checker/tests/invalid_generic_field_err.vv:4:15: error: expression does not return a value + 2 | + 3 | fn (u &User) debug_1() { + 4 | println('${u.typo}') + | ~~~~ + 5 | } + 6 | +vlib/v/checker/tests/invalid_generic_field_err.vv:8:9: error: `User` has no property `typo` + 6 | + 7 | fn debug_2[T](t T) { + 8 | _ := t.typo + | ~~~~ + 9 | } + 10 | +vlib/v/checker/tests/invalid_generic_field_err.vv:8:4: error: assignment mismatch: 1 variable 0 values + 6 | + 7 | fn debug_2[T](t T) { + 8 | _ := t.typo + | ~~ + 9 | } + 10 | +vlib/v/checker/tests/invalid_generic_field_err.vv:12:15: error: `User` has no property `typo` + 10 | + 11 | fn debug_3[T](t T) { + 12 | println('${t.typo}') + | ~~~~ + 13 | } + 14 | +vlib/v/checker/tests/invalid_generic_field_err.vv:12:15: error: expression does not return a value + 10 | + 11 | fn debug_3[T](t T) { + 12 | println('${t.typo}') + | ~~~~ + 13 | } + 14 | +vlib/v/checker/tests/invalid_generic_field_err.vv:12:2: error: `println` can not print void expressions + 10 | + 11 | fn debug_3[T](t T) { + 12 | println('${t.typo}') + | ~~~~~~~~~~~~~~~~~~~~ + 13 | } + 14 | diff --git a/vlib/v/checker/tests/invalid_generic_field_err.vv b/vlib/v/checker/tests/invalid_generic_field_err.vv new file mode 100644 index 00000000000000..9604754799632d --- /dev/null +++ b/vlib/v/checker/tests/invalid_generic_field_err.vv @@ -0,0 +1,20 @@ +struct User {} + +fn (u &User) debug_1() { + println('${u.typo}') +} + +fn debug_2[T](t T) { + _ := t.typo +} + +fn debug_3[T](t T) { + println('${t.typo}') +} + +fn main() { + u := &User{} + u.debug_1() + debug_2(u) + debug_3(u) +}