diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2979d27e3b..42f482aa27 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -276,9 +276,9 @@ jobs: - name: usecache run: | ./v -usecache examples/hello_world.v && examples/hello_world -# ./v -o v2 -usecache cmd/v -# ./v2 -o v3 -usecache cmd/v -# ./v3 -usecache examples/tetris/tetris.v + ./v -o v2 -usecache cmd/v + ./v2 -o v3 -usecache cmd/v + ./v3 -o tetris -usecache examples/tetris/tetris.v - name: Test symlink run: ./v symlink # - name: Set up pg database diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index b2d0425cc2..00b704a68e 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -5008,7 +5008,18 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) { } } else { - if g.pref.build_mode != .build_module { + // NB: -usecache uses prebuilt modules, each compiled with: + // `v build-module vlib/module` + // combined with a top level program, that is compiled with: + // `v -usecache toplevel` + // For it to work, the consts optimisations should be identical, because + // only the top level program will have the const initialisation code for + // all the modules. + // TODO: encapsulate const initialisation for each module in a separate function, + // that is just called by the top level program in _vinit, instead of generating + // all the code inside _vinit for each module. + use_cache_mode := g.pref.build_mode == .build_module || g.pref.use_cache + if !use_cache_mode { if ct_value := field.comptime_expr_value() { if g.const_decl_precomputed(field.mod, name, ct_value, field.typ) { continue