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