diff --git a/doc/docs.md b/doc/docs.md index 52fcd7f0754cb2..026a9a29cc5aad 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -5533,6 +5533,9 @@ that are substituted at compile time: - `@VEXEROOT` => will be substituted with the *folder*, where the V executable is (as a string). - `@VHASH` => replaced with the shortened commit hash of the V compiler (as a string). +- `@VCURRENTHASH` => Similar to `@VHASH`, but changes when the compiler is + recompiled on a different commit (after local modifications, or after + using git bisect etc). - `@VMOD_FILE` => replaced with the contents of the nearest v.mod file (as a string). - `@VMODROOT` => will be substituted with the *folder*, where the nearest v.mod file is (as a string). diff --git a/vlib/builtin/builtin.c.v b/vlib/builtin/builtin.c.v index b101e6d96cd3e8..a7107b41560beb 100644 --- a/vlib/builtin/builtin.c.v +++ b/vlib/builtin/builtin.c.v @@ -50,7 +50,7 @@ fn panic_debug(line_no int, file string, mod string, fn_name string, s string) { eprintln(' function: ${fn_name}()') eprintln(' message: ${s}') eprintln(' file: ${file}:${line_no}') - eprintln(' v hash: ${@VHASH}') + eprintln(' v hash: ${@VHASH}') // TODO: use @VCURRENTHASH when bootstrapped eprintln('=========================================') $if native { C.exit(1) // TODO: native backtraces @@ -104,7 +104,7 @@ pub fn panic(s string) { } $else { eprint('V panic: ') eprintln(s) - eprintln('v hash: ${@VHASH}') + eprintln('v hash: ${@VHASH}') // TODO: use @VCURRENTHASH when bootstrapped $if native { C.exit(1) // TODO: native backtraces } $else $if exit_after_panic_message ? { diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 5a08f46559253a..e30497c43f3bd8 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -133,6 +133,8 @@ mut: goto_labels map[string]ast.GotoLabel // to check for unused goto labels enum_data_type ast.Type fn_return_type ast.Type + + v_current_commit_hash string // same as V_CURRENT_COMMIT_HASH } pub fn new_checker(table &ast.Table, pref_ &pref.Preferences) &Checker { @@ -145,6 +147,7 @@ pub fn new_checker(table &ast.Table, pref_ &pref.Preferences) &Checker { pref: pref_ timers: util.new_timers(should_print: timers_should_print, label: 'checker') match_exhaustive_cutoff_limit: pref_.checker_match_exhaustive_cutoff_limit + v_current_commit_hash: version.githash(pref_.building_v) } } @@ -3368,6 +3371,9 @@ fn (mut c Checker) at_expr(mut node ast.AtExpr) ast.Type { .vhash { node.val = version.vhash() } + .v_current_hash { + node.val = c.v_current_commit_hash + } .vmod_file { // cache the vmod content, do not read it many times if c.vmod_file_content.len == 0 { diff --git a/vlib/v/eval/eval.v b/vlib/v/eval/eval.v index 14ed1f34814c03..c55abd9dbb2c9c 100644 --- a/vlib/v/eval/eval.v +++ b/vlib/v/eval/eval.v @@ -282,9 +282,8 @@ fn (e Eval) error(msg string) { } fn (e Eval) panic(s string) { - commithash := unsafe { tos5(&char(C.V_CURRENT_COMMIT_HASH)) } eprintln('V panic: ${s}') - eprintln('V hash: ${commithash}') + eprintln('V hash: ${@VCURRENTHASH}') e.print_backtrace() exit(1) } diff --git a/vlib/v/gen/js/js.v b/vlib/v/gen/js/js.v index 196d536921b539..5626ba79aa0848 100644 --- a/vlib/v/gen/js/js.v +++ b/vlib/v/gen/js/js.v @@ -5,7 +5,6 @@ import v.ast import v.token import v.pref import v.util -import v.util.version import v.depgraph import encoding.base64 import v.gen.js.sourcemap @@ -247,7 +246,7 @@ pub fn gen(files []&ast.File, table &ast.Table, pref_ &pref.Preferences) string // deps_resolved := graph.resolve() // nodes := deps_resolved.nodes - mut out := g.definitions.str() + g.hashes() + mut out := g.definitions.str() if !g.pref.output_es5 { out += '\nlet wasmExportObject;\n' @@ -491,12 +490,6 @@ pub fn (mut g JsGen) init() { g.definitions.writeln('function ReturnException(val) { this.val = val; }') } -pub fn (g JsGen) hashes() string { - mut res := '// V_COMMIT_HASH ${version.vhash()}\n' - res += '// V_CURRENT_COMMIT_HASH ${version.githash(g.pref.building_v)}\n' - return res -} - [noreturn] fn verror(msg string) { eprintln('jsgen error: ${msg}') diff --git a/vlib/v/gen/native/expr.v b/vlib/v/gen/native/expr.v index 775f4642bd5625..6d0e289c1f62b4 100644 --- a/vlib/v/gen/native/expr.v +++ b/vlib/v/gen/native/expr.v @@ -8,6 +8,9 @@ import v.util fn (mut g Gen) expr(node ast.Expr) { match node { + ast.AtExpr { + g.allocate_string(g.comptime_at(node), 3, .rel32) + } ast.ParExpr { g.expr(node.expr) } diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v index e97897215e424c..5a88dc22bcbb12 100644 --- a/vlib/v/parser/comptime.v +++ b/vlib/v/parser/comptime.v @@ -352,6 +352,7 @@ fn (mut p Parser) at() ast.AtExpr { '@FILE_LINE' { token.AtKind.file_path_line_nr } '@LOCATION' { token.AtKind.location } '@COLUMN' { token.AtKind.column_nr } + '@VCURRENTHASH' { token.AtKind.v_current_hash } '@VHASH' { token.AtKind.vhash } '@VMOD_FILE' { token.AtKind.vmod_file } '@VEXE' { token.AtKind.vexe_path } diff --git a/vlib/v/token/token.v b/vlib/v/token/token.v index 70d6bc62cd633a..00a2dbd1dfecc5 100644 --- a/vlib/v/token/token.v +++ b/vlib/v/token/token.v @@ -173,6 +173,7 @@ pub enum AtKind { line_nr column_nr vhash + v_current_hash vmod_file vmodroot_path vroot_path // obsolete @@ -187,7 +188,8 @@ pub const ( .unsigned_right_shift_assign] valid_at_tokens = ['@VROOT', '@VMODROOT', '@VEXEROOT', '@FN', '@METHOD', '@MOD', '@STRUCT', - '@VEXE', '@FILE', '@LINE', '@COLUMN', '@VHASH', '@VMOD_FILE', '@FILE_LINE', '@LOCATION'] + '@VEXE', '@FILE', '@LINE', '@COLUMN', '@VHASH', '@VCURRENTHASH', '@VMOD_FILE', '@FILE_LINE', + '@LOCATION'] token_str = build_token_str()