json: fix json.decode autofree codegen (fix #23834) (#23839)

This commit is contained in:
Felipe Pena 2025-03-02 04:52:26 -03:00 committed by GitHub
parent c37309f6b9
commit 05a6e557cf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 1 deletions

View File

@ -2477,9 +2477,10 @@ fn (mut g Gen) call_args(node ast.CallExpr) {
}
} else {
if use_tmp_var_autofree {
n := if node.name == 'json.decode' { i + 2 } else { i + 1 }
// TODO: copypasta, move to an inline fn
fn_name := node.name.replace('.', '_')
name := '_arg_expr_${fn_name}_${i + 1}_${node.pos.pos}'
name := '_arg_expr_${fn_name}_${n}_${node.pos.pos}'
g.write('/*af arg2*/' + name)
} else {
g.expr(arg.expr)

View File

@ -0,0 +1,2 @@
string _arg_expr_json_decode_2_271 = string_str(msg);
cJSON* _t2 = json__json_parse(/*af arg2*/_arg_expr_json_decode_2_271);

View File

@ -0,0 +1,19 @@
// vtest vflags: -autofree
module main
import json
fn main() {
input := '{"methode":"test"}'
decode_message(input) or {}
assert true
}
type BaseMessage = struct {
methode string @[json: methode]
}
pub fn decode_message(msg string) !string {
decoded_json := json.decode(BaseMessage, msg.str())!
return decoded_json.str()
}