Skip to content

Commit

Permalink
cgen: fix temporary var name conflicts between const definitions with…
Browse files Browse the repository at this point in the history
… or_block in different files(fix #19149) (#19894)
  • Loading branch information
shove70 committed Nov 16, 2023
1 parent 4458e49 commit c3cf9ee
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
34 changes: 26 additions & 8 deletions vlib/v/gen/c/cgen.v
Expand Up @@ -5315,7 +5315,8 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) {
dep_names: g.table.dependent_names_in_expr(field_expr)
}
} 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 {
Expand All @@ -5332,9 +5333,11 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) {
|| field.expr.return_type.has_flag(.result) {
g.inside_const_opt_or_res = true
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 {
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
}
Expand Down Expand Up @@ -5376,9 +5379,20 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) {
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 {
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)
}
}
}
Expand Down Expand Up @@ -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}' }
}

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{}`
// (C doesn't allow init expressions that can't be resolved at compile time).
mut styp := g.typ(typ)
Expand All @@ -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);')
}
} else {
if unwrap_option {
if surround_cbr {
init.writeln('{')
}
if unwrap_option {
init.writeln(g.expr_string_surround('\t${cname} = *(${styp}*)', expr, '.data;'))
init.writeln('}')
} else {
init.writeln(g.expr_string_surround('\t${cname} = ', expr, ';'))
}
if surround_cbr {
init.writeln('}')
}
}
mut def := '${styp} ${cname}'
expr_sym := g.table.sym(typ)
Expand Down
@@ -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
}

0 comments on commit c3cf9ee

Please sign in to comment.