Skip to content

Commit fe618a5

Browse files
committed
cgen: improve the stability of generated code for auto string methods
1 parent de54be0 commit fe618a5

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

vlib/v/gen/c/auto_str_methods.v

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ fn (mut g Gen) gen_str_for_alias(info ast.Alias, styp string, str_fn_name string
284284
g.auto_str_funcs.writeln('${g.static_non_parallel}string ${str_fn_name}(${arg_def}) { return indent_${str_fn_name}(it, 0); }')
285285
g.definitions.writeln('${g.static_non_parallel}string indent_${str_fn_name}(${arg_def}, int indent_count); // auto')
286286
g.auto_str_funcs.writeln('${g.static_non_parallel}string indent_${str_fn_name}(${arg_def}, int indent_count) {')
287+
old := g.reset_tmp_count()
288+
defer { g.tmp_count = old }
287289
g.auto_str_funcs.writeln('\tstring indents = string_repeat(_SLIT(" "), indent_count);')
288290
if str_method_expects_ptr {
289291
it_arg := if is_c_struct { 'it' } else { '&it' }
@@ -954,7 +956,8 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, lang ast.Language, styp strin
954956
g.auto_fn_definitions << fn_builder.str()
955957
}
956958
fn_builder.writeln('string indent_${str_fn_name}(${arg_def}, int indent_count) {')
957-
959+
old := g.reset_tmp_count()
960+
defer { g.tmp_count = old }
958961
clean_struct_v_type_name := if info.is_anon { 'struct ' } else { util.strip_main_name(typ_str) }
959962
// generate ident / indent length = 4 spaces
960963
if info.fields.len == 0 {

vlib/v/gen/c/cgen.v

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2024,8 +2024,10 @@ pub fn (mut g Gen) current_tmp_var() string {
20242024
return prefix_with_counter('_t', g.tmp_count)
20252025
}
20262026

2027-
pub fn (mut g Gen) reset_tmp_count() {
2027+
pub fn (mut g Gen) reset_tmp_count() int {
2028+
old := g.tmp_count
20282029
g.tmp_count = 0
2030+
return old
20292031
}
20302032

20312033
fn (mut g Gen) decrement_inside_ternary() {

0 commit comments

Comments
 (0)