1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: prohibit illegal types in string interpolation literals (#10705)

This commit is contained in:
shadowninja55 2021-07-09 17:00:12 -04:00 committed by GitHub
parent fbd6b91086
commit a1088275b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 0 deletions

View File

@ -437,6 +437,12 @@ pub fn (mut c Checker) string_inter_lit(mut node ast.StringInterLiteral) ast.Typ
c.inside_println_arg = true
for i, expr in node.exprs {
ftyp := c.expr(expr)
if ftyp == ast.void_type {
c.error('expression does not return a value', expr.position())
} 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.position())
}
c.fail_if_unreadable(expr, ftyp, 'interpolation object')
node.expr_types << ftyp
typ := c.table.unalias_num_type(ftyp)

View File

@ -0,0 +1,8 @@
vlib/v/checker/tests/bad_types_in_string_inter_lit.vv:1:12: error: expression does not return a value
1 | println('${exit(0)}')
| ~~~~~~~
2 | println('${char(48)}')
vlib/v/checker/tests/bad_types_in_string_inter_lit.vv:2:12: error: expression returning type `char` cannot be used in string interpolation directly, print its address or cast it to an integer instead
1 | println('${exit(0)}')
2 | println('${char(48)}')
| ~~~~~~~~

View File

@ -0,0 +1,2 @@
println('${exit(0)}')
println('${char(48)}')

View File

@ -14,3 +14,9 @@ Did you mean `translate`?
| ~~~~~~~~~~~~
28 | println('p: $p')
29 | println('v: $v')
vlib/v/checker/tests/unknown_method_suggest_name.vv:30:15: error: expression does not return a value
28 | println('p: $p')
29 | println('v: $v')
30 | println('z: $z')
| ^
31 | }

View File

@ -6,3 +6,10 @@ Did you mean `xxxx`?
| ~~~~
12 | println('x: $x')
13 | }
vlib/v/checker/tests/unknown_struct_field_suggest_name.vv:12:19: error: expression does not return a value
10 | println('p: $p')
11 | for x in p.xxxa {
12 | println('x: $x')
| ^
13 | }
14 | }