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

json.decode: add check for shared variable (#18237)

This commit is contained in:
Felipe Pena 2023-05-24 00:47:09 -03:00 committed by GitHub
parent 6698fe4f60
commit 598673314b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 0 deletions

View File

@ -635,6 +635,9 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
}
}
panic('unreachable')
} else if node.args.len > 0 && fn_name == 'json.encode' && node.args[0].typ.has_flag(.shared_f) {
c.error('json.encode cannot handle shared data', node.pos)
return ast.void_type
} else if node.args.len > 0 && fn_name == 'json.decode' {
if node.args.len != 2 {
c.error("json.decode expects 2 arguments, a type and a string (e.g `json.decode(T, '')`)",

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/json_decode_shared_err.vv:6:16: error: json.encode cannot handle shared data
4 | shared data := [1, 2, 3]
5 | rlock data {
6 | println(json.encode(data))
| ~~~~~~~~~~~~
7 | }
8 | }

View File

@ -0,0 +1,8 @@
import json
fn main() {
shared data := [1, 2, 3]
rlock data {
println(json.encode(data))
}
}