mirror of
https://github.com/vlang/v.git
synced 2025-09-09 23:39:39 -04:00
cgen: fix temporary var name conflicts between const definitions with or_block in different files(fix #19149) (#19894)
This commit is contained in:
parent
4458e49652
commit
c3cf9ee832
@ -5315,7 +5315,8 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) {
|
|||||||
dep_names: g.table.dependent_names_in_expr(field_expr)
|
dep_names: g.table.dependent_names_in_expr(field_expr)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
g.const_decl_init_later(field.mod, name, field.expr, field.typ, false)
|
g.const_decl_init_later(field.mod, name, field.expr, field.typ, false,
|
||||||
|
false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ast.StringLiteral {
|
ast.StringLiteral {
|
||||||
@ -5332,9 +5333,11 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) {
|
|||||||
|| field.expr.return_type.has_flag(.result) {
|
|| field.expr.return_type.has_flag(.result) {
|
||||||
g.inside_const_opt_or_res = true
|
g.inside_const_opt_or_res = true
|
||||||
unwrap_opt_res := field.expr.or_block.kind != .absent
|
unwrap_opt_res := field.expr.or_block.kind != .absent
|
||||||
g.const_decl_init_later(field.mod, name, field.expr, field.typ, unwrap_opt_res)
|
g.const_decl_init_later(field.mod, name, field.expr, field.typ, unwrap_opt_res,
|
||||||
|
unwrap_opt_res)
|
||||||
} else {
|
} else {
|
||||||
g.const_decl_init_later(field.mod, name, field.expr, field.typ, false)
|
g.const_decl_init_later(field.mod, name, field.expr, field.typ, false,
|
||||||
|
false)
|
||||||
}
|
}
|
||||||
g.inside_const_opt_or_res = false
|
g.inside_const_opt_or_res = false
|
||||||
}
|
}
|
||||||
@ -5376,9 +5379,20 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
g.const_decl_init_later(field.mod, name, field.expr, field.typ, false)
|
g.const_decl_init_later(field.mod, name, field.expr, field.typ, false,
|
||||||
|
false)
|
||||||
|
} else if field.expr is ast.InfixExpr {
|
||||||
|
mut has_unwrap_opt_res := false
|
||||||
|
if field.expr.left is ast.CallExpr {
|
||||||
|
has_unwrap_opt_res = field.expr.left.or_block.kind != .absent
|
||||||
|
} else if field.expr.right is ast.CallExpr {
|
||||||
|
has_unwrap_opt_res = field.expr.right.or_block.kind != .absent
|
||||||
|
}
|
||||||
|
g.const_decl_init_later(field.mod, name, field.expr, field.typ, false,
|
||||||
|
has_unwrap_opt_res)
|
||||||
} else {
|
} else {
|
||||||
g.const_decl_init_later(field.mod, name, field.expr, field.typ, false)
|
g.const_decl_init_later(field.mod, name, field.expr, field.typ, false,
|
||||||
|
false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -5534,7 +5548,7 @@ fn (mut g Gen) c_const_name(name string) string {
|
|||||||
return if g.pref.translated && !g.is_builtin_mod { name } else { '_const_${name}' }
|
return if g.pref.translated && !g.is_builtin_mod { name } else { '_const_${name}' }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut g Gen) const_decl_init_later(mod string, name string, expr ast.Expr, typ ast.Type, unwrap_option bool) {
|
fn (mut g Gen) const_decl_init_later(mod string, name string, expr ast.Expr, typ ast.Type, unwrap_option bool, surround_cbr bool) {
|
||||||
// Initialize more complex consts in `void _vinit/2{}`
|
// Initialize more complex consts in `void _vinit/2{}`
|
||||||
// (C doesn't allow init expressions that can't be resolved at compile time).
|
// (C doesn't allow init expressions that can't be resolved at compile time).
|
||||||
mut styp := g.typ(typ)
|
mut styp := g.typ(typ)
|
||||||
@ -5547,13 +5561,17 @@ fn (mut g Gen) const_decl_init_later(mod string, name string, expr ast.Expr, typ
|
|||||||
init.writeln('\t_const_os__args = os__init_os_args(___argc, (byte**)___argv);')
|
init.writeln('\t_const_os__args = os__init_os_args(___argc, (byte**)___argv);')
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if unwrap_option {
|
if surround_cbr {
|
||||||
init.writeln('{')
|
init.writeln('{')
|
||||||
|
}
|
||||||
|
if unwrap_option {
|
||||||
init.writeln(g.expr_string_surround('\t${cname} = *(${styp}*)', expr, '.data;'))
|
init.writeln(g.expr_string_surround('\t${cname} = *(${styp}*)', expr, '.data;'))
|
||||||
init.writeln('}')
|
|
||||||
} else {
|
} else {
|
||||||
init.writeln(g.expr_string_surround('\t${cname} = ', expr, ';'))
|
init.writeln(g.expr_string_surround('\t${cname} = ', expr, ';'))
|
||||||
}
|
}
|
||||||
|
if surround_cbr {
|
||||||
|
init.writeln('}')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
mut def := '${styp} ${cname}'
|
mut def := '${styp} ${cname}'
|
||||||
expr_sym := g.table.sym(typ)
|
expr_sym := g.table.sym(typ)
|
||||||
|
@ -0,0 +1,5 @@
|
|||||||
|
module consts_with_or_blocks_in_different_files
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
const a_const = os.config_dir() or { panic(err) } + '/a'
|
@ -0,0 +1,10 @@
|
|||||||
|
module consts_with_or_blocks_in_different_files
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
const b_const = os.config_dir() or { panic(err) } + '/b'
|
||||||
|
|
||||||
|
// Just to test the two files in this folder are compiled normally
|
||||||
|
fn test_consts_with_or_blocks_in_different_files() {
|
||||||
|
assert true
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user