Skip to content

Commit

Permalink
checker: fix missing check for invalid argument for builtin (fix #23511
Browse files Browse the repository at this point in the history
…) (#23515)
  • Loading branch information
felipensp authored Jan 19, 2025
1 parent b51dfcf commit cf0100f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
5 changes: 4 additions & 1 deletion vlib/v/checker/str.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 49 additions & 0 deletions vlib/v/checker/tests/invalid_generic_field_err.out
Original file line number Diff line number Diff line change
@@ -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 |
20 changes: 20 additions & 0 deletions vlib/v/checker/tests/invalid_generic_field_err.vv
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit cf0100f

Please sign in to comment.