diff --git a/vlib/v/gen/c/array.v b/vlib/v/gen/c/array.v index ca70328188..494e737294 100644 --- a/vlib/v/gen/c/array.v +++ b/vlib/v/gen/c/array.v @@ -382,7 +382,7 @@ fn (mut g Gen) gen_array_sort(node ast.CallExpr) { // `users.sort(a.age > b.age)` // Generate a comparison function for a custom type elem_stype := g.typ(info.elem_type) - mut compare_fn := 'compare_${elem_stype.replace('*', '_ptr')}' + mut compare_fn := 'compare_${g.unique_file_path_hash}_${elem_stype.replace('*', '_ptr')}' mut comparison_type := g.unwrap(ast.void_type) mut left_expr, mut right_expr := '', '' // the only argument can only be an infix expression like `a < b` or `b.field > a.field` @@ -443,7 +443,7 @@ fn (mut g Gen) gen_array_sort(node ast.CallExpr) { } stype_arg := g.typ(info.elem_type) - g.definitions.writeln('int ${compare_fn}($stype_arg* a, $stype_arg* b) {') + g.definitions.writeln('VV_LOCAL_SYMBOL int ${compare_fn}($stype_arg* a, $stype_arg* b) {') c_condition := if comparison_type.sym.has_method('<') { '${g.typ(comparison_type.typ)}__lt($left_expr, $right_expr)' } else if comparison_type.unaliased_sym.has_method('<') { diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 00b704a68e..9abbb39919 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -5,6 +5,7 @@ module c import os import strings +import hash.fnv1a import v.ast import v.pref import v.token @@ -72,6 +73,7 @@ mut: json_forward_decls strings.Builder // json type forward decls sql_buf strings.Builder // for writing exprs to args via `sqlite3_bind_int()` etc file &ast.File + unique_file_path_hash u64 // a hash of file.path, used for making auxilary fn generation unique (like `compare_xyz`) fn_decl &ast.FnDecl // pointer to the FnDecl we are currently inside otherwise 0 last_fn_c_name string tmp_count int // counter for unique tmp vars (_tmp1, _tmp2 etc); resets at the start of each fn. @@ -579,7 +581,7 @@ pub fn (mut g Gen) free_builders() { pub fn (mut g Gen) gen_file() { g.timers.start('cgen_file $g.file.path') - + g.unique_file_path_hash = fnv1a.sum64_string(g.file.path) if g.pref.is_vlines { g.vlines_path = util.vlines_escape_path(g.file.path, g.pref.ccompiler) g.is_vlines_enabled = true