cgen: fix tmp var redeclaration on const inited later (fix #24521) (fix #24517) (#24524)

This commit is contained in:
Felipe Pena 2025-05-19 10:16:35 -03:00 committed by GitHub
parent 171df75cc7
commit 18f1874586
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 30 additions and 1 deletions

View file

@ -340,10 +340,15 @@ fn (mut g Gen) const_decl_init_later(mod string, name string, expr ast.Expr, typ
func := (expr_sym.info as ast.FnType).func
def = g.fn_var_signature(func.return_type, func.params.map(it.typ), cname)
}
init_str := init.str().trim_right('\n')
g.global_const_defs[util.no_dots(name)] = GlobalConstDef{
mod: mod
def: '${def}; // inited later'
init: init.str().trim_right('\n')
init: if init_str.count('\n') > 1 {
'{\n${init_str}\n}'
} else {
init_str
}
dep_names: g.table.dependent_names_in_expr(expr)
}
if g.is_autofree {

View file

@ -0,0 +1,12 @@
module main
import json
pub struct JwtHeader {
pub:
alg string
typ string
cty string
}
const f1 = json.encode(JwtHeader{ alg: '1', typ: '2', cty: '3' })

View file

@ -0,0 +1,5 @@
module main
import json
const f2 = json.encode(JwtHeader{ alg: 'a', typ: 'b', cty: 'c' })

View file

@ -0,0 +1,7 @@
module main
fn main() {
println(f1)
println(f2)
println('OK')
}