Skip to content

Commit

Permalink
v.eval: lookup constants in builtin, when they are not found in the…
Browse files Browse the repository at this point in the history
… current module; add test (#23745)
  • Loading branch information
spytheman authored Feb 17, 2025
1 parent 428b328 commit 4446434
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
6 changes: 5 additions & 1 deletion vlib/v/eval/eval.v
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ pub fn (mut e Eval) eval(mut files []&ast.File) {

// first arg is receiver (if method)
pub fn (mut e Eval) run_func(func ast.FnDecl, _args ...Object) {
e.back_trace << EvalTrace{func.idx, func.source_file.idx, func.pos.line_nr}
e.back_trace << EvalTrace{func.idx, if unsafe { func.source_file != 0 } {
func.source_file.idx
} else {
0
}, func.pos.line_nr}
old_mod := e.cur_mod
old_file := e.cur_file
e.cur_mod = func.mod
Expand Down
9 changes: 8 additions & 1 deletion vlib/v/eval/expr.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,18 @@ pub fn (mut e Eval) expr(expr ast.Expr, expecting ast.Type) Object {
return e.local_vars[expr.name].val
}
.constant {
key := expr.name.all_after_last('.')
return if expr.name.contains('.') {
e.mods[expr.name.all_before_last('.')]
} else {
e.mods[e.cur_mod]
}[expr.name.all_after_last('.')] or { ast.EmptyStmt{} } as Object
}[key] or {
if builtin_constant := e.mods['builtin'][key] {
builtin_constant
} else {
e.error('unknown constant `${key}`')
}
} as Object
}
else {
e.error('unknown ident kind for `${expr.name}`: ${expr.kind}')
Expand Down
4 changes: 4 additions & 0 deletions vlib/v/eval/testdata/constants_and_casts.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-2147483648
2147483647
2147483647
-2147483648
11 changes: 11 additions & 0 deletions vlib/v/eval/testdata/constants_and_casts.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const local_max_int = int(2147483647)
const local_min_int = int(-2147483648)
const i64_min_int32 = i64(local_min_int + 1) - 1
const i64_max_int32 = i64(local_max_int - 1) + 1
const abc = i64(max_int)
const def = i64(min_int)

println(i64_min_int32)
println(i64_max_int32)
println(abc)
println(def)

0 comments on commit 4446434

Please sign in to comment.