checker: fix missing check for invalid argument for builtin (fix #23511) (#23515)

This commit is contained in:
Felipe Pena 2025-01-19 11:36:38 -03:00 committed by GitHub
parent b51dfcfe7d
commit cf0100f140
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 73 additions and 1 deletions

View File

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

View File

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

View File

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