From 598673314b602fe7fc9f7c656eae563deec0debd Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Wed, 24 May 2023 00:47:09 -0300 Subject: [PATCH] json.decode: add check for shared variable (#18237) --- vlib/v/checker/fn.v | 3 +++ vlib/v/checker/tests/json_decode_shared_err.out | 7 +++++++ vlib/v/checker/tests/json_decode_shared_err.vv | 8 ++++++++ 3 files changed, 18 insertions(+) create mode 100644 vlib/v/checker/tests/json_decode_shared_err.out create mode 100644 vlib/v/checker/tests/json_decode_shared_err.vv diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index b7e4ed3672..99a6ed5eca 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -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, '')`)", diff --git a/vlib/v/checker/tests/json_decode_shared_err.out b/vlib/v/checker/tests/json_decode_shared_err.out new file mode 100644 index 0000000000..0295cc9b7c --- /dev/null +++ b/vlib/v/checker/tests/json_decode_shared_err.out @@ -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 | } diff --git a/vlib/v/checker/tests/json_decode_shared_err.vv b/vlib/v/checker/tests/json_decode_shared_err.vv new file mode 100644 index 0000000000..7cec9f4b2a --- /dev/null +++ b/vlib/v/checker/tests/json_decode_shared_err.vv @@ -0,0 +1,8 @@ +import json + +fn main() { + shared data := [1, 2, 3] + rlock data { + println(json.encode(data)) + } +}